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