OSDN Git Service

Add new test - do not warn about (non-)redundant friend declaration.
[pf3gnuchains/gcc-fork.git] / gcc / regrename.c
1 /* Register renaming for the GNU compiler.
2    Copyright (C) 2000 Free Software Foundation, Inc.
3
4    This file is part of GNU CC.
5
6    GNU CC is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GNU CC is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GNU CC; see the file COPYING.  If not, write to
18    the Free Software Foundation, 59 Temple Place - Suite 330,
19    Boston, MA 02111-1307, USA.  */
20
21 #define REG_OK_STRICT
22
23 #include "config.h"
24 #include "system.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "insn-config.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "reload.h"
32 #include "output.h"
33 #include "function.h"
34 #include "recog.h"
35 #include "flags.h"
36 #include "obstack.h"
37
38 #define obstack_chunk_alloc xmalloc
39 #define obstack_chunk_free free
40
41 #ifndef REGNO_MODE_OK_FOR_BASE_P
42 #define REGNO_MODE_OK_FOR_BASE_P(REGNO, MODE) REGNO_OK_FOR_BASE_P (REGNO)
43 #endif
44
45 #ifndef REG_MODE_OK_FOR_BASE_P
46 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
47 #endif
48
49 static const char *const reg_class_names[] = REG_CLASS_NAMES;
50
51 struct du_chain
52 {
53   struct du_chain *next_chain;
54   struct du_chain *next_use;
55
56   rtx insn;
57   rtx *loc;
58   enum reg_class class;
59   unsigned int need_caller_save_reg:1;
60   unsigned int earlyclobber:1;
61 };
62
63 enum scan_actions
64 {
65   terminate_all_read,
66   terminate_overlapping_read,
67   terminate_write,
68   terminate_dead,
69   mark_read,
70   mark_write
71 };
72
73 static const char * const scan_actions_name[] =
74 {
75   "terminate_all_read",
76   "terminate_overlapping_read",
77   "terminate_write",
78   "terminate_dead",
79   "mark_read",
80   "mark_write"
81 };
82
83 static struct obstack rename_obstack;
84
85 static void do_replace PARAMS ((struct du_chain *, int));
86 static void scan_rtx_reg PARAMS ((rtx, rtx *, enum reg_class,
87                                   enum scan_actions, enum op_type, int));
88 static void scan_rtx_address PARAMS ((rtx, rtx *, enum reg_class,
89                                       enum scan_actions, enum machine_mode));
90 static void scan_rtx PARAMS ((rtx, rtx *, enum reg_class,
91                               enum scan_actions, enum op_type, int));
92 static struct du_chain *build_def_use PARAMS ((basic_block));
93 static void dump_def_use_chain PARAMS ((struct du_chain *));
94 static void note_sets PARAMS ((rtx, rtx, void *));
95 static void clear_dead_regs PARAMS ((HARD_REG_SET *, enum machine_mode, rtx));
96 static void merge_overlapping_regs PARAMS ((basic_block, HARD_REG_SET *,
97                                             struct du_chain *));
98
99 /* Called through note_stores from update_life.  Find sets of registers, and
100    record them in *DATA (which is actually a HARD_REG_SET *).  */
101
102 static void
103 note_sets (x, set, data)
104      rtx x;
105      rtx set ATTRIBUTE_UNUSED;
106      void *data;
107 {
108   HARD_REG_SET *pset = (HARD_REG_SET *) data;
109   unsigned int regno;
110   int nregs;
111   if (GET_CODE (x) != REG)
112     return;
113   regno = REGNO (x);
114   nregs = HARD_REGNO_NREGS (regno, GET_MODE (x));
115   while (nregs-- > 0)
116     SET_HARD_REG_BIT (*pset, regno + nregs);
117 }
118
119 /* Clear all registers from *PSET for which a note of kind KIND can be found
120    in the list NOTES.  */
121
122 static void
123 clear_dead_regs (pset, kind, notes)
124      HARD_REG_SET *pset;
125      enum machine_mode kind;
126      rtx notes;
127 {
128   rtx note;
129   for (note = notes; note; note = XEXP (note, 1))
130     if (REG_NOTE_KIND (note) == kind && REG_P (XEXP (note, 0)))
131       {
132         rtx reg = XEXP (note, 0);
133         unsigned int regno = REGNO (reg);
134         int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
135         while (nregs-- > 0)
136           CLEAR_HARD_REG_BIT (*pset, regno + nregs);
137       }
138 }
139
140 /* For a def-use chain CHAIN in basic block B, find which registers overlap
141    its lifetime and set the corresponding bits in *PSET.  */
142
143 static void
144 merge_overlapping_regs (b, pset, chain)
145      basic_block b;
146      HARD_REG_SET *pset;
147      struct du_chain *chain;
148 {
149   struct du_chain *t = chain;
150   rtx insn;
151   HARD_REG_SET live;
152
153   REG_SET_TO_HARD_REG_SET (live, b->global_live_at_start);
154   insn = b->head;
155   while (t)
156     {
157       /* Search forward until the next reference to the register to be
158          renamed.  */
159       while (insn != t->insn)
160         {
161           if (INSN_P (insn))
162             {
163               clear_dead_regs (&live, REG_DEAD, REG_NOTES (insn));
164               note_stores (PATTERN (insn), note_sets, (void *) &live);
165               /* Only record currently live regs if we are inside the
166                  reg's live range.  */
167               if (t != chain)
168                 IOR_HARD_REG_SET (*pset, live);
169               clear_dead_regs (&live, REG_UNUSED, REG_NOTES (insn));  
170             }
171           insn = NEXT_INSN (insn);
172         }
173
174       IOR_HARD_REG_SET (*pset, live);
175
176       /* For the last reference, also merge in all registers set in the
177          same insn.
178          @@@ We only have take earlyclobbered sets into account.  */
179       if (! t->next_use)
180         note_stores (PATTERN (insn), note_sets, (void *) pset);
181
182       t = t->next_use;
183     }
184 }
185
186 /* Perform register renaming on the current function.  */
187
188 void
189 regrename_optimize ()
190 {
191   int tick[FIRST_PSEUDO_REGISTER];
192   int this_tick = 0;
193   int b;
194   char *first_obj;
195
196   memset (tick, 0, sizeof tick);
197
198   gcc_obstack_init (&rename_obstack);
199   first_obj = (char *) obstack_alloc (&rename_obstack, 0);
200
201   for (b = 0; b < n_basic_blocks; b++)
202     {
203       basic_block bb = BASIC_BLOCK (b);
204       struct du_chain *all_chains = 0;
205       HARD_REG_SET unavailable;
206       HARD_REG_SET regs_seen;
207
208       CLEAR_HARD_REG_SET (unavailable);
209
210       if (rtl_dump_file)
211         fprintf (rtl_dump_file, "\nBasic block %d:\n", b);
212
213       all_chains = build_def_use (bb);
214
215       if (rtl_dump_file)
216         dump_def_use_chain (all_chains);
217
218       CLEAR_HARD_REG_SET (unavailable);
219       /* Don't clobber traceback for noreturn functions.  */
220       if (frame_pointer_needed)
221         {
222           SET_HARD_REG_BIT (unavailable, FRAME_POINTER_REGNUM);
223 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
224           SET_HARD_REG_BIT (unavailable, HARD_FRAME_POINTER_REGNUM);
225 #endif
226         }
227
228       CLEAR_HARD_REG_SET (regs_seen);
229       while (all_chains)
230         {
231           int new_reg, best_new_reg = -1;
232           int n_uses;
233           struct du_chain *this = all_chains;
234           struct du_chain *tmp, *last;
235           HARD_REG_SET this_unavailable;
236           int reg = REGNO (*this->loc), treg;
237           int nregs = HARD_REGNO_NREGS (reg, GET_MODE (*this->loc));
238           int i;
239
240           all_chains = this->next_chain;
241           
242 #if 0 /* This just disables optimization opportunities.  */
243           /* Only rename once we've seen the reg more than once.  */
244           if (! TEST_HARD_REG_BIT (regs_seen, reg))
245             {
246               SET_HARD_REG_BIT (regs_seen, reg);
247               continue;
248             }
249 #endif
250
251           if (fixed_regs[reg] || global_regs[reg]
252 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
253               || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
254 #else
255               || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
256 #endif
257               )
258             continue;
259
260           COPY_HARD_REG_SET (this_unavailable, unavailable);
261
262           /* Find last entry on chain (which has the need_caller_save bit),
263              count number of uses, and narrow the set of registers we can
264              use for renaming.  */
265           n_uses = 0;
266           for (last = this; last->next_use; last = last->next_use)
267             {
268               n_uses++;
269               IOR_COMPL_HARD_REG_SET (this_unavailable,
270                                       reg_class_contents[last->class]);
271             }
272           if (n_uses < 1)
273             continue;
274
275           IOR_COMPL_HARD_REG_SET (this_unavailable,
276                                   reg_class_contents[last->class]);
277
278           if (this->need_caller_save_reg)
279             IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
280
281           merge_overlapping_regs (bb, &this_unavailable, this);
282
283           /* Now potential_regs is a reasonable approximation, let's
284              have a closer look at each register still in there.  */
285           for (treg = 0; treg < FIRST_PSEUDO_REGISTER; treg++)
286             {
287               new_reg = treg;
288               for (i = nregs - 1; i >= 0; --i)
289                 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
290                     || fixed_regs[new_reg + i]
291                     || global_regs[new_reg + i]
292                     /* Can't use regs which aren't saved by the prologue.  */
293                     || (! regs_ever_live[new_reg + i]
294                         && ! call_used_regs[new_reg + i])
295 #ifdef LEAF_REGISTERS
296                     /* We can't use a non-leaf register if we're in a 
297                        leaf function.  */
298                     || (current_function_is_leaf 
299                         && !LEAF_REGISTERS[new_reg + i])
300 #endif
301 #ifdef HARD_REGNO_RENAME_OK
302                     || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
303 #endif
304                     )
305                   break;
306               if (i >= 0)
307                 continue;
308
309               /* See whether it accepts all modes that occur in
310                  definition and uses.  */
311               for (tmp = this; tmp; tmp = tmp->next_use)
312                 if (! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc)))
313                   break;
314               if (! tmp)
315                 {
316                   if (best_new_reg == -1
317                       || tick[best_new_reg] > tick[new_reg])
318                     best_new_reg = new_reg;
319                 }
320             }
321
322           if (rtl_dump_file)
323             {
324               fprintf (rtl_dump_file, "Register %s in insn %d",
325                        reg_names[reg], INSN_UID (last->insn));
326               if (last->need_caller_save_reg)
327                 fprintf (rtl_dump_file, " crosses a call");
328               }
329
330           if (best_new_reg == -1)
331             {
332               if (rtl_dump_file)
333                 fprintf (rtl_dump_file, "; no available registers\n");
334               continue;
335             }
336
337           do_replace (this, best_new_reg);
338           tick[best_new_reg] = this_tick++;
339
340           if (rtl_dump_file)
341             fprintf (rtl_dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
342         }
343
344       obstack_free (&rename_obstack, first_obj);
345     }
346
347   obstack_free (&rename_obstack, NULL);
348
349   if (rtl_dump_file)
350     fputc ('\n', rtl_dump_file);
351
352   count_or_remove_death_notes (NULL, 1);
353   update_life_info (NULL, UPDATE_LIFE_LOCAL,
354                     PROP_REG_INFO | PROP_DEATH_NOTES);
355 }
356
357 static void
358 do_replace (chain, reg)
359      struct du_chain *chain;
360      int reg;
361 {
362   while (chain)
363     {
364       unsigned int regno = ORIGINAL_REGNO (*chain->loc);
365       *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
366       if (regno >= FIRST_PSEUDO_REGISTER)
367         ORIGINAL_REGNO (*chain->loc) = regno;
368       chain = chain->next_use;
369     }
370 }
371
372
373 static struct du_chain *open_chains;
374 static struct du_chain *closed_chains;
375
376 static void
377 scan_rtx_reg (insn, loc, class, action, type, earlyclobber)
378      rtx insn;
379      rtx *loc;
380      enum reg_class class;
381      enum scan_actions action;
382      enum op_type type;
383      int earlyclobber;
384 {
385   struct du_chain **p;
386   rtx x = *loc;
387   enum machine_mode mode = GET_MODE (x);
388   int this_regno = REGNO (x);
389   int this_nregs = HARD_REGNO_NREGS (this_regno, mode);
390
391   if (action == mark_write)
392     {
393       if (type == OP_OUT)
394         {
395           struct du_chain *this = (struct du_chain *)
396             obstack_alloc (&rename_obstack, sizeof (struct du_chain));
397           this->next_use = 0;
398           this->next_chain = open_chains;
399           this->loc = loc;
400           this->insn = insn;
401           this->class = class;
402           this->need_caller_save_reg = 0;
403           this->earlyclobber = earlyclobber;
404           open_chains = this;
405         }
406       return;
407     }
408
409   if ((type == OP_OUT && action != terminate_write)
410       || (type != OP_OUT && action == terminate_write))
411     return;
412
413   for (p = &open_chains; *p;)
414     {
415       struct du_chain *this = *p;
416
417       /* Check if the chain has been terminated if it has then skip to
418          the next chain.
419
420          This can happen when we've already appended the location to
421          the chain in Step 3, but are trying to hide in-out operands
422          from terminate_write in Step 5.  */
423
424       if (*this->loc == cc0_rtx)
425         p = &this->next_chain;
426       else
427         {
428           int regno = REGNO (*this->loc);
429           int nregs = HARD_REGNO_NREGS (regno, GET_MODE (*this->loc));
430           int exact_match = (regno == this_regno && nregs == this_nregs);
431
432           if (regno + nregs <= this_regno
433               || this_regno + this_nregs <= regno)
434             {
435               p = &this->next_chain;
436               continue;
437             }
438
439           if (action == mark_read)
440             {
441               if (! exact_match)
442                 abort ();
443
444               /* ??? Class NO_REGS can happen if the md file makes use of 
445                  EXTRA_CONSTRAINTS to match registers.  Which is arguably
446                  wrong, but there we are.  Since we know not what this may
447                  be replaced with, terminate the chain.  */
448               if (class != NO_REGS)
449                 {
450                   this = (struct du_chain *)
451                     obstack_alloc (&rename_obstack, sizeof (struct du_chain));
452                   this->next_use = 0;
453                   this->next_chain = (*p)->next_chain;
454                   this->loc = loc;
455                   this->insn = insn;
456                   this->class = class;
457                   this->need_caller_save_reg = 0;
458                   while (*p)
459                     p = &(*p)->next_use;
460                   *p = this;
461                   return;
462                 }
463             }
464
465           if (action != terminate_overlapping_read || ! exact_match)
466             {
467               struct du_chain *next = this->next_chain;
468
469               /* Whether the terminated chain can be used for renaming
470                  depends on the action and this being an exact match.
471                  In either case, we remove this element from open_chains.  */
472
473               if ((action == terminate_dead || action == terminate_write)
474                   && exact_match)
475                 {
476                   this->next_chain = closed_chains;
477                   closed_chains = this;
478                   if (rtl_dump_file)
479                     fprintf (rtl_dump_file,
480                              "Closing chain %s at insn %d (%s)\n",
481                              reg_names[REGNO (*this->loc)], INSN_UID (insn),
482                              scan_actions_name[(int) action]);
483                 }
484               else
485                 {
486                   if (rtl_dump_file)
487                     fprintf (rtl_dump_file,
488                              "Discarding chain %s at insn %d (%s)\n",
489                              reg_names[REGNO (*this->loc)], INSN_UID (insn),
490                              scan_actions_name[(int) action]);
491                 }
492               *p = next;
493             }
494           else
495             p = &this->next_chain;
496         }
497     }
498 }
499
500 /* Adapted from find_reloads_address_1.  CLASS is INDEX_REG_CLASS or
501    BASE_REG_CLASS depending on how the register is being considered.  */
502
503 static void
504 scan_rtx_address (insn, loc, class, action, mode)
505      rtx insn;
506      rtx *loc;
507      enum reg_class class;
508      enum scan_actions action;
509      enum machine_mode mode;
510 {
511   rtx x = *loc;
512   RTX_CODE code = GET_CODE (x);
513   const char *fmt;
514   int i, j;
515
516   if (action == mark_write)
517     return;
518
519   switch (code)
520     {
521     case PLUS:
522       {
523         rtx orig_op0 = XEXP (x, 0);
524         rtx orig_op1 = XEXP (x, 1);
525         RTX_CODE code0 = GET_CODE (orig_op0);
526         RTX_CODE code1 = GET_CODE (orig_op1);
527         rtx op0 = orig_op0;
528         rtx op1 = orig_op1;
529         rtx *locI = NULL;
530         rtx *locB = NULL;
531
532         if (GET_CODE (op0) == SUBREG)
533           {
534             op0 = SUBREG_REG (op0);
535             code0 = GET_CODE (op0);
536           }
537
538         if (GET_CODE (op1) == SUBREG)
539           {
540             op1 = SUBREG_REG (op1);
541             code1 = GET_CODE (op1);
542           }
543
544         if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
545             || code0 == ZERO_EXTEND || code1 == MEM)
546           {
547             locI = &XEXP (x, 0);
548             locB = &XEXP (x, 1);
549           }
550         else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
551                  || code1 == ZERO_EXTEND || code0 == MEM)
552           {
553             locI = &XEXP (x, 1);
554             locB = &XEXP (x, 0);
555           }
556         else if (code0 == CONST_INT || code0 == CONST
557                  || code0 == SYMBOL_REF || code0 == LABEL_REF)
558           locB = &XEXP (x, 1);
559         else if (code1 == CONST_INT || code1 == CONST
560                  || code1 == SYMBOL_REF || code1 == LABEL_REF)
561           locB = &XEXP (x, 0);
562         else if (code0 == REG && code1 == REG)
563           {
564             int index_op;
565
566             if (REG_OK_FOR_INDEX_P (op0)
567                 && REG_MODE_OK_FOR_BASE_P (op1, mode))
568               index_op = 0;
569             else if (REG_OK_FOR_INDEX_P (op1)
570                      && REG_MODE_OK_FOR_BASE_P (op0, mode))
571               index_op = 1;
572             else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
573               index_op = 0;
574             else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
575               index_op = 1;
576             else if (REG_OK_FOR_INDEX_P (op1))
577               index_op = 1;
578             else
579               index_op = 0;
580
581             locI = &XEXP (x, index_op);
582             locB = &XEXP (x, !index_op);
583           }
584         else if (code0 == REG)
585           {
586             locI = &XEXP (x, 0);
587             locB = &XEXP (x, 1);
588           }
589         else if (code1 == REG)
590           {
591             locI = &XEXP (x, 1);
592             locB = &XEXP (x, 0);
593           }
594
595         if (locI)
596           scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
597         if (locB)
598           scan_rtx_address (insn, locB, BASE_REG_CLASS, action, mode);
599         return;
600       }
601
602     case POST_INC:
603     case POST_DEC:
604     case POST_MODIFY:
605     case PRE_INC:
606     case PRE_DEC:
607     case PRE_MODIFY:
608 #ifndef AUTO_INC_DEC
609       /* If the target doesn't claim to handle autoinc, this must be
610          something special, like a stack push.  Kill this chain.  */
611       action = terminate_all_read;
612 #endif
613       break;
614
615     case MEM:
616       scan_rtx_address (insn, &XEXP (x, 0), BASE_REG_CLASS, action,
617                         GET_MODE (x));
618       return;
619
620     case REG:
621       scan_rtx_reg (insn, loc, class, action, OP_IN, 0);
622       return;
623
624     default:
625       break;
626     }
627
628   fmt = GET_RTX_FORMAT (code);
629   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
630     {
631       if (fmt[i] == 'e')
632         scan_rtx_address (insn, &XEXP (x, i), class, action, mode);
633       else if (fmt[i] == 'E')
634         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
635           scan_rtx_address (insn, &XVECEXP (x, i, j), class, action, mode);
636     }
637 }
638
639 static void
640 scan_rtx (insn, loc, class, action, type, earlyclobber)
641      rtx insn;
642      rtx *loc;
643      enum reg_class class;
644      enum scan_actions action;
645      enum op_type type;
646      int earlyclobber;
647 {
648   const char *fmt;
649   rtx x = *loc;
650   enum rtx_code code = GET_CODE (x);
651   int i, j;
652
653   code = GET_CODE (x);
654   switch (code)
655     {
656     case CONST:
657     case CONST_INT:
658     case CONST_DOUBLE:
659     case SYMBOL_REF:
660     case LABEL_REF:
661     case CC0:
662     case PC:
663       return;
664
665     case REG:
666       scan_rtx_reg (insn, loc, class, action, type, earlyclobber);
667       return;
668
669     case MEM:
670       scan_rtx_address (insn, &XEXP (x, 0), BASE_REG_CLASS, action,
671                         GET_MODE (x));
672       return;
673
674     case SET:
675       scan_rtx (insn, &SET_SRC (x), class, action, OP_IN, 0);
676       scan_rtx (insn, &SET_DEST (x), class, action, OP_OUT, 0);
677       return;
678
679     case STRICT_LOW_PART:
680       scan_rtx (insn, &XEXP (x, 0), class, action, OP_INOUT, earlyclobber);
681       return;
682
683     case ZERO_EXTRACT:
684     case SIGN_EXTRACT: 
685       scan_rtx (insn, &XEXP (x, 0), class, action,
686                 type == OP_IN ? OP_IN : OP_INOUT, earlyclobber);
687       scan_rtx (insn, &XEXP (x, 1), class, action, OP_IN, 0);
688       scan_rtx (insn, &XEXP (x, 2), class, action, OP_IN, 0);
689       return;
690
691     case POST_INC:
692     case PRE_INC:
693     case POST_DEC:
694     case PRE_DEC:
695     case POST_MODIFY:
696     case PRE_MODIFY:
697       /* Should only happen inside MEM.  */
698       abort ();
699
700     case CLOBBER:
701       scan_rtx (insn, &SET_DEST (x), class, action, OP_OUT, 1);
702       return;
703
704     case EXPR_LIST:
705       scan_rtx (insn, &XEXP (x, 0), class, action, type, 0);
706       if (XEXP (x, 1))
707         scan_rtx (insn, &XEXP (x, 1), class, action, type, 0);
708       return;
709
710     default:
711       break;
712     }
713
714   fmt = GET_RTX_FORMAT (code);
715   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
716     {
717       if (fmt[i] == 'e')
718         scan_rtx (insn, &XEXP (x, i), class, action, type, 0);
719       else if (fmt[i] == 'E')
720         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
721           scan_rtx (insn, &XVECEXP (x, i, j), class, action, type, 0);
722     }
723 }
724
725 /* Build def/use chain */
726
727 static struct du_chain *
728 build_def_use (bb)
729      basic_block bb;
730 {
731   rtx insn;
732
733   open_chains = closed_chains = NULL;
734
735   for (insn = bb->head; ; insn = NEXT_INSN (insn))
736     {
737       if (INSN_P (insn))
738         {
739           int n_ops;
740           rtx note;
741           rtx old_operands[MAX_RECOG_OPERANDS];
742           rtx old_dups[MAX_DUP_OPERANDS];
743           int i;
744           int alt;
745           int predicated;
746
747           /* Process the insn, determining its effect on the def-use
748              chains.  We perform the following steps with the register
749              references in the insn:
750              (1) Any read that overlaps an open chain, but doesn't exactly
751                  match, causes that chain to be closed.  We can't deal
752                  with overlaps yet.
753              (2) Any read outside an operand causes any chain it overlaps
754                  with to be closed, since we can't replace it.
755              (3) Any read inside an operand is added if there's already
756                  an open chain for it.
757              (4) For any REG_DEAD note we find, close open chains that
758                  overlap it.
759              (5) For any write we find, close open chains that overlap it.
760              (6) For any write we find in an operand, make a new chain.
761              (7) For any REG_UNUSED, close any chains we just opened.  */
762
763           extract_insn (insn);
764           constrain_operands (1);
765           preprocess_constraints ();
766           alt = which_alternative;
767           n_ops = recog_data.n_operands;
768
769           /* Simplify the code below by rewriting things to reflect
770              matching constraints.  Also promote OP_OUT to OP_INOUT
771              in predicated instructions.  */
772
773           predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
774           for (i = 0; i < n_ops; ++i)
775             {
776               int matches = recog_op_alt[i][alt].matches;
777               if (matches >= 0)
778                 recog_op_alt[i][alt].class = recog_op_alt[matches][alt].class;
779               if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
780                   || (predicated && recog_data.operand_type[i] == OP_OUT))
781                 recog_data.operand_type[i] = OP_INOUT;
782             }
783
784           /* Step 1: Close chains for which we have overlapping reads.  */
785           for (i = 0; i < n_ops; i++)
786             scan_rtx (insn, recog_data.operand_loc[i],
787                       NO_REGS, terminate_overlapping_read,
788                       recog_data.operand_type[i], 0);
789
790           /* Step 2: Close chains for which we have reads outside operands.
791              We do this by munging all operands into CC0, and closing 
792              everything remaining.  */
793
794           for (i = 0; i < n_ops; i++)
795             {
796               old_operands[i] = recog_data.operand[i];
797               /* Don't squash match_operator or match_parallel here, since
798                  we don't know that all of the contained registers are 
799                  reachable by proper operands.  */
800               if (recog_data.constraints[i][0] == '\0')
801                 continue;
802               *recog_data.operand_loc[i] = cc0_rtx;
803             }
804           for (i = 0; i < recog_data.n_dups; i++)
805             {
806               old_dups[i] = *recog_data.dup_loc[i];
807               *recog_data.dup_loc[i] = cc0_rtx;
808             }
809
810           scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
811                     OP_IN, 0);
812
813           for (i = 0; i < recog_data.n_dups; i++)
814             *recog_data.dup_loc[i] = old_dups[i];
815           for (i = 0; i < n_ops; i++)
816             *recog_data.operand_loc[i] = old_operands[i];
817
818           /* Step 2B: Can't rename function call argument registers.  */
819           if (GET_CODE (insn) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (insn))
820             scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
821                       NO_REGS, terminate_all_read, OP_IN, 0);
822
823           /* Step 3: Append to chains for reads inside operands.  */
824           for (i = 0; i < n_ops + recog_data.n_dups; i++)
825             {
826               int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
827               rtx *loc = (i < n_ops
828                           ? recog_data.operand_loc[opn]
829                           : recog_data.dup_loc[i - n_ops]);
830               enum reg_class class = recog_op_alt[opn][alt].class;
831               enum op_type type = recog_data.operand_type[opn];
832
833               /* Don't scan match_operand here, since we've no reg class
834                  information to pass down.  Any operands that we could
835                  substitute in will be represented elsewhere.  */
836               if (recog_data.constraints[opn][0] == '\0')
837                 continue;
838
839               if (recog_op_alt[opn][alt].is_address)
840                 scan_rtx_address (insn, loc, class, mark_read, VOIDmode);
841               else
842                 scan_rtx (insn, loc, class, mark_read, type, 0);
843             }
844
845           /* Step 4: Close chains for registers that die here.
846              Also record updates for REG_INC notes.  */
847           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
848             {
849               if (REG_NOTE_KIND (note) == REG_DEAD)
850                 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
851                           OP_IN, 0);
852               else if (REG_NOTE_KIND (note) == REG_INC)
853                 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
854                           OP_INOUT, 0);
855             }
856
857           /* Step 4B: If this is a call, any chain live at this point
858              requires a caller-saved reg.  */
859           if (GET_CODE (insn) == CALL_INSN)
860             {
861               struct du_chain *p;
862               for (p = open_chains; p; p = p->next_chain)
863                 p->need_caller_save_reg = 1;
864             }
865
866           /* Step 5: Close open chains that overlap writes.  Similar to
867              step 2, we hide in-out operands, since we do not want to
868              close these chains.  */
869
870           for (i = 0; i < n_ops; i++)
871             {
872               old_operands[i] = recog_data.operand[i];
873               if (recog_data.operand_type[i] == OP_INOUT)
874                 *recog_data.operand_loc[i] = cc0_rtx;
875             }
876           for (i = 0; i < recog_data.n_dups; i++)
877             {
878               int opn = recog_data.dup_num[i];
879               old_dups[i] = *recog_data.dup_loc[i];
880               if (recog_data.operand_type[opn] == OP_INOUT)
881                 *recog_data.dup_loc[i] = cc0_rtx;
882             }
883
884           scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
885
886           for (i = 0; i < recog_data.n_dups; i++)
887             *recog_data.dup_loc[i] = old_dups[i];
888           for (i = 0; i < n_ops; i++)
889             *recog_data.operand_loc[i] = old_operands[i];
890
891           /* Step 6: Begin new chains for writes inside operands.  */
892           /* ??? Many targets have output constraints on the SET_DEST
893              of a call insn, which is stupid, since these are certainly
894              ABI defined hard registers.  Don't change calls at all.  */
895           if (GET_CODE (insn) != CALL_INSN)
896             for (i = 0; i < n_ops + recog_data.n_dups; i++)
897               {
898                 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
899                 rtx *loc = (i < n_ops
900                             ? recog_data.operand_loc[opn]
901                             : recog_data.dup_loc[i - n_ops]);
902                 enum reg_class class = recog_op_alt[opn][alt].class;
903
904                 if (recog_data.operand_type[opn] == OP_OUT)
905                   scan_rtx (insn, loc, class, mark_write, OP_OUT,
906                             recog_op_alt[opn][alt].earlyclobber);
907               }
908
909           /* Step 7: Close chains for registers that were never
910              really used here.  */
911           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
912             if (REG_NOTE_KIND (note) == REG_UNUSED)
913               scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
914                         OP_IN, 0);
915         }
916       if (insn == bb->end)
917         break;
918     }
919
920   /* Since we close every chain when we find a REG_DEAD note, anything that
921      is still open lives past the basic block, so it can't be renamed.  */
922   return closed_chains;
923 }
924
925 /* Dump all def/use chains in CHAINS to RTL_DUMP_FILE.  They are
926    printed in reverse order as that's how we build them.  */
927
928 static void
929 dump_def_use_chain (chains)
930      struct du_chain *chains;
931 {
932   while (chains)
933     {
934       struct du_chain *this = chains;
935       int r = REGNO (*this->loc);
936       int nregs = HARD_REGNO_NREGS (r, GET_MODE (*this->loc));
937       fprintf (rtl_dump_file, "Register %s (%d):", reg_names[r], nregs);
938       while (this)
939         {
940           fprintf (rtl_dump_file, " %d [%s]", INSN_UID (this->insn),
941                    reg_class_names[this->class]);
942           this = this->next_use;
943         }
944       fprintf (rtl_dump_file, "\n");
945       chains = chains->next_chain;
946     }
947 }