OSDN Git Service

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