OSDN Git Service

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