OSDN Git Service

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