re PR c/61271 (10 * possible coding error with logical not (!))
[platform/upstream/gcc.git] / gcc / lra-spills.c
1 /* Change pseudos by memory.
2    Copyright (C) 2010-2014 Free Software Foundation, Inc.
3    Contributed by Vladimir Makarov <vmakarov@redhat.com>.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21
22 /* This file contains code for a pass to change spilled pseudos into
23    memory.
24
25    The pass creates necessary stack slots and assigns spilled pseudos
26    to the stack slots in following way:
27
28    for all spilled pseudos P most frequently used first do
29      for all stack slots S do
30        if P doesn't conflict with pseudos assigned to S then
31          assign S to P and goto to the next pseudo process
32        end
33      end
34      create new stack slot S and assign P to S
35    end
36
37    The actual algorithm is bit more complicated because of different
38    pseudo sizes.
39
40    After that the code changes spilled pseudos (except ones created
41    from scratches) by corresponding stack slot memory in RTL.
42
43    If at least one stack slot was created, we need to run more passes
44    because we have new addresses which should be checked and because
45    the old address displacements might change and address constraints
46    (or insn memory constraints) might not be satisfied any more.
47
48    For some targets, the pass can spill some pseudos into hard
49    registers of different class (usually into vector registers)
50    instead of spilling them into memory if it is possible and
51    profitable.  Spilling GENERAL_REGS pseudo into SSE registers for
52    Intel Corei7 is an example of such optimization.  And this is
53    actually recommended by Intel optimization guide.
54
55    The file also contains code for final change of pseudos on hard
56    regs correspondingly assigned to them.  */
57
58 #include "config.h"
59 #include "system.h"
60 #include "coretypes.h"
61 #include "tm.h"
62 #include "rtl.h"
63 #include "tm_p.h"
64 #include "insn-config.h"
65 #include "recog.h"
66 #include "output.h"
67 #include "regs.h"
68 #include "hard-reg-set.h"
69 #include "flags.h"
70 #include "function.h"
71 #include "expr.h"
72 #include "basic-block.h"
73 #include "except.h"
74 #include "timevar.h"
75 #include "target.h"
76 #include "lra-int.h"
77 #include "ira.h"
78 #include "df.h"
79
80
81 /* Max regno at the start of the pass.  */
82 static int regs_num;
83
84 /* Map spilled regno -> hard regno used instead of memory for
85    spilling.  */
86 static rtx *spill_hard_reg;
87
88 /* The structure describes stack slot of a spilled pseudo.  */
89 struct pseudo_slot
90 {
91   /* Number (0, 1, ...) of the stack slot to which given pseudo
92      belongs.  */
93   int slot_num;
94   /* First or next slot with the same slot number.  */
95   struct pseudo_slot *next, *first;
96   /* Memory representing the spilled pseudo.  */
97   rtx mem;
98 };
99
100 /* The stack slots for each spilled pseudo.  Indexed by regnos.  */
101 static struct pseudo_slot *pseudo_slots;
102
103 /* The structure describes a register or a stack slot which can be
104    used for several spilled pseudos.  */
105 struct slot
106 {
107   /* First pseudo with given stack slot.  */
108   int regno;
109   /* Hard reg into which the slot pseudos are spilled.  The value is
110      negative for pseudos spilled into memory.  */
111   int hard_regno;
112   /* Memory representing the all stack slot.  It can be different from
113      memory representing a pseudo belonging to give stack slot because
114      pseudo can be placed in a part of the corresponding stack slot.
115      The value is NULL for pseudos spilled into a hard reg.  */
116   rtx mem;
117   /* Combined live ranges of all pseudos belonging to given slot.  It
118      is used to figure out that a new spilled pseudo can use given
119      stack slot.  */
120   lra_live_range_t live_ranges;
121 };
122
123 /* Array containing info about the stack slots.  The array element is
124    indexed by the stack slot number in the range [0..slots_num).  */
125 static struct slot *slots;
126 /* The number of the stack slots currently existing.  */
127 static int slots_num;
128
129 /* Set up memory of the spilled pseudo I.  The function can allocate
130    the corresponding stack slot if it is not done yet.  */
131 static void
132 assign_mem_slot (int i)
133 {
134   rtx x = NULL_RTX;
135   enum machine_mode mode = GET_MODE (regno_reg_rtx[i]);
136   unsigned int inherent_size = PSEUDO_REGNO_BYTES (i);
137   unsigned int inherent_align = GET_MODE_ALIGNMENT (mode);
138   unsigned int max_ref_width = GET_MODE_SIZE (lra_reg_info[i].biggest_mode);
139   unsigned int total_size = MAX (inherent_size, max_ref_width);
140   unsigned int min_align = max_ref_width * BITS_PER_UNIT;
141   int adjust = 0;
142
143   lra_assert (regno_reg_rtx[i] != NULL_RTX && REG_P (regno_reg_rtx[i])
144               && lra_reg_info[i].nrefs != 0 && reg_renumber[i] < 0);
145
146   x = slots[pseudo_slots[i].slot_num].mem;
147
148   /* We can use a slot already allocated because it is guaranteed the
149      slot provides both enough inherent space and enough total
150      space.  */
151   if (x)
152     ;
153   /* Each pseudo has an inherent size which comes from its own mode,
154      and a total size which provides room for paradoxical subregs
155      which refer to the pseudo reg in wider modes.  We allocate a new
156      slot, making sure that it has enough inherent space and total
157      space.  */
158   else
159     {
160       rtx stack_slot;
161
162       /* No known place to spill from => no slot to reuse.  */
163       x = assign_stack_local (mode, total_size,
164                               min_align > inherent_align
165                               || total_size > inherent_size ? -1 : 0);
166       stack_slot = x;
167       /* Cancel the big-endian correction done in assign_stack_local.
168          Get the address of the beginning of the slot.  This is so we
169          can do a big-endian correction unconditionally below.  */
170       if (BYTES_BIG_ENDIAN)
171         {
172           adjust = inherent_size - total_size;
173           if (adjust)
174             stack_slot
175               = adjust_address_nv (x,
176                                    mode_for_size (total_size * BITS_PER_UNIT,
177                                                   MODE_INT, 1),
178                                    adjust);
179         }
180       slots[pseudo_slots[i].slot_num].mem = stack_slot;
181     }
182
183   /* On a big endian machine, the "address" of the slot is the address
184      of the low part that fits its inherent mode.  */
185   if (BYTES_BIG_ENDIAN && inherent_size < total_size)
186     adjust += (total_size - inherent_size);
187
188   x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
189
190   /* Set all of the memory attributes as appropriate for a spill.  */
191   set_mem_attrs_for_spill (x);
192   pseudo_slots[i].mem = x;
193 }
194
195 /* Sort pseudos according their usage frequencies.  */
196 static int
197 regno_freq_compare (const void *v1p, const void *v2p)
198 {
199   const int regno1 = *(const int *) v1p;
200   const int regno2 = *(const int *) v2p;
201   int diff;
202
203   if ((diff = lra_reg_info[regno2].freq - lra_reg_info[regno1].freq) != 0)
204     return diff;
205   return regno1 - regno2;
206 }
207
208 /* Redefine STACK_GROWS_DOWNWARD in terms of 0 or 1.  */
209 #ifdef STACK_GROWS_DOWNWARD
210 # undef STACK_GROWS_DOWNWARD
211 # define STACK_GROWS_DOWNWARD 1
212 #else
213 # define STACK_GROWS_DOWNWARD 0
214 #endif
215
216 /* Sort pseudos according to their slots, putting the slots in the order
217    that they should be allocated.  Slots with lower numbers have the highest
218    priority and should get the smallest displacement from the stack or
219    frame pointer (whichever is being used).
220
221    The first allocated slot is always closest to the frame pointer,
222    so prefer lower slot numbers when frame_pointer_needed.  If the stack
223    and frame grow in the same direction, then the first allocated slot is
224    always closest to the initial stack pointer and furthest away from the
225    final stack pointer, so allocate higher numbers first when using the
226    stack pointer in that case.  The reverse is true if the stack and
227    frame grow in opposite directions.  */
228 static int
229 pseudo_reg_slot_compare (const void *v1p, const void *v2p)
230 {
231   const int regno1 = *(const int *) v1p;
232   const int regno2 = *(const int *) v2p;
233   int diff, slot_num1, slot_num2;
234   int total_size1, total_size2;
235
236   slot_num1 = pseudo_slots[regno1].slot_num;
237   slot_num2 = pseudo_slots[regno2].slot_num;
238   if ((diff = slot_num1 - slot_num2) != 0)
239     return (frame_pointer_needed
240             || (!FRAME_GROWS_DOWNWARD) == STACK_GROWS_DOWNWARD ? diff : -diff);
241   total_size1 = GET_MODE_SIZE (lra_reg_info[regno1].biggest_mode);
242   total_size2 = GET_MODE_SIZE (lra_reg_info[regno2].biggest_mode);
243   if ((diff = total_size2 - total_size1) != 0)
244     return diff;
245   return regno1 - regno2;
246 }
247
248 /* Assign spill hard registers to N pseudos in PSEUDO_REGNOS which is
249    sorted in order of highest frequency first.  Put the pseudos which
250    did not get a spill hard register at the beginning of array
251    PSEUDO_REGNOS.  Return the number of such pseudos.  */
252 static int
253 assign_spill_hard_regs (int *pseudo_regnos, int n)
254 {
255   int i, k, p, regno, res, spill_class_size, hard_regno, nr;
256   enum reg_class rclass, spill_class;
257   enum machine_mode mode;
258   lra_live_range_t r;
259   rtx_insn *insn;
260   rtx set;
261   basic_block bb;
262   HARD_REG_SET conflict_hard_regs;
263   bitmap_head ok_insn_bitmap;
264   bitmap setjump_crosses = regstat_get_setjmp_crosses ();
265   /* Hard registers which can not be used for any purpose at given
266      program point because they are unallocatable or already allocated
267      for other pseudos.  */
268   HARD_REG_SET *reserved_hard_regs;
269
270   if (! lra_reg_spill_p)
271     return n;
272   /* Set up reserved hard regs for every program point.  */
273   reserved_hard_regs = XNEWVEC (HARD_REG_SET, lra_live_max_point);
274   for (p = 0; p < lra_live_max_point; p++)
275     COPY_HARD_REG_SET (reserved_hard_regs[p], lra_no_alloc_regs);
276   for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
277     if (lra_reg_info[i].nrefs != 0
278         && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
279       for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
280         for (p = r->start; p <= r->finish; p++)
281           add_to_hard_reg_set (&reserved_hard_regs[p],
282                                lra_reg_info[i].biggest_mode, hard_regno);
283   bitmap_initialize (&ok_insn_bitmap, &reg_obstack);
284   FOR_EACH_BB_FN (bb, cfun)
285     FOR_BB_INSNS (bb, insn)
286       if (DEBUG_INSN_P (insn)
287           || ((set = single_set (insn)) != NULL_RTX
288               && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))))
289         bitmap_set_bit (&ok_insn_bitmap, INSN_UID (insn));
290   for (res = i = 0; i < n; i++)
291     {
292       regno = pseudo_regnos[i];
293       rclass = lra_get_allocno_class (regno);
294       if (bitmap_bit_p (setjump_crosses, regno)
295           || (spill_class
296               = ((enum reg_class)
297                  targetm.spill_class ((reg_class_t) rclass,
298                                       PSEUDO_REGNO_MODE (regno)))) == NO_REGS
299           || bitmap_intersect_compl_p (&lra_reg_info[regno].insn_bitmap,
300                                        &ok_insn_bitmap))
301         {
302           pseudo_regnos[res++] = regno;
303           continue;
304         }
305       lra_assert (spill_class != NO_REGS);
306       COPY_HARD_REG_SET (conflict_hard_regs,
307                          lra_reg_info[regno].conflict_hard_regs);
308       for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
309         for (p = r->start; p <= r->finish; p++)
310           IOR_HARD_REG_SET (conflict_hard_regs, reserved_hard_regs[p]);
311       spill_class_size = ira_class_hard_regs_num[spill_class];
312       mode = lra_reg_info[regno].biggest_mode;
313       for (k = 0; k < spill_class_size; k++)
314         {
315           hard_regno = ira_class_hard_regs[spill_class][k];
316           if (! overlaps_hard_reg_set_p (conflict_hard_regs, mode, hard_regno))
317             break;
318         }
319       if (k >= spill_class_size)
320         {
321            /* There is no available regs -- assign memory later.  */
322           pseudo_regnos[res++] = regno;
323           continue;
324         }
325       if (lra_dump_file != NULL)
326         fprintf (lra_dump_file, "  Spill r%d into hr%d\n", regno, hard_regno);
327       /* Update reserved_hard_regs.  */
328       for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
329         for (p = r->start; p <= r->finish; p++)
330           add_to_hard_reg_set (&reserved_hard_regs[p],
331                                lra_reg_info[regno].biggest_mode, hard_regno);
332       spill_hard_reg[regno]
333         = gen_raw_REG (PSEUDO_REGNO_MODE (regno), hard_regno);
334       for (nr = 0;
335            nr < hard_regno_nregs[hard_regno][lra_reg_info[regno].biggest_mode];
336            nr++)
337         /* Just loop.  */
338         df_set_regs_ever_live (hard_regno + nr, true);
339     }
340   bitmap_clear (&ok_insn_bitmap);
341   free (reserved_hard_regs);
342   return res;
343 }
344
345 /* Add pseudo REGNO to slot SLOT_NUM.  */
346 static void
347 add_pseudo_to_slot (int regno, int slot_num)
348 {
349   struct pseudo_slot *first;
350
351   if (slots[slot_num].regno < 0)
352     {
353       /* It is the first pseudo in the slot.  */
354       slots[slot_num].regno = regno;
355       pseudo_slots[regno].first = &pseudo_slots[regno];
356       pseudo_slots[regno].next = NULL;
357     }
358   else
359     {
360       first = pseudo_slots[regno].first = &pseudo_slots[slots[slot_num].regno];
361       pseudo_slots[regno].next = first->next;
362       first->next = &pseudo_slots[regno];
363     }
364   pseudo_slots[regno].mem = NULL_RTX;
365   pseudo_slots[regno].slot_num = slot_num;
366   slots[slot_num].live_ranges
367     = lra_merge_live_ranges (slots[slot_num].live_ranges,
368                              lra_copy_live_range_list
369                              (lra_reg_info[regno].live_ranges));
370 }
371
372 /* Assign stack slot numbers to pseudos in array PSEUDO_REGNOS of
373    length N.  Sort pseudos in PSEUDO_REGNOS for subsequent assigning
374    memory stack slots.  */
375 static void
376 assign_stack_slot_num_and_sort_pseudos (int *pseudo_regnos, int n)
377 {
378   int i, j, regno;
379
380   slots_num = 0;
381   /* Assign stack slot numbers to spilled pseudos, use smaller numbers
382      for most frequently used pseudos.  */
383   for (i = 0; i < n; i++)
384     {
385       regno = pseudo_regnos[i];
386       if (! flag_ira_share_spill_slots)
387         j = slots_num;
388       else
389         {
390           for (j = 0; j < slots_num; j++)
391             if (slots[j].hard_regno < 0
392                 && ! (lra_intersected_live_ranges_p
393                       (slots[j].live_ranges,
394                        lra_reg_info[regno].live_ranges)))
395               break;
396         }
397       if (j >= slots_num)
398         {
399           /* New slot.  */
400           slots[j].live_ranges = NULL;
401           slots[j].regno = slots[j].hard_regno = -1;
402           slots[j].mem = NULL_RTX;
403           slots_num++;
404         }
405       add_pseudo_to_slot (regno, j);
406     }
407   /* Sort regnos according to their slot numbers.  */
408   qsort (pseudo_regnos, n, sizeof (int), pseudo_reg_slot_compare);
409 }
410
411 /* Recursively process LOC in INSN and change spilled pseudos to the
412    corresponding memory or spilled hard reg.  Ignore spilled pseudos
413    created from the scratches.  */
414 static void
415 remove_pseudos (rtx *loc, rtx_insn *insn)
416 {
417   int i;
418   rtx hard_reg;
419   const char *fmt;
420   enum rtx_code code;
421
422   if (*loc == NULL_RTX)
423     return;
424   code = GET_CODE (*loc);
425   if (code == REG && (i = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
426       && lra_get_regno_hard_regno (i) < 0
427       /* We do not want to assign memory for former scratches because
428          it might result in an address reload for some targets.  In
429          any case we transform such pseudos not getting hard registers
430          into scratches back.  */
431       && ! lra_former_scratch_p (i))
432     {
433       if ((hard_reg = spill_hard_reg[i]) != NULL_RTX)
434         *loc = copy_rtx (hard_reg);
435       else
436         {
437           rtx x = lra_eliminate_regs_1 (insn, pseudo_slots[i].mem,
438                                         GET_MODE (pseudo_slots[i].mem),
439                                         false, false, true);
440           *loc = x != pseudo_slots[i].mem ? x : copy_rtx (x);
441         }
442       return;
443     }
444
445   fmt = GET_RTX_FORMAT (code);
446   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
447     {
448       if (fmt[i] == 'e')
449         remove_pseudos (&XEXP (*loc, i), insn);
450       else if (fmt[i] == 'E')
451         {
452           int j;
453
454           for (j = XVECLEN (*loc, i) - 1; j >= 0; j--)
455             remove_pseudos (&XVECEXP (*loc, i, j), insn);
456         }
457     }
458 }
459
460 /* Convert spilled pseudos into their stack slots or spill hard regs,
461    put insns to process on the constraint stack (that is all insns in
462    which pseudos were changed to memory or spill hard regs).   */
463 static void
464 spill_pseudos (void)
465 {
466   basic_block bb;
467   rtx_insn *insn;
468   int i;
469   bitmap_head spilled_pseudos, changed_insns;
470
471   bitmap_initialize (&spilled_pseudos, &reg_obstack);
472   bitmap_initialize (&changed_insns, &reg_obstack);
473   for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
474     {
475       if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
476           && ! lra_former_scratch_p (i))
477         {
478           bitmap_set_bit (&spilled_pseudos, i);
479           bitmap_ior_into (&changed_insns, &lra_reg_info[i].insn_bitmap);
480         }
481     }
482   FOR_EACH_BB_FN (bb, cfun)
483     {
484       FOR_BB_INSNS (bb, insn)
485         if (bitmap_bit_p (&changed_insns, INSN_UID (insn)))
486           {
487             rtx *link_loc, link;
488             remove_pseudos (&PATTERN (insn), insn);
489             if (CALL_P (insn))
490               remove_pseudos (&CALL_INSN_FUNCTION_USAGE (insn), insn);
491             for (link_loc = &REG_NOTES (insn);
492                  (link = *link_loc) != NULL_RTX;
493                  link_loc = &XEXP (link, 1))
494               {
495                 switch (REG_NOTE_KIND (link))
496                   {
497                   case REG_FRAME_RELATED_EXPR:
498                   case REG_CFA_DEF_CFA:
499                   case REG_CFA_ADJUST_CFA:
500                   case REG_CFA_OFFSET:
501                   case REG_CFA_REGISTER:
502                   case REG_CFA_EXPRESSION:
503                   case REG_CFA_RESTORE:
504                   case REG_CFA_SET_VDRAP:
505                     remove_pseudos (&XEXP (link, 0), insn);
506                     break;
507                   default:
508                     break;
509                   }
510               }
511             if (lra_dump_file != NULL)
512               fprintf (lra_dump_file,
513                        "Changing spilled pseudos to memory in insn #%u\n",
514                        INSN_UID (insn));
515             lra_push_insn (insn);
516             if (lra_reg_spill_p || targetm.different_addr_displacement_p ())
517               lra_set_used_insn_alternative (insn, -1);
518           }
519         else if (CALL_P (insn))
520           /* Presence of any pseudo in CALL_INSN_FUNCTION_USAGE does
521              not affect value of insn_bitmap of the corresponding
522              lra_reg_info.  That is because we don't need to reload
523              pseudos in CALL_INSN_FUNCTION_USAGEs.  So if we process
524              only insns in the insn_bitmap of given pseudo here, we
525              can miss the pseudo in some
526              CALL_INSN_FUNCTION_USAGEs.  */
527           remove_pseudos (&CALL_INSN_FUNCTION_USAGE (insn), insn);
528       bitmap_and_compl_into (df_get_live_in (bb), &spilled_pseudos);
529       bitmap_and_compl_into (df_get_live_out (bb), &spilled_pseudos);
530     }
531   bitmap_clear (&spilled_pseudos);
532   bitmap_clear (&changed_insns);
533 }
534
535 /* Return true if we need to change some pseudos into memory.  */
536 bool
537 lra_need_for_spills_p (void)
538 {
539   int i; max_regno = max_reg_num ();
540
541   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
542     if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
543         && ! lra_former_scratch_p (i))
544       return true;
545   return false;
546 }
547
548 /* Change spilled pseudos into memory or spill hard regs.  Put changed
549    insns on the constraint stack (these insns will be considered on
550    the next constraint pass).  The changed insns are all insns in
551    which pseudos were changed.  */
552 void
553 lra_spill (void)
554 {
555   int i, n, curr_regno;
556   int *pseudo_regnos;
557
558   regs_num = max_reg_num ();
559   spill_hard_reg = XNEWVEC (rtx, regs_num);
560   pseudo_regnos = XNEWVEC (int, regs_num);
561   for (n = 0, i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
562     if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
563         /* We do not want to assign memory for former scratches.  */
564         && ! lra_former_scratch_p (i))
565       {
566         spill_hard_reg[i] = NULL_RTX;
567         pseudo_regnos[n++] = i;
568       }
569   lra_assert (n > 0);
570   pseudo_slots = XNEWVEC (struct pseudo_slot, regs_num);
571   slots = XNEWVEC (struct slot, regs_num);
572   /* Sort regnos according their usage frequencies.  */
573   qsort (pseudo_regnos, n, sizeof (int), regno_freq_compare);
574   n = assign_spill_hard_regs (pseudo_regnos, n);
575   assign_stack_slot_num_and_sort_pseudos (pseudo_regnos, n);
576   for (i = 0; i < n; i++)
577     if (pseudo_slots[pseudo_regnos[i]].mem == NULL_RTX)
578       assign_mem_slot (pseudo_regnos[i]);
579   if (n > 0 && crtl->stack_alignment_needed)
580     /* If we have a stack frame, we must align it now.  The stack size
581        may be a part of the offset computation for register
582        elimination.  */
583     assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
584   if (lra_dump_file != NULL)
585     {
586       for (i = 0; i < slots_num; i++)
587         {
588           fprintf (lra_dump_file, "  Slot %d regnos (width = %d):", i,
589                    GET_MODE_SIZE (GET_MODE (slots[i].mem)));
590           for (curr_regno = slots[i].regno;;
591                curr_regno = pseudo_slots[curr_regno].next - pseudo_slots)
592             {
593               fprintf (lra_dump_file, "  %d", curr_regno);
594               if (pseudo_slots[curr_regno].next == NULL)
595                 break;
596             }
597           fprintf (lra_dump_file, "\n");
598         }
599     }
600   spill_pseudos ();
601   free (slots);
602   free (pseudo_slots);
603   free (pseudo_regnos);
604   free (spill_hard_reg);
605 }
606
607 /* Apply alter_subreg for subregs of regs in *LOC.  Use FINAL_P for
608    alter_subreg calls. Return true if any subreg of reg is
609    processed.  */
610 static bool
611 alter_subregs (rtx *loc, bool final_p)
612 {
613   int i;
614   rtx x = *loc;
615   bool res;
616   const char *fmt;
617   enum rtx_code code;
618
619   if (x == NULL_RTX)
620     return false;
621   code = GET_CODE (x);
622   if (code == SUBREG && REG_P (SUBREG_REG (x)))
623     {
624       lra_assert (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER);
625       alter_subreg (loc, final_p);
626       return true;
627     }
628   fmt = GET_RTX_FORMAT (code);
629   res = false;
630   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
631     {
632       if (fmt[i] == 'e')
633         {
634           if (alter_subregs (&XEXP (x, i), final_p))
635             res = true;
636         }
637       else if (fmt[i] == 'E')
638         {
639           int j;
640
641           for (j = XVECLEN (x, i) - 1; j >= 0; j--)
642             if (alter_subregs (&XVECEXP (x, i, j), final_p))
643               res = true;
644         }
645     }
646   return res;
647 }
648
649 /* Return true if REGNO is used for return in the current
650    function.  */
651 static bool
652 return_regno_p (unsigned int regno)
653 {
654   rtx outgoing = crtl->return_rtx;
655
656   if (! outgoing)
657     return false;
658
659   if (REG_P (outgoing))
660     return REGNO (outgoing) == regno;
661   else if (GET_CODE (outgoing) == PARALLEL)
662     {
663       int i;
664
665       for (i = 0; i < XVECLEN (outgoing, 0); i++)
666         {
667           rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
668
669           if (REG_P (x) && REGNO (x) == regno)
670             return true;
671         }
672     }
673   return false;
674 }
675
676 /* Final change of pseudos got hard registers into the corresponding
677    hard registers and removing temporary clobbers.  */
678 void
679 lra_final_code_change (void)
680 {
681   int i, hard_regno;
682   basic_block bb;
683   rtx_insn *insn, *curr;
684   int max_regno = max_reg_num ();
685
686   for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
687     if (lra_reg_info[i].nrefs != 0
688         && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
689       SET_REGNO (regno_reg_rtx[i], hard_regno);
690   FOR_EACH_BB_FN (bb, cfun)
691     FOR_BB_INSNS_SAFE (bb, insn, curr)
692       if (INSN_P (insn))
693         {
694           rtx pat = PATTERN (insn);
695
696           if (GET_CODE (pat) == CLOBBER && LRA_TEMP_CLOBBER_P (pat))
697             {
698               /* Remove clobbers temporarily created in LRA.  We don't
699                  need them anymore and don't want to waste compiler
700                  time processing them in a few subsequent passes.  */
701               lra_invalidate_insn_data (insn);
702               delete_insn (insn);
703               continue;
704             }
705
706           /* IRA can generate move insns involving pseudos.  It is
707              better remove them earlier to speed up compiler a bit.
708              It is also better to do it here as they might not pass
709              final RTL check in LRA, (e.g. insn moving a control
710              register into itself).  So remove an useless move insn
711              unless next insn is USE marking the return reg (we should
712              save this as some subsequent optimizations assume that
713              such original insns are saved).  */
714           if (NONJUMP_INSN_P (insn) && GET_CODE (pat) == SET
715               && REG_P (SET_SRC (pat)) && REG_P (SET_DEST (pat))
716               && REGNO (SET_SRC (pat)) == REGNO (SET_DEST (pat))
717               && ! return_regno_p (REGNO (SET_SRC (pat))))
718             {
719               lra_invalidate_insn_data (insn);
720               delete_insn (insn);
721               continue;
722             }
723         
724           lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
725           struct lra_static_insn_data *static_id = id->insn_static_data;
726           bool insn_change_p = false;
727
728           for (i = id->insn_static_data->n_operands - 1; i >= 0; i--)
729             if ((DEBUG_INSN_P (insn) || ! static_id->operand[i].is_operator)
730                 && alter_subregs (id->operand_loc[i], ! DEBUG_INSN_P (insn)))
731               {
732                 lra_update_dup (id, i);
733                 insn_change_p = true;
734               }
735           if (insn_change_p)
736             lra_update_operator_dups (id);
737         }
738 }