OSDN Git Service

Revert "Fix PR debug/49047"
[pf3gnuchains/gcc-fork.git] / gcc / regrename.c
1 /* Register renaming for the GNU compiler.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3    2010 Free Software Foundation, Inc.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 3, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but WITHOUT
13    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14    or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
15    License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING3.  If not see
19    <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl-error.h"
26 #include "tm_p.h"
27 #include "insn-config.h"
28 #include "regs.h"
29 #include "addresses.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
32 #include "reload.h"
33 #include "output.h"
34 #include "function.h"
35 #include "recog.h"
36 #include "flags.h"
37 #include "obstack.h"
38 #include "timevar.h"
39 #include "tree-pass.h"
40 #include "df.h"
41 #include "target.h"
42
43 /* This file implements the RTL register renaming pass of the compiler.  It is
44    a semi-local pass whose goal is to maximize the usage of the register file
45    of the processor by substituting registers for others in the solution given
46    by the register allocator.  The algorithm is as follows:
47
48      1. Local def/use chains are built: within each basic block, chains are
49         opened and closed; if a chain isn't closed at the end of the block,
50         it is dropped.
51
52      2. For each chain, the set of possible renaming registers is computed.
53         This takes into account the renaming of previously processed chains.
54         Optionally, a preferred class is computed for the renaming register.
55
56      3. The best renaming register is computed for the chain in the above set,
57         using a round-robin allocation.  If a preferred class exists, then the
58         round-robin allocation is done within the class first, if possible.
59         The round-robin allocation of renaming registers itself is global.
60
61      4. If a renaming register has been found, it is substituted in the chain.
62
63   Targets can parameterize the pass by specifying a preferred class for the
64   renaming register for a given (super)class of registers to be renamed.  */
65
66 #if HOST_BITS_PER_WIDE_INT <= MAX_RECOG_OPERANDS
67 #error "Use a different bitmap implementation for untracked_operands."
68 #endif
69
70 /* We keep linked lists of DU_HEAD structures, each of which describes
71    a chain of occurrences of a reg.  */
72 struct du_head
73 {
74   /* The next chain.  */
75   struct du_head *next_chain;
76   /* The first and last elements of this chain.  */
77   struct du_chain *first, *last;
78   /* Describes the register being tracked.  */
79   unsigned regno, nregs;
80
81   /* A unique id to be used as an index into the conflicts bitmaps.  */
82   unsigned id;
83   /* A bitmap to record conflicts with other chains.  */
84   bitmap_head conflicts;
85   /* Conflicts with untracked hard registers.  */
86   HARD_REG_SET hard_conflicts;
87
88   /* Nonzero if the chain is finished; zero if it is still open.  */
89   unsigned int terminated:1;
90   /* Nonzero if the chain crosses a call.  */
91   unsigned int need_caller_save_reg:1;
92   /* Nonzero if the register is used in a way that prevents renaming,
93      such as the SET_DEST of a CALL_INSN or an asm operand that used
94      to be a hard register.  */
95   unsigned int cannot_rename:1;
96 };
97
98 /* This struct describes a single occurrence of a register.  */
99 struct du_chain
100 {
101   /* Links to the next occurrence of the register.  */
102   struct du_chain *next_use;
103
104   /* The insn where the register appears.  */
105   rtx insn;
106   /* The location inside the insn.  */
107   rtx *loc;
108   /* The register class required by the insn at this location.  */
109   ENUM_BITFIELD(reg_class) cl : 16;
110 };
111
112 enum scan_actions
113 {
114   terminate_write,
115   terminate_dead,
116   mark_all_read,
117   mark_read,
118   mark_write,
119   /* mark_access is for marking the destination regs in
120      REG_FRAME_RELATED_EXPR notes (as if they were read) so that the
121      note is updated properly.  */
122   mark_access
123 };
124
125 static const char * const scan_actions_name[] =
126 {
127   "terminate_write",
128   "terminate_dead",
129   "mark_all_read",
130   "mark_read",
131   "mark_write",
132   "mark_access"
133 };
134
135 static struct obstack rename_obstack;
136
137 static void do_replace (struct du_head *, int);
138 static void scan_rtx_reg (rtx, rtx *, enum reg_class,
139                           enum scan_actions, enum op_type);
140 static void scan_rtx_address (rtx, rtx *, enum reg_class,
141                               enum scan_actions, enum machine_mode);
142 static void scan_rtx (rtx, rtx *, enum reg_class, enum scan_actions,
143                       enum op_type);
144 static struct du_head *build_def_use (basic_block);
145 static void dump_def_use_chain (struct du_head *);
146
147 typedef struct du_head *du_head_p;
148 DEF_VEC_P (du_head_p);
149 DEF_VEC_ALLOC_P (du_head_p, heap);
150 static VEC(du_head_p, heap) *id_to_chain;
151
152 static void
153 free_chain_data (void)
154 {
155   int i;
156   du_head_p ptr;
157   for (i = 0; VEC_iterate(du_head_p, id_to_chain, i, ptr); i++)
158     bitmap_clear (&ptr->conflicts);
159
160   VEC_free (du_head_p, heap, id_to_chain);
161 }
162
163 /* For a def-use chain HEAD, find which registers overlap its lifetime and
164    set the corresponding bits in *PSET.  */
165
166 static void
167 merge_overlapping_regs (HARD_REG_SET *pset, struct du_head *head)
168 {
169   bitmap_iterator bi;
170   unsigned i;
171   IOR_HARD_REG_SET (*pset, head->hard_conflicts);
172   EXECUTE_IF_SET_IN_BITMAP (&head->conflicts, 0, i, bi)
173     {
174       du_head_p other = VEC_index (du_head_p, id_to_chain, i);
175       unsigned j = other->nregs;
176       while (j-- > 0)
177         SET_HARD_REG_BIT (*pset, other->regno + j);
178     }
179 }
180
181 /* Check if NEW_REG can be the candidate register to rename for
182    REG in THIS_HEAD chain.  THIS_UNAVAILABLE is a set of unavailable hard
183    registers.  */
184
185 static bool
186 check_new_reg_p (int reg ATTRIBUTE_UNUSED, int new_reg,
187                  struct du_head *this_head, HARD_REG_SET this_unavailable)
188 {
189   enum machine_mode mode = GET_MODE (*this_head->first->loc);
190   int nregs = hard_regno_nregs[new_reg][mode];
191   int i;
192   struct du_chain *tmp;
193
194   for (i = nregs - 1; i >= 0; --i)
195     if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
196         || fixed_regs[new_reg + i]
197         || global_regs[new_reg + i]
198         /* Can't use regs which aren't saved by the prologue.  */
199         || (! df_regs_ever_live_p (new_reg + i)
200             && ! call_used_regs[new_reg + i])
201 #ifdef LEAF_REGISTERS
202         /* We can't use a non-leaf register if we're in a
203            leaf function.  */
204         || (current_function_is_leaf
205             && !LEAF_REGISTERS[new_reg + i])
206 #endif
207 #ifdef HARD_REGNO_RENAME_OK
208         || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
209 #endif
210         )
211       return false;
212
213   /* See whether it accepts all modes that occur in
214      definition and uses.  */
215   for (tmp = this_head->first; tmp; tmp = tmp->next_use)
216     if ((! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
217          && ! DEBUG_INSN_P (tmp->insn))
218         || (this_head->need_caller_save_reg
219             && ! (HARD_REGNO_CALL_PART_CLOBBERED
220                   (reg, GET_MODE (*tmp->loc)))
221             && (HARD_REGNO_CALL_PART_CLOBBERED
222                 (new_reg, GET_MODE (*tmp->loc)))))
223       return false;
224
225   return true;
226 }
227
228 /* Perform register renaming on the current function.  */
229
230 static unsigned int
231 regrename_optimize (void)
232 {
233   int tick[FIRST_PSEUDO_REGISTER];
234   int this_tick = 0;
235   basic_block bb;
236   char *first_obj;
237
238   df_set_flags (DF_LR_RUN_DCE);
239   df_note_add_problem ();
240   df_analyze ();
241   df_set_flags (DF_DEFER_INSN_RESCAN);
242
243   memset (tick, 0, sizeof tick);
244
245   gcc_obstack_init (&rename_obstack);
246   first_obj = XOBNEWVAR (&rename_obstack, char, 0);
247
248   FOR_EACH_BB (bb)
249     {
250       struct du_head *all_chains = 0;
251       HARD_REG_SET unavailable;
252 #if 0
253       HARD_REG_SET regs_seen;
254       CLEAR_HARD_REG_SET (regs_seen);
255 #endif
256
257       id_to_chain = VEC_alloc (du_head_p, heap, 0);
258
259       CLEAR_HARD_REG_SET (unavailable);
260
261       if (dump_file)
262         fprintf (dump_file, "\nBasic block %d:\n", bb->index);
263
264       all_chains = build_def_use (bb);
265
266       if (dump_file)
267         dump_def_use_chain (all_chains);
268
269       CLEAR_HARD_REG_SET (unavailable);
270       /* Don't clobber traceback for noreturn functions.  */
271       if (frame_pointer_needed)
272         {
273           add_to_hard_reg_set (&unavailable, Pmode, FRAME_POINTER_REGNUM);
274 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
275           add_to_hard_reg_set (&unavailable, Pmode, HARD_FRAME_POINTER_REGNUM);
276 #endif
277         }
278
279       while (all_chains)
280         {
281           int new_reg, best_new_reg, best_nregs;
282           int n_uses;
283           struct du_head *this_head = all_chains;
284           struct du_chain *tmp;
285           HARD_REG_SET this_unavailable;
286           int reg = this_head->regno;
287           int pass;
288           enum reg_class super_class = NO_REGS;
289           enum reg_class preferred_class;
290           bool has_preferred_class;
291
292           all_chains = this_head->next_chain;
293
294           if (this_head->cannot_rename)
295             continue;
296
297           best_new_reg = reg;
298           best_nregs = this_head->nregs;
299
300 #if 0 /* This just disables optimization opportunities.  */
301           /* Only rename once we've seen the reg more than once.  */
302           if (! TEST_HARD_REG_BIT (regs_seen, reg))
303             {
304               SET_HARD_REG_BIT (regs_seen, reg);
305               continue;
306             }
307 #endif
308
309           if (fixed_regs[reg] || global_regs[reg]
310 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
311               || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
312 #else
313               || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
314 #endif
315               )
316             continue;
317
318           COPY_HARD_REG_SET (this_unavailable, unavailable);
319
320           /* Iterate over elements in the chain in order to:
321              1. Count number of uses, and narrow the set of registers we can
322                 use for renaming.
323              2. Compute the superunion of register classes in this chain.  */
324           n_uses = 0;
325           super_class = NO_REGS;
326           for (tmp = this_head->first; tmp; tmp = tmp->next_use)
327             {
328               if (DEBUG_INSN_P (tmp->insn))
329                 continue;
330               n_uses++;
331               IOR_COMPL_HARD_REG_SET (this_unavailable,
332                                       reg_class_contents[tmp->cl]);
333               super_class
334                 = reg_class_superunion[(int) super_class][(int) tmp->cl];
335             }
336
337           if (n_uses < 2)
338             continue;
339
340           /* Further narrow the set of registers we can use for renaming.
341              If the chain needs a call-saved register, mark the call-used
342              registers as unavailable.  */
343           if (this_head->need_caller_save_reg)
344             IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
345
346           /* And mark registers that overlap its lifetime as unavailable.  */
347           merge_overlapping_regs (&this_unavailable, this_head);
348
349           /* Compute preferred rename class of super union of all the classes
350              in the chain.  */
351           preferred_class
352             = (enum reg_class) targetm.preferred_rename_class (super_class);
353
354           /* If PREFERRED_CLASS is not NO_REGS, we iterate in the first pass
355              over registers that belong to PREFERRED_CLASS and try to find the
356              best register within the class.  If that failed, we iterate in
357              the second pass over registers that don't belong to the class.
358              If PREFERRED_CLASS is NO_REGS, we iterate over all registers in
359              ascending order without any preference.  */
360           has_preferred_class = (preferred_class != NO_REGS);
361           for (pass = (has_preferred_class ? 0 : 1); pass < 2; pass++)
362             {
363               for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
364                 {
365                   if (has_preferred_class
366                       && (pass == 0)
367                          != TEST_HARD_REG_BIT
368                             (reg_class_contents[preferred_class], new_reg))
369                     continue;
370
371                   /* In the first pass, we force the renaming of registers that
372                      don't belong to PREFERRED_CLASS to registers that do, even
373                      though the latters were used not very long ago.  */
374                   if (check_new_reg_p (reg, new_reg, this_head,
375                                        this_unavailable)
376                       && ((pass == 0
377                            && !TEST_HARD_REG_BIT
378                                (reg_class_contents[preferred_class],
379                                 best_new_reg))
380                           || tick[best_new_reg] > tick[new_reg]))
381                     {
382                       enum machine_mode mode
383                         = GET_MODE (*this_head->first->loc);
384                       best_new_reg = new_reg;
385                       best_nregs = hard_regno_nregs[new_reg][mode];
386                     }
387                 }
388               if (pass == 0 && best_new_reg != reg)
389                 break;
390             }
391
392           if (dump_file)
393             {
394               fprintf (dump_file, "Register %s in insn %d",
395                        reg_names[reg], INSN_UID (this_head->first->insn));
396               if (this_head->need_caller_save_reg)
397                 fprintf (dump_file, " crosses a call");
398             }
399
400           if (best_new_reg == reg)
401             {
402               tick[reg] = ++this_tick;
403               if (dump_file)
404                 fprintf (dump_file, "; no available better choice\n");
405               continue;
406             }
407
408           if (dump_file)
409             fprintf (dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
410
411           do_replace (this_head, best_new_reg);
412           this_head->regno = best_new_reg;
413           this_head->nregs = best_nregs;
414           tick[best_new_reg] = ++this_tick;
415           df_set_regs_ever_live (best_new_reg, true);
416         }
417
418       free_chain_data ();
419       obstack_free (&rename_obstack, first_obj);
420     }
421
422   obstack_free (&rename_obstack, NULL);
423
424   if (dump_file)
425     fputc ('\n', dump_file);
426
427   return 0;
428 }
429
430 static void
431 do_replace (struct du_head *head, int reg)
432 {
433   struct du_chain *chain;
434   unsigned int base_regno = head->regno;
435   bool found_note = false;
436
437   gcc_assert (! DEBUG_INSN_P (head->first->insn));
438
439   for (chain = head->first; chain; chain = chain->next_use)
440     {
441       unsigned int regno = ORIGINAL_REGNO (*chain->loc);
442       struct reg_attrs *attr = REG_ATTRS (*chain->loc);
443       int reg_ptr = REG_POINTER (*chain->loc);
444
445       if (DEBUG_INSN_P (chain->insn) && REGNO (*chain->loc) != base_regno)
446         INSN_VAR_LOCATION_LOC (chain->insn) = gen_rtx_UNKNOWN_VAR_LOC ();
447       else
448         {
449           rtx note;
450
451           *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
452           if (regno >= FIRST_PSEUDO_REGISTER)
453             ORIGINAL_REGNO (*chain->loc) = regno;
454           REG_ATTRS (*chain->loc) = attr;
455           REG_POINTER (*chain->loc) = reg_ptr;
456
457           for (note = REG_NOTES (chain->insn); note; note = XEXP (note, 1))
458             {
459               enum reg_note kind = REG_NOTE_KIND (note);
460               if (kind == REG_DEAD || kind == REG_UNUSED)
461                 {
462                   rtx reg = XEXP (note, 0);
463                   gcc_assert (HARD_REGISTER_P (reg));
464
465                   if (REGNO (reg) == base_regno)
466                     {
467                       found_note = true;
468                       if (kind == REG_DEAD
469                           && reg_set_p (*chain->loc, chain->insn))
470                         remove_note (chain->insn, note);
471                       else
472                         XEXP (note, 0) = *chain->loc;
473                       break;
474                     }
475                 }
476             }
477         }
478
479       df_insn_rescan (chain->insn);
480     }
481   if (!found_note)
482     {
483       /* If the chain's first insn is the same as the last, we should have
484          found a REG_UNUSED note.  */
485       gcc_assert (head->first->insn != head->last->insn);
486       if (!reg_set_p (*head->last->loc, head->last->insn))
487         add_reg_note (head->last->insn, REG_DEAD, *head->last->loc);
488     }
489 }
490
491
492 /* Walk all chains starting with CHAINS and record that they conflict with
493    another chain whose id is ID.  */
494
495 static void
496 mark_conflict (struct du_head *chains, unsigned id)
497 {
498   while (chains)
499     {
500       bitmap_set_bit (&chains->conflicts, id);
501       chains = chains->next_chain;
502     }
503 }
504
505 /* True if we found a register with a size mismatch, which means that we
506    can't track its lifetime accurately.  If so, we abort the current block
507    without renaming.  */
508 static bool fail_current_block;
509
510 /* The id to be given to the next opened chain.  */
511 static unsigned current_id;
512
513 /* List of currently open chains, and closed chains that can be renamed.  */
514 static struct du_head *open_chains;
515 static struct du_head *closed_chains;
516
517 /* Bitmap of open chains.  The bits set always match the list found in
518    open_chains.  */
519 static bitmap_head open_chains_set;
520
521 /* Record the registers being tracked in open_chains.  */
522 static HARD_REG_SET live_in_chains;
523
524 /* Record the registers that are live but not tracked.  The intersection
525    between this and live_in_chains is empty.  */
526 static HARD_REG_SET live_hard_regs;
527
528 /* Return true if OP is a reg for which all bits are set in PSET, false
529    if all bits are clear.
530    In other cases, set fail_current_block and return false.  */
531
532 static bool
533 verify_reg_in_set (rtx op, HARD_REG_SET *pset)
534 {
535   unsigned regno, nregs;
536   bool all_live, all_dead;
537   if (!REG_P (op))
538     return false;
539
540   regno = REGNO (op);
541   nregs = hard_regno_nregs[regno][GET_MODE (op)];
542   all_live = all_dead = true;
543   while (nregs-- > 0)
544     if (TEST_HARD_REG_BIT (*pset, regno + nregs))
545       all_dead = false;
546     else
547       all_live = false;
548   if (!all_dead && !all_live)
549     {
550       fail_current_block = true;
551       return false;
552     }
553   return all_live;
554 }
555
556 /* Return true if OP is a reg that is being tracked already in some form.
557    May set fail_current_block if it sees an unhandled case of overlap.  */
558
559 static bool
560 verify_reg_tracked (rtx op)
561 {
562   return (verify_reg_in_set (op, &live_hard_regs)
563           || verify_reg_in_set (op, &live_in_chains));
564 }
565
566 /* Called through note_stores.  DATA points to a rtx_code, either SET or
567    CLOBBER, which tells us which kind of rtx to look at.  If we have a
568    match, record the set register in live_hard_regs and in the hard_conflicts
569    bitmap of open chains.  */
570
571 static void
572 note_sets_clobbers (rtx x, const_rtx set, void *data)
573 {
574   enum rtx_code code = *(enum rtx_code *)data;
575   struct du_head *chain;
576
577   if (GET_CODE (x) == SUBREG)
578     x = SUBREG_REG (x);
579   if (!REG_P (x) || GET_CODE (set) != code)
580     return;
581   /* There must not be pseudos at this point.  */
582   gcc_assert (HARD_REGISTER_P (x));
583   add_to_hard_reg_set (&live_hard_regs, GET_MODE (x), REGNO (x));
584   for (chain = open_chains; chain; chain = chain->next_chain)
585     add_to_hard_reg_set (&chain->hard_conflicts, GET_MODE (x), REGNO (x));
586 }
587
588 /* Create a new chain for THIS_NREGS registers starting at THIS_REGNO,
589    and record its occurrence in *LOC, which is being written to in INSN.
590    This access requires a register of class CL.  */
591
592 static void
593 create_new_chain (unsigned this_regno, unsigned this_nregs, rtx *loc,
594                   rtx insn, enum reg_class cl)
595 {
596   struct du_head *head = XOBNEW (&rename_obstack, struct du_head);
597   struct du_chain *this_du;
598   int nregs;
599
600   head->next_chain = open_chains;
601   open_chains = head;
602   head->regno = this_regno;
603   head->nregs = this_nregs;
604   head->need_caller_save_reg = 0;
605   head->cannot_rename = 0;
606   head->terminated = 0;
607
608   VEC_safe_push (du_head_p, heap, id_to_chain, head);
609   head->id = current_id++;
610
611   bitmap_initialize (&head->conflicts, &bitmap_default_obstack);
612   bitmap_copy (&head->conflicts, &open_chains_set);
613   mark_conflict (open_chains, head->id);
614
615   /* Since we're tracking this as a chain now, remove it from the
616      list of conflicting live hard registers and track it in
617      live_in_chains instead.  */
618   nregs = head->nregs;
619   while (nregs-- > 0)
620     {
621       SET_HARD_REG_BIT (live_in_chains, head->regno + nregs);
622       CLEAR_HARD_REG_BIT (live_hard_regs, head->regno + nregs);
623     }
624
625   COPY_HARD_REG_SET (head->hard_conflicts, live_hard_regs);
626   bitmap_set_bit (&open_chains_set, head->id);
627
628   open_chains = head;
629
630   if (dump_file)
631     {
632       fprintf (dump_file, "Creating chain %s (%d)",
633                reg_names[head->regno], head->id);
634       if (insn != NULL_RTX)
635         fprintf (dump_file, " at insn %d", INSN_UID (insn));
636       fprintf (dump_file, "\n");
637     }
638
639   if (insn == NULL_RTX)
640     {
641       head->first = head->last = NULL;
642       return;
643     }
644
645   this_du = XOBNEW (&rename_obstack, struct du_chain);
646   head->first = head->last = this_du;
647
648   this_du->next_use = 0;
649   this_du->loc = loc;
650   this_du->insn = insn;
651   this_du->cl = cl;
652 }
653
654 static void
655 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class cl, enum scan_actions action,
656               enum op_type type)
657 {
658   struct du_head **p;
659   rtx x = *loc;
660   enum machine_mode mode = GET_MODE (x);
661   unsigned this_regno = REGNO (x);
662   unsigned this_nregs = hard_regno_nregs[this_regno][mode];
663
664   if (action == mark_write)
665     {
666       if (type == OP_OUT)
667         create_new_chain (this_regno, this_nregs, loc, insn, cl);
668       return;
669     }
670
671   if ((type == OP_OUT) != (action == terminate_write || action == mark_access))
672     return;
673
674   for (p = &open_chains; *p;)
675     {
676       struct du_head *head = *p;
677       struct du_head *next = head->next_chain;
678       int exact_match = (head->regno == this_regno
679                          && head->nregs == this_nregs);
680       int superset = (this_regno <= head->regno
681                       && this_regno + this_nregs >= head->regno + head->nregs);
682       int subset = (this_regno >= head->regno
683                       && this_regno + this_nregs <= head->regno + head->nregs);
684
685       if (head->terminated
686           || head->regno + head->nregs <= this_regno
687           || this_regno + this_nregs <= head->regno)
688         {
689           p = &head->next_chain;
690           continue;
691         }
692
693       if (action == mark_read || action == mark_access)
694         {
695           /* ??? Class NO_REGS can happen if the md file makes use of
696              EXTRA_CONSTRAINTS to match registers.  Which is arguably
697              wrong, but there we are.  */
698
699           if (cl == NO_REGS || (!exact_match && !DEBUG_INSN_P (insn)))
700             {
701               if (dump_file)
702                 fprintf (dump_file,
703                          "Cannot rename chain %s (%d) at insn %d (%s)\n",
704                          reg_names[head->regno], head->id, INSN_UID (insn),
705                          scan_actions_name[(int) action]);
706               head->cannot_rename = 1;
707               if (superset)
708                 {
709                   unsigned nregs = this_nregs;
710                   head->regno = this_regno;
711                   head->nregs = this_nregs;
712                   while (nregs-- > 0)
713                     SET_HARD_REG_BIT (live_in_chains, head->regno + nregs);
714                   if (dump_file)
715                     fprintf (dump_file,
716                              "Widening register in chain %s (%d) at insn %d\n",
717                              reg_names[head->regno], head->id, INSN_UID (insn));
718                 }
719               else if (!subset)
720                 {
721                   fail_current_block = true;
722                   if (dump_file)
723                     fprintf (dump_file,
724                              "Failing basic block due to unhandled overlap\n");
725                 }
726             }
727           else
728             {
729               struct du_chain *this_du;
730               this_du = XOBNEW (&rename_obstack, struct du_chain);
731               this_du->next_use = 0;
732               this_du->loc = loc;
733               this_du->insn = insn;
734               this_du->cl = cl;
735               if (head->first == NULL)
736                 head->first = this_du;
737               else
738                 head->last->next_use = this_du;
739               head->last = this_du;
740             }
741           /* Avoid adding the same location in a DEBUG_INSN multiple times,
742              which could happen with non-exact overlap.  */
743           if (DEBUG_INSN_P (insn))
744             return;
745           /* Otherwise, find any other chains that do not match exactly;
746              ensure they all get marked unrenamable.  */
747           p = &head->next_chain;
748           continue;
749         }
750
751       /* Whether the terminated chain can be used for renaming
752          depends on the action and this being an exact match.
753          In either case, we remove this element from open_chains.  */
754
755       if ((action == terminate_dead || action == terminate_write)
756           && superset)
757         {
758           unsigned nregs;
759
760           head->terminated = 1;
761           head->next_chain = closed_chains;
762           closed_chains = head;
763           bitmap_clear_bit (&open_chains_set, head->id);
764
765           nregs = head->nregs;
766           while (nregs-- > 0)
767             CLEAR_HARD_REG_BIT (live_in_chains, head->regno + nregs);
768
769           *p = next;
770           if (dump_file)
771             fprintf (dump_file,
772                      "Closing chain %s (%d) at insn %d (%s)\n",
773                      reg_names[head->regno], head->id, INSN_UID (insn),
774                      scan_actions_name[(int) action]);
775         }
776       else if (action == terminate_dead || action == terminate_write)
777         {
778           /* In this case, tracking liveness gets too hard.  Fail the
779              entire basic block.  */
780           if (dump_file)
781             fprintf (dump_file,
782                      "Failing basic block due to unhandled overlap\n");
783           fail_current_block = true;
784           return;
785         }
786       else
787         {
788           head->cannot_rename = 1;
789           if (dump_file)
790             fprintf (dump_file,
791                      "Cannot rename chain %s (%d) at insn %d (%s)\n",
792                      reg_names[head->regno], head->id, INSN_UID (insn),
793                      scan_actions_name[(int) action]);
794           p = &head->next_chain;
795         }
796     }
797 }
798
799 /* Adapted from find_reloads_address_1.  CL is INDEX_REG_CLASS or
800    BASE_REG_CLASS depending on how the register is being considered.  */
801
802 static void
803 scan_rtx_address (rtx insn, rtx *loc, enum reg_class cl,
804                   enum scan_actions action, enum machine_mode mode)
805 {
806   rtx x = *loc;
807   RTX_CODE code = GET_CODE (x);
808   const char *fmt;
809   int i, j;
810
811   if (action == mark_write || action == mark_access)
812     return;
813
814   switch (code)
815     {
816     case PLUS:
817       {
818         rtx orig_op0 = XEXP (x, 0);
819         rtx orig_op1 = XEXP (x, 1);
820         RTX_CODE code0 = GET_CODE (orig_op0);
821         RTX_CODE code1 = GET_CODE (orig_op1);
822         rtx op0 = orig_op0;
823         rtx op1 = orig_op1;
824         rtx *locI = NULL;
825         rtx *locB = NULL;
826         enum rtx_code index_code = SCRATCH;
827
828         if (GET_CODE (op0) == SUBREG)
829           {
830             op0 = SUBREG_REG (op0);
831             code0 = GET_CODE (op0);
832           }
833
834         if (GET_CODE (op1) == SUBREG)
835           {
836             op1 = SUBREG_REG (op1);
837             code1 = GET_CODE (op1);
838           }
839
840         if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
841             || code0 == ZERO_EXTEND || code1 == MEM)
842           {
843             locI = &XEXP (x, 0);
844             locB = &XEXP (x, 1);
845             index_code = GET_CODE (*locI);
846           }
847         else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
848                  || code1 == ZERO_EXTEND || code0 == MEM)
849           {
850             locI = &XEXP (x, 1);
851             locB = &XEXP (x, 0);
852             index_code = GET_CODE (*locI);
853           }
854         else if (code0 == CONST_INT || code0 == CONST
855                  || code0 == SYMBOL_REF || code0 == LABEL_REF)
856           {
857             locB = &XEXP (x, 1);
858             index_code = GET_CODE (XEXP (x, 0));
859           }
860         else if (code1 == CONST_INT || code1 == CONST
861                  || code1 == SYMBOL_REF || code1 == LABEL_REF)
862           {
863             locB = &XEXP (x, 0);
864             index_code = GET_CODE (XEXP (x, 1));
865           }
866         else if (code0 == REG && code1 == REG)
867           {
868             int index_op;
869             unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
870
871             if (REGNO_OK_FOR_INDEX_P (regno1)
872                 && regno_ok_for_base_p (regno0, mode, PLUS, REG))
873               index_op = 1;
874             else if (REGNO_OK_FOR_INDEX_P (regno0)
875                      && regno_ok_for_base_p (regno1, mode, PLUS, REG))
876               index_op = 0;
877             else if (regno_ok_for_base_p (regno0, mode, PLUS, REG)
878                      || REGNO_OK_FOR_INDEX_P (regno1))
879               index_op = 1;
880             else if (regno_ok_for_base_p (regno1, mode, PLUS, REG))
881               index_op = 0;
882             else
883               index_op = 1;
884
885             locI = &XEXP (x, index_op);
886             locB = &XEXP (x, !index_op);
887             index_code = GET_CODE (*locI);
888           }
889         else if (code0 == REG)
890           {
891             locI = &XEXP (x, 0);
892             locB = &XEXP (x, 1);
893             index_code = GET_CODE (*locI);
894           }
895         else if (code1 == REG)
896           {
897             locI = &XEXP (x, 1);
898             locB = &XEXP (x, 0);
899             index_code = GET_CODE (*locI);
900           }
901
902         if (locI)
903           scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
904         if (locB)
905           scan_rtx_address (insn, locB, base_reg_class (mode, PLUS, index_code),
906                             action, mode);
907
908         return;
909       }
910
911     case POST_INC:
912     case POST_DEC:
913     case POST_MODIFY:
914     case PRE_INC:
915     case PRE_DEC:
916     case PRE_MODIFY:
917 #ifndef AUTO_INC_DEC
918       /* If the target doesn't claim to handle autoinc, this must be
919          something special, like a stack push.  Kill this chain.  */
920       action = mark_all_read;
921 #endif
922       break;
923
924     case MEM:
925       scan_rtx_address (insn, &XEXP (x, 0),
926                         base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
927                         GET_MODE (x));
928       return;
929
930     case REG:
931       scan_rtx_reg (insn, loc, cl, action, OP_IN);
932       return;
933
934     default:
935       break;
936     }
937
938   fmt = GET_RTX_FORMAT (code);
939   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
940     {
941       if (fmt[i] == 'e')
942         scan_rtx_address (insn, &XEXP (x, i), cl, action, mode);
943       else if (fmt[i] == 'E')
944         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
945           scan_rtx_address (insn, &XVECEXP (x, i, j), cl, action, mode);
946     }
947 }
948
949 static void
950 scan_rtx (rtx insn, rtx *loc, enum reg_class cl, enum scan_actions action,
951           enum op_type type)
952 {
953   const char *fmt;
954   rtx x = *loc;
955   enum rtx_code code = GET_CODE (x);
956   int i, j;
957
958   code = GET_CODE (x);
959   switch (code)
960     {
961     case CONST:
962     case CONST_INT:
963     case CONST_DOUBLE:
964     case CONST_FIXED:
965     case CONST_VECTOR:
966     case SYMBOL_REF:
967     case LABEL_REF:
968     case CC0:
969     case PC:
970       return;
971
972     case REG:
973       scan_rtx_reg (insn, loc, cl, action, type);
974       return;
975
976     case MEM:
977       scan_rtx_address (insn, &XEXP (x, 0),
978                         base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
979                         GET_MODE (x));
980       return;
981
982     case SET:
983       scan_rtx (insn, &SET_SRC (x), cl, action, OP_IN);
984       scan_rtx (insn, &SET_DEST (x), cl, action,
985                 (GET_CODE (PATTERN (insn)) == COND_EXEC
986                  && verify_reg_tracked (SET_DEST (x))) ? OP_INOUT : OP_OUT);
987       return;
988
989     case STRICT_LOW_PART:
990       scan_rtx (insn, &XEXP (x, 0), cl, action,
991                 verify_reg_tracked (XEXP (x, 0)) ? OP_INOUT : OP_OUT);
992       return;
993
994     case ZERO_EXTRACT:
995     case SIGN_EXTRACT:
996       scan_rtx (insn, &XEXP (x, 0), cl, action,
997                 (type == OP_IN ? OP_IN :
998                  verify_reg_tracked (XEXP (x, 0)) ? OP_INOUT : OP_OUT));
999       scan_rtx (insn, &XEXP (x, 1), cl, action, OP_IN);
1000       scan_rtx (insn, &XEXP (x, 2), cl, action, OP_IN);
1001       return;
1002
1003     case POST_INC:
1004     case PRE_INC:
1005     case POST_DEC:
1006     case PRE_DEC:
1007     case POST_MODIFY:
1008     case PRE_MODIFY:
1009       /* Should only happen inside MEM.  */
1010       gcc_unreachable ();
1011
1012     case CLOBBER:
1013       scan_rtx (insn, &SET_DEST (x), cl, action,
1014                 (GET_CODE (PATTERN (insn)) == COND_EXEC
1015                  && verify_reg_tracked (SET_DEST (x))) ? OP_INOUT : OP_OUT);
1016       return;
1017
1018     case EXPR_LIST:
1019       scan_rtx (insn, &XEXP (x, 0), cl, action, type);
1020       if (XEXP (x, 1))
1021         scan_rtx (insn, &XEXP (x, 1), cl, action, type);
1022       return;
1023
1024     default:
1025       break;
1026     }
1027
1028   fmt = GET_RTX_FORMAT (code);
1029   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1030     {
1031       if (fmt[i] == 'e')
1032         scan_rtx (insn, &XEXP (x, i), cl, action, type);
1033       else if (fmt[i] == 'E')
1034         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1035           scan_rtx (insn, &XVECEXP (x, i, j), cl, action, type);
1036     }
1037 }
1038
1039 /* Hide operands of the current insn (of which there are N_OPS) by
1040    substituting cc0 for them.
1041    Previous values are stored in the OLD_OPERANDS and OLD_DUPS.
1042    For every bit set in DO_NOT_HIDE, we leave the operand alone.
1043    If INOUT_AND_EC_ONLY is set, we only do this for OP_INOUT type operands
1044    and earlyclobbers.  */
1045
1046 static void
1047 hide_operands (int n_ops, rtx *old_operands, rtx *old_dups,
1048                unsigned HOST_WIDE_INT do_not_hide, bool inout_and_ec_only)
1049 {
1050   int i;
1051   int alt = which_alternative;
1052   for (i = 0; i < n_ops; i++)
1053     {
1054       old_operands[i] = recog_data.operand[i];
1055       /* Don't squash match_operator or match_parallel here, since
1056          we don't know that all of the contained registers are
1057          reachable by proper operands.  */
1058       if (recog_data.constraints[i][0] == '\0')
1059         continue;
1060       if (do_not_hide & (1 << i))
1061         continue;
1062       if (!inout_and_ec_only || recog_data.operand_type[i] == OP_INOUT
1063           || recog_op_alt[i][alt].earlyclobber)
1064         *recog_data.operand_loc[i] = cc0_rtx;
1065     }
1066   for (i = 0; i < recog_data.n_dups; i++)
1067     {
1068       int opn = recog_data.dup_num[i];
1069       old_dups[i] = *recog_data.dup_loc[i];
1070       if (do_not_hide & (1 << opn))
1071         continue;
1072       if (!inout_and_ec_only || recog_data.operand_type[opn] == OP_INOUT
1073           || recog_op_alt[opn][alt].earlyclobber)
1074         *recog_data.dup_loc[i] = cc0_rtx;
1075     }
1076 }
1077
1078 /* Undo the substitution performed by hide_operands.  INSN is the insn we
1079    are processing; the arguments are the same as in hide_operands.  */
1080
1081 static void
1082 restore_operands (rtx insn, int n_ops, rtx *old_operands, rtx *old_dups)
1083 {
1084   int i;
1085   for (i = 0; i < recog_data.n_dups; i++)
1086     *recog_data.dup_loc[i] = old_dups[i];
1087   for (i = 0; i < n_ops; i++)
1088     *recog_data.operand_loc[i] = old_operands[i];
1089   if (recog_data.n_dups)
1090     df_insn_rescan (insn);
1091 }
1092
1093 /* For each output operand of INSN, call scan_rtx to create a new
1094    open chain.  Do this only for normal or earlyclobber outputs,
1095    depending on EARLYCLOBBER.  */
1096
1097 static void
1098 record_out_operands (rtx insn, bool earlyclobber)
1099 {
1100   int n_ops = recog_data.n_operands;
1101   int alt = which_alternative;
1102
1103   int i;
1104
1105   for (i = 0; i < n_ops + recog_data.n_dups; i++)
1106     {
1107       int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
1108       rtx *loc = (i < n_ops
1109                   ? recog_data.operand_loc[opn]
1110                   : recog_data.dup_loc[i - n_ops]);
1111       rtx op = *loc;
1112       enum reg_class cl = recog_op_alt[opn][alt].cl;
1113
1114       struct du_head *prev_open;
1115
1116       if (recog_data.operand_type[opn] != OP_OUT
1117           || recog_op_alt[opn][alt].earlyclobber != earlyclobber)
1118         continue;
1119
1120       prev_open = open_chains;
1121       scan_rtx (insn, loc, cl, mark_write, OP_OUT);
1122
1123       /* ??? Many targets have output constraints on the SET_DEST
1124          of a call insn, which is stupid, since these are certainly
1125          ABI defined hard registers.  For these, and for asm operands
1126          that originally referenced hard registers, we must record that
1127          the chain cannot be renamed.  */
1128       if (CALL_P (insn)
1129           || (asm_noperands (PATTERN (insn)) > 0
1130               && REG_P (op)
1131               && REGNO (op) == ORIGINAL_REGNO (op)))
1132         {
1133           if (prev_open != open_chains)
1134             open_chains->cannot_rename = 1;
1135         }
1136     }
1137 }
1138
1139 /* Build def/use chain.  */
1140
1141 static struct du_head *
1142 build_def_use (basic_block bb)
1143 {
1144   rtx insn;
1145   df_ref *def_rec;
1146   unsigned HOST_WIDE_INT untracked_operands;
1147
1148   open_chains = closed_chains = NULL;
1149
1150   fail_current_block = false;
1151
1152   current_id = 0;
1153   bitmap_initialize (&open_chains_set, &bitmap_default_obstack);
1154   CLEAR_HARD_REG_SET (live_in_chains);
1155   REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_in (bb));
1156   for (def_rec = df_get_artificial_defs (bb->index); *def_rec; def_rec++)
1157     {
1158       df_ref def = *def_rec;
1159       if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
1160         SET_HARD_REG_BIT (live_hard_regs, DF_REF_REGNO (def));
1161     }
1162
1163   for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1164     {
1165       if (NONDEBUG_INSN_P (insn))
1166         {
1167           int n_ops;
1168           rtx note;
1169           rtx old_operands[MAX_RECOG_OPERANDS];
1170           rtx old_dups[MAX_DUP_OPERANDS];
1171           int i;
1172           int alt;
1173           int predicated;
1174           enum rtx_code set_code = SET;
1175           enum rtx_code clobber_code = CLOBBER;
1176
1177           /* Process the insn, determining its effect on the def-use
1178              chains and live hard registers.  We perform the following
1179              steps with the register references in the insn, simulating
1180              its effect:
1181              (1) Deal with earlyclobber operands and CLOBBERs of non-operands
1182                  by creating chains and marking hard regs live.
1183              (2) Any read outside an operand causes any chain it overlaps
1184                  with to be marked unrenamable.
1185              (3) Any read inside an operand is added if there's already
1186                  an open chain for it.
1187              (4) For any REG_DEAD note we find, close open chains that
1188                  overlap it.
1189              (5) For any non-earlyclobber write we find, close open chains
1190                  that overlap it.
1191              (6) For any non-earlyclobber write we find in an operand, make
1192                  a new chain or mark the hard register as live.
1193              (7) For any REG_UNUSED, close any chains we just opened.
1194
1195              We cannot deal with situations where we track a reg in one mode
1196              and see a reference in another mode; these will cause the chain
1197              to be marked unrenamable or even cause us to abort the entire
1198              basic block.  */
1199
1200           extract_insn (insn);
1201           if (! constrain_operands (1))
1202             fatal_insn_not_found (insn);
1203           preprocess_constraints ();
1204           alt = which_alternative;
1205           n_ops = recog_data.n_operands;
1206           untracked_operands = 0;
1207
1208           /* Simplify the code below by rewriting things to reflect
1209              matching constraints.  Also promote OP_OUT to OP_INOUT in
1210              predicated instructions, but only for register operands
1211              that are already tracked, so that we can create a chain
1212              when the first SET makes a register live.  */
1213
1214           predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1215           for (i = 0; i < n_ops; ++i)
1216             {
1217               rtx op = recog_data.operand[i];
1218               int matches = recog_op_alt[i][alt].matches;
1219               if (matches >= 0)
1220                 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
1221               if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1222                   || (predicated && recog_data.operand_type[i] == OP_OUT))
1223                 {
1224                   recog_data.operand_type[i] = OP_INOUT;
1225                   /* A special case to deal with instruction patterns that
1226                      have matching operands with different modes.  If we're
1227                      not already tracking such a reg, we won't start here,
1228                      and we must instead make sure to make the operand visible
1229                      to the machinery that tracks hard registers.  */
1230                   if (matches >= 0
1231                       && (GET_MODE_SIZE (recog_data.operand_mode[i])
1232                           != GET_MODE_SIZE (recog_data.operand_mode[matches]))
1233                       && !verify_reg_in_set (op, &live_in_chains))
1234                     {
1235                       untracked_operands |= 1 << i;
1236                       untracked_operands |= 1 << matches;
1237                     }
1238                 }
1239               /* If there's an in-out operand with a register that is not
1240                  being tracked at all yet, open a chain.  */
1241               if (recog_data.operand_type[i] == OP_INOUT
1242                   && !(untracked_operands & (1 << i))
1243                   && REG_P (op)
1244                   && !verify_reg_tracked (op))
1245                 {
1246                   enum machine_mode mode = GET_MODE (op);
1247                   unsigned this_regno = REGNO (op);
1248                   unsigned this_nregs = hard_regno_nregs[this_regno][mode];
1249                   create_new_chain (this_regno, this_nregs, NULL, NULL_RTX,
1250                                     NO_REGS);
1251                 }
1252             }
1253
1254           if (fail_current_block)
1255             break;
1256
1257           /* Step 1a: Mark hard registers that are clobbered in this insn,
1258              outside an operand, as live.  */
1259           hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1260                          false);
1261           note_stores (PATTERN (insn), note_sets_clobbers, &clobber_code);
1262           restore_operands (insn, n_ops, old_operands, old_dups);
1263
1264           /* Step 1b: Begin new chains for earlyclobbered writes inside
1265              operands.  */
1266           record_out_operands (insn, true);
1267
1268           /* Step 2: Mark chains for which we have reads outside operands
1269              as unrenamable.
1270              We do this by munging all operands into CC0, and closing
1271              everything remaining.  */
1272
1273           hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1274                          false);
1275           scan_rtx (insn, &PATTERN (insn), NO_REGS, mark_all_read, OP_IN);
1276           restore_operands (insn, n_ops, old_operands, old_dups);
1277
1278           /* Step 2B: Can't rename function call argument registers.  */
1279           if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
1280             scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
1281                       NO_REGS, mark_all_read, OP_IN);
1282
1283           /* Step 2C: Can't rename asm operands that were originally
1284              hard registers.  */
1285           if (asm_noperands (PATTERN (insn)) > 0)
1286             for (i = 0; i < n_ops; i++)
1287               {
1288                 rtx *loc = recog_data.operand_loc[i];
1289                 rtx op = *loc;
1290
1291                 if (REG_P (op)
1292                     && REGNO (op) == ORIGINAL_REGNO (op)
1293                     && (recog_data.operand_type[i] == OP_IN
1294                         || recog_data.operand_type[i] == OP_INOUT))
1295                   scan_rtx (insn, loc, NO_REGS, mark_all_read, OP_IN);
1296               }
1297
1298           /* Step 3: Append to chains for reads inside operands.  */
1299           for (i = 0; i < n_ops + recog_data.n_dups; i++)
1300             {
1301               int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
1302               rtx *loc = (i < n_ops
1303                           ? recog_data.operand_loc[opn]
1304                           : recog_data.dup_loc[i - n_ops]);
1305               enum reg_class cl = recog_op_alt[opn][alt].cl;
1306               enum op_type type = recog_data.operand_type[opn];
1307
1308               /* Don't scan match_operand here, since we've no reg class
1309                  information to pass down.  Any operands that we could
1310                  substitute in will be represented elsewhere.  */
1311               if (recog_data.constraints[opn][0] == '\0'
1312                   || untracked_operands & (1 << opn))
1313                 continue;
1314
1315               if (recog_op_alt[opn][alt].is_address)
1316                 scan_rtx_address (insn, loc, cl, mark_read, VOIDmode);
1317               else
1318                 scan_rtx (insn, loc, cl, mark_read, type);
1319             }
1320
1321           /* Step 3B: Record updates for regs in REG_INC notes, and
1322              source regs in REG_FRAME_RELATED_EXPR notes.  */
1323           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1324             if (REG_NOTE_KIND (note) == REG_INC
1325                 || REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
1326               scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
1327                         OP_INOUT);
1328
1329           /* Step 4: Close chains for registers that die here.  */
1330           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1331             if (REG_NOTE_KIND (note) == REG_DEAD)
1332               {
1333                 remove_from_hard_reg_set (&live_hard_regs,
1334                                           GET_MODE (XEXP (note, 0)),
1335                                           REGNO (XEXP (note, 0)));
1336                 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
1337                           OP_IN);
1338               }
1339
1340           /* Step 4B: If this is a call, any chain live at this point
1341              requires a caller-saved reg.  */
1342           if (CALL_P (insn))
1343             {
1344               struct du_head *p;
1345               for (p = open_chains; p; p = p->next_chain)
1346                 p->need_caller_save_reg = 1;
1347             }
1348
1349           /* Step 5: Close open chains that overlap writes.  Similar to
1350              step 2, we hide in-out operands, since we do not want to
1351              close these chains.  We also hide earlyclobber operands,
1352              since we've opened chains for them in step 1, and earlier
1353              chains they would overlap with must have been closed at
1354              the previous insn at the latest, as such operands cannot
1355              possibly overlap with any input operands.  */
1356
1357           hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1358                          true);
1359           scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN);
1360           restore_operands (insn, n_ops, old_operands, old_dups);
1361
1362           /* Step 6a: Mark hard registers that are set in this insn,
1363              outside an operand, as live.  */
1364           hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1365                          false);
1366           note_stores (PATTERN (insn), note_sets_clobbers, &set_code);
1367           restore_operands (insn, n_ops, old_operands, old_dups);
1368
1369           /* Step 6b: Begin new chains for writes inside operands.  */
1370           record_out_operands (insn, false);
1371
1372           /* Step 6c: Record destination regs in REG_FRAME_RELATED_EXPR
1373              notes for update.  */
1374           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1375             if (REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
1376               scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_access,
1377                         OP_INOUT);
1378
1379           /* Step 7: Close chains for registers that were never
1380              really used here.  */
1381           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1382             if (REG_NOTE_KIND (note) == REG_UNUSED)
1383               {
1384                 remove_from_hard_reg_set (&live_hard_regs,
1385                                           GET_MODE (XEXP (note, 0)),
1386                                           REGNO (XEXP (note, 0)));
1387                 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
1388                           OP_IN);
1389               }
1390         }
1391       else if (DEBUG_INSN_P (insn)
1392                && !VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn)))
1393         {
1394           scan_rtx (insn, &INSN_VAR_LOCATION_LOC (insn),
1395                     ALL_REGS, mark_read, OP_IN);
1396         }
1397       if (insn == BB_END (bb))
1398         break;
1399     }
1400
1401   bitmap_clear (&open_chains_set);
1402
1403   if (fail_current_block)
1404     return NULL;
1405
1406   /* Since we close every chain when we find a REG_DEAD note, anything that
1407      is still open lives past the basic block, so it can't be renamed.  */
1408   return closed_chains;
1409 }
1410
1411 /* Dump all def/use chains in CHAINS to DUMP_FILE.  They are
1412    printed in reverse order as that's how we build them.  */
1413
1414 static void
1415 dump_def_use_chain (struct du_head *head)
1416 {
1417   while (head)
1418     {
1419       struct du_chain *this_du = head->first;
1420       fprintf (dump_file, "Register %s (%d):",
1421                reg_names[head->regno], head->nregs);
1422       while (this_du)
1423         {
1424           fprintf (dump_file, " %d [%s]", INSN_UID (this_du->insn),
1425                    reg_class_names[this_du->cl]);
1426           this_du = this_du->next_use;
1427         }
1428       fprintf (dump_file, "\n");
1429       head = head->next_chain;
1430     }
1431 }
1432
1433 \f
1434 static bool
1435 gate_handle_regrename (void)
1436 {
1437   return (optimize > 0 && (flag_rename_registers));
1438 }
1439
1440 struct rtl_opt_pass pass_regrename =
1441 {
1442  {
1443   RTL_PASS,
1444   "rnreg",                              /* name */
1445   gate_handle_regrename,                /* gate */
1446   regrename_optimize,                   /* execute */
1447   NULL,                                 /* sub */
1448   NULL,                                 /* next */
1449   0,                                    /* static_pass_number */
1450   TV_RENAME_REGISTERS,                  /* tv_id */
1451   0,                                    /* properties_required */
1452   0,                                    /* properties_provided */
1453   0,                                    /* properties_destroyed */
1454   0,                                    /* todo_flags_start */
1455   TODO_df_finish | TODO_verify_rtl_sharing |
1456   TODO_dump_func                        /* todo_flags_finish */
1457  }
1458 };
1459