OSDN Git Service

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