OSDN Git Service

bc93b8918173184f56ffe6dd13807b8478d22302
[pf3gnuchains/gcc-fork.git] / gcc / ra-conflict.c
1 /* Allocate registers for pseudo-registers that span basic blocks.
2    Copyright (C) 2007 Free Software Foundation, Inc.
3    Contributed by Kenneth Zadeck <zadeck@naturalbridge.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 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "machmode.h"
27 #include "hard-reg-set.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "flags.h"
31 #include "regs.h"
32 #include "function.h"
33 #include "insn-config.h"
34 #include "recog.h"
35 #include "reload.h"
36 #include "output.h"
37 #include "toplev.h"
38 #include "tree-pass.h"
39 #include "timevar.h"
40 #include "df.h"
41 #include "vecprim.h"
42 #include "ra.h"
43 #include "sbitmap.h"
44 #include "sparseset.h"
45
46 /* Externs defined in regs.h.  */
47
48 int max_allocno;
49 struct allocno *allocno;
50 HOST_WIDEST_FAST_INT *conflicts;
51 int *reg_allocno;
52 HOST_WIDE_INT *partial_bitnum;
53 HOST_WIDE_INT max_bitnum;
54 alloc_pool adjacency_pool;
55 adjacency_t **adjacency;
56
57 typedef struct df_ref * df_ref_t;
58 DEF_VEC_P(df_ref_t);
59 DEF_VEC_ALLOC_P(df_ref_t,heap);
60
61 /* Macros to determine the bit number within the triangular bit matrix for
62    the two allocnos Low and HIGH, with LOW strictly less than HIGH.  */
63
64 #define CONFLICT_BITNUM(I, J) \
65   (((I) < (J)) ? (partial_bitnum[I] + (J)) : (partial_bitnum[J] + (I)))
66
67 #define CONFLICT_BITNUM_FAST(I, I_PARTIAL_BITNUM, J) \
68   (((I) < (J)) ? ((I_PARTIAL_BITNUM) + (J)) : (partial_bitnum[J] + (I)))
69
70 bool
71 conflict_p (int allocno1, int allocno2)
72 {
73   HOST_WIDE_INT bitnum;
74   HOST_WIDEST_FAST_INT word, mask;
75
76 #ifdef ENABLE_CHECKING
77   int blk1, blk2;
78
79   gcc_assert (allocno1 >= 0 && allocno1 < max_allocno);
80   gcc_assert (allocno2 >= 0 && allocno2 < max_allocno);
81
82   blk1 = regno_basic_block (allocno[allocno1].reg);
83   blk2 = regno_basic_block (allocno[allocno2].reg);
84   gcc_assert (blk1 == 0 || blk2 == 0 || blk1 == blk2);
85 #endif
86
87   if (allocno1 == allocno2)
88     /* By definition, an allocno does not conflict with itself.  */
89     return 0;
90
91   bitnum = CONFLICT_BITNUM (allocno1, allocno2);
92
93 #ifdef ENABLE_CHECKING
94   gcc_assert (bitnum >= 0 && bitnum < max_bitnum);
95 #endif
96
97   word = conflicts[bitnum / HOST_BITS_PER_WIDEST_FAST_INT];
98   mask = (HOST_WIDEST_FAST_INT) 1 << (bitnum % HOST_BITS_PER_WIDEST_FAST_INT);
99   return (word & mask) != 0;
100 }
101
102 /* Add conflict edges between ALLOCNO1 and ALLOCNO2.  */
103
104 static void
105 set_conflict (int allocno1, int allocno2)
106 {
107   HOST_WIDE_INT bitnum, index;
108   HOST_WIDEST_FAST_INT word, mask;
109
110 #ifdef ENABLE_CHECKING
111   int blk1, blk2;
112
113   gcc_assert (allocno1 >= 0 && allocno1 < max_allocno);
114   gcc_assert (allocno2 >= 0 && allocno2 < max_allocno);
115
116   blk1 = regno_basic_block (allocno[allocno1].reg);
117   blk2 = regno_basic_block (allocno[allocno2].reg);
118   gcc_assert (blk1 == 0 || blk2 == 0 || blk1 == blk2);
119 #endif
120
121   /* By definition, an allocno does not conflict with itself.  */
122   if (allocno1 == allocno2)
123     return;
124
125   bitnum = CONFLICT_BITNUM (allocno1, allocno2);
126
127 #ifdef ENABLE_CHECKING
128   gcc_assert (bitnum >= 0 && bitnum < max_bitnum);
129 #endif
130
131   index = bitnum / HOST_BITS_PER_WIDEST_FAST_INT;
132   word = conflicts[index];
133   mask = (HOST_WIDEST_FAST_INT) 1 << (bitnum % HOST_BITS_PER_WIDEST_FAST_INT);
134
135   if ((word & mask) == 0)
136     {
137       conflicts[index] = word | mask;
138       add_neighbor (allocno1, allocno2);
139       add_neighbor (allocno2, allocno1);
140     }
141 }
142
143 /* Add conflict edges between ALLOCNO1 and all allocnos currently live.  */
144
145 static void
146 set_conflicts (int allocno1, sparseset live)
147 {
148   int i;
149   HOST_WIDE_INT bitnum, index;
150   HOST_WIDEST_FAST_INT word, mask;
151   HOST_WIDE_INT partial_bitnum_allocno1;
152
153 #ifdef ENABLE_CHECKING
154   gcc_assert (allocno1 >= 0 && allocno1 < max_allocno);
155 #endif
156
157   partial_bitnum_allocno1 = partial_bitnum[allocno1];
158
159   EXECUTE_IF_SET_IN_SPARSESET (live, i)
160   {
161     /* By definition, an allocno does not conflict with itself.  */
162     if (allocno1 == i)
163       continue;
164
165 #ifdef ENABLE_CHECKING
166     gcc_assert (i >= 0 && i < max_allocno);
167 #endif
168
169     bitnum = CONFLICT_BITNUM_FAST (allocno1, partial_bitnum_allocno1, i);
170
171 #ifdef ENABLE_CHECKING
172     gcc_assert (bitnum >= 0 && bitnum < max_bitnum);
173 #endif
174
175     index = bitnum / HOST_BITS_PER_WIDEST_FAST_INT;
176     word = conflicts[index];
177     mask = (HOST_WIDEST_FAST_INT) 1 << (bitnum % HOST_BITS_PER_WIDEST_FAST_INT);
178
179     if ((word & mask) == 0)
180       {
181         conflicts[index] = word | mask;
182         add_neighbor (allocno1, i);
183         add_neighbor (i, allocno1);
184       }
185   }
186 }
187
188
189 /* Add a conflict between R1 and R2.  */
190
191 static void
192 record_one_conflict_between_regnos (enum machine_mode mode1, int r1, 
193                                     enum machine_mode mode2, int r2)
194 {
195   int allocno1 = reg_allocno[r1];
196   int allocno2 = reg_allocno[r2];
197
198   if (dump_file)
199     fprintf (dump_file, "    rocbr adding %d<=>%d\n", r1, r2);
200
201   if (allocno1 >= 0 && allocno2 >= 0)
202     set_conflict (allocno1, allocno2);
203   else if (allocno1 >= 0)
204     {
205       if (r2 < FIRST_PSEUDO_REGISTER)
206         add_to_hard_reg_set (&allocno[allocno1].hard_reg_conflicts, mode2, r2);
207     }
208   else if (allocno2 >= 0)
209     {
210       if (r1 < FIRST_PSEUDO_REGISTER)
211         add_to_hard_reg_set (&allocno[allocno2].hard_reg_conflicts, mode1, r1);
212     }
213
214   /* Now, recursively handle the reg_renumber cases.  */
215   if (reg_renumber[r1] >= 0)
216     record_one_conflict_between_regnos (mode1, reg_renumber[r1], mode2, r2);
217
218   if (reg_renumber[r2] >= 0)
219     record_one_conflict_between_regnos (mode1, r1, mode2, reg_renumber[r2]);
220 }
221
222
223 /* Record a conflict between register REGNO and everything currently
224    live.  REGNO must not be a pseudo reg that was allocated by
225    local_alloc; such numbers must be translated through reg_renumber
226    before calling here.  */
227
228 static void
229 record_one_conflict (sparseset allocnos_live, 
230                      HARD_REG_SET *hard_regs_live, int regno)
231 {
232   int i;
233
234   if (regno < FIRST_PSEUDO_REGISTER)
235     /* When a hard register becomes live, record conflicts with live
236        pseudo regs.  */
237     EXECUTE_IF_SET_IN_SPARSESET (allocnos_live, i)
238       {
239         SET_HARD_REG_BIT (allocno[i].hard_reg_conflicts, regno);
240         if (dump_file)
241           fprintf (dump_file, "  roc adding %d<=>%d\n", allocno[i].reg, regno);
242       }
243   else
244     /* When a pseudo-register becomes live, record conflicts first
245        with hard regs, then with other pseudo regs.  */
246     {
247       int ialloc = reg_allocno[regno];
248
249       if (dump_file)
250         {
251           fprintf (dump_file, "  roc adding %d<=>(", regno);
252           for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
253             if (TEST_HARD_REG_BIT (*hard_regs_live, i) 
254                 && !TEST_HARD_REG_BIT (allocno[ialloc].hard_reg_conflicts, i))
255               fprintf (dump_file, "%d ", i);
256           
257           EXECUTE_IF_SET_IN_SPARSESET (allocnos_live, i)
258             {
259               if (!conflict_p (ialloc, i))
260                 fprintf (dump_file, "%d ", allocno[i].reg);
261             }
262           fprintf (dump_file, ")\n");
263         }
264
265       IOR_HARD_REG_SET (allocno[ialloc].hard_reg_conflicts, *hard_regs_live);
266       set_conflicts (ialloc, allocnos_live);
267     }
268 }
269
270
271 /* Handle the case where REG is set by the insn being scanned, during
272    the backward scan to accumulate conflicts.  Record a conflict with
273    all other registers already live.
274
275    REG might actually be something other than a register; if so, we do
276    nothing.  */
277
278 static void
279 mark_reg_store (sparseset allocnos_live, 
280                 HARD_REG_SET *hard_regs_live, 
281                 struct df_ref *ref)
282 {
283   rtx reg = DF_REF_REG (ref);
284   unsigned int regno = DF_REF_REGNO (ref);
285   enum machine_mode mode = GET_MODE (reg);
286
287   /* Either this is one of the max_allocno pseudo regs not allocated,
288      or it is or has a hardware reg.  First handle the pseudo-regs.  */
289   if (regno >= FIRST_PSEUDO_REGISTER && reg_allocno[regno] >= 0)
290     record_one_conflict (allocnos_live, hard_regs_live, regno);
291
292   if (reg_renumber[regno] >= 0)
293     regno = reg_renumber[regno];
294
295   /* Handle hardware regs (and pseudos allocated to hard regs).  */
296   if (regno < FIRST_PSEUDO_REGISTER && ! fixed_regs[regno])
297     {
298       unsigned int start = regno;
299       unsigned int last = end_hard_regno (mode, regno);
300       if ((GET_CODE (reg) == SUBREG) && !DF_REF_FLAGS_IS_SET (ref, DF_REF_EXTRACT))
301         {
302           start += subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
303                                         SUBREG_BYTE (reg), GET_MODE (reg));
304           last = start + subreg_nregs_with_regno (regno, reg);
305         }
306
307       regno = start;
308       while (regno < last)
309         record_one_conflict (allocnos_live, hard_regs_live, regno++);
310     }
311 }
312
313
314 /* Return true if REGNO with MODE can be assigned to a register in
315    CL.  */
316
317 static bool
318 may_overlap_class_p (enum machine_mode mode, unsigned int regno, 
319                      enum reg_class rc)
320 {
321   if (regno >= FIRST_PSEUDO_REGISTER)
322     {
323       enum reg_class pref_class = reg_preferred_class (regno);
324       enum reg_class alt_class = reg_alternate_class (regno);
325       return (reg_classes_intersect_p (rc, pref_class)
326               || reg_classes_intersect_p (rc, alt_class));
327     }
328   else
329     return in_hard_reg_set_p (reg_class_contents[rc], mode, regno);
330 }
331
332
333 /* SRC is an input operand to an instruction in which register DEST is
334    an output operand.  SRC may be bound to a member of class SRC_CLASS
335    and DEST may be bound to an earlyclobbered register that overlaps
336    SRC_CLASS.  If SRC is a register that might be allocated a member
337    of SRC_CLASS, add a conflict between it and DEST.  */
338
339 static void
340 add_conflicts_for_earlyclobber (rtx dest, enum reg_class src_class, rtx src)
341 {
342   if (GET_CODE (src) == SUBREG)
343     src = SUBREG_REG (src);
344   if (REG_P (src)
345       && may_overlap_class_p (GET_MODE (src), REGNO (src), src_class))
346     record_one_conflict_between_regnos (GET_MODE (src), REGNO (src),
347                                         GET_MODE (dest), REGNO (dest));
348 }
349
350
351 /* Look at the defs in INSN and determine if any of them are marked as
352    early clobber.  If they are marked as early clobber, add a conflict
353    between any input operand that could be allocated to the same
354    register.  */
355
356 static void
357 set_conflicts_for_earlyclobber (rtx insn)
358 {
359   int alt;
360   int def;
361   int use;
362
363   extract_insn (insn);
364   preprocess_constraints ();
365
366   if (dump_file) 
367     fprintf (dump_file, "  starting early clobber conflicts.\n");
368
369   for (alt = 0; alt < recog_data.n_alternatives; alt++)
370     for (def = 0; def < recog_data.n_operands; def++)
371       if ((recog_op_alt[def][alt].earlyclobber)
372           && (recog_op_alt[def][alt].cl != NO_REGS))
373         {
374           rtx dreg = recog_data.operand[def];
375           enum machine_mode dmode = recog_data.operand_mode[def];
376           if (GET_CODE (dreg) == SUBREG)
377             dreg = SUBREG_REG (dreg);
378           if (REG_P (dreg)
379               &&  may_overlap_class_p (dmode, REGNO (dreg), recog_op_alt[def][alt].cl))
380
381             for (use = 0; use < recog_data.n_operands; use++)
382               if (use != def
383                   && recog_data.operand_type[use] != OP_OUT
384                   && reg_classes_intersect_p (recog_op_alt[def][alt].cl,
385                                               recog_op_alt[use][alt].cl))
386                 {
387                   add_conflicts_for_earlyclobber (dreg,
388                                                   recog_op_alt[use][alt].cl,
389                                                   recog_data.operand[use]);
390                   /*  Reload may end up swapping commutative operands,
391                       so you have to take both orderings into account.
392                       The constraints for the two operands can be
393                       completely different.  (Indeed, if the
394                       constraints for the two operands are the same
395                       for all alternatives, there's no point marking
396                       them as commutative.)  */
397                   if (use < recog_data.n_operands + 1
398                       && recog_data.constraints[use][0] == '%')
399                     add_conflicts_for_earlyclobber (dreg,
400                                                     recog_op_alt[use][alt].cl,
401                                                     recog_data.operand[use + 1]);
402                 }
403         }
404 }
405
406
407 /* Init LIVE_SUBREGS[ALLOCNUM] and LIVE_SUBREGS_USED[ALLOCNUM] using
408    REG to the the number of nregs, and INIT_VALUE to get the
409    initialization.  ALLOCNUM need not be the regno of REG.  */
410
411 void
412 ra_init_live_subregs (bool init_value, 
413                       sbitmap *live_subregs, 
414                       int *live_subregs_used,
415                       int allocnum, 
416                       rtx reg)
417 {
418   unsigned int regno = REGNO (SUBREG_REG (reg));
419   int size = GET_MODE_SIZE (GET_MODE (regno_reg_rtx[regno]));
420
421   gcc_assert (size > 0);
422
423   /* Been there, done that.  */
424   if (live_subregs_used[allocnum])
425     return;
426
427   /* Create a new one with zeros.  */
428   if (live_subregs[allocnum] == NULL)
429     live_subregs[allocnum] = sbitmap_alloc (size);
430
431   /* If the entire reg was live before blasting into subregs, we need
432      to init all of the subregs to ones else init to 0.  */
433   if (init_value)
434     sbitmap_ones (live_subregs[allocnum]);
435   else 
436     sbitmap_zero (live_subregs[allocnum]);
437
438   /* Set the number of bits that we really want.  */
439   live_subregs_used[allocnum] = size;
440 }
441
442
443 /* Set REG to be not live in the sets ALLOCNOS_LIVE, LIVE_SUBREGS,
444    HARD_REGS_LIVE.  If EXTRACT is false, assume that the entire reg is
445    set not live even if REG is a subreg.  */
446
447 inline static void
448 clear_reg_in_live (sparseset allocnos_live,
449                    sbitmap *live_subregs, 
450                    int *live_subregs_used,
451                    HARD_REG_SET *hard_regs_live, 
452                    rtx reg,
453                    bool extract)
454 {
455   unsigned int regno = (GET_CODE (reg) == SUBREG) 
456     ? REGNO (SUBREG_REG (reg)): REGNO (reg);
457   int allocnum = reg_allocno[regno];
458
459   if (allocnum >= 0)
460     {
461       if ((GET_CODE (reg) == SUBREG) && !extract)
462
463         {
464           unsigned int start = SUBREG_BYTE (reg);
465           unsigned int last = start + GET_MODE_SIZE (GET_MODE (reg));
466
467           ra_init_live_subregs (sparseset_bit_p (allocnos_live, allocnum), 
468                                 live_subregs, live_subregs_used, allocnum, reg);
469
470           /* Ignore the paradoxical bits.  */
471           if ((int)last > live_subregs_used[allocnum])
472             last = live_subregs_used[allocnum];
473
474           while (start < last)
475             {
476               RESET_BIT (live_subregs[allocnum], start);
477               start++;
478             }
479
480           if (sbitmap_empty_p (live_subregs[allocnum]))
481             {
482               live_subregs_used[allocnum] = 0;
483               sparseset_clear_bit (allocnos_live, allocnum);
484             }
485           else
486             /* Set the allocnos live here because that bit has to be
487                true to get us to look at the live_subregs fields.  */
488             sparseset_set_bit (allocnos_live, allocnum);
489         }
490       else
491         {
492           /* Resetting the live_subregs_used is effectively saying do not use the 
493              subregs because we are writing the whole pseudo.  */
494           live_subregs_used[allocnum] = 0;
495           sparseset_clear_bit (allocnos_live, allocnum);
496         }
497     }
498
499   if (regno >= FIRST_PSEUDO_REGISTER)
500     return;
501
502   /* Handle hardware regs (and pseudos allocated to hard regs).  */
503   if (! fixed_regs[regno])
504     {
505       unsigned int start = regno;
506       if ((GET_CODE (reg) == SUBREG) && !extract)
507         {
508           unsigned int last;
509           start += SUBREG_BYTE (reg);
510           last = start + subreg_nregs_with_regno (regno, reg);
511           regno = start;
512
513           while (regno < last)
514             {
515               CLEAR_HARD_REG_BIT (*hard_regs_live, regno);
516               regno++;
517             }
518         }
519       else
520         remove_from_hard_reg_set (hard_regs_live, GET_MODE (reg), regno);
521     }
522 }
523
524
525
526 /* Set REG to be live in the sets ALLOCNOS_LIVE, LIVE_SUBREGS,
527    HARD_REGS_LIVE.  If EXTRACT is false, assume that the entire reg is
528    set live even if REG is a subreg.  */
529
530 inline static void
531 set_reg_in_live (sparseset allocnos_live, 
532                  sbitmap *live_subregs, 
533                  int *live_subregs_used,
534                  HARD_REG_SET *hard_regs_live, 
535                  rtx reg,
536                  bool extract)
537 {
538   unsigned int regno = (GET_CODE (reg) == SUBREG) 
539     ? REGNO (SUBREG_REG (reg)): REGNO (reg);
540   int allocnum = reg_allocno[regno];
541
542   if (allocnum >= 0)
543     {
544       if ((GET_CODE (reg) == SUBREG) && !extract)
545         {
546           unsigned int start = SUBREG_BYTE (reg);
547           unsigned int last = start + GET_MODE_SIZE (GET_MODE (reg));
548
549           ra_init_live_subregs (sparseset_bit_p (allocnos_live, allocnum), 
550                                 live_subregs, live_subregs_used, allocnum, reg);
551           
552           /* Ignore the paradoxical bits.  */
553           if ((int)last > live_subregs_used[allocnum])
554             last = live_subregs_used[allocnum];
555
556           while (start < last)
557             {
558               SET_BIT (live_subregs[allocnum], start);
559               start++;
560             }
561         }
562       else
563         /* Resetting the live_subregs_used is effectively saying do not use the 
564            subregs because we are writing the whole pseudo.  */
565           live_subregs_used[allocnum] = 0;
566      
567       sparseset_set_bit (allocnos_live, allocnum);
568     }
569       
570   if (regno >= FIRST_PSEUDO_REGISTER)
571     return;
572
573   /* Handle hardware regs (and pseudos allocated to hard regs).  */
574   if (! fixed_regs[regno])
575     {
576       if ((GET_CODE (reg) == SUBREG) && !extract)
577         {
578           unsigned int start = regno;
579           unsigned int last;
580
581           start += subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
582                                         SUBREG_BYTE (reg), GET_MODE (reg));
583           last = start + subreg_nregs_with_regno (regno, reg);
584           regno = start;
585
586           while (regno < last)
587             {
588               SET_HARD_REG_BIT (*hard_regs_live, regno);
589               regno++;
590             }
591         }
592       else
593         add_to_hard_reg_set (hard_regs_live, GET_MODE (reg), regno);
594     }
595 }
596
597
598 /* Add hard reg conflicts to RENUMBERS_LIVE assuming that pseudo in
599    allocno[ALLOCNUM] is allocated to a set of hard regs starting at
600    RENUMBER.
601
602    We are smart about the case where only subregs of REG have been
603    set, as indicated by LIVE_SUBREGS[ALLOCNUM] and
604    LIVE_SUBREGS_USED[ALLOCNUM].  See global_conflicts for description
605    of LIVE_SUBREGS and LIVE_SUBREGS_USED.  */
606
607 inline static void
608 set_renumbers_live (HARD_REG_SET *renumbers_live,   
609                     sbitmap *live_subregs, 
610                     int *live_subregs_used,
611                     int allocnum, int renumber)
612 {
613   /* The width of the pseudo.  */
614   int nbytes = live_subregs_used[allocnum];
615   int regno = allocno[allocnum].reg;
616   enum machine_mode mode = GET_MODE (regno_reg_rtx[regno]);
617
618   if (dump_file)
619     fprintf (dump_file, "  set_renumbers_live %d->%d ", 
620              regno, renumber);
621
622   if (nbytes > 0)
623     {
624       int i;
625       sbitmap live_subs = live_subregs[allocnum];
626
627       /* First figure out how many hard regs we are considering using.  */
628       int target_nregs = hard_regno_nregs[renumber][mode];
629
630       /* Now figure out the number of bytes per hard reg.  Note that
631          this may be different that what would be obtained by looking
632          at the mode in the pseudo.  For instance, a complex number
633          made up of 2 32-bit parts gets mapped to 2 hard regs, even if
634          the hardregs are 64-bit floating point values.  */
635       int target_width = nbytes / target_nregs;
636       
637       if (dump_file)
638         fprintf (dump_file, "target_nregs=%d target_width=%d nbytes=%d", 
639                  target_nregs, target_width, nbytes);
640
641       for (i = 0; i < target_nregs; i++)
642         {
643           int j;
644           bool set = false;
645           for (j = 0; j < target_width; j++)
646             {
647               int reg_start = i * target_width;
648               if (reg_start + j >= nbytes)
649                 break;
650               set |= TEST_BIT (live_subs, reg_start + j);
651             }
652
653           if (set)
654             SET_HARD_REG_BIT (*renumbers_live, renumber + i);
655         }
656     }
657   else
658     add_to_hard_reg_set (renumbers_live, mode, renumber);
659
660   if (dump_file)
661     fprintf (dump_file, "\n");
662 }
663
664 /* Dump out a REF with its reg_renumber range to FILE using
665    PREFIX.  */
666
667 static void
668 dump_ref (FILE *file, 
669           const char * prefix, 
670           const char * suffix, 
671           rtx reg,
672           unsigned int regno,
673           sbitmap *live_subregs, 
674           int *live_subregs_used
675 )
676 {
677   int allocnum = reg_allocno[regno];
678
679   fprintf (file, "%s %d", prefix, regno);
680   if (allocnum >= 0 
681       && live_subregs_used[allocnum] > 0)
682     {
683       int j;
684       char s = '[';
685       
686       for (j = 0; j < live_subregs_used[allocnum]; j++)
687         if (TEST_BIT (live_subregs[allocnum], j))
688           {
689             fprintf (dump_file, "%c%d", s, j);
690             s = ',';
691           }
692       fprintf (dump_file, "]");
693     }
694
695   if (reg_renumber[regno] >= 0)
696     {
697       enum machine_mode mode = GET_MODE (reg);
698       unsigned int start;
699       unsigned int last;
700
701       regno = reg_renumber[regno];
702
703       start = regno;
704       last = end_hard_regno (mode, regno);
705       if (GET_CODE (reg) == SUBREG)
706         {
707           start += subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
708                                         SUBREG_BYTE (reg), GET_MODE (reg));
709           last = start + subreg_nregs_with_regno (regno, reg);
710         }
711
712       if (start == last - 1)
713         fprintf (file, "(%d)", start);
714       else 
715         fprintf (file, "(%d:%d..%d)", regno, start, last-1);
716     }
717   fprintf (file, suffix);
718 }
719
720
721 /* Scan the rtl code and record all conflicts and register preferences in the
722    conflict matrices and preference tables.  */
723
724 void
725 global_conflicts (void)
726 {
727   unsigned int i;
728   basic_block bb;
729   rtx insn;
730
731   /* Regs that have allocnos can be in either 
732      hard_regs_live (if regno < FIRST_PSEUDO_REGISTER) or 
733      allocnos_live (if regno >= FIRST_PSEUDO_REGISTER) or 
734      both if local_alloc has preallocated it and reg_renumber >= 0.  */
735
736   HARD_REG_SET hard_regs_live;
737   HARD_REG_SET renumbers_live;
738   sparseset allocnos_live;
739   bitmap live = BITMAP_ALLOC (NULL);
740   VEC (df_ref_t, heap) *clobbers = NULL;
741   VEC (df_ref_t, heap) *dying_regs = NULL;
742
743   /* live_subregs is a vector used to keep accurate information about
744      which hardregs are live in multiword pseudos.  live_subregs and
745      live_subregs_used are indexed by reg_allocno.  The live_subreg
746      entry for a particular pseudo is a bitmap with one bit per byte
747      of the register.  It is only used if the corresponding element is
748      non zero in live_subregs_used.  The value in live_subregs_used is
749      number of bytes that the pseudo can occupy.  */
750   sbitmap *live_subregs = XCNEWVEC (sbitmap, max_allocno);
751   int *live_subregs_used = XNEWVEC (int, max_allocno);
752
753   if (dump_file)
754     {
755       fprintf (dump_file, "fixed registers : "); 
756       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
757         if (fixed_regs[i])
758           fprintf (dump_file, "%d ", i);
759       fprintf (dump_file, "\n");
760     }
761
762   allocnos_live = sparseset_alloc (max_allocno);
763
764   FOR_EACH_BB (bb)
765     {
766       bitmap_iterator bi;
767
768       bitmap_copy (live, DF_LIVE_OUT (bb));
769       df_simulate_artificial_refs_at_end (bb, live);
770
771       sparseset_clear (allocnos_live);
772       memset (live_subregs_used, 0, max_allocno * sizeof (int));
773       CLEAR_HARD_REG_SET (hard_regs_live);
774       CLEAR_HARD_REG_SET (renumbers_live);
775
776       /* Initialize allocnos_live and hard_regs_live for bottom of block.  */
777       EXECUTE_IF_SET_IN_BITMAP (live, 0, i, bi)
778         {
779           if (i >= FIRST_PSEUDO_REGISTER)
780             break;
781           if (! fixed_regs[i])
782             SET_HARD_REG_BIT (hard_regs_live, i);
783         }
784     
785       EXECUTE_IF_SET_IN_BITMAP (live, FIRST_PSEUDO_REGISTER, i, bi)
786         {
787           int allocnum = reg_allocno[i];
788
789           if (allocnum >= 0)
790             {
791               int renumber = reg_renumber[i];
792               rtx reg = regno_reg_rtx[i];
793
794               set_reg_in_live (allocnos_live, live_subregs, live_subregs_used, 
795                                &hard_regs_live, reg, false);
796               if (renumber >= 0 && renumber < FIRST_PSEUDO_REGISTER)
797                 set_renumbers_live (&renumbers_live, live_subregs, live_subregs_used, 
798                                     allocnum, renumber);
799             }
800         } 
801
802       if (dump_file)
803         fprintf (dump_file, "\nstarting basic block %d\n\n", bb->index);
804
805       FOR_BB_INSNS_REVERSE (bb, insn)
806         {
807           unsigned int uid = INSN_UID (insn);
808           struct df_ref **def_rec;
809           struct df_ref **use_rec;
810
811           if (!INSN_P (insn))
812             continue;   
813
814           if (dump_file)
815             {
816               fprintf (dump_file, "insn = %d live = hardregs [", uid);
817               
818               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
819                 if (TEST_HARD_REG_BIT (hard_regs_live, i))
820                   fprintf (dump_file, "%d ", i);
821
822               fprintf (dump_file, "] renumbered [");
823               for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
824                 if (TEST_HARD_REG_BIT (renumbers_live, i))
825                   fprintf (dump_file, "%d ", i);
826
827               fprintf (dump_file, "] pseudos [");
828               EXECUTE_IF_SET_IN_SPARSESET (allocnos_live, i)
829                 {
830                   dump_ref (dump_file, " ", "", regno_reg_rtx[allocno[i].reg],
831                             allocno[i].reg, live_subregs, live_subregs_used);
832                 }
833               fprintf (dump_file, "]\n");
834             }
835
836           /* Add the defs into live.  Most of them will already be
837              there, the ones that are missing are the unused ones and
838              the clobbers.  We do this in order to make sure that
839              interferences are added between every def and everything
840              that is live across the insn.  These defs will be removed
841              later.  */
842           for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
843             {
844               struct df_ref *def = *def_rec;
845
846               /* FIXME: Ignoring may clobbers is technically the wrong
847                  thing to do.  However the old version of the this
848                  code ignores may clobbers (and instead has many
849                  places in the register allocator to handle these
850                  constraints).  It is quite likely that with a new
851                  allocator, the correct thing to do is to not ignore
852                  the constraints and then do not put in the large
853                  number of special checks.  */
854               if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
855                 {
856                   rtx reg = DF_REF_REG (def);
857                   set_reg_in_live (allocnos_live, live_subregs, live_subregs_used, 
858                                    &hard_regs_live, reg, 
859                                    DF_REF_FLAGS_IS_SET (def, DF_REF_EXTRACT));
860                   if (dump_file)
861                     dump_ref (dump_file, "  adding def", "\n",
862                               reg, DF_REF_REGNO (def), live_subregs, live_subregs_used);
863                 }
864             }
865           
866           /* Add the hardregs into renumbers_live to build the
867              interferences.  Renumbers_live will be rebuilt in the
868              next step from scratch, so corrupting it here is no
869              problem.  */
870           IOR_HARD_REG_SET (renumbers_live, hard_regs_live);
871
872           /* Add the interferences for the defs.  */
873           for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
874             {
875               struct df_ref *def = *def_rec;
876               if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
877                 mark_reg_store (allocnos_live, &renumbers_live, def);
878             }
879           
880           /* Remove the defs from the live sets.  Leave the partial
881              and conditional defs in the set because they do not
882              kill.  */
883           VEC_truncate (df_ref_t, clobbers, 0);
884           for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
885             {
886               struct df_ref *def = *def_rec;
887
888               if (!DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL))
889                 {
890                   rtx reg = DF_REF_REG (def);
891
892                   clear_reg_in_live (allocnos_live, live_subregs, live_subregs_used,
893                                      &hard_regs_live, reg,
894                                      DF_REF_FLAGS_IS_SET (def, DF_REF_EXTRACT));
895                   if (dump_file)
896                     dump_ref (dump_file, "  clearing def", "\n", 
897                               reg, DF_REF_REGNO (def), live_subregs, live_subregs_used);
898                 }
899               
900               if (DF_REF_FLAGS_IS_SET (def, DF_REF_MUST_CLOBBER))
901                 VEC_safe_push (df_ref_t, heap, clobbers, def);
902             }
903           
904           /* Go thru all of the live pseudos and reset renumbers_live.
905              We must start from scratch here because there could have
906              been several pseudos alive that have the same
907              reg_renumber and if we see a clobber for one of them, we
908              cannot not want to kill the renumbers from the other
909              pseudos.  */
910           CLEAR_HARD_REG_SET (renumbers_live);
911           EXECUTE_IF_SET_IN_SPARSESET (allocnos_live, i)
912             {
913               unsigned int regno = allocno[i].reg;
914               int renumber = reg_renumber[regno];
915
916               if (renumber >= 0 && renumber < FIRST_PSEUDO_REGISTER)
917                 set_renumbers_live (&renumbers_live, live_subregs, live_subregs_used, 
918                                     i, renumber);
919             }
920                                          
921           /* Add the uses to the live sets.  Keep track of the regs
922              that are dying inside the insn, this set will be useful
923              later.  */
924           VEC_truncate (df_ref_t, dying_regs, 0);
925           for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
926             {
927               struct df_ref *use = *use_rec;
928               unsigned int regno = DF_REF_REGNO (use);
929               bool added = false;
930               int renumber = reg_renumber[regno];
931               int allocnum = reg_allocno[regno];
932               bool renumbering = false;
933               rtx reg = DF_REF_REG (use);
934
935               /* DF_REF_READ_WRITE on a use means that this use is
936                  fabricated from a def that is a partial set to a
937                  multiword reg.  Here, we only model the subreg case
938                  precisely so we do not need to look at the fabricated
939                  use unless that set also happens to wrapped in a
940                  ZERO_EXTRACT. */
941               if (DF_REF_FLAGS_IS_SET (use, DF_REF_READ_WRITE) 
942                   && (!DF_REF_FLAGS_IS_SET (use, DF_REF_EXTRACT)) 
943                   && DF_REF_FLAGS_IS_SET (use, DF_REF_SUBREG))
944                 continue;
945               
946               if (dump_file)
947                 dump_ref (dump_file, "  seeing use", "\n",
948                           reg, regno, live_subregs, live_subregs_used);
949
950               if (allocnum >= 0)
951                 {
952                   if (GET_CODE (reg) == SUBREG
953                       && !DF_REF_FLAGS_IS_SET (use, DF_REF_EXTRACT)) 
954                     {
955                       unsigned int start = SUBREG_BYTE (reg);
956                       unsigned int last = start + GET_MODE_SIZE (GET_MODE (reg));
957
958                       ra_init_live_subregs (sparseset_bit_p (allocnos_live, allocnum), 
959                                             live_subregs, live_subregs_used, allocnum, reg);
960                       
961                       /* Ignore the paradoxical bits.  */
962                       if ((int)last > live_subregs_used[allocnum])
963                         last = live_subregs_used[allocnum];
964                       
965                       while (start < last)
966                         {
967                           if (!TEST_BIT (live_subregs[allocnum], start)) 
968                             {
969                               if (dump_file)
970                                 fprintf (dump_file, "    dying pseudo subreg %d[%d]\n", regno, start);
971                               SET_BIT (live_subregs[allocnum], start);
972                               
973                               added = true;
974                             }
975                           start++;
976                         }
977                       
978                       sparseset_set_bit (allocnos_live, allocnum);
979                       if (renumber >= 0 && renumber < FIRST_PSEUDO_REGISTER)
980                         set_renumbers_live (&renumbers_live, live_subregs, live_subregs_used, 
981                                             allocnum, renumber);
982                     }
983                   else if (live_subregs_used[allocnum] > 0
984                            || !sparseset_bit_p (allocnos_live, allocnum))
985                     {
986                       if (dump_file)
987                         fprintf (dump_file, "    %sdying pseudo\n", 
988                                  (live_subregs_used[allocnum] > 0) ? "partially ": "");
989                       /* Resetting the live_subregs_used is
990                          effectively saying do not use the subregs
991                          because we are reading the whole pseudo.  */
992                       live_subregs_used[allocnum] = 0;
993                       sparseset_set_bit (allocnos_live, allocnum);
994                       if (renumber >= 0 && renumber < FIRST_PSEUDO_REGISTER)
995                         set_renumbers_live (&renumbers_live, live_subregs, live_subregs_used, 
996                                             allocnum, renumber);
997                       added = true;
998                     }
999                 }
1000
1001               if (renumber >= 0 && renumber < FIRST_PSEUDO_REGISTER)
1002                 {
1003                   regno = renumber;
1004                   renumbering = true;
1005                 }
1006               
1007               if (regno < FIRST_PSEUDO_REGISTER)
1008                 {
1009                   unsigned int start = regno;
1010                   unsigned int last;
1011                   if (GET_CODE (reg) == SUBREG)
1012                     {
1013                       start += subreg_regno_offset (regno, GET_MODE (SUBREG_REG (reg)),
1014                                                     SUBREG_BYTE (reg), GET_MODE (reg));
1015                       last = start + subreg_nregs_with_regno (regno, reg);
1016                     }
1017                   else
1018                     last = end_hard_regno (GET_MODE (reg), regno);
1019                   
1020                   regno = start;
1021                   while (regno < last)
1022                     {
1023                       if ((!TEST_HARD_REG_BIT (hard_regs_live, regno)) 
1024                           && (!TEST_HARD_REG_BIT (renumbers_live, regno)) 
1025                           && ! fixed_regs[regno])
1026                         {
1027                           if (dump_file)
1028                             fprintf (dump_file, "    dying hard reg %d\n", regno);
1029                           if (renumbering)
1030                             SET_HARD_REG_BIT (renumbers_live, regno);
1031                           else
1032                             SET_HARD_REG_BIT (hard_regs_live, regno);
1033                           
1034                           added = true;
1035                         }
1036                       regno++;
1037                     }
1038                 }
1039               if (added)
1040                 VEC_safe_push (df_ref_t, heap, dying_regs, use);
1041             }
1042           
1043           /* These three cases are all closely related, they all deal
1044              with some set of outputs of the insn need to conflict
1045              with some of the registers that are used by the insn but
1046              die within the insn. If no registers die within the insn,
1047              the tests can be skipped. */
1048
1049           if (VEC_length (df_ref_t, dying_regs) > 0)
1050             {
1051               int k;
1052               /* There appears to be an ambiguity as to what a clobber
1053                  means in an insn.  In some cases, the clobber happens
1054                  within the processing of the insn and in some cases
1055                  it happens at the end of processing the insn.  There
1056                  is currently no way to distinguish these two cases so
1057                  this code causes real clobbers to interfere with
1058                  registers that die within an insn.
1059
1060                  This is consistent with the prior version of
1061                  interference graph builder but is was discovered
1062                  while developing this version of the code, that on
1063                  some architectures such as the x86-64, the clobbers
1064                  only appear to happen at the end of the insn.
1065                  However, the ppc-32 contains clobbers for which these
1066                  interferences are necessary.
1067
1068                  FIXME: We should consider either adding a new kind of
1069                  clobber, or adding a flag to the clobber distinguish
1070                  these two cases.  */
1071               if (dump_file && VEC_length (df_ref_t, clobbers))
1072                 fprintf (dump_file, "  clobber conflicts\n");
1073               for (k = VEC_length (df_ref_t, clobbers) - 1; k >= 0; k--)
1074                 {
1075                   struct df_ref *def = VEC_index (df_ref_t, clobbers, k);
1076                   int j;
1077
1078                   for (j = VEC_length (df_ref_t, dying_regs) - 1; j >= 0; j--)
1079                     {
1080                       struct df_ref *use = VEC_index (df_ref_t, dying_regs, j);
1081                       record_one_conflict_between_regnos (GET_MODE (DF_REF_REG (def)),
1082                                                           DF_REF_REGNO (def),
1083                                                           GET_MODE (DF_REF_REG (use)),
1084                                                           DF_REF_REGNO (use));
1085                     }
1086                 }
1087
1088               /* Early clobbers, by definition, need to not only
1089                  clobber the registers that are live across the insn
1090                  but need to clobber the registers that die within the
1091                  insn.  The clobbering for registers live across the
1092                  insn is handled above.  */ 
1093               set_conflicts_for_earlyclobber (insn);
1094
1095               /* If INSN is a store with multiple outputs, then any
1096                  reg that dies here and is used inside of the address
1097                  of the output must conflict with the other outputs.
1098
1099                  FIXME: There has been some discussion as to whether
1100                  this is right place to handle this issue.  This is a
1101                  hold over from an early version global conflicts.
1102
1103                  1) There is some evidence that code only deals with a
1104                  bug that is only on the m68k.  The conditions of this
1105                  test are such that this case only triggers for a very
1106                  peculiar insn, one that is a parallel where one of
1107                  the sets is a store and the other sets a reg that is
1108                  used in the address of the store.  See
1109                  http://gcc.gnu.org/ml/gcc-patches/1998-12/msg00259.html
1110
1111                  2) The situation that this is addressing is a bug in
1112                  the part of reload that handles stores, adding this
1113                  conflict only hides the problem.  (Of course no one
1114                  really wants to fix reload so it is understandable
1115                  why a bandaid was just added here.)
1116
1117                  Just because an output is unused does not mean the
1118                  compiler can assume the side effect will not occur.
1119                  Consider if REG appears in the address of an output
1120                  and we reload the output.  If we allocate REG to the
1121                  same hard register as an unused output we could set
1122                  the hard register before the output reload insn.
1123
1124                  3) This could actually be handled by making the other
1125                  (non store) operand of the insn be an early clobber.
1126                  This would insert the same conflict, even if it is
1127                  not technically an early clobber.  */
1128
1129               /* It is unsafe to use !single_set here since it will ignore an
1130                  unused output.  */
1131               if (GET_CODE (PATTERN (insn)) == PARALLEL && multiple_sets (insn))
1132                 { 
1133                   int j;
1134                   if (dump_file)
1135                     fprintf (dump_file, "  multiple sets\n");
1136                   for (j = VEC_length (df_ref_t, dying_regs) - 1; j >= 0; j--)
1137                     {
1138                       int used_in_output = 0;
1139                       struct df_ref *use = VEC_index (df_ref_t, dying_regs, j);
1140                       rtx reg = DF_REF_REG (use);
1141                       int uregno = DF_REF_REGNO (use);
1142                       enum machine_mode umode = GET_MODE (DF_REF_REG (use));
1143                       int k;
1144
1145                       for (k = XVECLEN (PATTERN (insn), 0) - 1; k >= 0; k--)
1146                         {
1147                           rtx set = XVECEXP (PATTERN (insn), 0, k);
1148                           if (GET_CODE (set) == SET
1149                               && !REG_P (SET_DEST (set))
1150                               && !rtx_equal_p (reg, SET_DEST (set))
1151                               && reg_overlap_mentioned_p (reg, SET_DEST (set)))
1152                             used_in_output = 1;
1153                         }
1154                       if (used_in_output)
1155                         for (k = XVECLEN (PATTERN (insn), 0) - 1; k >= 0; k--)
1156                           {
1157                             rtx set = XVECEXP (PATTERN (insn), 0, k);
1158                             if (GET_CODE (set) == SET
1159                                 && REG_P (SET_DEST (set))
1160                                 && !rtx_equal_p (reg, SET_DEST (set)))
1161                               record_one_conflict_between_regnos (GET_MODE (SET_DEST (set)),
1162                                                                   REGNO (SET_DEST (set)), 
1163                                                                   umode, uregno);
1164                           }
1165                     }
1166                 }
1167             }
1168         }
1169
1170       /* Add the renumbers live to the hard_regs_live for the next few
1171          calls.  All of this gets recomputed at the top of the loop so
1172          there is no harm.  */
1173       IOR_HARD_REG_SET (hard_regs_live, renumbers_live);
1174           
1175 #ifdef EH_RETURN_DATA_REGNO
1176       if (bb_has_eh_pred (bb))
1177         {
1178           unsigned int i;
1179     
1180           for (i = 0; ; ++i)
1181             {
1182               unsigned int regno = EH_RETURN_DATA_REGNO (i);
1183               if (regno == INVALID_REGNUM)
1184                 break;
1185               record_one_conflict (allocnos_live, &hard_regs_live, regno);
1186             }
1187         }
1188 #endif
1189
1190       if (bb_has_abnormal_pred (bb))
1191         {
1192           unsigned int i;
1193 #ifdef STACK_REGS
1194           /* Pseudos can't go in stack regs at the start of a basic block that
1195              is reached by an abnormal edge. Likewise for call clobbered regs,
1196              because caller-save, fixup_abnormal_edges and possibly the table
1197              driven EH machinery are not quite ready to handle such regs live
1198              across such edges.  */
1199           EXECUTE_IF_SET_IN_SPARSESET (allocnos_live, i)
1200             {
1201               allocno[i].no_stack_reg = 1;
1202             }
1203
1204           for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
1205             record_one_conflict (allocnos_live, &hard_regs_live, i);
1206 #endif
1207           
1208           /* No need to record conflicts for call clobbered regs if we have
1209              nonlocal labels around, as we don't ever try to allocate such
1210              regs in this case.  */
1211           if (! current_function_has_nonlocal_label)
1212             for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1213               if (call_used_regs [i])
1214                 record_one_conflict (allocnos_live, &hard_regs_live, i);
1215         }
1216     }
1217   
1218   for (i = 0; i < (unsigned int)max_allocno; i++)
1219     if (live_subregs[i])
1220       free (live_subregs[i]);
1221
1222   /* Clean up.  */
1223   free (allocnos_live);
1224   free (live_subregs);
1225   free (live_subregs_used);
1226   VEC_free (df_ref_t, heap, dying_regs);
1227   VEC_free (df_ref_t, heap, clobbers);
1228   BITMAP_FREE (live);
1229 }