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"
45 struct du_chain
*next_chain
;
46 struct du_chain
*next_use
;
50 ENUM_BITFIELD(reg_class
) cl
: 16;
51 unsigned int need_caller_save_reg
:1;
52 unsigned int earlyclobber
:1;
58 terminate_overlapping_read
,
63 /* mark_access is for marking the destination regs in
64 REG_FRAME_RELATED_EXPR notes (as if they were read) so that the
65 note is updated properly. */
69 static const char * const scan_actions_name
[] =
72 "terminate_overlapping_read",
80 static struct obstack rename_obstack
;
82 static void do_replace (struct du_chain
*, int);
83 static void scan_rtx_reg (rtx
, rtx
*, enum reg_class
,
84 enum scan_actions
, enum op_type
, int);
85 static void scan_rtx_address (rtx
, rtx
*, enum reg_class
,
86 enum scan_actions
, enum machine_mode
);
87 static void scan_rtx (rtx
, rtx
*, enum reg_class
, enum scan_actions
,
89 static struct du_chain
*build_def_use (basic_block
);
90 static void dump_def_use_chain (struct du_chain
*);
91 static void note_sets (rtx
, const_rtx
, void *);
92 static void clear_dead_regs (HARD_REG_SET
*, enum reg_note
, rtx
);
93 static void merge_overlapping_regs (basic_block
, HARD_REG_SET
*,
96 /* Called through note_stores. Find sets of registers, and
97 record them in *DATA (which is actually a HARD_REG_SET *). */
100 note_sets (rtx x
, const_rtx set ATTRIBUTE_UNUSED
, void *data
)
102 HARD_REG_SET
*pset
= (HARD_REG_SET
*) data
;
104 if (GET_CODE (x
) == SUBREG
)
108 /* There must not be pseudos at this point. */
109 gcc_assert (HARD_REGISTER_P (x
));
110 add_to_hard_reg_set (pset
, GET_MODE (x
), REGNO (x
));
113 /* Clear all registers from *PSET for which a note of kind KIND can be found
114 in the list NOTES. */
117 clear_dead_regs (HARD_REG_SET
*pset
, enum reg_note kind
, rtx notes
)
120 for (note
= notes
; note
; note
= XEXP (note
, 1))
121 if (REG_NOTE_KIND (note
) == kind
&& REG_P (XEXP (note
, 0)))
123 rtx reg
= XEXP (note
, 0);
124 /* There must not be pseudos at this point. */
125 gcc_assert (HARD_REGISTER_P (reg
));
126 remove_from_hard_reg_set (pset
, GET_MODE (reg
), REGNO (reg
));
130 /* For a def-use chain CHAIN in basic block B, find which registers overlap
131 its lifetime and set the corresponding bits in *PSET. */
134 merge_overlapping_regs (basic_block b
, HARD_REG_SET
*pset
,
135 struct du_chain
*chain
)
137 struct du_chain
*t
= chain
;
142 REG_SET_TO_HARD_REG_SET (live
, df_get_live_in (b
));
143 for (def_rec
= df_get_artificial_defs (b
->index
); *def_rec
; def_rec
++)
145 df_ref def
= *def_rec
;
146 if (DF_REF_FLAGS (def
) & DF_REF_AT_TOP
)
147 SET_HARD_REG_BIT (live
, DF_REF_REGNO (def
));
152 /* Search forward until the next reference to the register to be
154 while (insn
!= t
->insn
)
158 clear_dead_regs (&live
, REG_DEAD
, REG_NOTES (insn
));
159 note_stores (PATTERN (insn
), note_sets
, (void *) &live
);
160 /* Only record currently live regs if we are inside the
163 IOR_HARD_REG_SET (*pset
, live
);
164 clear_dead_regs (&live
, REG_UNUSED
, REG_NOTES (insn
));
166 insn
= NEXT_INSN (insn
);
169 IOR_HARD_REG_SET (*pset
, live
);
171 /* For the last reference, also merge in all registers set in the
173 @@@ We only have take earlyclobbered sets into account. */
175 note_stores (PATTERN (insn
), note_sets
, (void *) pset
);
181 /* Perform register renaming on the current function. */
184 regrename_optimize (void)
186 int tick
[FIRST_PSEUDO_REGISTER
];
191 df_set_flags (DF_LR_RUN_DCE
);
192 df_note_add_problem ();
194 df_set_flags (DF_DEFER_INSN_RESCAN
);
196 memset (tick
, 0, sizeof tick
);
198 gcc_obstack_init (&rename_obstack
);
199 first_obj
= XOBNEWVAR (&rename_obstack
, char, 0);
203 struct du_chain
*all_chains
= 0;
204 HARD_REG_SET unavailable
;
205 HARD_REG_SET regs_seen
;
207 CLEAR_HARD_REG_SET (unavailable
);
210 fprintf (dump_file
, "\nBasic block %d:\n", bb
->index
);
212 all_chains
= build_def_use (bb
);
215 dump_def_use_chain (all_chains
);
217 CLEAR_HARD_REG_SET (unavailable
);
218 /* Don't clobber traceback for noreturn functions. */
219 if (frame_pointer_needed
)
221 add_to_hard_reg_set (&unavailable
, Pmode
, FRAME_POINTER_REGNUM
);
222 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
223 add_to_hard_reg_set (&unavailable
, Pmode
, HARD_FRAME_POINTER_REGNUM
);
227 CLEAR_HARD_REG_SET (regs_seen
);
230 int new_reg
, best_new_reg
;
232 struct du_chain
*this_du
= all_chains
;
233 struct du_chain
*tmp
;
234 HARD_REG_SET this_unavailable
;
235 int reg
= REGNO (*this_du
->loc
);
238 all_chains
= this_du
->next_chain
;
242 #if 0 /* This just disables optimization opportunities. */
243 /* Only rename once we've seen the reg more than once. */
244 if (! TEST_HARD_REG_BIT (regs_seen
, reg
))
246 SET_HARD_REG_BIT (regs_seen
, reg
);
251 if (fixed_regs
[reg
] || global_regs
[reg
]
252 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
253 || (frame_pointer_needed
&& reg
== HARD_FRAME_POINTER_REGNUM
)
255 || (frame_pointer_needed
&& reg
== FRAME_POINTER_REGNUM
)
260 COPY_HARD_REG_SET (this_unavailable
, unavailable
);
262 /* Count number of uses, and narrow the set of registers we can
265 for (tmp
= this_du
; tmp
; tmp
= tmp
->next_use
)
267 if (DEBUG_INSN_P (tmp
->insn
))
270 IOR_COMPL_HARD_REG_SET (this_unavailable
,
271 reg_class_contents
[tmp
->cl
]);
277 if (this_du
->need_caller_save_reg
)
278 IOR_HARD_REG_SET (this_unavailable
, call_used_reg_set
);
280 merge_overlapping_regs (bb
, &this_unavailable
, this_du
);
282 /* Now potential_regs is a reasonable approximation, let's
283 have a closer look at each register still in there. */
284 for (new_reg
= 0; new_reg
< FIRST_PSEUDO_REGISTER
; new_reg
++)
286 int nregs
= hard_regno_nregs
[new_reg
][GET_MODE (*this_du
->loc
)];
288 for (i
= nregs
- 1; i
>= 0; --i
)
289 if (TEST_HARD_REG_BIT (this_unavailable
, new_reg
+ i
)
290 || fixed_regs
[new_reg
+ i
]
291 || global_regs
[new_reg
+ i
]
292 /* Can't use regs which aren't saved by the prologue. */
293 || (! df_regs_ever_live_p (new_reg
+ i
)
294 && ! call_used_regs
[new_reg
+ i
])
295 #ifdef LEAF_REGISTERS
296 /* We can't use a non-leaf register if we're in a
298 || (current_function_is_leaf
299 && !LEAF_REGISTERS
[new_reg
+ i
])
301 #ifdef HARD_REGNO_RENAME_OK
302 || ! HARD_REGNO_RENAME_OK (reg
+ i
, new_reg
+ i
)
309 /* See whether it accepts all modes that occur in
310 definition and uses. */
311 for (tmp
= this_du
; tmp
; tmp
= tmp
->next_use
)
312 if ((! HARD_REGNO_MODE_OK (new_reg
, GET_MODE (*tmp
->loc
))
313 && ! DEBUG_INSN_P (tmp
->insn
))
314 || (tmp
->need_caller_save_reg
315 && ! (HARD_REGNO_CALL_PART_CLOBBERED
316 (reg
, GET_MODE (*tmp
->loc
)))
317 && (HARD_REGNO_CALL_PART_CLOBBERED
318 (new_reg
, GET_MODE (*tmp
->loc
)))))
322 if (tick
[best_new_reg
] > tick
[new_reg
])
323 best_new_reg
= new_reg
;
329 fprintf (dump_file
, "Register %s in insn %d",
330 reg_names
[reg
], INSN_UID (this_du
->insn
));
331 if (this_du
->need_caller_save_reg
)
332 fprintf (dump_file
, " crosses a call");
335 if (best_new_reg
== reg
)
337 tick
[reg
] = ++this_tick
;
339 fprintf (dump_file
, "; no available better choice\n");
344 fprintf (dump_file
, ", renamed as %s\n", reg_names
[best_new_reg
]);
346 do_replace (this_du
, best_new_reg
);
347 tick
[best_new_reg
] = ++this_tick
;
348 df_set_regs_ever_live (best_new_reg
, true);
351 obstack_free (&rename_obstack
, first_obj
);
354 obstack_free (&rename_obstack
, NULL
);
357 fputc ('\n', dump_file
);
363 do_replace (struct du_chain
*chain
, int reg
)
365 unsigned int base_regno
= REGNO (*chain
->loc
);
367 gcc_assert (! DEBUG_INSN_P (chain
->insn
));
371 unsigned int regno
= ORIGINAL_REGNO (*chain
->loc
);
372 struct reg_attrs
* attr
= REG_ATTRS (*chain
->loc
);
373 int reg_ptr
= REG_POINTER (*chain
->loc
);
375 if (DEBUG_INSN_P (chain
->insn
) && REGNO (*chain
->loc
) != base_regno
)
376 INSN_VAR_LOCATION_LOC (chain
->insn
) = gen_rtx_UNKNOWN_VAR_LOC ();
381 *chain
->loc
= gen_raw_REG (GET_MODE (*chain
->loc
), reg
);
382 if (regno
>= FIRST_PSEUDO_REGISTER
)
383 ORIGINAL_REGNO (*chain
->loc
) = regno
;
384 REG_ATTRS (*chain
->loc
) = attr
;
385 REG_POINTER (*chain
->loc
) = reg_ptr
;
387 for (note
= REG_NOTES (chain
->insn
); note
; note
= XEXP (note
, 1))
389 if (REG_NOTE_KIND (note
) == REG_DEAD
390 || REG_NOTE_KIND (note
) == REG_UNUSED
)
392 rtx reg
= XEXP (note
, 0);
393 gcc_assert (HARD_REGISTER_P (reg
));
395 if (REGNO (reg
) == base_regno
)
396 XEXP (note
, 0) = *chain
->loc
;
401 df_insn_rescan (chain
->insn
);
402 chain
= chain
->next_use
;
407 static struct du_chain
*open_chains
;
408 static struct du_chain
*closed_chains
;
411 scan_rtx_reg (rtx insn
, rtx
*loc
, enum reg_class cl
,
412 enum scan_actions action
, enum op_type type
, int earlyclobber
)
416 enum machine_mode mode
= GET_MODE (x
);
417 int this_regno
= REGNO (x
);
418 int this_nregs
= hard_regno_nregs
[this_regno
][mode
];
420 if (action
== mark_write
)
424 struct du_chain
*this_du
= XOBNEW (&rename_obstack
, struct du_chain
);
425 this_du
->next_use
= 0;
426 this_du
->next_chain
= open_chains
;
428 this_du
->insn
= insn
;
430 this_du
->need_caller_save_reg
= 0;
431 this_du
->earlyclobber
= earlyclobber
;
432 open_chains
= this_du
;
437 if ((type
== OP_OUT
) != (action
== terminate_write
|| action
== mark_access
))
440 for (p
= &open_chains
; *p
;)
442 struct du_chain
*this_du
= *p
;
444 /* Check if the chain has been terminated if it has then skip to
447 This can happen when we've already appended the location to
448 the chain in Step 3, but are trying to hide in-out operands
449 from terminate_write in Step 5. */
451 if (*this_du
->loc
== cc0_rtx
)
452 p
= &this_du
->next_chain
;
455 int regno
= REGNO (*this_du
->loc
);
456 int nregs
= hard_regno_nregs
[regno
][GET_MODE (*this_du
->loc
)];
457 int exact_match
= (regno
== this_regno
&& nregs
== this_nregs
);
459 if (regno
+ nregs
<= this_regno
460 || this_regno
+ this_nregs
<= regno
)
462 p
= &this_du
->next_chain
;
466 if (action
== mark_read
|| action
== mark_access
)
468 gcc_assert (exact_match
|| DEBUG_INSN_P (insn
));
470 /* ??? Class NO_REGS can happen if the md file makes use of
471 EXTRA_CONSTRAINTS to match registers. Which is arguably
472 wrong, but there we are. Since we know not what this may
473 be replaced with, terminate the chain. */
476 this_du
= XOBNEW (&rename_obstack
, struct du_chain
);
477 this_du
->next_use
= 0;
478 this_du
->next_chain
= (*p
)->next_chain
;
480 this_du
->insn
= insn
;
482 this_du
->need_caller_save_reg
= 0;
490 if (action
!= terminate_overlapping_read
|| ! exact_match
)
492 struct du_chain
*next
= this_du
->next_chain
;
494 /* Whether the terminated chain can be used for renaming
495 depends on the action and this being an exact match.
496 In either case, we remove this element from open_chains. */
498 if ((action
== terminate_dead
|| action
== terminate_write
)
501 this_du
->next_chain
= closed_chains
;
502 closed_chains
= this_du
;
505 "Closing chain %s at insn %d (%s)\n",
506 reg_names
[REGNO (*this_du
->loc
)], INSN_UID (insn
),
507 scan_actions_name
[(int) action
]);
513 "Discarding chain %s at insn %d (%s)\n",
514 reg_names
[REGNO (*this_du
->loc
)], INSN_UID (insn
),
515 scan_actions_name
[(int) action
]);
520 p
= &this_du
->next_chain
;
525 /* Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
526 BASE_REG_CLASS depending on how the register is being considered. */
529 scan_rtx_address (rtx insn
, rtx
*loc
, enum reg_class cl
,
530 enum scan_actions action
, enum machine_mode mode
)
533 RTX_CODE code
= GET_CODE (x
);
537 if (action
== mark_write
|| action
== mark_access
)
544 rtx orig_op0
= XEXP (x
, 0);
545 rtx orig_op1
= XEXP (x
, 1);
546 RTX_CODE code0
= GET_CODE (orig_op0
);
547 RTX_CODE code1
= GET_CODE (orig_op1
);
552 enum rtx_code index_code
= SCRATCH
;
554 if (GET_CODE (op0
) == SUBREG
)
556 op0
= SUBREG_REG (op0
);
557 code0
= GET_CODE (op0
);
560 if (GET_CODE (op1
) == SUBREG
)
562 op1
= SUBREG_REG (op1
);
563 code1
= GET_CODE (op1
);
566 if (code0
== MULT
|| code0
== SIGN_EXTEND
|| code0
== TRUNCATE
567 || code0
== ZERO_EXTEND
|| code1
== MEM
)
571 index_code
= GET_CODE (*locI
);
573 else if (code1
== MULT
|| code1
== SIGN_EXTEND
|| code1
== TRUNCATE
574 || code1
== ZERO_EXTEND
|| code0
== MEM
)
578 index_code
= GET_CODE (*locI
);
580 else if (code0
== CONST_INT
|| code0
== CONST
581 || code0
== SYMBOL_REF
|| code0
== LABEL_REF
)
584 index_code
= GET_CODE (XEXP (x
, 0));
586 else if (code1
== CONST_INT
|| code1
== CONST
587 || code1
== SYMBOL_REF
|| code1
== LABEL_REF
)
590 index_code
= GET_CODE (XEXP (x
, 1));
592 else if (code0
== REG
&& code1
== REG
)
595 unsigned regno0
= REGNO (op0
), regno1
= REGNO (op1
);
597 if (REGNO_OK_FOR_INDEX_P (regno1
)
598 && regno_ok_for_base_p (regno0
, mode
, PLUS
, REG
))
600 else if (REGNO_OK_FOR_INDEX_P (regno0
)
601 && regno_ok_for_base_p (regno1
, mode
, PLUS
, REG
))
603 else if (regno_ok_for_base_p (regno0
, mode
, PLUS
, REG
)
604 || REGNO_OK_FOR_INDEX_P (regno1
))
606 else if (regno_ok_for_base_p (regno1
, mode
, PLUS
, REG
))
611 locI
= &XEXP (x
, index_op
);
612 locB
= &XEXP (x
, !index_op
);
613 index_code
= GET_CODE (*locI
);
615 else if (code0
== REG
)
619 index_code
= GET_CODE (*locI
);
621 else if (code1
== REG
)
625 index_code
= GET_CODE (*locI
);
629 scan_rtx_address (insn
, locI
, INDEX_REG_CLASS
, action
, mode
);
631 scan_rtx_address (insn
, locB
, base_reg_class (mode
, PLUS
, index_code
),
644 /* If the target doesn't claim to handle autoinc, this must be
645 something special, like a stack push. Kill this chain. */
646 action
= terminate_all_read
;
651 scan_rtx_address (insn
, &XEXP (x
, 0),
652 base_reg_class (GET_MODE (x
), MEM
, SCRATCH
), action
,
657 scan_rtx_reg (insn
, loc
, cl
, action
, OP_IN
, 0);
664 fmt
= GET_RTX_FORMAT (code
);
665 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
668 scan_rtx_address (insn
, &XEXP (x
, i
), cl
, action
, mode
);
669 else if (fmt
[i
] == 'E')
670 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
671 scan_rtx_address (insn
, &XVECEXP (x
, i
, j
), cl
, action
, mode
);
676 scan_rtx (rtx insn
, rtx
*loc
, enum reg_class cl
,
677 enum scan_actions action
, enum op_type type
, int earlyclobber
)
681 enum rtx_code code
= GET_CODE (x
);
699 scan_rtx_reg (insn
, loc
, cl
, action
, type
, earlyclobber
);
703 scan_rtx_address (insn
, &XEXP (x
, 0),
704 base_reg_class (GET_MODE (x
), MEM
, SCRATCH
), action
,
709 scan_rtx (insn
, &SET_SRC (x
), cl
, action
, OP_IN
, 0);
710 scan_rtx (insn
, &SET_DEST (x
), cl
, action
,
711 GET_CODE (PATTERN (insn
)) == COND_EXEC
? OP_INOUT
: OP_OUT
, 0);
714 case STRICT_LOW_PART
:
715 scan_rtx (insn
, &XEXP (x
, 0), cl
, action
, OP_INOUT
, earlyclobber
);
720 scan_rtx (insn
, &XEXP (x
, 0), cl
, action
,
721 type
== OP_IN
? OP_IN
: OP_INOUT
, earlyclobber
);
722 scan_rtx (insn
, &XEXP (x
, 1), cl
, action
, OP_IN
, 0);
723 scan_rtx (insn
, &XEXP (x
, 2), cl
, action
, OP_IN
, 0);
732 /* Should only happen inside MEM. */
736 scan_rtx (insn
, &SET_DEST (x
), cl
, action
,
737 GET_CODE (PATTERN (insn
)) == COND_EXEC
? OP_INOUT
: OP_OUT
, 0);
741 scan_rtx (insn
, &XEXP (x
, 0), cl
, action
, type
, 0);
743 scan_rtx (insn
, &XEXP (x
, 1), cl
, action
, type
, 0);
750 fmt
= GET_RTX_FORMAT (code
);
751 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
754 scan_rtx (insn
, &XEXP (x
, i
), cl
, action
, type
, 0);
755 else if (fmt
[i
] == 'E')
756 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
757 scan_rtx (insn
, &XVECEXP (x
, i
, j
), cl
, action
, type
, 0);
761 /* Build def/use chain. */
763 static struct du_chain
*
764 build_def_use (basic_block bb
)
768 open_chains
= closed_chains
= NULL
;
770 for (insn
= BB_HEAD (bb
); ; insn
= NEXT_INSN (insn
))
772 if (NONDEBUG_INSN_P (insn
))
776 rtx old_operands
[MAX_RECOG_OPERANDS
];
777 rtx old_dups
[MAX_DUP_OPERANDS
];
782 /* Process the insn, determining its effect on the def-use
783 chains. We perform the following steps with the register
784 references in the insn:
785 (1) Any read that overlaps an open chain, but doesn't exactly
786 match, causes that chain to be closed. We can't deal
788 (2) Any read outside an operand causes any chain it overlaps
789 with to be closed, since we can't replace it.
790 (3) Any read inside an operand is added if there's already
791 an open chain for it.
792 (4) For any REG_DEAD note we find, close open chains that
794 (5) For any write we find, close open chains that overlap it.
795 (6) For any write we find in an operand, make a new chain.
796 (7) For any REG_UNUSED, close any chains we just opened. */
798 icode
= recog_memoized (insn
);
800 if (! constrain_operands (1))
801 fatal_insn_not_found (insn
);
802 preprocess_constraints ();
803 alt
= which_alternative
;
804 n_ops
= recog_data
.n_operands
;
806 /* Simplify the code below by rewriting things to reflect
807 matching constraints. Also promote OP_OUT to OP_INOUT
808 in predicated instructions. */
810 predicated
= GET_CODE (PATTERN (insn
)) == COND_EXEC
;
811 for (i
= 0; i
< n_ops
; ++i
)
813 int matches
= recog_op_alt
[i
][alt
].matches
;
815 recog_op_alt
[i
][alt
].cl
= recog_op_alt
[matches
][alt
].cl
;
816 if (matches
>= 0 || recog_op_alt
[i
][alt
].matched
>= 0
817 || (predicated
&& recog_data
.operand_type
[i
] == OP_OUT
))
818 recog_data
.operand_type
[i
] = OP_INOUT
;
821 /* Step 1: Close chains for which we have overlapping reads. */
822 for (i
= 0; i
< n_ops
; i
++)
823 scan_rtx (insn
, recog_data
.operand_loc
[i
],
824 NO_REGS
, terminate_overlapping_read
,
825 recog_data
.operand_type
[i
], 0);
827 /* Step 2: Close chains for which we have reads outside operands.
828 We do this by munging all operands into CC0, and closing
829 everything remaining. */
831 for (i
= 0; i
< n_ops
; i
++)
833 old_operands
[i
] = recog_data
.operand
[i
];
834 /* Don't squash match_operator or match_parallel here, since
835 we don't know that all of the contained registers are
836 reachable by proper operands. */
837 if (recog_data
.constraints
[i
][0] == '\0')
839 *recog_data
.operand_loc
[i
] = cc0_rtx
;
841 for (i
= 0; i
< recog_data
.n_dups
; i
++)
843 old_dups
[i
] = *recog_data
.dup_loc
[i
];
844 *recog_data
.dup_loc
[i
] = cc0_rtx
;
847 scan_rtx (insn
, &PATTERN (insn
), NO_REGS
, terminate_all_read
,
850 for (i
= 0; i
< recog_data
.n_dups
; i
++)
851 *recog_data
.dup_loc
[i
] = old_dups
[i
];
852 for (i
= 0; i
< n_ops
; i
++)
853 *recog_data
.operand_loc
[i
] = old_operands
[i
];
854 if (recog_data
.n_dups
)
855 df_insn_rescan (insn
);
857 /* Step 2B: Can't rename function call argument registers. */
858 if (CALL_P (insn
) && CALL_INSN_FUNCTION_USAGE (insn
))
859 scan_rtx (insn
, &CALL_INSN_FUNCTION_USAGE (insn
),
860 NO_REGS
, terminate_all_read
, OP_IN
, 0);
862 /* Step 2C: Can't rename asm operands that were originally
864 if (asm_noperands (PATTERN (insn
)) > 0)
865 for (i
= 0; i
< n_ops
; i
++)
867 rtx
*loc
= recog_data
.operand_loc
[i
];
871 && REGNO (op
) == ORIGINAL_REGNO (op
)
872 && (recog_data
.operand_type
[i
] == OP_IN
873 || recog_data
.operand_type
[i
] == OP_INOUT
))
874 scan_rtx (insn
, loc
, NO_REGS
, terminate_all_read
, OP_IN
, 0);
877 /* Step 3: Append to chains for reads inside operands. */
878 for (i
= 0; i
< n_ops
+ recog_data
.n_dups
; i
++)
880 int opn
= i
< n_ops
? i
: recog_data
.dup_num
[i
- n_ops
];
881 rtx
*loc
= (i
< n_ops
882 ? recog_data
.operand_loc
[opn
]
883 : recog_data
.dup_loc
[i
- n_ops
]);
884 enum reg_class cl
= recog_op_alt
[opn
][alt
].cl
;
885 enum op_type type
= recog_data
.operand_type
[opn
];
887 /* Don't scan match_operand here, since we've no reg class
888 information to pass down. Any operands that we could
889 substitute in will be represented elsewhere. */
890 if (recog_data
.constraints
[opn
][0] == '\0')
893 if (recog_op_alt
[opn
][alt
].is_address
)
894 scan_rtx_address (insn
, loc
, cl
, mark_read
, VOIDmode
);
896 scan_rtx (insn
, loc
, cl
, mark_read
, type
, 0);
899 /* Step 3B: Record updates for regs in REG_INC notes, and
900 source regs in REG_FRAME_RELATED_EXPR notes. */
901 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
902 if (REG_NOTE_KIND (note
) == REG_INC
903 || REG_NOTE_KIND (note
) == REG_FRAME_RELATED_EXPR
)
904 scan_rtx (insn
, &XEXP (note
, 0), ALL_REGS
, mark_read
,
907 /* Step 4: Close chains for registers that die here. */
908 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
909 if (REG_NOTE_KIND (note
) == REG_DEAD
)
910 scan_rtx (insn
, &XEXP (note
, 0), NO_REGS
, terminate_dead
,
913 /* Step 4B: If this is a call, any chain live at this point
914 requires a caller-saved reg. */
918 for (p
= open_chains
; p
; p
= p
->next_chain
)
919 p
->need_caller_save_reg
= 1;
922 /* Step 5: Close open chains that overlap writes. Similar to
923 step 2, we hide in-out operands, since we do not want to
924 close these chains. */
926 for (i
= 0; i
< n_ops
; i
++)
928 old_operands
[i
] = recog_data
.operand
[i
];
929 if (recog_data
.operand_type
[i
] == OP_INOUT
)
930 *recog_data
.operand_loc
[i
] = cc0_rtx
;
932 for (i
= 0; i
< recog_data
.n_dups
; i
++)
934 int opn
= recog_data
.dup_num
[i
];
935 old_dups
[i
] = *recog_data
.dup_loc
[i
];
936 if (recog_data
.operand_type
[opn
] == OP_INOUT
)
937 *recog_data
.dup_loc
[i
] = cc0_rtx
;
940 scan_rtx (insn
, &PATTERN (insn
), NO_REGS
, terminate_write
, OP_IN
, 0);
942 for (i
= 0; i
< recog_data
.n_dups
; i
++)
943 *recog_data
.dup_loc
[i
] = old_dups
[i
];
944 for (i
= 0; i
< n_ops
; i
++)
945 *recog_data
.operand_loc
[i
] = old_operands
[i
];
947 /* Step 6: Begin new chains for writes inside operands. */
948 /* ??? Many targets have output constraints on the SET_DEST
949 of a call insn, which is stupid, since these are certainly
950 ABI defined hard registers. Don't change calls at all.
951 Similarly take special care for asm statement that originally
952 referenced hard registers. */
953 if (asm_noperands (PATTERN (insn
)) > 0)
955 for (i
= 0; i
< n_ops
; i
++)
956 if (recog_data
.operand_type
[i
] == OP_OUT
)
958 rtx
*loc
= recog_data
.operand_loc
[i
];
960 enum reg_class cl
= recog_op_alt
[i
][alt
].cl
;
963 && REGNO (op
) == ORIGINAL_REGNO (op
))
966 scan_rtx (insn
, loc
, cl
, mark_write
, OP_OUT
,
967 recog_op_alt
[i
][alt
].earlyclobber
);
970 else if (!CALL_P (insn
))
971 for (i
= 0; i
< n_ops
+ recog_data
.n_dups
; i
++)
973 int opn
= i
< n_ops
? i
: recog_data
.dup_num
[i
- n_ops
];
974 rtx
*loc
= (i
< n_ops
975 ? recog_data
.operand_loc
[opn
]
976 : recog_data
.dup_loc
[i
- n_ops
]);
977 enum reg_class cl
= recog_op_alt
[opn
][alt
].cl
;
979 if (recog_data
.operand_type
[opn
] == OP_OUT
)
980 scan_rtx (insn
, loc
, cl
, mark_write
, OP_OUT
,
981 recog_op_alt
[opn
][alt
].earlyclobber
);
984 /* Step 6B: Record destination regs in REG_FRAME_RELATED_EXPR
986 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
987 if (REG_NOTE_KIND (note
) == REG_FRAME_RELATED_EXPR
)
988 scan_rtx (insn
, &XEXP (note
, 0), ALL_REGS
, mark_access
,
991 /* Step 7: Close chains for registers that were never
993 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
994 if (REG_NOTE_KIND (note
) == REG_UNUSED
)
995 scan_rtx (insn
, &XEXP (note
, 0), NO_REGS
, terminate_dead
,
998 else if (DEBUG_INSN_P (insn
)
999 && !VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn
)))
1001 scan_rtx (insn
, &INSN_VAR_LOCATION_LOC (insn
),
1002 ALL_REGS
, mark_read
, OP_IN
, 0);
1004 if (insn
== BB_END (bb
))
1008 /* Since we close every chain when we find a REG_DEAD note, anything that
1009 is still open lives past the basic block, so it can't be renamed. */
1010 return closed_chains
;
1013 /* Dump all def/use chains in CHAINS to DUMP_FILE. They are
1014 printed in reverse order as that's how we build them. */
1017 dump_def_use_chain (struct du_chain
*chains
)
1021 struct du_chain
*this_du
= chains
;
1022 int r
= REGNO (*this_du
->loc
);
1023 int nregs
= hard_regno_nregs
[r
][GET_MODE (*this_du
->loc
)];
1024 fprintf (dump_file
, "Register %s (%d):", reg_names
[r
], nregs
);
1027 fprintf (dump_file
, " %d [%s]", INSN_UID (this_du
->insn
),
1028 reg_class_names
[this_du
->cl
]);
1029 this_du
= this_du
->next_use
;
1031 fprintf (dump_file
, "\n");
1032 chains
= chains
->next_chain
;
1038 gate_handle_regrename (void)
1040 return (optimize
> 0 && (flag_rename_registers
));
1043 struct rtl_opt_pass pass_regrename
=
1048 gate_handle_regrename
, /* gate */
1049 regrename_optimize
, /* execute */
1052 0, /* static_pass_number */
1053 TV_RENAME_REGISTERS
, /* tv_id */
1054 0, /* properties_required */
1055 0, /* properties_provided */
1056 0, /* properties_destroyed */
1057 0, /* todo_flags_start */
1058 TODO_df_finish
| TODO_verify_rtl_sharing
|
1059 TODO_dump_func
/* todo_flags_finish */