OSDN Git Service

2002-03-03 Aldy Hernandez <aldyh@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / regrename.c
1 /* Register renaming for the GNU compiler.
2    Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
3
4    This file is part of GCC.
5
6    GCC is free software; you can redistribute it and/or modify it
7    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    GCC is distributed in the hope that it will be useful, but WITHOUT
12    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
14    License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GCC; see the file COPYING.  If not, write to the Free
18    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19    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 "toplev.h"
37 #include "obstack.h"
38
39 #define obstack_chunk_alloc xmalloc
40 #define obstack_chunk_free free
41
42 #ifndef REGNO_MODE_OK_FOR_BASE_P
43 #define REGNO_MODE_OK_FOR_BASE_P(REGNO, MODE) REGNO_OK_FOR_BASE_P (REGNO)
44 #endif
45
46 #ifndef REG_MODE_OK_FOR_BASE_P
47 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
48 #endif
49
50 static const char *const reg_class_names[] = REG_CLASS_NAMES;
51
52 struct du_chain
53 {
54   struct du_chain *next_chain;
55   struct du_chain *next_use;
56
57   rtx insn;
58   rtx *loc;
59   enum reg_class class;
60   unsigned int need_caller_save_reg:1;
61   unsigned int earlyclobber:1;
62 };
63
64 enum scan_actions
65 {
66   terminate_all_read,
67   terminate_overlapping_read,
68   terminate_write,
69   terminate_dead,
70   mark_read,
71   mark_write
72 };
73
74 static const char * const scan_actions_name[] =
75 {
76   "terminate_all_read",
77   "terminate_overlapping_read",
78   "terminate_write",
79   "terminate_dead",
80   "mark_read",
81   "mark_write"
82 };
83
84 static struct obstack rename_obstack;
85
86 static void do_replace PARAMS ((struct du_chain *, int));
87 static void scan_rtx_reg PARAMS ((rtx, rtx *, enum reg_class,
88                                   enum scan_actions, enum op_type, int));
89 static void scan_rtx_address PARAMS ((rtx, rtx *, enum reg_class,
90                                       enum scan_actions, enum machine_mode));
91 static void scan_rtx PARAMS ((rtx, rtx *, enum reg_class,
92                               enum scan_actions, enum op_type, int));
93 static struct du_chain *build_def_use PARAMS ((basic_block));
94 static void dump_def_use_chain PARAMS ((struct du_chain *));
95 static void note_sets PARAMS ((rtx, rtx, void *));
96 static void clear_dead_regs PARAMS ((HARD_REG_SET *, enum machine_mode, rtx));
97 static void merge_overlapping_regs PARAMS ((basic_block, HARD_REG_SET *,
98                                             struct du_chain *));
99
100 /* Called through note_stores from update_life.  Find sets of registers, and
101    record them in *DATA (which is actually a HARD_REG_SET *).  */
102
103 static void
104 note_sets (x, set, data)
105      rtx x;
106      rtx set ATTRIBUTE_UNUSED;
107      void *data;
108 {
109   HARD_REG_SET *pset = (HARD_REG_SET *) data;
110   unsigned int regno;
111   int nregs;
112   if (GET_CODE (x) != REG)
113     return;
114   regno = REGNO (x);
115   nregs = HARD_REGNO_NREGS (regno, GET_MODE (x));
116
117   /* There must not be pseudos at this point.  */
118   if (regno + nregs > FIRST_PSEUDO_REGISTER)
119     abort ();
120
121   while (nregs-- > 0)
122     SET_HARD_REG_BIT (*pset, regno + nregs);
123 }
124
125 /* Clear all registers from *PSET for which a note of kind KIND can be found
126    in the list NOTES.  */
127
128 static void
129 clear_dead_regs (pset, kind, notes)
130      HARD_REG_SET *pset;
131      enum machine_mode kind;
132      rtx notes;
133 {
134   rtx note;
135   for (note = notes; note; note = XEXP (note, 1))
136     if (REG_NOTE_KIND (note) == kind && REG_P (XEXP (note, 0)))
137       {
138         rtx reg = XEXP (note, 0);
139         unsigned int regno = REGNO (reg);
140         int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
141
142         /* There must not be pseudos at this point.  */
143         if (regno + nregs > FIRST_PSEUDO_REGISTER)
144           abort ();
145
146         while (nregs-- > 0)
147           CLEAR_HARD_REG_BIT (*pset, regno + nregs);
148       }
149 }
150
151 /* For a def-use chain CHAIN in basic block B, find which registers overlap
152    its lifetime and set the corresponding bits in *PSET.  */
153
154 static void
155 merge_overlapping_regs (b, pset, chain)
156      basic_block b;
157      HARD_REG_SET *pset;
158      struct du_chain *chain;
159 {
160   struct du_chain *t = chain;
161   rtx insn;
162   HARD_REG_SET live;
163
164   REG_SET_TO_HARD_REG_SET (live, b->global_live_at_start);
165   insn = b->head;
166   while (t)
167     {
168       /* Search forward until the next reference to the register to be
169          renamed.  */
170       while (insn != t->insn)
171         {
172           if (INSN_P (insn))
173             {
174               clear_dead_regs (&live, REG_DEAD, REG_NOTES (insn));
175               note_stores (PATTERN (insn), note_sets, (void *) &live);
176               /* Only record currently live regs if we are inside the
177                  reg's live range.  */
178               if (t != chain)
179                 IOR_HARD_REG_SET (*pset, live);
180               clear_dead_regs (&live, REG_UNUSED, REG_NOTES (insn));  
181             }
182           insn = NEXT_INSN (insn);
183         }
184
185       IOR_HARD_REG_SET (*pset, live);
186
187       /* For the last reference, also merge in all registers set in the
188          same insn.
189          @@@ We only have take earlyclobbered sets into account.  */
190       if (! t->next_use)
191         note_stores (PATTERN (insn), note_sets, (void *) pset);
192
193       t = t->next_use;
194     }
195 }
196
197 /* Perform register renaming on the current function.  */
198
199 void
200 regrename_optimize ()
201 {
202   int tick[FIRST_PSEUDO_REGISTER];
203   int this_tick = 0;
204   int b;
205   char *first_obj;
206
207   memset (tick, 0, sizeof tick);
208
209   gcc_obstack_init (&rename_obstack);
210   first_obj = (char *) obstack_alloc (&rename_obstack, 0);
211
212   for (b = 0; b < n_basic_blocks; b++)
213     {
214       basic_block bb = BASIC_BLOCK (b);
215       struct du_chain *all_chains = 0;
216       HARD_REG_SET unavailable;
217       HARD_REG_SET regs_seen;
218
219       CLEAR_HARD_REG_SET (unavailable);
220
221       if (rtl_dump_file)
222         fprintf (rtl_dump_file, "\nBasic block %d:\n", b);
223
224       all_chains = build_def_use (bb);
225
226       if (rtl_dump_file)
227         dump_def_use_chain (all_chains);
228
229       CLEAR_HARD_REG_SET (unavailable);
230       /* Don't clobber traceback for noreturn functions.  */
231       if (frame_pointer_needed)
232         {
233           int i;
234           
235           for (i = HARD_REGNO_NREGS (FRAME_POINTER_REGNUM, Pmode); i--;)
236             SET_HARD_REG_BIT (unavailable, FRAME_POINTER_REGNUM + i);
237           
238 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
239           for (i = HARD_REGNO_NREGS (HARD_FRAME_POINTER_REGNUM, Pmode); i--;)
240             SET_HARD_REG_BIT (unavailable, HARD_FRAME_POINTER_REGNUM + i);
241 #endif
242         }
243
244       CLEAR_HARD_REG_SET (regs_seen);
245       while (all_chains)
246         {
247           int new_reg, best_new_reg = -1;
248           int n_uses;
249           struct du_chain *this = all_chains;
250           struct du_chain *tmp, *last;
251           HARD_REG_SET this_unavailable;
252           int reg = REGNO (*this->loc);
253           int i;
254
255           all_chains = this->next_chain;
256           
257 #if 0 /* This just disables optimization opportunities.  */
258           /* Only rename once we've seen the reg more than once.  */
259           if (! TEST_HARD_REG_BIT (regs_seen, reg))
260             {
261               SET_HARD_REG_BIT (regs_seen, reg);
262               continue;
263             }
264 #endif
265
266           if (fixed_regs[reg] || global_regs[reg]
267 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
268               || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
269 #else
270               || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
271 #endif
272               )
273             continue;
274
275           COPY_HARD_REG_SET (this_unavailable, unavailable);
276
277           /* Find last entry on chain (which has the need_caller_save bit),
278              count number of uses, and narrow the set of registers we can
279              use for renaming.  */
280           n_uses = 0;
281           for (last = this; last->next_use; last = last->next_use)
282             {
283               n_uses++;
284               IOR_COMPL_HARD_REG_SET (this_unavailable,
285                                       reg_class_contents[last->class]);
286             }
287           if (n_uses < 1)
288             continue;
289
290           IOR_COMPL_HARD_REG_SET (this_unavailable,
291                                   reg_class_contents[last->class]);
292
293           if (this->need_caller_save_reg)
294             IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
295
296           merge_overlapping_regs (bb, &this_unavailable, this);
297
298           /* Now potential_regs is a reasonable approximation, let's
299              have a closer look at each register still in there.  */
300           for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
301             {
302               int nregs = HARD_REGNO_NREGS (new_reg, GET_MODE (*this->loc));
303
304               for (i = nregs - 1; i >= 0; --i)
305                 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
306                     || fixed_regs[new_reg + i]
307                     || global_regs[new_reg + i]
308                     /* Can't use regs which aren't saved by the prologue.  */
309                     || (! regs_ever_live[new_reg + i]
310                         && ! call_used_regs[new_reg + i])
311 #ifdef LEAF_REGISTERS
312                     /* We can't use a non-leaf register if we're in a 
313                        leaf function.  */
314                     || (current_function_is_leaf 
315                         && !LEAF_REGISTERS[new_reg + i])
316 #endif
317 #ifdef HARD_REGNO_RENAME_OK
318                     || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
319 #endif
320                     )
321                   break;
322               if (i >= 0)
323                 continue;
324
325               /* See whether it accepts all modes that occur in
326                  definition and uses.  */
327               for (tmp = this; tmp; tmp = tmp->next_use)
328                 if (! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
329                     || (tmp->need_caller_save_reg
330                         && ! (HARD_REGNO_CALL_PART_CLOBBERED
331                               (reg, GET_MODE (*tmp->loc)))
332                         && (HARD_REGNO_CALL_PART_CLOBBERED
333                             (new_reg, GET_MODE (*tmp->loc)))))
334                   break;
335               if (! tmp)
336                 {
337                   if (best_new_reg == -1
338                       || tick[best_new_reg] > tick[new_reg])
339                     best_new_reg = new_reg;
340                 }
341             }
342
343           if (rtl_dump_file)
344             {
345               fprintf (rtl_dump_file, "Register %s in insn %d",
346                        reg_names[reg], INSN_UID (last->insn));
347               if (last->need_caller_save_reg)
348                 fprintf (rtl_dump_file, " crosses a call");
349               }
350
351           if (best_new_reg == -1)
352             {
353               if (rtl_dump_file)
354                 fprintf (rtl_dump_file, "; no available registers\n");
355               continue;
356             }
357
358           do_replace (this, best_new_reg);
359           tick[best_new_reg] = this_tick++;
360
361           if (rtl_dump_file)
362             fprintf (rtl_dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
363         }
364
365       obstack_free (&rename_obstack, first_obj);
366     }
367
368   obstack_free (&rename_obstack, NULL);
369
370   if (rtl_dump_file)
371     fputc ('\n', rtl_dump_file);
372
373   count_or_remove_death_notes (NULL, 1);
374   update_life_info (NULL, UPDATE_LIFE_LOCAL,
375                     PROP_REG_INFO | PROP_DEATH_NOTES);
376 }
377
378 static void
379 do_replace (chain, reg)
380      struct du_chain *chain;
381      int reg;
382 {
383   while (chain)
384     {
385       unsigned int regno = ORIGINAL_REGNO (*chain->loc);
386       *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
387       if (regno >= FIRST_PSEUDO_REGISTER)
388         ORIGINAL_REGNO (*chain->loc) = regno;
389       chain = chain->next_use;
390     }
391 }
392
393
394 static struct du_chain *open_chains;
395 static struct du_chain *closed_chains;
396
397 static void
398 scan_rtx_reg (insn, loc, class, action, type, earlyclobber)
399      rtx insn;
400      rtx *loc;
401      enum reg_class class;
402      enum scan_actions action;
403      enum op_type type;
404      int earlyclobber;
405 {
406   struct du_chain **p;
407   rtx x = *loc;
408   enum machine_mode mode = GET_MODE (x);
409   int this_regno = REGNO (x);
410   int this_nregs = HARD_REGNO_NREGS (this_regno, mode);
411
412   if (action == mark_write)
413     {
414       if (type == OP_OUT)
415         {
416           struct du_chain *this = (struct du_chain *)
417             obstack_alloc (&rename_obstack, sizeof (struct du_chain));
418           this->next_use = 0;
419           this->next_chain = open_chains;
420           this->loc = loc;
421           this->insn = insn;
422           this->class = class;
423           this->need_caller_save_reg = 0;
424           this->earlyclobber = earlyclobber;
425           open_chains = this;
426         }
427       return;
428     }
429
430   if ((type == OP_OUT && action != terminate_write)
431       || (type != OP_OUT && action == terminate_write))
432     return;
433
434   for (p = &open_chains; *p;)
435     {
436       struct du_chain *this = *p;
437
438       /* Check if the chain has been terminated if it has then skip to
439          the next chain.
440
441          This can happen when we've already appended the location to
442          the chain in Step 3, but are trying to hide in-out operands
443          from terminate_write in Step 5.  */
444
445       if (*this->loc == cc0_rtx)
446         p = &this->next_chain;
447       else
448         {
449           int regno = REGNO (*this->loc);
450           int nregs = HARD_REGNO_NREGS (regno, GET_MODE (*this->loc));
451           int exact_match = (regno == this_regno && nregs == this_nregs);
452
453           if (regno + nregs <= this_regno
454               || this_regno + this_nregs <= regno)
455             {
456               p = &this->next_chain;
457               continue;
458             }
459
460           if (action == mark_read)
461             {
462               if (! exact_match)
463                 abort ();
464
465               /* ??? Class NO_REGS can happen if the md file makes use of 
466                  EXTRA_CONSTRAINTS to match registers.  Which is arguably
467                  wrong, but there we are.  Since we know not what this may
468                  be replaced with, terminate the chain.  */
469               if (class != NO_REGS)
470                 {
471                   this = (struct du_chain *)
472                     obstack_alloc (&rename_obstack, sizeof (struct du_chain));
473                   this->next_use = 0;
474                   this->next_chain = (*p)->next_chain;
475                   this->loc = loc;
476                   this->insn = insn;
477                   this->class = class;
478                   this->need_caller_save_reg = 0;
479                   while (*p)
480                     p = &(*p)->next_use;
481                   *p = this;
482                   return;
483                 }
484             }
485
486           if (action != terminate_overlapping_read || ! exact_match)
487             {
488               struct du_chain *next = this->next_chain;
489
490               /* Whether the terminated chain can be used for renaming
491                  depends on the action and this being an exact match.
492                  In either case, we remove this element from open_chains.  */
493
494               if ((action == terminate_dead || action == terminate_write)
495                   && exact_match)
496                 {
497                   this->next_chain = closed_chains;
498                   closed_chains = this;
499                   if (rtl_dump_file)
500                     fprintf (rtl_dump_file,
501                              "Closing chain %s at insn %d (%s)\n",
502                              reg_names[REGNO (*this->loc)], INSN_UID (insn),
503                              scan_actions_name[(int) action]);
504                 }
505               else
506                 {
507                   if (rtl_dump_file)
508                     fprintf (rtl_dump_file,
509                              "Discarding chain %s at insn %d (%s)\n",
510                              reg_names[REGNO (*this->loc)], INSN_UID (insn),
511                              scan_actions_name[(int) action]);
512                 }
513               *p = next;
514             }
515           else
516             p = &this->next_chain;
517         }
518     }
519 }
520
521 /* Adapted from find_reloads_address_1.  CLASS is INDEX_REG_CLASS or
522    BASE_REG_CLASS depending on how the register is being considered.  */
523
524 static void
525 scan_rtx_address (insn, loc, class, action, mode)
526      rtx insn;
527      rtx *loc;
528      enum reg_class class;
529      enum scan_actions action;
530      enum machine_mode mode;
531 {
532   rtx x = *loc;
533   RTX_CODE code = GET_CODE (x);
534   const char *fmt;
535   int i, j;
536
537   if (action == mark_write)
538     return;
539
540   switch (code)
541     {
542     case PLUS:
543       {
544         rtx orig_op0 = XEXP (x, 0);
545         rtx orig_op1 = XEXP (x, 1);
546         RTX_CODE code0 = GET_CODE (orig_op0);
547         RTX_CODE code1 = GET_CODE (orig_op1);
548         rtx op0 = orig_op0;
549         rtx op1 = orig_op1;
550         rtx *locI = NULL;
551         rtx *locB = NULL;
552
553         if (GET_CODE (op0) == SUBREG)
554           {
555             op0 = SUBREG_REG (op0);
556             code0 = GET_CODE (op0);
557           }
558
559         if (GET_CODE (op1) == SUBREG)
560           {
561             op1 = SUBREG_REG (op1);
562             code1 = GET_CODE (op1);
563           }
564
565         if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
566             || code0 == ZERO_EXTEND || code1 == MEM)
567           {
568             locI = &XEXP (x, 0);
569             locB = &XEXP (x, 1);
570           }
571         else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
572                  || code1 == ZERO_EXTEND || code0 == MEM)
573           {
574             locI = &XEXP (x, 1);
575             locB = &XEXP (x, 0);
576           }
577         else if (code0 == CONST_INT || code0 == CONST
578                  || code0 == SYMBOL_REF || code0 == LABEL_REF)
579           locB = &XEXP (x, 1);
580         else if (code1 == CONST_INT || code1 == CONST
581                  || code1 == SYMBOL_REF || code1 == LABEL_REF)
582           locB = &XEXP (x, 0);
583         else if (code0 == REG && code1 == REG)
584           {
585             int index_op;
586
587             if (REG_OK_FOR_INDEX_P (op0)
588                 && REG_MODE_OK_FOR_BASE_P (op1, mode))
589               index_op = 0;
590             else if (REG_OK_FOR_INDEX_P (op1)
591                      && REG_MODE_OK_FOR_BASE_P (op0, mode))
592               index_op = 1;
593             else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
594               index_op = 0;
595             else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
596               index_op = 1;
597             else if (REG_OK_FOR_INDEX_P (op1))
598               index_op = 1;
599             else
600               index_op = 0;
601
602             locI = &XEXP (x, index_op);
603             locB = &XEXP (x, !index_op);
604           }
605         else if (code0 == REG)
606           {
607             locI = &XEXP (x, 0);
608             locB = &XEXP (x, 1);
609           }
610         else if (code1 == REG)
611           {
612             locI = &XEXP (x, 1);
613             locB = &XEXP (x, 0);
614           }
615
616         if (locI)
617           scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
618         if (locB)
619           scan_rtx_address (insn, locB, MODE_BASE_REG_CLASS (mode), action, mode);
620         return;
621       }
622
623     case POST_INC:
624     case POST_DEC:
625     case POST_MODIFY:
626     case PRE_INC:
627     case PRE_DEC:
628     case PRE_MODIFY:
629 #ifndef AUTO_INC_DEC
630       /* If the target doesn't claim to handle autoinc, this must be
631          something special, like a stack push.  Kill this chain.  */
632       action = terminate_all_read;
633 #endif
634       break;
635
636     case MEM:
637       scan_rtx_address (insn, &XEXP (x, 0),
638                         MODE_BASE_REG_CLASS (GET_MODE (x)), action,
639                         GET_MODE (x));
640       return;
641
642     case REG:
643       scan_rtx_reg (insn, loc, class, action, OP_IN, 0);
644       return;
645
646     default:
647       break;
648     }
649
650   fmt = GET_RTX_FORMAT (code);
651   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
652     {
653       if (fmt[i] == 'e')
654         scan_rtx_address (insn, &XEXP (x, i), class, action, mode);
655       else if (fmt[i] == 'E')
656         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
657           scan_rtx_address (insn, &XVECEXP (x, i, j), class, action, mode);
658     }
659 }
660
661 static void
662 scan_rtx (insn, loc, class, action, type, earlyclobber)
663      rtx insn;
664      rtx *loc;
665      enum reg_class class;
666      enum scan_actions action;
667      enum op_type type;
668      int earlyclobber;
669 {
670   const char *fmt;
671   rtx x = *loc;
672   enum rtx_code code = GET_CODE (x);
673   int i, j;
674
675   code = GET_CODE (x);
676   switch (code)
677     {
678     case CONST:
679     case CONST_INT:
680     case CONST_DOUBLE:
681     case CONST_VECTOR:
682     case SYMBOL_REF:
683     case LABEL_REF:
684     case CC0:
685     case PC:
686       return;
687
688     case REG:
689       scan_rtx_reg (insn, loc, class, action, type, earlyclobber);
690       return;
691
692     case MEM:
693       scan_rtx_address (insn, &XEXP (x, 0),
694                         MODE_BASE_REG_CLASS (GET_MODE (x)), action,
695                         GET_MODE (x));
696       return;
697
698     case SET:
699       scan_rtx (insn, &SET_SRC (x), class, action, OP_IN, 0);
700       scan_rtx (insn, &SET_DEST (x), class, action, OP_OUT, 0);
701       return;
702
703     case STRICT_LOW_PART:
704       scan_rtx (insn, &XEXP (x, 0), class, action, OP_INOUT, earlyclobber);
705       return;
706
707     case ZERO_EXTRACT:
708     case SIGN_EXTRACT: 
709       scan_rtx (insn, &XEXP (x, 0), class, action,
710                 type == OP_IN ? OP_IN : OP_INOUT, earlyclobber);
711       scan_rtx (insn, &XEXP (x, 1), class, action, OP_IN, 0);
712       scan_rtx (insn, &XEXP (x, 2), class, action, OP_IN, 0);
713       return;
714
715     case POST_INC:
716     case PRE_INC:
717     case POST_DEC:
718     case PRE_DEC:
719     case POST_MODIFY:
720     case PRE_MODIFY:
721       /* Should only happen inside MEM.  */
722       abort ();
723
724     case CLOBBER:
725       scan_rtx (insn, &SET_DEST (x), class, action, OP_OUT, 1);
726       return;
727
728     case EXPR_LIST:
729       scan_rtx (insn, &XEXP (x, 0), class, action, type, 0);
730       if (XEXP (x, 1))
731         scan_rtx (insn, &XEXP (x, 1), class, action, type, 0);
732       return;
733
734     default:
735       break;
736     }
737
738   fmt = GET_RTX_FORMAT (code);
739   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
740     {
741       if (fmt[i] == 'e')
742         scan_rtx (insn, &XEXP (x, i), class, action, type, 0);
743       else if (fmt[i] == 'E')
744         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
745           scan_rtx (insn, &XVECEXP (x, i, j), class, action, type, 0);
746     }
747 }
748
749 /* Build def/use chain */
750
751 static struct du_chain *
752 build_def_use (bb)
753      basic_block bb;
754 {
755   rtx insn;
756
757   open_chains = closed_chains = NULL;
758
759   for (insn = bb->head; ; insn = NEXT_INSN (insn))
760     {
761       if (INSN_P (insn))
762         {
763           int n_ops;
764           rtx note;
765           rtx old_operands[MAX_RECOG_OPERANDS];
766           rtx old_dups[MAX_DUP_OPERANDS];
767           int i;
768           int alt;
769           int predicated;
770
771           /* Process the insn, determining its effect on the def-use
772              chains.  We perform the following steps with the register
773              references in the insn:
774              (1) Any read that overlaps an open chain, but doesn't exactly
775                  match, causes that chain to be closed.  We can't deal
776                  with overlaps yet.
777              (2) Any read outside an operand causes any chain it overlaps
778                  with to be closed, since we can't replace it.
779              (3) Any read inside an operand is added if there's already
780                  an open chain for it.
781              (4) For any REG_DEAD note we find, close open chains that
782                  overlap it.
783              (5) For any write we find, close open chains that overlap it.
784              (6) For any write we find in an operand, make a new chain.
785              (7) For any REG_UNUSED, close any chains we just opened.  */
786
787           extract_insn (insn);
788           constrain_operands (1);
789           preprocess_constraints ();
790           alt = which_alternative;
791           n_ops = recog_data.n_operands;
792
793           /* Simplify the code below by rewriting things to reflect
794              matching constraints.  Also promote OP_OUT to OP_INOUT
795              in predicated instructions.  */
796
797           predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
798           for (i = 0; i < n_ops; ++i)
799             {
800               int matches = recog_op_alt[i][alt].matches;
801               if (matches >= 0)
802                 recog_op_alt[i][alt].class = recog_op_alt[matches][alt].class;
803               if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
804                   || (predicated && recog_data.operand_type[i] == OP_OUT))
805                 recog_data.operand_type[i] = OP_INOUT;
806             }
807
808           /* Step 1: Close chains for which we have overlapping reads.  */
809           for (i = 0; i < n_ops; i++)
810             scan_rtx (insn, recog_data.operand_loc[i],
811                       NO_REGS, terminate_overlapping_read,
812                       recog_data.operand_type[i], 0);
813
814           /* Step 2: Close chains for which we have reads outside operands.
815              We do this by munging all operands into CC0, and closing 
816              everything remaining.  */
817
818           for (i = 0; i < n_ops; i++)
819             {
820               old_operands[i] = recog_data.operand[i];
821               /* Don't squash match_operator or match_parallel here, since
822                  we don't know that all of the contained registers are 
823                  reachable by proper operands.  */
824               if (recog_data.constraints[i][0] == '\0')
825                 continue;
826               *recog_data.operand_loc[i] = cc0_rtx;
827             }
828           for (i = 0; i < recog_data.n_dups; i++)
829             {
830               old_dups[i] = *recog_data.dup_loc[i];
831               *recog_data.dup_loc[i] = cc0_rtx;
832             }
833
834           scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
835                     OP_IN, 0);
836
837           for (i = 0; i < recog_data.n_dups; i++)
838             *recog_data.dup_loc[i] = old_dups[i];
839           for (i = 0; i < n_ops; i++)
840             *recog_data.operand_loc[i] = old_operands[i];
841
842           /* Step 2B: Can't rename function call argument registers.  */
843           if (GET_CODE (insn) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (insn))
844             scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
845                       NO_REGS, terminate_all_read, OP_IN, 0);
846
847           /* Step 2C: Can't rename asm operands that were originally
848              hard registers.  */
849           if (asm_noperands (PATTERN (insn)) > 0)
850             for (i = 0; i < n_ops; i++)
851               {
852                 rtx *loc = recog_data.operand_loc[i];
853                 rtx op = *loc;
854
855                 if (GET_CODE (op) == REG
856                     && REGNO (op) == ORIGINAL_REGNO (op)
857                     && (recog_data.operand_type[i] == OP_IN
858                         || recog_data.operand_type[i] == OP_INOUT))
859                   scan_rtx (insn, loc, NO_REGS, terminate_all_read, OP_IN, 0);
860               }
861
862           /* Step 3: Append to chains for reads inside operands.  */
863           for (i = 0; i < n_ops + recog_data.n_dups; i++)
864             {
865               int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
866               rtx *loc = (i < n_ops
867                           ? recog_data.operand_loc[opn]
868                           : recog_data.dup_loc[i - n_ops]);
869               enum reg_class class = recog_op_alt[opn][alt].class;
870               enum op_type type = recog_data.operand_type[opn];
871
872               /* Don't scan match_operand here, since we've no reg class
873                  information to pass down.  Any operands that we could
874                  substitute in will be represented elsewhere.  */
875               if (recog_data.constraints[opn][0] == '\0')
876                 continue;
877
878               if (recog_op_alt[opn][alt].is_address)
879                 scan_rtx_address (insn, loc, class, mark_read, VOIDmode);
880               else
881                 scan_rtx (insn, loc, class, mark_read, type, 0);
882             }
883
884           /* Step 4: Close chains for registers that die here.
885              Also record updates for REG_INC notes.  */
886           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
887             {
888               if (REG_NOTE_KIND (note) == REG_DEAD)
889                 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
890                           OP_IN, 0);
891               else if (REG_NOTE_KIND (note) == REG_INC)
892                 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
893                           OP_INOUT, 0);
894             }
895
896           /* Step 4B: If this is a call, any chain live at this point
897              requires a caller-saved reg.  */
898           if (GET_CODE (insn) == CALL_INSN)
899             {
900               struct du_chain *p;
901               for (p = open_chains; p; p = p->next_chain)
902                 p->need_caller_save_reg = 1;
903             }
904
905           /* Step 5: Close open chains that overlap writes.  Similar to
906              step 2, we hide in-out operands, since we do not want to
907              close these chains.  */
908
909           for (i = 0; i < n_ops; i++)
910             {
911               old_operands[i] = recog_data.operand[i];
912               if (recog_data.operand_type[i] == OP_INOUT)
913                 *recog_data.operand_loc[i] = cc0_rtx;
914             }
915           for (i = 0; i < recog_data.n_dups; i++)
916             {
917               int opn = recog_data.dup_num[i];
918               old_dups[i] = *recog_data.dup_loc[i];
919               if (recog_data.operand_type[opn] == OP_INOUT)
920                 *recog_data.dup_loc[i] = cc0_rtx;
921             }
922
923           scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
924
925           for (i = 0; i < recog_data.n_dups; i++)
926             *recog_data.dup_loc[i] = old_dups[i];
927           for (i = 0; i < n_ops; i++)
928             *recog_data.operand_loc[i] = old_operands[i];
929
930           /* Step 6: Begin new chains for writes inside operands.  */
931           /* ??? Many targets have output constraints on the SET_DEST
932              of a call insn, which is stupid, since these are certainly
933              ABI defined hard registers.  Don't change calls at all.
934              Similarly take special care for asm statement that originally
935              referenced hard registers.  */
936           if (asm_noperands (PATTERN (insn)) > 0)
937             {
938               for (i = 0; i < n_ops; i++)
939                 if (recog_data.operand_type[i] == OP_OUT)
940                   {
941                     rtx *loc = recog_data.operand_loc[i];
942                     rtx op = *loc;
943                     enum reg_class class = recog_op_alt[i][alt].class;
944
945                     if (GET_CODE (op) == REG
946                         && REGNO (op) == ORIGINAL_REGNO (op))
947                       continue;
948
949                     scan_rtx (insn, loc, class, mark_write, OP_OUT,
950                               recog_op_alt[i][alt].earlyclobber);
951                   }
952             }
953           else if (GET_CODE (insn) != CALL_INSN)
954             for (i = 0; i < n_ops + recog_data.n_dups; i++)
955               {
956                 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
957                 rtx *loc = (i < n_ops
958                             ? recog_data.operand_loc[opn]
959                             : recog_data.dup_loc[i - n_ops]);
960                 enum reg_class class = recog_op_alt[opn][alt].class;
961
962                 if (recog_data.operand_type[opn] == OP_OUT)
963                   scan_rtx (insn, loc, class, mark_write, OP_OUT,
964                             recog_op_alt[opn][alt].earlyclobber);
965               }
966
967           /* Step 7: Close chains for registers that were never
968              really used here.  */
969           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
970             if (REG_NOTE_KIND (note) == REG_UNUSED)
971               scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
972                         OP_IN, 0);
973         }
974       if (insn == bb->end)
975         break;
976     }
977
978   /* Since we close every chain when we find a REG_DEAD note, anything that
979      is still open lives past the basic block, so it can't be renamed.  */
980   return closed_chains;
981 }
982
983 /* Dump all def/use chains in CHAINS to RTL_DUMP_FILE.  They are
984    printed in reverse order as that's how we build them.  */
985
986 static void
987 dump_def_use_chain (chains)
988      struct du_chain *chains;
989 {
990   while (chains)
991     {
992       struct du_chain *this = chains;
993       int r = REGNO (*this->loc);
994       int nregs = HARD_REGNO_NREGS (r, GET_MODE (*this->loc));
995       fprintf (rtl_dump_file, "Register %s (%d):", reg_names[r], nregs);
996       while (this)
997         {
998           fprintf (rtl_dump_file, " %d [%s]", INSN_UID (this->insn),
999                    reg_class_names[this->class]);
1000           this = this->next_use;
1001         }
1002       fprintf (rtl_dump_file, "\n");
1003       chains = chains->next_chain;
1004     }
1005 }
1006 \f
1007 /* The following code does forward propagation of hard register copies.
1008    The object is to eliminate as many dependencies as possible, so that
1009    we have the most scheduling freedom.  As a side effect, we also clean
1010    up some silly register allocation decisions made by reload.  This 
1011    code may be obsoleted by a new register allocator.  */
1012
1013 /* For each register, we have a list of registers that contain the same
1014    value.  The OLDEST_REGNO field points to the head of the list, and 
1015    the NEXT_REGNO field runs through the list.  The MODE field indicates
1016    what mode the data is known to be in; this field is VOIDmode when the
1017    register is not known to contain valid data.  */
1018
1019 struct value_data_entry
1020 {
1021   enum machine_mode mode;
1022   unsigned int oldest_regno;
1023   unsigned int next_regno;
1024 };
1025
1026 struct value_data
1027 {
1028   struct value_data_entry e[FIRST_PSEUDO_REGISTER];
1029   unsigned int max_value_regs;
1030 };
1031
1032 static void kill_value_regno PARAMS ((unsigned, struct value_data *));
1033 static void kill_value PARAMS ((rtx, struct value_data *));
1034 static void set_value_regno PARAMS ((unsigned, enum machine_mode,
1035                                      struct value_data *));
1036 static void init_value_data PARAMS ((struct value_data *));
1037 static void kill_clobbered_value PARAMS ((rtx, rtx, void *));
1038 static void kill_set_value PARAMS ((rtx, rtx, void *));
1039 static int kill_autoinc_value PARAMS ((rtx *, void *));
1040 static void copy_value PARAMS ((rtx, rtx, struct value_data *));
1041 static bool mode_change_ok PARAMS ((enum machine_mode, enum machine_mode,
1042                                     unsigned int));
1043 static rtx find_oldest_value_reg PARAMS ((enum reg_class, rtx,
1044                                           struct value_data *));
1045 static bool replace_oldest_value_reg PARAMS ((rtx *, enum reg_class, rtx,
1046                                               struct value_data *));
1047 static bool replace_oldest_value_addr PARAMS ((rtx *, enum reg_class,
1048                                                enum machine_mode, rtx,
1049                                                struct value_data *));
1050 static bool replace_oldest_value_mem PARAMS ((rtx, rtx, struct value_data *));
1051 static bool copyprop_hardreg_forward_1 PARAMS ((basic_block,
1052                                                  struct value_data *));
1053 extern void debug_value_data PARAMS ((struct value_data *));
1054 #ifdef ENABLE_CHECKING
1055 static void validate_value_data PARAMS ((struct value_data *));
1056 #endif
1057
1058 /* Kill register REGNO.  This involves removing it from any value lists,
1059    and resetting the value mode to VOIDmode.  */
1060
1061 static void
1062 kill_value_regno (regno, vd)
1063      unsigned int regno;
1064      struct value_data *vd;
1065 {
1066   unsigned int i, next;
1067
1068   if (vd->e[regno].oldest_regno != regno)
1069     {
1070       for (i = vd->e[regno].oldest_regno;
1071            vd->e[i].next_regno != regno;
1072            i = vd->e[i].next_regno)
1073         continue;
1074       vd->e[i].next_regno = vd->e[regno].next_regno;
1075     }
1076   else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
1077     {
1078       for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
1079         vd->e[i].oldest_regno = next;
1080     }
1081
1082   vd->e[regno].mode = VOIDmode;
1083   vd->e[regno].oldest_regno = regno;
1084   vd->e[regno].next_regno = INVALID_REGNUM;
1085
1086 #ifdef ENABLE_CHECKING
1087   validate_value_data (vd);
1088 #endif
1089 }
1090
1091 /* Kill X.  This is a convenience function for kill_value_regno
1092    so that we mind the mode the register is in.  */
1093
1094 static void
1095 kill_value (x, vd)
1096      rtx x;
1097      struct value_data *vd;
1098 {
1099   /* SUBREGS are supposed to have been eliminated by now.  But some
1100      ports, e.g. i386 sse, use them to smuggle vector type information
1101      through to instruction selection.  Each such SUBREG should simplify,
1102      so if we get a NULL  we've done something wrong elsewhere. */
1103
1104   if (GET_CODE (x) == SUBREG)
1105     x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
1106                          GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
1107   if (REG_P (x))
1108     {
1109       unsigned int regno = REGNO (x);
1110       unsigned int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
1111       unsigned int i, j;
1112
1113       /* Kill the value we're told to kill.  */
1114       for (i = 0; i < n; ++i)
1115         kill_value_regno (regno + i, vd);
1116
1117       /* Kill everything that overlapped what we're told to kill.  */
1118       if (regno < vd->max_value_regs)
1119         j = 0;
1120       else
1121         j = regno - vd->max_value_regs;
1122       for (; j < regno; ++j)
1123         {
1124           if (vd->e[j].mode == VOIDmode)
1125             continue;
1126           n = HARD_REGNO_NREGS (j, vd->e[j].mode);
1127           if (j + n > regno)
1128             for (i = 0; i < n; ++i)
1129               kill_value_regno (j + i, vd);
1130         }
1131     }
1132 }
1133
1134 /* Remember that REGNO is valid in MODE.  */
1135
1136 static void
1137 set_value_regno (regno, mode, vd)
1138      unsigned int regno;
1139      enum machine_mode mode;
1140      struct value_data *vd;
1141 {
1142   unsigned int nregs;
1143
1144   vd->e[regno].mode = mode;
1145
1146   nregs = HARD_REGNO_NREGS (regno, mode);
1147   if (nregs > vd->max_value_regs)
1148     vd->max_value_regs = nregs;
1149 }
1150
1151 /* Initialize VD such that there are no known relationships between regs.  */
1152
1153 static void
1154 init_value_data (vd)
1155      struct value_data *vd;
1156 {
1157   int i;
1158   for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1159     {
1160       vd->e[i].mode = VOIDmode;
1161       vd->e[i].oldest_regno = i;
1162       vd->e[i].next_regno = INVALID_REGNUM;
1163     }
1164   vd->max_value_regs = 0;
1165 }
1166
1167 /* Called through note_stores.  If X is clobbered, kill its value.  */
1168
1169 static void
1170 kill_clobbered_value (x, set, data)
1171      rtx x;
1172      rtx set;
1173      void *data;
1174 {
1175   struct value_data *vd = data;
1176   if (GET_CODE (set) == CLOBBER)
1177     kill_value (x, vd);
1178 }
1179
1180 /* Called through note_stores.  If X is set, not clobbered, kill its 
1181    current value and install it as the root of its own value list.  */
1182
1183 static void
1184 kill_set_value (x, set, data)
1185      rtx x;
1186      rtx set;
1187      void *data;
1188 {
1189   struct value_data *vd = data;
1190   if (GET_CODE (set) != CLOBBER)
1191     {
1192       kill_value (x, vd);
1193       if (REG_P (x))
1194         set_value_regno (REGNO (x), GET_MODE (x), vd);
1195     }
1196 }
1197
1198 /* Called through for_each_rtx.  Kill any register used as the base of an
1199    auto-increment expression, and install that register as the root of its
1200    own value list.  */
1201
1202 static int
1203 kill_autoinc_value (px, data)
1204      rtx *px;
1205      void *data;
1206 {
1207   rtx x = *px;
1208   struct value_data *vd = data;
1209
1210   if (GET_RTX_CLASS (GET_CODE (x)) == 'a')
1211     {
1212       x = XEXP (x, 0);
1213       kill_value (x, vd);
1214       set_value_regno (REGNO (x), Pmode, vd);
1215       return -1;
1216     }
1217
1218   return 0;
1219 }
1220
1221 /* Assert that SRC has been copied to DEST.  Adjust the data structures
1222    to reflect that SRC contains an older copy of the shared value.  */
1223
1224 static void
1225 copy_value (dest, src, vd)
1226      rtx dest;
1227      rtx src;
1228      struct value_data *vd;
1229 {
1230   unsigned int dr = REGNO (dest);
1231   unsigned int sr = REGNO (src);
1232   unsigned int dn, sn;
1233   unsigned int i;
1234
1235   /* ??? At present, it's possible to see noop sets.  It'd be nice if
1236      this were cleaned up beforehand...  */
1237   if (sr == dr)
1238     return;
1239
1240   /* Do not propagate copies to the stack pointer, as that can leave
1241      memory accesses with no scheduling dependancy on the stack update.  */
1242   if (dr == STACK_POINTER_REGNUM)
1243     return;
1244
1245   /* Likewise with the frame pointer, if we're using one.  */
1246   if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
1247     return;
1248
1249   /* If SRC and DEST overlap, don't record anything.  */
1250   dn = HARD_REGNO_NREGS (dr, GET_MODE (dest));
1251   sn = HARD_REGNO_NREGS (sr, GET_MODE (dest));
1252   if ((dr > sr && dr < sr + sn)
1253       || (sr > dr && sr < dr + dn))
1254     return;
1255
1256   /* If SRC had no assigned mode (i.e. we didn't know it was live)
1257      assign it now and assume the value came from an input argument
1258      or somesuch.  */
1259   if (vd->e[sr].mode == VOIDmode)
1260     set_value_regno (sr, vd->e[dr].mode, vd);
1261
1262   /* If SRC had been assigned a mode narrower than the copy, we can't
1263      link DEST into the chain, because not all of the pieces of the
1264      copy came from oldest_regno.  */
1265   else if (sn > (unsigned int) HARD_REGNO_NREGS (sr, vd->e[sr].mode))
1266     return;
1267
1268   /* Link DR at the end of the value chain used by SR.  */
1269
1270   vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
1271
1272   for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
1273     continue;
1274   vd->e[i].next_regno = dr;
1275
1276 #ifdef ENABLE_CHECKING
1277   validate_value_data (vd);
1278 #endif
1279 }
1280
1281 /* Return true if a mode change from ORIG to NEW is allowed for REGNO.  */
1282
1283 static bool
1284 mode_change_ok (orig_mode, new_mode, regno)
1285      enum machine_mode orig_mode, new_mode;
1286      unsigned int regno ATTRIBUTE_UNUSED;
1287 {
1288   if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
1289     return false;
1290
1291 #ifdef CLASS_CANNOT_CHANGE_MODE
1292   if (TEST_HARD_REG_BIT (reg_class_contents[CLASS_CANNOT_CHANGE_MODE], regno)
1293       && CLASS_CANNOT_CHANGE_MODE_P (orig_mode, new_mode))
1294     return false;
1295 #endif
1296
1297   return true;
1298 }
1299
1300 /* Find the oldest copy of the value contained in REGNO that is in
1301    register class CLASS and has mode MODE.  If found, return an rtx
1302    of that oldest register, otherwise return NULL.  */
1303
1304 static rtx
1305 find_oldest_value_reg (class, reg, vd)
1306      enum reg_class class;
1307      rtx reg;
1308      struct value_data *vd;
1309 {
1310   unsigned int regno = REGNO (reg);
1311   enum machine_mode mode = GET_MODE (reg);
1312   unsigned int i;
1313
1314   /* If we are accessing REG in some mode other that what we set it in,
1315      make sure that the replacement is valid.  In particular, consider
1316         (set (reg:DI r11) (...))
1317         (set (reg:SI r9) (reg:SI r11))
1318         (set (reg:SI r10) (...))
1319         (set (...) (reg:DI r9))
1320      Replacing r9 with r11 is invalid.  */
1321   if (mode != vd->e[regno].mode)
1322     {
1323       if (HARD_REGNO_NREGS (regno, mode)
1324           > HARD_REGNO_NREGS (regno, vd->e[regno].mode))
1325         return NULL_RTX;
1326     }
1327
1328   for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
1329     if (TEST_HARD_REG_BIT (reg_class_contents[class], i)
1330         && (vd->e[i].mode == mode
1331             || mode_change_ok (vd->e[i].mode, mode, i)))
1332       {
1333         rtx new = gen_rtx_raw_REG (mode, i);
1334         ORIGINAL_REGNO (new) = ORIGINAL_REGNO (reg);
1335         return new;
1336       }
1337
1338   return NULL_RTX;
1339 }
1340
1341 /* If possible, replace the register at *LOC with the oldest register
1342    in register class CLASS.  Return true if successfully replaced.  */
1343
1344 static bool
1345 replace_oldest_value_reg (loc, class, insn, vd)
1346      rtx *loc;
1347      enum reg_class class;
1348      rtx insn;
1349      struct value_data *vd;
1350 {
1351   rtx new = find_oldest_value_reg (class, *loc, vd);
1352   if (new)
1353     {
1354       if (rtl_dump_file)
1355         fprintf (rtl_dump_file, "insn %u: replaced reg %u with %u\n",
1356                  INSN_UID (insn), REGNO (*loc), REGNO (new));
1357
1358       *loc = new;
1359       return true;
1360     }
1361   return false;
1362 }
1363
1364 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1365    Adapted from find_reloads_address_1.  CLASS is INDEX_REG_CLASS or
1366    BASE_REG_CLASS depending on how the register is being considered.  */
1367
1368 static bool
1369 replace_oldest_value_addr (loc, class, mode, insn, vd)
1370      rtx *loc;
1371      enum reg_class class;
1372      enum machine_mode mode;
1373      rtx insn;
1374      struct value_data *vd;
1375 {
1376   rtx x = *loc;
1377   RTX_CODE code = GET_CODE (x);
1378   const char *fmt;
1379   int i, j;
1380   bool changed = false;
1381
1382   switch (code)
1383     {
1384     case PLUS:
1385       {
1386         rtx orig_op0 = XEXP (x, 0);
1387         rtx orig_op1 = XEXP (x, 1);
1388         RTX_CODE code0 = GET_CODE (orig_op0);
1389         RTX_CODE code1 = GET_CODE (orig_op1);
1390         rtx op0 = orig_op0;
1391         rtx op1 = orig_op1;
1392         rtx *locI = NULL;
1393         rtx *locB = NULL;
1394
1395         if (GET_CODE (op0) == SUBREG)
1396           {
1397             op0 = SUBREG_REG (op0);
1398             code0 = GET_CODE (op0);
1399           }
1400
1401         if (GET_CODE (op1) == SUBREG)
1402           {
1403             op1 = SUBREG_REG (op1);
1404             code1 = GET_CODE (op1);
1405           }
1406
1407         if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1408             || code0 == ZERO_EXTEND || code1 == MEM)
1409           {
1410             locI = &XEXP (x, 0);
1411             locB = &XEXP (x, 1);
1412           }
1413         else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1414                  || code1 == ZERO_EXTEND || code0 == MEM)
1415           {
1416             locI = &XEXP (x, 1);
1417             locB = &XEXP (x, 0);
1418           }
1419         else if (code0 == CONST_INT || code0 == CONST
1420                  || code0 == SYMBOL_REF || code0 == LABEL_REF)
1421           locB = &XEXP (x, 1);
1422         else if (code1 == CONST_INT || code1 == CONST
1423                  || code1 == SYMBOL_REF || code1 == LABEL_REF)
1424           locB = &XEXP (x, 0);
1425         else if (code0 == REG && code1 == REG)
1426           {
1427             int index_op;
1428
1429             if (REG_OK_FOR_INDEX_P (op0)
1430                 && REG_MODE_OK_FOR_BASE_P (op1, mode))
1431               index_op = 0;
1432             else if (REG_OK_FOR_INDEX_P (op1)
1433                      && REG_MODE_OK_FOR_BASE_P (op0, mode))
1434               index_op = 1;
1435             else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
1436               index_op = 0;
1437             else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
1438               index_op = 1;
1439             else if (REG_OK_FOR_INDEX_P (op1))
1440               index_op = 1;
1441             else
1442               index_op = 0;
1443
1444             locI = &XEXP (x, index_op);
1445             locB = &XEXP (x, !index_op);
1446           }
1447         else if (code0 == REG)
1448           {
1449             locI = &XEXP (x, 0);
1450             locB = &XEXP (x, 1);
1451           }
1452         else if (code1 == REG)
1453           {
1454             locI = &XEXP (x, 1);
1455             locB = &XEXP (x, 0);
1456           }
1457
1458         if (locI)
1459           changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
1460                                                 insn, vd);
1461         if (locB)
1462           changed |= replace_oldest_value_addr (locB,
1463                                                 MODE_BASE_REG_CLASS (mode),
1464                                                 mode, insn, vd);
1465         return changed;
1466       }
1467
1468     case POST_INC:
1469     case POST_DEC:
1470     case POST_MODIFY:
1471     case PRE_INC:
1472     case PRE_DEC:
1473     case PRE_MODIFY:
1474       return false;
1475
1476     case MEM:
1477       return replace_oldest_value_mem (x, insn, vd);
1478
1479     case REG:
1480       return replace_oldest_value_reg (loc, class, insn, vd);
1481
1482     default:
1483       break;
1484     }
1485
1486   fmt = GET_RTX_FORMAT (code);
1487   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1488     {
1489       if (fmt[i] == 'e')
1490         changed |= replace_oldest_value_addr (&XEXP (x, i), class, mode,
1491                                               insn, vd);
1492       else if (fmt[i] == 'E')
1493         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1494           changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), class,
1495                                                 mode, insn, vd);
1496     }
1497
1498   return changed;
1499 }
1500
1501 /* Similar to replace_oldest_value_reg, but X contains a memory.  */
1502
1503 static bool
1504 replace_oldest_value_mem (x, insn, vd)
1505      rtx x;
1506      rtx insn;
1507      struct value_data *vd;
1508 {
1509   return replace_oldest_value_addr (&XEXP (x, 0),
1510                                     MODE_BASE_REG_CLASS (GET_MODE (x)),
1511                                     GET_MODE (x), insn, vd);
1512 }
1513
1514 /* Perform the forward copy propagation on basic block BB.  */
1515
1516 static bool
1517 copyprop_hardreg_forward_1 (bb, vd)
1518      basic_block bb;
1519      struct value_data *vd;
1520 {
1521   bool changed = false;
1522   rtx insn;
1523
1524   for (insn = bb->head; ; insn = NEXT_INSN (insn))
1525     {
1526       int n_ops, i, alt, predicated;
1527       bool is_asm;
1528       rtx set;
1529
1530       if (! INSN_P (insn))
1531         {
1532           if (insn == bb->end)
1533             break;
1534           else
1535             continue;
1536         }
1537
1538       set = single_set (insn);
1539       extract_insn (insn);
1540       constrain_operands (1);
1541       preprocess_constraints ();
1542       alt = which_alternative;
1543       n_ops = recog_data.n_operands;
1544       is_asm = asm_noperands (PATTERN (insn)) >= 0;
1545
1546       /* Simplify the code below by rewriting things to reflect
1547          matching constraints.  Also promote OP_OUT to OP_INOUT
1548          in predicated instructions.  */
1549
1550       predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1551       for (i = 0; i < n_ops; ++i)
1552         {
1553           int matches = recog_op_alt[i][alt].matches;
1554           if (matches >= 0)
1555             recog_op_alt[i][alt].class = recog_op_alt[matches][alt].class;
1556           if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1557               || (predicated && recog_data.operand_type[i] == OP_OUT))
1558             recog_data.operand_type[i] = OP_INOUT;
1559         }
1560
1561       /* For each earlyclobber operand, zap the value data.  */
1562       for (i = 0; i < n_ops; i++)
1563         if (recog_op_alt[i][alt].earlyclobber)
1564           kill_value (recog_data.operand[i], vd);
1565
1566       /* Within asms, a clobber cannot overlap inputs or outputs.
1567          I wouldn't think this were true for regular insns, but
1568          scan_rtx treats them like that...  */
1569       note_stores (PATTERN (insn), kill_clobbered_value, vd);
1570
1571       /* Kill all auto-incremented values.  */
1572       /* ??? REG_INC is useless, since stack pushes aren't done that way.  */
1573       for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
1574
1575       /* Kill all early-clobbered operands.  */
1576       for (i = 0; i < n_ops; i++)
1577         if (recog_op_alt[i][alt].earlyclobber)
1578           kill_value (recog_data.operand[i], vd);
1579
1580       /* Special-case plain move instructions, since we may well
1581          be able to do the move from a different register class.  */
1582       if (set && REG_P (SET_SRC (set)))
1583         {
1584           rtx src = SET_SRC (set);
1585           unsigned int regno = REGNO (src);
1586           enum machine_mode mode = GET_MODE (src);
1587           unsigned int i;
1588           rtx new;
1589
1590           /* If we are accessing SRC in some mode other that what we
1591              set it in, make sure that the replacement is valid.  */
1592           if (mode != vd->e[regno].mode)
1593             {
1594               if (HARD_REGNO_NREGS (regno, mode)
1595                   > HARD_REGNO_NREGS (regno, vd->e[regno].mode))
1596                 goto no_move_special_case;
1597             }
1598
1599           /* If the destination is also a register, try to find a source
1600              register in the same class.  */
1601           if (REG_P (SET_DEST (set)))
1602             {
1603               new = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
1604               if (new && validate_change (insn, &SET_SRC (set), new, 0))
1605                 {
1606                   if (rtl_dump_file)
1607                     fprintf (rtl_dump_file,
1608                              "insn %u: replaced reg %u with %u\n",
1609                              INSN_UID (insn), regno, REGNO (new));
1610                   changed = true;
1611                   goto did_replacement;
1612                 }
1613             }
1614
1615           /* Otherwise, try all valid registers and see if its valid.  */
1616           for (i = vd->e[regno].oldest_regno; i != regno;
1617                i = vd->e[i].next_regno)
1618             if (vd->e[i].mode == mode
1619                 || mode_change_ok (vd->e[i].mode, mode, i))
1620               {
1621                 new = gen_rtx_raw_REG (mode, i);
1622                 if (validate_change (insn, &SET_SRC (set), new, 0))
1623                   {
1624                     ORIGINAL_REGNO (new) = ORIGINAL_REGNO (src);
1625                     if (rtl_dump_file)
1626                       fprintf (rtl_dump_file,
1627                                "insn %u: replaced reg %u with %u\n",
1628                                INSN_UID (insn), regno, REGNO (new));
1629                     changed = true;
1630                     goto did_replacement;
1631                   }
1632               }
1633         }
1634       no_move_special_case:
1635
1636       /* For each input operand, replace a hard register with the
1637          eldest live copy that's in an appropriate register class.  */
1638       for (i = 0; i < n_ops; i++)
1639         {
1640           bool replaced = false;
1641
1642           /* Don't scan match_operand here, since we've no reg class
1643              information to pass down.  Any operands that we could
1644              substitute in will be represented elsewhere.  */
1645           if (recog_data.constraints[i][0] == '\0')
1646             continue;
1647
1648           /* Don't replace in asms intentionally referencing hard regs.  */
1649           if (is_asm && GET_CODE (recog_data.operand[i]) == REG
1650               && (REGNO (recog_data.operand[i])
1651                   == ORIGINAL_REGNO (recog_data.operand[i])))
1652             continue;
1653
1654           if (recog_data.operand_type[i] == OP_IN)
1655             {
1656               if (recog_op_alt[i][alt].is_address)
1657                 replaced
1658                   = replace_oldest_value_addr (recog_data.operand_loc[i],
1659                                                recog_op_alt[i][alt].class,
1660                                                VOIDmode, insn, vd);
1661               else if (REG_P (recog_data.operand[i]))
1662                 replaced
1663                   = replace_oldest_value_reg (recog_data.operand_loc[i],
1664                                               recog_op_alt[i][alt].class,
1665                                               insn, vd);
1666               else if (GET_CODE (recog_data.operand[i]) == MEM)
1667                 replaced = replace_oldest_value_mem (recog_data.operand[i],
1668                                                      insn, vd);
1669             }
1670           else if (GET_CODE (recog_data.operand[i]) == MEM)
1671             replaced = replace_oldest_value_mem (recog_data.operand[i],
1672                                                  insn, vd);
1673
1674           /* If we performed any replacement, update match_dups.  */
1675           if (replaced)
1676             {
1677               int j;
1678               rtx new;
1679
1680               changed = true;
1681
1682               new = *recog_data.operand_loc[i];
1683               recog_data.operand[i] = new;
1684               for (j = 0; j < recog_data.n_dups; j++)
1685                 if (recog_data.dup_num[j] == i)
1686                   *recog_data.dup_loc[j] = new;
1687             }
1688         }
1689
1690     did_replacement:
1691       /* Clobber call-clobbered registers.  */
1692       if (GET_CODE (insn) == CALL_INSN)
1693         for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1694           if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1695             kill_value_regno (i, vd);
1696
1697       /* Notice stores.  */
1698       note_stores (PATTERN (insn), kill_set_value, vd);
1699
1700       /* Notice copies.  */
1701       if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1702         copy_value (SET_DEST (set), SET_SRC (set), vd);
1703
1704       if (insn == bb->end)
1705         break;
1706     }
1707
1708   return changed;
1709 }
1710
1711 /* Main entry point for the forward copy propagation optimization.  */
1712
1713 void
1714 copyprop_hardreg_forward ()
1715 {
1716   struct value_data *all_vd;
1717   bool need_refresh;
1718   int b;
1719
1720   need_refresh = false;
1721
1722   all_vd = xmalloc (sizeof (struct value_data) * n_basic_blocks);
1723
1724   for (b = 0; b < n_basic_blocks; b++)
1725     {
1726       basic_block bb = BASIC_BLOCK (b);
1727
1728       /* If a block has a single predecessor, that we've already
1729          processed, begin with the value data that was live at
1730          the end of the predecessor block.  */
1731       /* ??? Ought to use more intelligent queueing of blocks.  */
1732       if (bb->pred
1733           && ! bb->pred->pred_next 
1734           && ! (bb->pred->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1735           && bb->pred->src->index != ENTRY_BLOCK
1736           && bb->pred->src->index < b)
1737         all_vd[b] = all_vd[bb->pred->src->index];
1738       else
1739         init_value_data (all_vd + b);
1740
1741       if (copyprop_hardreg_forward_1 (bb, all_vd + b))
1742         need_refresh = true;
1743     }
1744
1745   if (need_refresh)
1746     {
1747       if (rtl_dump_file)
1748         fputs ("\n\n", rtl_dump_file);
1749
1750       /* ??? Irritatingly, delete_noop_moves does not take a set of blocks
1751          to scan, so we have to do a life update with no initial set of
1752          blocks Just In Case.  */
1753       delete_noop_moves (get_insns ());
1754       update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
1755                         PROP_DEATH_NOTES
1756                         | PROP_SCAN_DEAD_CODE
1757                         | PROP_KILL_DEAD_CODE);
1758     }
1759
1760   free (all_vd);
1761 }
1762
1763 /* Dump the value chain data to stderr.  */
1764
1765 void
1766 debug_value_data (vd)
1767      struct value_data *vd;
1768 {
1769   HARD_REG_SET set;
1770   unsigned int i, j;
1771
1772   CLEAR_HARD_REG_SET (set);
1773
1774   for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1775     if (vd->e[i].oldest_regno == i)
1776       {
1777         if (vd->e[i].mode == VOIDmode)
1778           {
1779             if (vd->e[i].next_regno != INVALID_REGNUM)
1780               fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1781                        i, vd->e[i].next_regno);
1782             continue;
1783           }
1784
1785         SET_HARD_REG_BIT (set, i);
1786         fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1787
1788         for (j = vd->e[i].next_regno;
1789              j != INVALID_REGNUM;
1790              j = vd->e[j].next_regno)
1791           {
1792             if (TEST_HARD_REG_BIT (set, j))
1793               {
1794                 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1795                 return;
1796               }
1797
1798             if (vd->e[j].oldest_regno != i)
1799               {
1800                 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1801                          j, vd->e[j].oldest_regno);
1802                 return;
1803               }
1804             SET_HARD_REG_BIT (set, j);
1805             fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1806           }
1807         fputc ('\n', stderr);
1808       }
1809
1810   for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1811     if (! TEST_HARD_REG_BIT (set, i)
1812         && (vd->e[i].mode != VOIDmode
1813             || vd->e[i].oldest_regno != i
1814             || vd->e[i].next_regno != INVALID_REGNUM))
1815       fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1816                i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1817                vd->e[i].next_regno);
1818 }
1819
1820 #ifdef ENABLE_CHECKING
1821 static void
1822 validate_value_data (vd)
1823      struct value_data *vd;
1824 {
1825   HARD_REG_SET set;
1826   unsigned int i, j;
1827
1828   CLEAR_HARD_REG_SET (set);
1829
1830   for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1831     if (vd->e[i].oldest_regno == i)
1832       {
1833         if (vd->e[i].mode == VOIDmode)
1834           {
1835             if (vd->e[i].next_regno != INVALID_REGNUM)
1836               internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1837                               i, vd->e[i].next_regno);
1838             continue;
1839           }
1840
1841         SET_HARD_REG_BIT (set, i);
1842
1843         for (j = vd->e[i].next_regno;
1844              j != INVALID_REGNUM;
1845              j = vd->e[j].next_regno)
1846           {
1847             if (TEST_HARD_REG_BIT (set, j))
1848               internal_error ("validate_value_data: Loop in regno chain (%u)",
1849                               j);
1850             if (vd->e[j].oldest_regno != i)
1851               internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1852                               j, vd->e[j].oldest_regno);
1853
1854             SET_HARD_REG_BIT (set, j);
1855           }
1856       }
1857
1858   for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1859     if (! TEST_HARD_REG_BIT (set, i)
1860         && (vd->e[i].mode != VOIDmode
1861             || vd->e[i].oldest_regno != i
1862             || vd->e[i].next_regno != INVALID_REGNUM))
1863       internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1864                       i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1865                       vd->e[i].next_regno);
1866 }
1867 #endif