1 /* Register renaming for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
5 This file is part of GCC.
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)
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.
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/>. */
23 #include "coretypes.h"
27 #include "insn-config.h"
29 #include "addresses.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
40 #include "tree-pass.h"
43 /* We keep linked lists of DU_HEAD structures, each of which describes
44 a chain of occurrences of a reg. */
48 struct du_head *next_chain;
49 /* The first and last elements of this chain. */
50 struct du_chain *first, *last;
51 /* Describes the register being tracked. */
52 unsigned regno, nregs;
53 /* Nonzero if the chain crosses a call. */
54 unsigned int need_caller_save_reg:1;
55 /* Nonzero if the chain is finished. */
56 unsigned int terminated:1;
59 /* This struct describes a single occurrence of a register. */
62 /* Links to the next occurrence of the register. */
63 struct du_chain *next_use;
65 /* The insn where the register appears. */
67 /* The location inside the insn. */
69 /* The register class required by the insn at this location. */
70 ENUM_BITFIELD(reg_class) cl : 16;
76 terminate_overlapping_read,
81 /* mark_access is for marking the destination regs in
82 REG_FRAME_RELATED_EXPR notes (as if they were read) so that the
83 note is updated properly. */
87 static const char * const scan_actions_name[] =
90 "terminate_overlapping_read",
98 static struct obstack rename_obstack;
100 static void do_replace (struct du_head *, int);
101 static void scan_rtx_reg (rtx, rtx *, enum reg_class,
102 enum scan_actions, enum op_type);
103 static void scan_rtx_address (rtx, rtx *, enum reg_class,
104 enum scan_actions, enum machine_mode);
105 static void scan_rtx (rtx, rtx *, enum reg_class, enum scan_actions,
107 static struct du_head *build_def_use (basic_block);
108 static void dump_def_use_chain (struct du_head *);
109 static void note_sets (rtx, const_rtx, void *);
110 static void clear_dead_regs (HARD_REG_SET *, enum reg_note, rtx);
111 static void merge_overlapping_regs (basic_block, HARD_REG_SET *,
114 /* Called through note_stores. Find sets of registers, and
115 record them in *DATA (which is actually a HARD_REG_SET *). */
118 note_sets (rtx x, const_rtx set ATTRIBUTE_UNUSED, void *data)
120 HARD_REG_SET *pset = (HARD_REG_SET *) data;
122 if (GET_CODE (x) == SUBREG)
126 /* There must not be pseudos at this point. */
127 gcc_assert (HARD_REGISTER_P (x));
128 add_to_hard_reg_set (pset, GET_MODE (x), REGNO (x));
131 /* Clear all registers from *PSET for which a note of kind KIND can be found
132 in the list NOTES. */
135 clear_dead_regs (HARD_REG_SET *pset, enum reg_note kind, rtx notes)
138 for (note = notes; note; note = XEXP (note, 1))
139 if (REG_NOTE_KIND (note) == kind && REG_P (XEXP (note, 0)))
141 rtx reg = XEXP (note, 0);
142 /* There must not be pseudos at this point. */
143 gcc_assert (HARD_REGISTER_P (reg));
144 remove_from_hard_reg_set (pset, GET_MODE (reg), REGNO (reg));
148 /* For a def-use chain HEAD in basic block B, find which registers overlap
149 its lifetime and set the corresponding bits in *PSET. */
152 merge_overlapping_regs (basic_block b, HARD_REG_SET *pset,
153 struct du_head *head)
160 REG_SET_TO_HARD_REG_SET (live, df_get_live_in (b));
161 for (def_rec = df_get_artificial_defs (b->index); *def_rec; def_rec++)
163 df_ref def = *def_rec;
164 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
165 SET_HARD_REG_BIT (live, DF_REF_REGNO (def));
172 /* Search forward until the next reference to the register to be
174 while (insn != t->insn)
178 clear_dead_regs (&live, REG_DEAD, REG_NOTES (insn));
179 note_stores (PATTERN (insn), note_sets, (void *) &live);
180 /* Only record currently live regs if we are inside the
182 if (t != head->first)
183 IOR_HARD_REG_SET (*pset, live);
184 clear_dead_regs (&live, REG_UNUSED, REG_NOTES (insn));
186 insn = NEXT_INSN (insn);
189 IOR_HARD_REG_SET (*pset, live);
191 /* For the last reference, also merge in all registers set in the
193 @@@ We only have take earlyclobbered sets into account. */
195 note_stores (PATTERN (insn), note_sets, (void *) pset);
201 /* Perform register renaming on the current function. */
204 regrename_optimize (void)
206 int tick[FIRST_PSEUDO_REGISTER];
211 df_set_flags (DF_LR_RUN_DCE);
212 df_note_add_problem ();
214 df_set_flags (DF_DEFER_INSN_RESCAN);
216 memset (tick, 0, sizeof tick);
218 gcc_obstack_init (&rename_obstack);
219 first_obj = XOBNEWVAR (&rename_obstack, char, 0);
223 struct du_head *all_chains = 0;
224 HARD_REG_SET unavailable;
225 HARD_REG_SET regs_seen;
227 CLEAR_HARD_REG_SET (unavailable);
230 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
232 all_chains = build_def_use (bb);
235 dump_def_use_chain (all_chains);
237 CLEAR_HARD_REG_SET (unavailable);
238 /* Don't clobber traceback for noreturn functions. */
239 if (frame_pointer_needed)
241 add_to_hard_reg_set (&unavailable, Pmode, FRAME_POINTER_REGNUM);
242 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
243 add_to_hard_reg_set (&unavailable, Pmode, HARD_FRAME_POINTER_REGNUM);
247 CLEAR_HARD_REG_SET (regs_seen);
250 int new_reg, best_new_reg;
252 struct du_head *this_head = all_chains;
253 struct du_chain *tmp;
254 HARD_REG_SET this_unavailable;
255 int reg = this_head->regno;
258 all_chains = this_head->next_chain;
262 #if 0 /* This just disables optimization opportunities. */
263 /* Only rename once we've seen the reg more than once. */
264 if (! TEST_HARD_REG_BIT (regs_seen, reg))
266 SET_HARD_REG_BIT (regs_seen, reg);
271 if (fixed_regs[reg] || global_regs[reg]
272 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
273 || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
275 || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
280 COPY_HARD_REG_SET (this_unavailable, unavailable);
282 /* Count number of uses, and narrow the set of registers we can
285 for (tmp = this_head->first; tmp; tmp = tmp->next_use)
287 if (DEBUG_INSN_P (tmp->insn))
290 IOR_COMPL_HARD_REG_SET (this_unavailable,
291 reg_class_contents[tmp->cl]);
297 if (this_head->need_caller_save_reg)
298 IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
300 merge_overlapping_regs (bb, &this_unavailable, this_head);
302 /* Now potential_regs is a reasonable approximation, let's
303 have a closer look at each register still in there. */
304 for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
306 enum machine_mode mode = GET_MODE (*this_head->first->loc);
307 int nregs = hard_regno_nregs[new_reg][mode];
309 for (i = nregs - 1; i >= 0; --i)
310 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
311 || fixed_regs[new_reg + i]
312 || global_regs[new_reg + i]
313 /* Can't use regs which aren't saved by the prologue. */
314 || (! df_regs_ever_live_p (new_reg + i)
315 && ! call_used_regs[new_reg + i])
316 #ifdef LEAF_REGISTERS
317 /* We can't use a non-leaf register if we're in a
319 || (current_function_is_leaf
320 && !LEAF_REGISTERS[new_reg + i])
322 #ifdef HARD_REGNO_RENAME_OK
323 || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
330 /* See whether it accepts all modes that occur in
331 definition and uses. */
332 for (tmp = this_head->first; tmp; tmp = tmp->next_use)
333 if ((! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
334 && ! DEBUG_INSN_P (tmp->insn))
335 || (this_head->need_caller_save_reg
336 && ! (HARD_REGNO_CALL_PART_CLOBBERED
337 (reg, GET_MODE (*tmp->loc)))
338 && (HARD_REGNO_CALL_PART_CLOBBERED
339 (new_reg, GET_MODE (*tmp->loc)))))
343 if (tick[best_new_reg] > tick[new_reg])
344 best_new_reg = new_reg;
350 fprintf (dump_file, "Register %s in insn %d",
351 reg_names[reg], INSN_UID (this_head->first->insn));
352 if (this_head->need_caller_save_reg)
353 fprintf (dump_file, " crosses a call");
356 if (best_new_reg == reg)
358 tick[reg] = ++this_tick;
360 fprintf (dump_file, "; no available better choice\n");
365 fprintf (dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
367 do_replace (this_head, best_new_reg);
368 tick[best_new_reg] = ++this_tick;
369 df_set_regs_ever_live (best_new_reg, true);
372 obstack_free (&rename_obstack, first_obj);
375 obstack_free (&rename_obstack, NULL);
378 fputc ('\n', dump_file);
384 do_replace (struct du_head *head, int reg)
386 struct du_chain *chain;
387 unsigned int base_regno = head->regno;
389 gcc_assert (! DEBUG_INSN_P (head->first->insn));
391 for (chain = head->first; chain; chain = chain->next_use)
393 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
394 struct reg_attrs *attr = REG_ATTRS (*chain->loc);
395 int reg_ptr = REG_POINTER (*chain->loc);
397 if (DEBUG_INSN_P (chain->insn) && REGNO (*chain->loc) != base_regno)
398 INSN_VAR_LOCATION_LOC (chain->insn) = gen_rtx_UNKNOWN_VAR_LOC ();
403 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
404 if (regno >= FIRST_PSEUDO_REGISTER)
405 ORIGINAL_REGNO (*chain->loc) = regno;
406 REG_ATTRS (*chain->loc) = attr;
407 REG_POINTER (*chain->loc) = reg_ptr;
409 for (note = REG_NOTES (chain->insn); note; note = XEXP (note, 1))
411 if (REG_NOTE_KIND (note) == REG_DEAD
412 || REG_NOTE_KIND (note) == REG_UNUSED)
414 rtx reg = XEXP (note, 0);
415 gcc_assert (HARD_REGISTER_P (reg));
417 if (REGNO (reg) == base_regno)
418 XEXP (note, 0) = *chain->loc;
423 df_insn_rescan (chain->insn);
428 static struct du_head *open_chains;
429 static struct du_head *closed_chains;
432 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class cl, enum scan_actions action,
437 enum machine_mode mode = GET_MODE (x);
438 unsigned this_regno = REGNO (x);
439 unsigned this_nregs = hard_regno_nregs[this_regno][mode];
441 if (action == mark_write)
445 struct du_head *head = XOBNEW (&rename_obstack, struct du_head);
446 struct du_chain *this_du = XOBNEW (&rename_obstack, struct du_chain);
447 head->next_chain = open_chains;
449 head->first = head->last = this_du;
450 head->regno = this_regno;
451 head->nregs = this_nregs;
452 head->need_caller_save_reg = 0;
453 head->terminated = 0;
455 this_du->next_use = 0;
457 this_du->insn = insn;
463 if ((type == OP_OUT) != (action == terminate_write || action == mark_access))
466 for (p = &open_chains; *p;)
468 struct du_head *head = *p;
470 /* Check if the chain has been terminated if it has then skip to
473 This can happen when we've already appended the location to
474 the chain in Step 3, but are trying to hide in-out operands
475 from terminate_write in Step 5. */
477 if (head->terminated)
478 p = &head->next_chain;
481 int exact_match = (head->regno == this_regno
482 && head->nregs == this_nregs);
484 if (head->regno + head->nregs <= this_regno
485 || this_regno + this_nregs <= head->regno)
487 p = &head->next_chain;
491 if (action == mark_read || action == mark_access)
493 gcc_assert (exact_match || DEBUG_INSN_P (insn));
495 /* ??? Class NO_REGS can happen if the md file makes use of
496 EXTRA_CONSTRAINTS to match registers. Which is arguably
497 wrong, but there we are. Since we know not what this may
498 be replaced with, terminate the chain. */
501 struct du_chain *this_du;
502 this_du = XOBNEW (&rename_obstack, struct du_chain);
503 this_du->next_use = 0;
505 this_du->insn = insn;
507 head->last->next_use = this_du;
508 head->last = this_du;
513 if (action != terminate_overlapping_read || ! exact_match)
515 struct du_head *next = head->next_chain;
517 /* Whether the terminated chain can be used for renaming
518 depends on the action and this being an exact match.
519 In either case, we remove this element from open_chains. */
521 head->terminated = 1;
522 if ((action == terminate_dead || action == terminate_write)
525 head->next_chain = closed_chains;
526 closed_chains = head;
529 "Closing chain %s at insn %d (%s)\n",
530 reg_names[head->regno], INSN_UID (insn),
531 scan_actions_name[(int) action]);
537 "Discarding chain %s at insn %d (%s)\n",
538 reg_names[head->regno], INSN_UID (insn),
539 scan_actions_name[(int) action]);
544 p = &head->next_chain;
549 /* Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
550 BASE_REG_CLASS depending on how the register is being considered. */
553 scan_rtx_address (rtx insn, rtx *loc, enum reg_class cl,
554 enum scan_actions action, enum machine_mode mode)
557 RTX_CODE code = GET_CODE (x);
561 if (action == mark_write || action == mark_access)
568 rtx orig_op0 = XEXP (x, 0);
569 rtx orig_op1 = XEXP (x, 1);
570 RTX_CODE code0 = GET_CODE (orig_op0);
571 RTX_CODE code1 = GET_CODE (orig_op1);
576 enum rtx_code index_code = SCRATCH;
578 if (GET_CODE (op0) == SUBREG)
580 op0 = SUBREG_REG (op0);
581 code0 = GET_CODE (op0);
584 if (GET_CODE (op1) == SUBREG)
586 op1 = SUBREG_REG (op1);
587 code1 = GET_CODE (op1);
590 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
591 || code0 == ZERO_EXTEND || code1 == MEM)
595 index_code = GET_CODE (*locI);
597 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
598 || code1 == ZERO_EXTEND || code0 == MEM)
602 index_code = GET_CODE (*locI);
604 else if (code0 == CONST_INT || code0 == CONST
605 || code0 == SYMBOL_REF || code0 == LABEL_REF)
608 index_code = GET_CODE (XEXP (x, 0));
610 else if (code1 == CONST_INT || code1 == CONST
611 || code1 == SYMBOL_REF || code1 == LABEL_REF)
614 index_code = GET_CODE (XEXP (x, 1));
616 else if (code0 == REG && code1 == REG)
619 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
621 if (REGNO_OK_FOR_INDEX_P (regno1)
622 && regno_ok_for_base_p (regno0, mode, PLUS, REG))
624 else if (REGNO_OK_FOR_INDEX_P (regno0)
625 && regno_ok_for_base_p (regno1, mode, PLUS, REG))
627 else if (regno_ok_for_base_p (regno0, mode, PLUS, REG)
628 || REGNO_OK_FOR_INDEX_P (regno1))
630 else if (regno_ok_for_base_p (regno1, mode, PLUS, REG))
635 locI = &XEXP (x, index_op);
636 locB = &XEXP (x, !index_op);
637 index_code = GET_CODE (*locI);
639 else if (code0 == REG)
643 index_code = GET_CODE (*locI);
645 else if (code1 == REG)
649 index_code = GET_CODE (*locI);
653 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
655 scan_rtx_address (insn, locB, base_reg_class (mode, PLUS, index_code),
668 /* If the target doesn't claim to handle autoinc, this must be
669 something special, like a stack push. Kill this chain. */
670 action = terminate_all_read;
675 scan_rtx_address (insn, &XEXP (x, 0),
676 base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
681 scan_rtx_reg (insn, loc, cl, action, OP_IN);
688 fmt = GET_RTX_FORMAT (code);
689 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
692 scan_rtx_address (insn, &XEXP (x, i), cl, action, mode);
693 else if (fmt[i] == 'E')
694 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
695 scan_rtx_address (insn, &XVECEXP (x, i, j), cl, action, mode);
700 scan_rtx (rtx insn, rtx *loc, enum reg_class cl, enum scan_actions action,
705 enum rtx_code code = GET_CODE (x);
723 scan_rtx_reg (insn, loc, cl, action, type);
727 scan_rtx_address (insn, &XEXP (x, 0),
728 base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
733 scan_rtx (insn, &SET_SRC (x), cl, action, OP_IN);
734 scan_rtx (insn, &SET_DEST (x), cl, action,
735 GET_CODE (PATTERN (insn)) == COND_EXEC ? OP_INOUT : OP_OUT);
738 case STRICT_LOW_PART:
739 scan_rtx (insn, &XEXP (x, 0), cl, action, OP_INOUT);
744 scan_rtx (insn, &XEXP (x, 0), cl, action,
745 type == OP_IN ? OP_IN : OP_INOUT);
746 scan_rtx (insn, &XEXP (x, 1), cl, action, OP_IN);
747 scan_rtx (insn, &XEXP (x, 2), cl, action, OP_IN);
756 /* Should only happen inside MEM. */
760 scan_rtx (insn, &SET_DEST (x), cl, action,
761 GET_CODE (PATTERN (insn)) == COND_EXEC ? OP_INOUT : OP_OUT);
765 scan_rtx (insn, &XEXP (x, 0), cl, action, type);
767 scan_rtx (insn, &XEXP (x, 1), cl, action, type);
774 fmt = GET_RTX_FORMAT (code);
775 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
778 scan_rtx (insn, &XEXP (x, i), cl, action, type);
779 else if (fmt[i] == 'E')
780 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
781 scan_rtx (insn, &XVECEXP (x, i, j), cl, action, type);
785 /* Hide operands of the current insn (of which there are N_OPS) by
786 substituting cc0 for them.
787 Previous values are stored in the OLD_OPERANDS and OLD_DUPS.
788 If INOUT_ONLY is set, we only do this for OP_INOUT type operands. */
791 hide_operands (int n_ops, rtx *old_operands, rtx *old_dups,
795 for (i = 0; i < n_ops; i++)
797 old_operands[i] = recog_data.operand[i];
798 /* Don't squash match_operator or match_parallel here, since
799 we don't know that all of the contained registers are
800 reachable by proper operands. */
801 if (recog_data.constraints[i][0] == '\0')
803 if (!inout_only || recog_data.operand_type[i] == OP_INOUT)
804 *recog_data.operand_loc[i] = cc0_rtx;
806 for (i = 0; i < recog_data.n_dups; i++)
808 int opn = recog_data.dup_num[i];
809 old_dups[i] = *recog_data.dup_loc[i];
810 if (!inout_only || recog_data.operand_type[opn] == OP_INOUT)
811 *recog_data.dup_loc[i] = cc0_rtx;
815 /* Undo the substitution performed by hide_operands. INSN is the insn we
816 are processing; the arguments are the same as in hide_operands. */
819 restore_operands (rtx insn, int n_ops, rtx *old_operands, rtx *old_dups)
822 for (i = 0; i < recog_data.n_dups; i++)
823 *recog_data.dup_loc[i] = old_dups[i];
824 for (i = 0; i < n_ops; i++)
825 *recog_data.operand_loc[i] = old_operands[i];
826 if (recog_data.n_dups)
827 df_insn_rescan (insn);
830 /* For each output operand of INSN, call scan_rtx to create a new
834 record_out_operands (rtx insn)
836 int n_ops = recog_data.n_operands;
837 int alt = which_alternative;
841 for (i = 0; i < n_ops + recog_data.n_dups; i++)
843 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
844 rtx *loc = (i < n_ops
845 ? recog_data.operand_loc[opn]
846 : recog_data.dup_loc[i - n_ops]);
848 enum reg_class cl = recog_op_alt[opn][alt].cl;
850 struct du_head *prev_open;
852 if (recog_data.operand_type[opn] != OP_OUT)
855 prev_open = open_chains;
856 scan_rtx (insn, loc, cl, mark_write, OP_OUT);
858 /* ??? Many targets have output constraints on the SET_DEST
859 of a call insn, which is stupid, since these are certainly
860 ABI defined hard registers. For these, and for asm operands
861 that originally referenced hard registers, we must record that
862 the chain cannot be renamed. */
864 || (asm_noperands (PATTERN (insn)) > 0
866 && REGNO (op) == ORIGINAL_REGNO (op)))
868 if (prev_open != open_chains)
869 open_chains->first->cl = NO_REGS;
874 /* Build def/use chain. */
876 static struct du_head *
877 build_def_use (basic_block bb)
881 open_chains = closed_chains = NULL;
883 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
885 if (NONDEBUG_INSN_P (insn))
889 rtx old_operands[MAX_RECOG_OPERANDS];
890 rtx old_dups[MAX_DUP_OPERANDS];
895 /* Process the insn, determining its effect on the def-use
896 chains. We perform the following steps with the register
897 references in the insn:
898 (1) Any read that overlaps an open chain, but doesn't exactly
899 match, causes that chain to be closed. We can't deal
901 (2) Any read outside an operand causes any chain it overlaps
902 with to be closed, since we can't replace it.
903 (3) Any read inside an operand is added if there's already
904 an open chain for it.
905 (4) For any REG_DEAD note we find, close open chains that
907 (5) For any write we find, close open chains that overlap it.
908 (6) For any write we find in an operand, make a new chain.
909 (7) For any REG_UNUSED, close any chains we just opened. */
911 icode = recog_memoized (insn);
913 if (! constrain_operands (1))
914 fatal_insn_not_found (insn);
915 preprocess_constraints ();
916 alt = which_alternative;
917 n_ops = recog_data.n_operands;
919 /* Simplify the code below by rewriting things to reflect
920 matching constraints. Also promote OP_OUT to OP_INOUT
921 in predicated instructions. */
923 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
924 for (i = 0; i < n_ops; ++i)
926 int matches = recog_op_alt[i][alt].matches;
928 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
929 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
930 || (predicated && recog_data.operand_type[i] == OP_OUT))
931 recog_data.operand_type[i] = OP_INOUT;
934 /* Step 1: Close chains for which we have overlapping reads. */
935 for (i = 0; i < n_ops; i++)
936 scan_rtx (insn, recog_data.operand_loc[i],
937 NO_REGS, terminate_overlapping_read,
938 recog_data.operand_type[i]);
940 /* Step 2: Close chains for which we have reads outside operands.
941 We do this by munging all operands into CC0, and closing
942 everything remaining. */
944 hide_operands (n_ops, old_operands, old_dups, false);
945 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
947 restore_operands (insn, n_ops, old_operands, old_dups);
949 /* Step 2B: Can't rename function call argument registers. */
950 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
951 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
952 NO_REGS, terminate_all_read, OP_IN);
954 /* Step 2C: Can't rename asm operands that were originally
956 if (asm_noperands (PATTERN (insn)) > 0)
957 for (i = 0; i < n_ops; i++)
959 rtx *loc = recog_data.operand_loc[i];
963 && REGNO (op) == ORIGINAL_REGNO (op)
964 && (recog_data.operand_type[i] == OP_IN
965 || recog_data.operand_type[i] == OP_INOUT))
966 scan_rtx (insn, loc, NO_REGS, terminate_all_read, OP_IN);
969 /* Step 3: Append to chains for reads inside operands. */
970 for (i = 0; i < n_ops + recog_data.n_dups; i++)
972 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
973 rtx *loc = (i < n_ops
974 ? recog_data.operand_loc[opn]
975 : recog_data.dup_loc[i - n_ops]);
976 enum reg_class cl = recog_op_alt[opn][alt].cl;
977 enum op_type type = recog_data.operand_type[opn];
979 /* Don't scan match_operand here, since we've no reg class
980 information to pass down. Any operands that we could
981 substitute in will be represented elsewhere. */
982 if (recog_data.constraints[opn][0] == '\0')
985 if (recog_op_alt[opn][alt].is_address)
986 scan_rtx_address (insn, loc, cl, mark_read, VOIDmode);
988 scan_rtx (insn, loc, cl, mark_read, type);
991 /* Step 3B: Record updates for regs in REG_INC notes, and
992 source regs in REG_FRAME_RELATED_EXPR notes. */
993 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
994 if (REG_NOTE_KIND (note) == REG_INC
995 || REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
996 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
999 /* Step 4: Close chains for registers that die here. */
1000 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1001 if (REG_NOTE_KIND (note) == REG_DEAD)
1002 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
1005 /* Step 4B: If this is a call, any chain live at this point
1006 requires a caller-saved reg. */
1010 for (p = open_chains; p; p = p->next_chain)
1011 p->need_caller_save_reg = 1;
1014 /* Step 5: Close open chains that overlap writes. Similar to
1015 step 2, we hide in-out operands, since we do not want to
1016 close these chains. */
1018 hide_operands (n_ops, old_operands, old_dups, true);
1019 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN);
1020 restore_operands (insn, n_ops, old_operands, old_dups);
1022 /* Step 6: Begin new chains for writes inside operands. */
1023 record_out_operands (insn);
1025 /* Step 6B: Record destination regs in REG_FRAME_RELATED_EXPR
1026 notes for update. */
1027 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1028 if (REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
1029 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_access,
1032 /* Step 7: Close chains for registers that were never
1033 really used here. */
1034 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1035 if (REG_NOTE_KIND (note) == REG_UNUSED)
1036 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
1039 else if (DEBUG_INSN_P (insn)
1040 && !VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn)))
1042 scan_rtx (insn, &INSN_VAR_LOCATION_LOC (insn),
1043 ALL_REGS, mark_read, OP_IN);
1045 if (insn == BB_END (bb))
1049 /* Since we close every chain when we find a REG_DEAD note, anything that
1050 is still open lives past the basic block, so it can't be renamed. */
1051 return closed_chains;
1054 /* Dump all def/use chains in CHAINS to DUMP_FILE. They are
1055 printed in reverse order as that's how we build them. */
1058 dump_def_use_chain (struct du_head *head)
1062 struct du_chain *this_du = head->first;
1063 fprintf (dump_file, "Register %s (%d):",
1064 reg_names[head->regno], head->nregs);
1067 fprintf (dump_file, " %d [%s]", INSN_UID (this_du->insn),
1068 reg_class_names[this_du->cl]);
1069 this_du = this_du->next_use;
1071 fprintf (dump_file, "\n");
1072 head = head->next_chain;
1078 gate_handle_regrename (void)
1080 return (optimize > 0 && (flag_rename_registers));
1083 struct rtl_opt_pass pass_regrename =
1088 gate_handle_regrename, /* gate */
1089 regrename_optimize, /* execute */
1092 0, /* static_pass_number */
1093 TV_RENAME_REGISTERS, /* tv_id */
1094 0, /* properties_required */
1095 0, /* properties_provided */
1096 0, /* properties_destroyed */
1097 0, /* todo_flags_start */
1098 TODO_df_finish | TODO_verify_rtl_sharing |
1099 TODO_dump_func /* todo_flags_finish */