1 /* Register renaming for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
25 #include "coretypes.h"
29 #include "insn-config.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
41 #ifndef REG_MODE_OK_FOR_BASE_P
42 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
45 static const char *const reg_class_names
[] = REG_CLASS_NAMES
;
49 struct du_chain
*next_chain
;
50 struct du_chain
*next_use
;
55 unsigned int need_caller_save_reg
:1;
56 unsigned int earlyclobber
:1;
62 terminate_overlapping_read
,
69 static const char * const scan_actions_name
[] =
72 "terminate_overlapping_read",
79 static struct obstack rename_obstack
;
81 static void do_replace
PARAMS ((struct du_chain
*, int));
82 static void scan_rtx_reg
PARAMS ((rtx
, rtx
*, enum reg_class
,
83 enum scan_actions
, enum op_type
, int));
84 static void scan_rtx_address
PARAMS ((rtx
, rtx
*, enum reg_class
,
85 enum scan_actions
, enum machine_mode
));
86 static void scan_rtx
PARAMS ((rtx
, rtx
*, enum reg_class
,
87 enum scan_actions
, enum op_type
, int));
88 static struct du_chain
*build_def_use
PARAMS ((basic_block
));
89 static void dump_def_use_chain
PARAMS ((struct du_chain
*));
90 static void note_sets
PARAMS ((rtx
, rtx
, void *));
91 static void clear_dead_regs
PARAMS ((HARD_REG_SET
*, enum machine_mode
, rtx
));
92 static void merge_overlapping_regs
PARAMS ((basic_block
, HARD_REG_SET
*,
95 /* Called through note_stores from update_life. Find sets of registers, and
96 record them in *DATA (which is actually a HARD_REG_SET *). */
99 note_sets (x
, set
, data
)
101 rtx set ATTRIBUTE_UNUSED
;
104 HARD_REG_SET
*pset
= (HARD_REG_SET
*) data
;
107 if (GET_CODE (x
) != REG
)
110 nregs
= HARD_REGNO_NREGS (regno
, GET_MODE (x
));
112 /* There must not be pseudos at this point. */
113 if (regno
+ nregs
> FIRST_PSEUDO_REGISTER
)
117 SET_HARD_REG_BIT (*pset
, regno
+ nregs
);
120 /* Clear all registers from *PSET for which a note of kind KIND can be found
121 in the list NOTES. */
124 clear_dead_regs (pset
, kind
, notes
)
126 enum machine_mode kind
;
130 for (note
= notes
; note
; note
= XEXP (note
, 1))
131 if (REG_NOTE_KIND (note
) == kind
&& REG_P (XEXP (note
, 0)))
133 rtx reg
= XEXP (note
, 0);
134 unsigned int regno
= REGNO (reg
);
135 int nregs
= HARD_REGNO_NREGS (regno
, GET_MODE (reg
));
137 /* There must not be pseudos at this point. */
138 if (regno
+ nregs
> FIRST_PSEUDO_REGISTER
)
142 CLEAR_HARD_REG_BIT (*pset
, regno
+ nregs
);
146 /* For a def-use chain CHAIN in basic block B, find which registers overlap
147 its lifetime and set the corresponding bits in *PSET. */
150 merge_overlapping_regs (b
, pset
, chain
)
153 struct du_chain
*chain
;
155 struct du_chain
*t
= chain
;
159 REG_SET_TO_HARD_REG_SET (live
, b
->global_live_at_start
);
163 /* Search forward until the next reference to the register to be
165 while (insn
!= t
->insn
)
169 clear_dead_regs (&live
, REG_DEAD
, REG_NOTES (insn
));
170 note_stores (PATTERN (insn
), note_sets
, (void *) &live
);
171 /* Only record currently live regs if we are inside the
174 IOR_HARD_REG_SET (*pset
, live
);
175 clear_dead_regs (&live
, REG_UNUSED
, REG_NOTES (insn
));
177 insn
= NEXT_INSN (insn
);
180 IOR_HARD_REG_SET (*pset
, live
);
182 /* For the last reference, also merge in all registers set in the
184 @@@ We only have take earlyclobbered sets into account. */
186 note_stores (PATTERN (insn
), note_sets
, (void *) pset
);
192 /* Perform register renaming on the current function. */
195 regrename_optimize ()
197 int tick
[FIRST_PSEUDO_REGISTER
];
202 memset (tick
, 0, sizeof tick
);
204 gcc_obstack_init (&rename_obstack
);
205 first_obj
= (char *) obstack_alloc (&rename_obstack
, 0);
209 struct du_chain
*all_chains
= 0;
210 HARD_REG_SET unavailable
;
211 HARD_REG_SET regs_seen
;
213 CLEAR_HARD_REG_SET (unavailable
);
216 fprintf (rtl_dump_file
, "\nBasic block %d:\n", bb
->index
);
218 all_chains
= build_def_use (bb
);
221 dump_def_use_chain (all_chains
);
223 CLEAR_HARD_REG_SET (unavailable
);
224 /* Don't clobber traceback for noreturn functions. */
225 if (frame_pointer_needed
)
229 for (i
= HARD_REGNO_NREGS (FRAME_POINTER_REGNUM
, Pmode
); i
--;)
230 SET_HARD_REG_BIT (unavailable
, FRAME_POINTER_REGNUM
+ i
);
232 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
233 for (i
= HARD_REGNO_NREGS (HARD_FRAME_POINTER_REGNUM
, Pmode
); i
--;)
234 SET_HARD_REG_BIT (unavailable
, HARD_FRAME_POINTER_REGNUM
+ i
);
238 CLEAR_HARD_REG_SET (regs_seen
);
241 int new_reg
, best_new_reg
= -1;
243 struct du_chain
*this = all_chains
;
244 struct du_chain
*tmp
, *last
;
245 HARD_REG_SET this_unavailable
;
246 int reg
= REGNO (*this->loc
);
249 all_chains
= this->next_chain
;
251 #if 0 /* This just disables optimization opportunities. */
252 /* Only rename once we've seen the reg more than once. */
253 if (! TEST_HARD_REG_BIT (regs_seen
, reg
))
255 SET_HARD_REG_BIT (regs_seen
, reg
);
260 if (fixed_regs
[reg
] || global_regs
[reg
]
261 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
262 || (frame_pointer_needed
&& reg
== HARD_FRAME_POINTER_REGNUM
)
264 || (frame_pointer_needed
&& reg
== FRAME_POINTER_REGNUM
)
269 COPY_HARD_REG_SET (this_unavailable
, unavailable
);
271 /* Find last entry on chain (which has the need_caller_save bit),
272 count number of uses, and narrow the set of registers we can
275 for (last
= this; last
->next_use
; last
= last
->next_use
)
278 IOR_COMPL_HARD_REG_SET (this_unavailable
,
279 reg_class_contents
[last
->class]);
284 IOR_COMPL_HARD_REG_SET (this_unavailable
,
285 reg_class_contents
[last
->class]);
287 if (this->need_caller_save_reg
)
288 IOR_HARD_REG_SET (this_unavailable
, call_used_reg_set
);
290 merge_overlapping_regs (bb
, &this_unavailable
, this);
292 /* Now potential_regs is a reasonable approximation, let's
293 have a closer look at each register still in there. */
294 for (new_reg
= 0; new_reg
< FIRST_PSEUDO_REGISTER
; new_reg
++)
296 int nregs
= HARD_REGNO_NREGS (new_reg
, GET_MODE (*this->loc
));
298 for (i
= nregs
- 1; i
>= 0; --i
)
299 if (TEST_HARD_REG_BIT (this_unavailable
, new_reg
+ i
)
300 || fixed_regs
[new_reg
+ i
]
301 || global_regs
[new_reg
+ i
]
302 /* Can't use regs which aren't saved by the prologue. */
303 || (! regs_ever_live
[new_reg
+ i
]
304 && ! call_used_regs
[new_reg
+ i
])
305 #ifdef LEAF_REGISTERS
306 /* We can't use a non-leaf register if we're in a
308 || (current_function_is_leaf
309 && !LEAF_REGISTERS
[new_reg
+ i
])
311 #ifdef HARD_REGNO_RENAME_OK
312 || ! HARD_REGNO_RENAME_OK (reg
+ i
, new_reg
+ i
)
319 /* See whether it accepts all modes that occur in
320 definition and uses. */
321 for (tmp
= this; tmp
; tmp
= tmp
->next_use
)
322 if (! HARD_REGNO_MODE_OK (new_reg
, GET_MODE (*tmp
->loc
))
323 || (tmp
->need_caller_save_reg
324 && ! (HARD_REGNO_CALL_PART_CLOBBERED
325 (reg
, GET_MODE (*tmp
->loc
)))
326 && (HARD_REGNO_CALL_PART_CLOBBERED
327 (new_reg
, GET_MODE (*tmp
->loc
)))))
331 if (best_new_reg
== -1
332 || tick
[best_new_reg
] > tick
[new_reg
])
333 best_new_reg
= new_reg
;
339 fprintf (rtl_dump_file
, "Register %s in insn %d",
340 reg_names
[reg
], INSN_UID (last
->insn
));
341 if (last
->need_caller_save_reg
)
342 fprintf (rtl_dump_file
, " crosses a call");
345 if (best_new_reg
== -1)
348 fprintf (rtl_dump_file
, "; no available registers\n");
352 do_replace (this, best_new_reg
);
353 tick
[best_new_reg
] = this_tick
++;
356 fprintf (rtl_dump_file
, ", renamed as %s\n", reg_names
[best_new_reg
]);
359 obstack_free (&rename_obstack
, first_obj
);
362 obstack_free (&rename_obstack
, NULL
);
365 fputc ('\n', rtl_dump_file
);
367 count_or_remove_death_notes (NULL
, 1);
368 update_life_info (NULL
, UPDATE_LIFE_LOCAL
,
369 PROP_REG_INFO
| PROP_DEATH_NOTES
);
373 do_replace (chain
, reg
)
374 struct du_chain
*chain
;
379 unsigned int regno
= ORIGINAL_REGNO (*chain
->loc
);
380 *chain
->loc
= gen_raw_REG (GET_MODE (*chain
->loc
), reg
);
381 if (regno
>= FIRST_PSEUDO_REGISTER
)
382 ORIGINAL_REGNO (*chain
->loc
) = regno
;
383 chain
= chain
->next_use
;
388 static struct du_chain
*open_chains
;
389 static struct du_chain
*closed_chains
;
392 scan_rtx_reg (insn
, loc
, class, action
, type
, earlyclobber
)
395 enum reg_class
class;
396 enum scan_actions action
;
402 enum machine_mode mode
= GET_MODE (x
);
403 int this_regno
= REGNO (x
);
404 int this_nregs
= HARD_REGNO_NREGS (this_regno
, mode
);
406 if (action
== mark_write
)
410 struct du_chain
*this = (struct du_chain
*)
411 obstack_alloc (&rename_obstack
, sizeof (struct du_chain
));
413 this->next_chain
= open_chains
;
417 this->need_caller_save_reg
= 0;
418 this->earlyclobber
= earlyclobber
;
424 if ((type
== OP_OUT
&& action
!= terminate_write
)
425 || (type
!= OP_OUT
&& action
== terminate_write
))
428 for (p
= &open_chains
; *p
;)
430 struct du_chain
*this = *p
;
432 /* Check if the chain has been terminated if it has then skip to
435 This can happen when we've already appended the location to
436 the chain in Step 3, but are trying to hide in-out operands
437 from terminate_write in Step 5. */
439 if (*this->loc
== cc0_rtx
)
440 p
= &this->next_chain
;
443 int regno
= REGNO (*this->loc
);
444 int nregs
= HARD_REGNO_NREGS (regno
, GET_MODE (*this->loc
));
445 int exact_match
= (regno
== this_regno
&& nregs
== this_nregs
);
447 if (regno
+ nregs
<= this_regno
448 || this_regno
+ this_nregs
<= regno
)
450 p
= &this->next_chain
;
454 if (action
== mark_read
)
459 /* ??? Class NO_REGS can happen if the md file makes use of
460 EXTRA_CONSTRAINTS to match registers. Which is arguably
461 wrong, but there we are. Since we know not what this may
462 be replaced with, terminate the chain. */
463 if (class != NO_REGS
)
465 this = (struct du_chain
*)
466 obstack_alloc (&rename_obstack
, sizeof (struct du_chain
));
468 this->next_chain
= (*p
)->next_chain
;
472 this->need_caller_save_reg
= 0;
480 if (action
!= terminate_overlapping_read
|| ! exact_match
)
482 struct du_chain
*next
= this->next_chain
;
484 /* Whether the terminated chain can be used for renaming
485 depends on the action and this being an exact match.
486 In either case, we remove this element from open_chains. */
488 if ((action
== terminate_dead
|| action
== terminate_write
)
491 this->next_chain
= closed_chains
;
492 closed_chains
= this;
494 fprintf (rtl_dump_file
,
495 "Closing chain %s at insn %d (%s)\n",
496 reg_names
[REGNO (*this->loc
)], INSN_UID (insn
),
497 scan_actions_name
[(int) action
]);
502 fprintf (rtl_dump_file
,
503 "Discarding chain %s at insn %d (%s)\n",
504 reg_names
[REGNO (*this->loc
)], INSN_UID (insn
),
505 scan_actions_name
[(int) action
]);
510 p
= &this->next_chain
;
515 /* Adapted from find_reloads_address_1. CLASS is INDEX_REG_CLASS or
516 BASE_REG_CLASS depending on how the register is being considered. */
519 scan_rtx_address (insn
, loc
, class, action
, mode
)
522 enum reg_class
class;
523 enum scan_actions action
;
524 enum machine_mode mode
;
527 RTX_CODE code
= GET_CODE (x
);
531 if (action
== mark_write
)
538 rtx orig_op0
= XEXP (x
, 0);
539 rtx orig_op1
= XEXP (x
, 1);
540 RTX_CODE code0
= GET_CODE (orig_op0
);
541 RTX_CODE code1
= GET_CODE (orig_op1
);
547 if (GET_CODE (op0
) == SUBREG
)
549 op0
= SUBREG_REG (op0
);
550 code0
= GET_CODE (op0
);
553 if (GET_CODE (op1
) == SUBREG
)
555 op1
= SUBREG_REG (op1
);
556 code1
= GET_CODE (op1
);
559 if (code0
== MULT
|| code0
== SIGN_EXTEND
|| code0
== TRUNCATE
560 || code0
== ZERO_EXTEND
|| code1
== MEM
)
565 else if (code1
== MULT
|| code1
== SIGN_EXTEND
|| code1
== TRUNCATE
566 || code1
== ZERO_EXTEND
|| code0
== MEM
)
571 else if (code0
== CONST_INT
|| code0
== CONST
572 || code0
== SYMBOL_REF
|| code0
== LABEL_REF
)
574 else if (code1
== CONST_INT
|| code1
== CONST
575 || code1
== SYMBOL_REF
|| code1
== LABEL_REF
)
577 else if (code0
== REG
&& code1
== REG
)
581 if (REG_OK_FOR_INDEX_P (op0
)
582 && REG_MODE_OK_FOR_BASE_P (op1
, mode
))
584 else if (REG_OK_FOR_INDEX_P (op1
)
585 && REG_MODE_OK_FOR_BASE_P (op0
, mode
))
587 else if (REG_MODE_OK_FOR_BASE_P (op1
, mode
))
589 else if (REG_MODE_OK_FOR_BASE_P (op0
, mode
))
591 else if (REG_OK_FOR_INDEX_P (op1
))
596 locI
= &XEXP (x
, index_op
);
597 locB
= &XEXP (x
, !index_op
);
599 else if (code0
== REG
)
604 else if (code1
== REG
)
611 scan_rtx_address (insn
, locI
, INDEX_REG_CLASS
, action
, mode
);
613 scan_rtx_address (insn
, locB
, MODE_BASE_REG_CLASS (mode
), action
, mode
);
624 /* If the target doesn't claim to handle autoinc, this must be
625 something special, like a stack push. Kill this chain. */
626 action
= terminate_all_read
;
631 scan_rtx_address (insn
, &XEXP (x
, 0),
632 MODE_BASE_REG_CLASS (GET_MODE (x
)), action
,
637 scan_rtx_reg (insn
, loc
, class, action
, OP_IN
, 0);
644 fmt
= GET_RTX_FORMAT (code
);
645 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
648 scan_rtx_address (insn
, &XEXP (x
, i
), class, action
, mode
);
649 else if (fmt
[i
] == 'E')
650 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
651 scan_rtx_address (insn
, &XVECEXP (x
, i
, j
), class, action
, mode
);
656 scan_rtx (insn
, loc
, class, action
, type
, earlyclobber
)
659 enum reg_class
class;
660 enum scan_actions action
;
666 enum rtx_code code
= GET_CODE (x
);
683 scan_rtx_reg (insn
, loc
, class, action
, type
, earlyclobber
);
687 scan_rtx_address (insn
, &XEXP (x
, 0),
688 MODE_BASE_REG_CLASS (GET_MODE (x
)), action
,
693 scan_rtx (insn
, &SET_SRC (x
), class, action
, OP_IN
, 0);
694 scan_rtx (insn
, &SET_DEST (x
), class, action
, OP_OUT
, 0);
697 case STRICT_LOW_PART
:
698 scan_rtx (insn
, &XEXP (x
, 0), class, action
, OP_INOUT
, earlyclobber
);
703 scan_rtx (insn
, &XEXP (x
, 0), class, action
,
704 type
== OP_IN
? OP_IN
: OP_INOUT
, earlyclobber
);
705 scan_rtx (insn
, &XEXP (x
, 1), class, action
, OP_IN
, 0);
706 scan_rtx (insn
, &XEXP (x
, 2), class, action
, OP_IN
, 0);
715 /* Should only happen inside MEM. */
719 scan_rtx (insn
, &SET_DEST (x
), class, action
, OP_OUT
, 1);
723 scan_rtx (insn
, &XEXP (x
, 0), class, action
, type
, 0);
725 scan_rtx (insn
, &XEXP (x
, 1), class, action
, type
, 0);
732 fmt
= GET_RTX_FORMAT (code
);
733 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
736 scan_rtx (insn
, &XEXP (x
, i
), class, action
, type
, 0);
737 else if (fmt
[i
] == 'E')
738 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
739 scan_rtx (insn
, &XVECEXP (x
, i
, j
), class, action
, type
, 0);
743 /* Build def/use chain. */
745 static struct du_chain
*
751 open_chains
= closed_chains
= NULL
;
753 for (insn
= bb
->head
; ; insn
= NEXT_INSN (insn
))
759 rtx old_operands
[MAX_RECOG_OPERANDS
];
760 rtx old_dups
[MAX_DUP_OPERANDS
];
765 /* Process the insn, determining its effect on the def-use
766 chains. We perform the following steps with the register
767 references in the insn:
768 (1) Any read that overlaps an open chain, but doesn't exactly
769 match, causes that chain to be closed. We can't deal
771 (2) Any read outside an operand causes any chain it overlaps
772 with to be closed, since we can't replace it.
773 (3) Any read inside an operand is added if there's already
774 an open chain for it.
775 (4) For any REG_DEAD note we find, close open chains that
777 (5) For any write we find, close open chains that overlap it.
778 (6) For any write we find in an operand, make a new chain.
779 (7) For any REG_UNUSED, close any chains we just opened. */
781 icode
= recog_memoized (insn
);
783 if (! constrain_operands (1))
784 fatal_insn_not_found (insn
);
785 preprocess_constraints ();
786 alt
= which_alternative
;
787 n_ops
= recog_data
.n_operands
;
789 /* Simplify the code below by rewriting things to reflect
790 matching constraints. Also promote OP_OUT to OP_INOUT
791 in predicated instructions. */
793 predicated
= GET_CODE (PATTERN (insn
)) == COND_EXEC
;
794 for (i
= 0; i
< n_ops
; ++i
)
796 int matches
= recog_op_alt
[i
][alt
].matches
;
798 recog_op_alt
[i
][alt
].class = recog_op_alt
[matches
][alt
].class;
799 if (matches
>= 0 || recog_op_alt
[i
][alt
].matched
>= 0
800 || (predicated
&& recog_data
.operand_type
[i
] == OP_OUT
))
801 recog_data
.operand_type
[i
] = OP_INOUT
;
804 /* Step 1: Close chains for which we have overlapping reads. */
805 for (i
= 0; i
< n_ops
; i
++)
806 scan_rtx (insn
, recog_data
.operand_loc
[i
],
807 NO_REGS
, terminate_overlapping_read
,
808 recog_data
.operand_type
[i
], 0);
810 /* Step 2: Close chains for which we have reads outside operands.
811 We do this by munging all operands into CC0, and closing
812 everything remaining. */
814 for (i
= 0; i
< n_ops
; i
++)
816 old_operands
[i
] = recog_data
.operand
[i
];
817 /* Don't squash match_operator or match_parallel here, since
818 we don't know that all of the contained registers are
819 reachable by proper operands. */
820 if (recog_data
.constraints
[i
][0] == '\0')
822 *recog_data
.operand_loc
[i
] = cc0_rtx
;
824 for (i
= 0; i
< recog_data
.n_dups
; i
++)
826 int dup_num
= recog_data
.dup_num
[i
];
828 old_dups
[i
] = *recog_data
.dup_loc
[i
];
829 *recog_data
.dup_loc
[i
] = cc0_rtx
;
831 /* For match_dup of match_operator or match_parallel, share
832 them, so that we don't miss changes in the dup. */
834 && insn_data
[icode
].operand
[dup_num
].eliminable
== 0)
835 old_dups
[i
] = recog_data
.operand
[dup_num
];
838 scan_rtx (insn
, &PATTERN (insn
), NO_REGS
, terminate_all_read
,
841 for (i
= 0; i
< recog_data
.n_dups
; i
++)
842 *recog_data
.dup_loc
[i
] = old_dups
[i
];
843 for (i
= 0; i
< n_ops
; i
++)
844 *recog_data
.operand_loc
[i
] = old_operands
[i
];
846 /* Step 2B: Can't rename function call argument registers. */
847 if (GET_CODE (insn
) == CALL_INSN
&& CALL_INSN_FUNCTION_USAGE (insn
))
848 scan_rtx (insn
, &CALL_INSN_FUNCTION_USAGE (insn
),
849 NO_REGS
, terminate_all_read
, OP_IN
, 0);
851 /* Step 2C: Can't rename asm operands that were originally
853 if (asm_noperands (PATTERN (insn
)) > 0)
854 for (i
= 0; i
< n_ops
; i
++)
856 rtx
*loc
= recog_data
.operand_loc
[i
];
859 if (GET_CODE (op
) == REG
860 && REGNO (op
) == ORIGINAL_REGNO (op
)
861 && (recog_data
.operand_type
[i
] == OP_IN
862 || recog_data
.operand_type
[i
] == OP_INOUT
))
863 scan_rtx (insn
, loc
, NO_REGS
, terminate_all_read
, OP_IN
, 0);
866 /* Step 3: Append to chains for reads inside operands. */
867 for (i
= 0; i
< n_ops
+ recog_data
.n_dups
; i
++)
869 int opn
= i
< n_ops
? i
: recog_data
.dup_num
[i
- n_ops
];
870 rtx
*loc
= (i
< n_ops
871 ? recog_data
.operand_loc
[opn
]
872 : recog_data
.dup_loc
[i
- n_ops
]);
873 enum reg_class
class = recog_op_alt
[opn
][alt
].class;
874 enum op_type type
= recog_data
.operand_type
[opn
];
876 /* Don't scan match_operand here, since we've no reg class
877 information to pass down. Any operands that we could
878 substitute in will be represented elsewhere. */
879 if (recog_data
.constraints
[opn
][0] == '\0')
882 if (recog_op_alt
[opn
][alt
].is_address
)
883 scan_rtx_address (insn
, loc
, class, mark_read
, VOIDmode
);
885 scan_rtx (insn
, loc
, class, mark_read
, type
, 0);
888 /* Step 4: Close chains for registers that die here.
889 Also record updates for REG_INC notes. */
890 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
892 if (REG_NOTE_KIND (note
) == REG_DEAD
)
893 scan_rtx (insn
, &XEXP (note
, 0), NO_REGS
, terminate_dead
,
895 else if (REG_NOTE_KIND (note
) == REG_INC
)
896 scan_rtx (insn
, &XEXP (note
, 0), ALL_REGS
, mark_read
,
900 /* Step 4B: If this is a call, any chain live at this point
901 requires a caller-saved reg. */
902 if (GET_CODE (insn
) == CALL_INSN
)
905 for (p
= open_chains
; p
; p
= p
->next_chain
)
906 p
->need_caller_save_reg
= 1;
909 /* Step 5: Close open chains that overlap writes. Similar to
910 step 2, we hide in-out operands, since we do not want to
911 close these chains. */
913 for (i
= 0; i
< n_ops
; i
++)
915 old_operands
[i
] = recog_data
.operand
[i
];
916 if (recog_data
.operand_type
[i
] == OP_INOUT
)
917 *recog_data
.operand_loc
[i
] = cc0_rtx
;
919 for (i
= 0; i
< recog_data
.n_dups
; i
++)
921 int opn
= recog_data
.dup_num
[i
];
922 old_dups
[i
] = *recog_data
.dup_loc
[i
];
923 if (recog_data
.operand_type
[opn
] == OP_INOUT
)
924 *recog_data
.dup_loc
[i
] = cc0_rtx
;
927 scan_rtx (insn
, &PATTERN (insn
), NO_REGS
, terminate_write
, OP_IN
, 0);
929 for (i
= 0; i
< recog_data
.n_dups
; i
++)
930 *recog_data
.dup_loc
[i
] = old_dups
[i
];
931 for (i
= 0; i
< n_ops
; i
++)
932 *recog_data
.operand_loc
[i
] = old_operands
[i
];
934 /* Step 6: Begin new chains for writes inside operands. */
935 /* ??? Many targets have output constraints on the SET_DEST
936 of a call insn, which is stupid, since these are certainly
937 ABI defined hard registers. Don't change calls at all.
938 Similarly take special care for asm statement that originally
939 referenced hard registers. */
940 if (asm_noperands (PATTERN (insn
)) > 0)
942 for (i
= 0; i
< n_ops
; i
++)
943 if (recog_data
.operand_type
[i
] == OP_OUT
)
945 rtx
*loc
= recog_data
.operand_loc
[i
];
947 enum reg_class
class = recog_op_alt
[i
][alt
].class;
949 if (GET_CODE (op
) == REG
950 && REGNO (op
) == ORIGINAL_REGNO (op
))
953 scan_rtx (insn
, loc
, class, mark_write
, OP_OUT
,
954 recog_op_alt
[i
][alt
].earlyclobber
);
957 else if (GET_CODE (insn
) != CALL_INSN
)
958 for (i
= 0; i
< n_ops
+ recog_data
.n_dups
; i
++)
960 int opn
= i
< n_ops
? i
: recog_data
.dup_num
[i
- n_ops
];
961 rtx
*loc
= (i
< n_ops
962 ? recog_data
.operand_loc
[opn
]
963 : recog_data
.dup_loc
[i
- n_ops
]);
964 enum reg_class
class = recog_op_alt
[opn
][alt
].class;
966 if (recog_data
.operand_type
[opn
] == OP_OUT
)
967 scan_rtx (insn
, loc
, class, mark_write
, OP_OUT
,
968 recog_op_alt
[opn
][alt
].earlyclobber
);
971 /* Step 7: Close chains for registers that were never
973 for (note
= REG_NOTES (insn
); note
; note
= XEXP (note
, 1))
974 if (REG_NOTE_KIND (note
) == REG_UNUSED
)
975 scan_rtx (insn
, &XEXP (note
, 0), NO_REGS
, terminate_dead
,
982 /* Since we close every chain when we find a REG_DEAD note, anything that
983 is still open lives past the basic block, so it can't be renamed. */
984 return closed_chains
;
987 /* Dump all def/use chains in CHAINS to RTL_DUMP_FILE. They are
988 printed in reverse order as that's how we build them. */
991 dump_def_use_chain (chains
)
992 struct du_chain
*chains
;
996 struct du_chain
*this = chains
;
997 int r
= REGNO (*this->loc
);
998 int nregs
= HARD_REGNO_NREGS (r
, GET_MODE (*this->loc
));
999 fprintf (rtl_dump_file
, "Register %s (%d):", reg_names
[r
], nregs
);
1002 fprintf (rtl_dump_file
, " %d [%s]", INSN_UID (this->insn
),
1003 reg_class_names
[this->class]);
1004 this = this->next_use
;
1006 fprintf (rtl_dump_file
, "\n");
1007 chains
= chains
->next_chain
;
1011 /* The following code does forward propagation of hard register copies.
1012 The object is to eliminate as many dependencies as possible, so that
1013 we have the most scheduling freedom. As a side effect, we also clean
1014 up some silly register allocation decisions made by reload. This
1015 code may be obsoleted by a new register allocator. */
1017 /* For each register, we have a list of registers that contain the same
1018 value. The OLDEST_REGNO field points to the head of the list, and
1019 the NEXT_REGNO field runs through the list. The MODE field indicates
1020 what mode the data is known to be in; this field is VOIDmode when the
1021 register is not known to contain valid data. */
1023 struct value_data_entry
1025 enum machine_mode mode
;
1026 unsigned int oldest_regno
;
1027 unsigned int next_regno
;
1032 struct value_data_entry e
[FIRST_PSEUDO_REGISTER
];
1033 unsigned int max_value_regs
;
1036 static void kill_value_regno
PARAMS ((unsigned, struct value_data
*));
1037 static void kill_value
PARAMS ((rtx
, struct value_data
*));
1038 static void set_value_regno
PARAMS ((unsigned, enum machine_mode
,
1039 struct value_data
*));
1040 static void init_value_data
PARAMS ((struct value_data
*));
1041 static void kill_clobbered_value
PARAMS ((rtx
, rtx
, void *));
1042 static void kill_set_value
PARAMS ((rtx
, rtx
, void *));
1043 static int kill_autoinc_value
PARAMS ((rtx
*, void *));
1044 static void copy_value
PARAMS ((rtx
, rtx
, struct value_data
*));
1045 static bool mode_change_ok
PARAMS ((enum machine_mode
, enum machine_mode
,
1047 static rtx maybe_mode_change
PARAMS ((enum machine_mode
, enum machine_mode
,
1048 enum machine_mode
, unsigned int,
1050 static rtx find_oldest_value_reg
PARAMS ((enum reg_class
, rtx
,
1051 struct value_data
*));
1052 static bool replace_oldest_value_reg
PARAMS ((rtx
*, enum reg_class
, rtx
,
1053 struct value_data
*));
1054 static bool replace_oldest_value_addr
PARAMS ((rtx
*, enum reg_class
,
1055 enum machine_mode
, rtx
,
1056 struct value_data
*));
1057 static bool replace_oldest_value_mem
PARAMS ((rtx
, rtx
, struct value_data
*));
1058 static bool copyprop_hardreg_forward_1
PARAMS ((basic_block
,
1059 struct value_data
*));
1060 extern void debug_value_data
PARAMS ((struct value_data
*));
1061 #ifdef ENABLE_CHECKING
1062 static void validate_value_data
PARAMS ((struct value_data
*));
1065 /* Kill register REGNO. This involves removing it from any value lists,
1066 and resetting the value mode to VOIDmode. */
1069 kill_value_regno (regno
, vd
)
1071 struct value_data
*vd
;
1073 unsigned int i
, next
;
1075 if (vd
->e
[regno
].oldest_regno
!= regno
)
1077 for (i
= vd
->e
[regno
].oldest_regno
;
1078 vd
->e
[i
].next_regno
!= regno
;
1079 i
= vd
->e
[i
].next_regno
)
1081 vd
->e
[i
].next_regno
= vd
->e
[regno
].next_regno
;
1083 else if ((next
= vd
->e
[regno
].next_regno
) != INVALID_REGNUM
)
1085 for (i
= next
; i
!= INVALID_REGNUM
; i
= vd
->e
[i
].next_regno
)
1086 vd
->e
[i
].oldest_regno
= next
;
1089 vd
->e
[regno
].mode
= VOIDmode
;
1090 vd
->e
[regno
].oldest_regno
= regno
;
1091 vd
->e
[regno
].next_regno
= INVALID_REGNUM
;
1093 #ifdef ENABLE_CHECKING
1094 validate_value_data (vd
);
1098 /* Kill X. This is a convenience function for kill_value_regno
1099 so that we mind the mode the register is in. */
1104 struct value_data
*vd
;
1106 /* SUBREGS are supposed to have been eliminated by now. But some
1107 ports, e.g. i386 sse, use them to smuggle vector type information
1108 through to instruction selection. Each such SUBREG should simplify,
1109 so if we get a NULL we've done something wrong elsewhere. */
1111 if (GET_CODE (x
) == SUBREG
)
1112 x
= simplify_subreg (GET_MODE (x
), SUBREG_REG (x
),
1113 GET_MODE (SUBREG_REG (x
)), SUBREG_BYTE (x
));
1116 unsigned int regno
= REGNO (x
);
1117 unsigned int n
= HARD_REGNO_NREGS (regno
, GET_MODE (x
));
1120 /* Kill the value we're told to kill. */
1121 for (i
= 0; i
< n
; ++i
)
1122 kill_value_regno (regno
+ i
, vd
);
1124 /* Kill everything that overlapped what we're told to kill. */
1125 if (regno
< vd
->max_value_regs
)
1128 j
= regno
- vd
->max_value_regs
;
1129 for (; j
< regno
; ++j
)
1131 if (vd
->e
[j
].mode
== VOIDmode
)
1133 n
= HARD_REGNO_NREGS (j
, vd
->e
[j
].mode
);
1135 for (i
= 0; i
< n
; ++i
)
1136 kill_value_regno (j
+ i
, vd
);
1141 /* Remember that REGNO is valid in MODE. */
1144 set_value_regno (regno
, mode
, vd
)
1146 enum machine_mode mode
;
1147 struct value_data
*vd
;
1151 vd
->e
[regno
].mode
= mode
;
1153 nregs
= HARD_REGNO_NREGS (regno
, mode
);
1154 if (nregs
> vd
->max_value_regs
)
1155 vd
->max_value_regs
= nregs
;
1158 /* Initialize VD such that there are no known relationships between regs. */
1161 init_value_data (vd
)
1162 struct value_data
*vd
;
1165 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
1167 vd
->e
[i
].mode
= VOIDmode
;
1168 vd
->e
[i
].oldest_regno
= i
;
1169 vd
->e
[i
].next_regno
= INVALID_REGNUM
;
1171 vd
->max_value_regs
= 0;
1174 /* Called through note_stores. If X is clobbered, kill its value. */
1177 kill_clobbered_value (x
, set
, data
)
1182 struct value_data
*vd
= data
;
1183 if (GET_CODE (set
) == CLOBBER
)
1187 /* Called through note_stores. If X is set, not clobbered, kill its
1188 current value and install it as the root of its own value list. */
1191 kill_set_value (x
, set
, data
)
1196 struct value_data
*vd
= data
;
1197 if (GET_CODE (set
) != CLOBBER
)
1201 set_value_regno (REGNO (x
), GET_MODE (x
), vd
);
1205 /* Called through for_each_rtx. Kill any register used as the base of an
1206 auto-increment expression, and install that register as the root of its
1210 kill_autoinc_value (px
, data
)
1215 struct value_data
*vd
= data
;
1217 if (GET_RTX_CLASS (GET_CODE (x
)) == 'a')
1221 set_value_regno (REGNO (x
), Pmode
, vd
);
1228 /* Assert that SRC has been copied to DEST. Adjust the data structures
1229 to reflect that SRC contains an older copy of the shared value. */
1232 copy_value (dest
, src
, vd
)
1235 struct value_data
*vd
;
1237 unsigned int dr
= REGNO (dest
);
1238 unsigned int sr
= REGNO (src
);
1239 unsigned int dn
, sn
;
1242 /* ??? At present, it's possible to see noop sets. It'd be nice if
1243 this were cleaned up beforehand... */
1247 /* Do not propagate copies to the stack pointer, as that can leave
1248 memory accesses with no scheduling dependency on the stack update. */
1249 if (dr
== STACK_POINTER_REGNUM
)
1252 /* Likewise with the frame pointer, if we're using one. */
1253 if (frame_pointer_needed
&& dr
== HARD_FRAME_POINTER_REGNUM
)
1256 /* If SRC and DEST overlap, don't record anything. */
1257 dn
= HARD_REGNO_NREGS (dr
, GET_MODE (dest
));
1258 sn
= HARD_REGNO_NREGS (sr
, GET_MODE (dest
));
1259 if ((dr
> sr
&& dr
< sr
+ sn
)
1260 || (sr
> dr
&& sr
< dr
+ dn
))
1263 /* If SRC had no assigned mode (i.e. we didn't know it was live)
1264 assign it now and assume the value came from an input argument
1266 if (vd
->e
[sr
].mode
== VOIDmode
)
1267 set_value_regno (sr
, vd
->e
[dr
].mode
, vd
);
1269 /* If we are narrowing the input to a smaller number of hard regs,
1270 and it is in big endian, we are really extracting a high part.
1271 Since we generally associate a low part of a value with the value itself,
1272 we must not do the same for the high part.
1273 Note we can still get low parts for the same mode combination through
1274 a two-step copy involving differently sized hard regs.
1275 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
1276 (set (reg:DI r0) (reg:DI fr0))
1277 (set (reg:SI fr2) (reg:SI r0))
1278 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
1279 (set (reg:SI fr2) (reg:SI fr0))
1280 loads the high part of (reg:DI fr0) into fr2.
1282 We can't properly represent the latter case in our tables, so don't
1283 record anything then. */
1284 else if (sn
< (unsigned int) HARD_REGNO_NREGS (sr
, vd
->e
[sr
].mode
)
1285 && (GET_MODE_SIZE (vd
->e
[sr
].mode
) > UNITS_PER_WORD
1286 ? WORDS_BIG_ENDIAN
: BYTES_BIG_ENDIAN
))
1289 /* If SRC had been assigned a mode narrower than the copy, we can't
1290 link DEST into the chain, because not all of the pieces of the
1291 copy came from oldest_regno. */
1292 else if (sn
> (unsigned int) HARD_REGNO_NREGS (sr
, vd
->e
[sr
].mode
))
1295 /* Link DR at the end of the value chain used by SR. */
1297 vd
->e
[dr
].oldest_regno
= vd
->e
[sr
].oldest_regno
;
1299 for (i
= sr
; vd
->e
[i
].next_regno
!= INVALID_REGNUM
; i
= vd
->e
[i
].next_regno
)
1301 vd
->e
[i
].next_regno
= dr
;
1303 #ifdef ENABLE_CHECKING
1304 validate_value_data (vd
);
1308 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
1311 mode_change_ok (orig_mode
, new_mode
, regno
)
1312 enum machine_mode orig_mode
, new_mode
;
1313 unsigned int regno ATTRIBUTE_UNUSED
;
1315 if (GET_MODE_SIZE (orig_mode
) < GET_MODE_SIZE (new_mode
))
1318 #ifdef CANNOT_CHANGE_MODE_CLASS
1319 return !REG_CANNOT_CHANGE_MODE_P (regno
, orig_mode
, new_mode
);
1325 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
1326 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
1328 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
1331 maybe_mode_change (orig_mode
, copy_mode
, new_mode
, regno
, copy_regno
)
1332 enum machine_mode orig_mode
, copy_mode
, new_mode
;
1333 unsigned int regno
, copy_regno ATTRIBUTE_UNUSED
;
1335 if (orig_mode
== new_mode
)
1336 return gen_rtx_raw_REG (new_mode
, regno
);
1337 else if (mode_change_ok (orig_mode
, new_mode
, regno
))
1339 int copy_nregs
= HARD_REGNO_NREGS (copy_regno
, copy_mode
);
1340 int use_nregs
= HARD_REGNO_NREGS (copy_regno
, new_mode
);
1342 = GET_MODE_SIZE (copy_mode
) / copy_nregs
* (copy_nregs
- use_nregs
);
1344 = GET_MODE_SIZE (orig_mode
) - GET_MODE_SIZE (new_mode
) - copy_offset
;
1345 int byteoffset
= offset
% UNITS_PER_WORD
;
1346 int wordoffset
= offset
- byteoffset
;
1348 offset
= ((WORDS_BIG_ENDIAN
? wordoffset
: 0)
1349 + (BYTES_BIG_ENDIAN
? byteoffset
: 0));
1350 return gen_rtx_raw_REG (new_mode
,
1351 regno
+ subreg_regno_offset (regno
, orig_mode
,
1358 /* Find the oldest copy of the value contained in REGNO that is in
1359 register class CLASS and has mode MODE. If found, return an rtx
1360 of that oldest register, otherwise return NULL. */
1363 find_oldest_value_reg (class, reg
, vd
)
1364 enum reg_class
class;
1366 struct value_data
*vd
;
1368 unsigned int regno
= REGNO (reg
);
1369 enum machine_mode mode
= GET_MODE (reg
);
1372 /* If we are accessing REG in some mode other that what we set it in,
1373 make sure that the replacement is valid. In particular, consider
1374 (set (reg:DI r11) (...))
1375 (set (reg:SI r9) (reg:SI r11))
1376 (set (reg:SI r10) (...))
1377 (set (...) (reg:DI r9))
1378 Replacing r9 with r11 is invalid. */
1379 if (mode
!= vd
->e
[regno
].mode
)
1381 if (HARD_REGNO_NREGS (regno
, mode
)
1382 > HARD_REGNO_NREGS (regno
, vd
->e
[regno
].mode
))
1386 for (i
= vd
->e
[regno
].oldest_regno
; i
!= regno
; i
= vd
->e
[i
].next_regno
)
1388 enum machine_mode oldmode
= vd
->e
[i
].mode
;
1391 if (TEST_HARD_REG_BIT (reg_class_contents
[class], i
)
1392 && (new = maybe_mode_change (oldmode
, vd
->e
[regno
].mode
, mode
, i
,
1395 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (reg
);
1403 /* If possible, replace the register at *LOC with the oldest register
1404 in register class CLASS. Return true if successfully replaced. */
1407 replace_oldest_value_reg (loc
, class, insn
, vd
)
1409 enum reg_class
class;
1411 struct value_data
*vd
;
1413 rtx
new = find_oldest_value_reg (class, *loc
, vd
);
1417 fprintf (rtl_dump_file
, "insn %u: replaced reg %u with %u\n",
1418 INSN_UID (insn
), REGNO (*loc
), REGNO (new));
1426 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1427 Adapted from find_reloads_address_1. CLASS is INDEX_REG_CLASS or
1428 BASE_REG_CLASS depending on how the register is being considered. */
1431 replace_oldest_value_addr (loc
, class, mode
, insn
, vd
)
1433 enum reg_class
class;
1434 enum machine_mode mode
;
1436 struct value_data
*vd
;
1439 RTX_CODE code
= GET_CODE (x
);
1442 bool changed
= false;
1448 rtx orig_op0
= XEXP (x
, 0);
1449 rtx orig_op1
= XEXP (x
, 1);
1450 RTX_CODE code0
= GET_CODE (orig_op0
);
1451 RTX_CODE code1
= GET_CODE (orig_op1
);
1457 if (GET_CODE (op0
) == SUBREG
)
1459 op0
= SUBREG_REG (op0
);
1460 code0
= GET_CODE (op0
);
1463 if (GET_CODE (op1
) == SUBREG
)
1465 op1
= SUBREG_REG (op1
);
1466 code1
= GET_CODE (op1
);
1469 if (code0
== MULT
|| code0
== SIGN_EXTEND
|| code0
== TRUNCATE
1470 || code0
== ZERO_EXTEND
|| code1
== MEM
)
1472 locI
= &XEXP (x
, 0);
1473 locB
= &XEXP (x
, 1);
1475 else if (code1
== MULT
|| code1
== SIGN_EXTEND
|| code1
== TRUNCATE
1476 || code1
== ZERO_EXTEND
|| code0
== MEM
)
1478 locI
= &XEXP (x
, 1);
1479 locB
= &XEXP (x
, 0);
1481 else if (code0
== CONST_INT
|| code0
== CONST
1482 || code0
== SYMBOL_REF
|| code0
== LABEL_REF
)
1483 locB
= &XEXP (x
, 1);
1484 else if (code1
== CONST_INT
|| code1
== CONST
1485 || code1
== SYMBOL_REF
|| code1
== LABEL_REF
)
1486 locB
= &XEXP (x
, 0);
1487 else if (code0
== REG
&& code1
== REG
)
1491 if (REG_OK_FOR_INDEX_P (op0
)
1492 && REG_MODE_OK_FOR_BASE_P (op1
, mode
))
1494 else if (REG_OK_FOR_INDEX_P (op1
)
1495 && REG_MODE_OK_FOR_BASE_P (op0
, mode
))
1497 else if (REG_MODE_OK_FOR_BASE_P (op1
, mode
))
1499 else if (REG_MODE_OK_FOR_BASE_P (op0
, mode
))
1501 else if (REG_OK_FOR_INDEX_P (op1
))
1506 locI
= &XEXP (x
, index_op
);
1507 locB
= &XEXP (x
, !index_op
);
1509 else if (code0
== REG
)
1511 locI
= &XEXP (x
, 0);
1512 locB
= &XEXP (x
, 1);
1514 else if (code1
== REG
)
1516 locI
= &XEXP (x
, 1);
1517 locB
= &XEXP (x
, 0);
1521 changed
|= replace_oldest_value_addr (locI
, INDEX_REG_CLASS
, mode
,
1524 changed
|= replace_oldest_value_addr (locB
,
1525 MODE_BASE_REG_CLASS (mode
),
1539 return replace_oldest_value_mem (x
, insn
, vd
);
1542 return replace_oldest_value_reg (loc
, class, insn
, vd
);
1548 fmt
= GET_RTX_FORMAT (code
);
1549 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1552 changed
|= replace_oldest_value_addr (&XEXP (x
, i
), class, mode
,
1554 else if (fmt
[i
] == 'E')
1555 for (j
= XVECLEN (x
, i
) - 1; j
>= 0; j
--)
1556 changed
|= replace_oldest_value_addr (&XVECEXP (x
, i
, j
), class,
1563 /* Similar to replace_oldest_value_reg, but X contains a memory. */
1566 replace_oldest_value_mem (x
, insn
, vd
)
1569 struct value_data
*vd
;
1571 return replace_oldest_value_addr (&XEXP (x
, 0),
1572 MODE_BASE_REG_CLASS (GET_MODE (x
)),
1573 GET_MODE (x
), insn
, vd
);
1576 /* Perform the forward copy propagation on basic block BB. */
1579 copyprop_hardreg_forward_1 (bb
, vd
)
1581 struct value_data
*vd
;
1583 bool changed
= false;
1586 for (insn
= bb
->head
; ; insn
= NEXT_INSN (insn
))
1588 int n_ops
, i
, alt
, predicated
;
1592 if (! INSN_P (insn
))
1594 if (insn
== bb
->end
)
1600 set
= single_set (insn
);
1601 extract_insn (insn
);
1602 if (! constrain_operands (1))
1603 fatal_insn_not_found (insn
);
1604 preprocess_constraints ();
1605 alt
= which_alternative
;
1606 n_ops
= recog_data
.n_operands
;
1607 is_asm
= asm_noperands (PATTERN (insn
)) >= 0;
1609 /* Simplify the code below by rewriting things to reflect
1610 matching constraints. Also promote OP_OUT to OP_INOUT
1611 in predicated instructions. */
1613 predicated
= GET_CODE (PATTERN (insn
)) == COND_EXEC
;
1614 for (i
= 0; i
< n_ops
; ++i
)
1616 int matches
= recog_op_alt
[i
][alt
].matches
;
1618 recog_op_alt
[i
][alt
].class = recog_op_alt
[matches
][alt
].class;
1619 if (matches
>= 0 || recog_op_alt
[i
][alt
].matched
>= 0
1620 || (predicated
&& recog_data
.operand_type
[i
] == OP_OUT
))
1621 recog_data
.operand_type
[i
] = OP_INOUT
;
1624 /* For each earlyclobber operand, zap the value data. */
1625 for (i
= 0; i
< n_ops
; i
++)
1626 if (recog_op_alt
[i
][alt
].earlyclobber
)
1627 kill_value (recog_data
.operand
[i
], vd
);
1629 /* Within asms, a clobber cannot overlap inputs or outputs.
1630 I wouldn't think this were true for regular insns, but
1631 scan_rtx treats them like that... */
1632 note_stores (PATTERN (insn
), kill_clobbered_value
, vd
);
1634 /* Kill all auto-incremented values. */
1635 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
1636 for_each_rtx (&PATTERN (insn
), kill_autoinc_value
, vd
);
1638 /* Kill all early-clobbered operands. */
1639 for (i
= 0; i
< n_ops
; i
++)
1640 if (recog_op_alt
[i
][alt
].earlyclobber
)
1641 kill_value (recog_data
.operand
[i
], vd
);
1643 /* Special-case plain move instructions, since we may well
1644 be able to do the move from a different register class. */
1645 if (set
&& REG_P (SET_SRC (set
)))
1647 rtx src
= SET_SRC (set
);
1648 unsigned int regno
= REGNO (src
);
1649 enum machine_mode mode
= GET_MODE (src
);
1653 /* If we are accessing SRC in some mode other that what we
1654 set it in, make sure that the replacement is valid. */
1655 if (mode
!= vd
->e
[regno
].mode
)
1657 if (HARD_REGNO_NREGS (regno
, mode
)
1658 > HARD_REGNO_NREGS (regno
, vd
->e
[regno
].mode
))
1659 goto no_move_special_case
;
1662 /* If the destination is also a register, try to find a source
1663 register in the same class. */
1664 if (REG_P (SET_DEST (set
)))
1666 new = find_oldest_value_reg (REGNO_REG_CLASS (regno
), src
, vd
);
1667 if (new && validate_change (insn
, &SET_SRC (set
), new, 0))
1670 fprintf (rtl_dump_file
,
1671 "insn %u: replaced reg %u with %u\n",
1672 INSN_UID (insn
), regno
, REGNO (new));
1674 goto did_replacement
;
1678 /* Otherwise, try all valid registers and see if its valid. */
1679 for (i
= vd
->e
[regno
].oldest_regno
; i
!= regno
;
1680 i
= vd
->e
[i
].next_regno
)
1682 new = maybe_mode_change (vd
->e
[i
].mode
, vd
->e
[regno
].mode
,
1684 if (new != NULL_RTX
)
1686 if (validate_change (insn
, &SET_SRC (set
), new, 0))
1688 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (src
);
1690 fprintf (rtl_dump_file
,
1691 "insn %u: replaced reg %u with %u\n",
1692 INSN_UID (insn
), regno
, REGNO (new));
1694 goto did_replacement
;
1699 no_move_special_case
:
1701 /* For each input operand, replace a hard register with the
1702 eldest live copy that's in an appropriate register class. */
1703 for (i
= 0; i
< n_ops
; i
++)
1705 bool replaced
= false;
1707 /* Don't scan match_operand here, since we've no reg class
1708 information to pass down. Any operands that we could
1709 substitute in will be represented elsewhere. */
1710 if (recog_data
.constraints
[i
][0] == '\0')
1713 /* Don't replace in asms intentionally referencing hard regs. */
1714 if (is_asm
&& GET_CODE (recog_data
.operand
[i
]) == REG
1715 && (REGNO (recog_data
.operand
[i
])
1716 == ORIGINAL_REGNO (recog_data
.operand
[i
])))
1719 if (recog_data
.operand_type
[i
] == OP_IN
)
1721 if (recog_op_alt
[i
][alt
].is_address
)
1723 = replace_oldest_value_addr (recog_data
.operand_loc
[i
],
1724 recog_op_alt
[i
][alt
].class,
1725 VOIDmode
, insn
, vd
);
1726 else if (REG_P (recog_data
.operand
[i
]))
1728 = replace_oldest_value_reg (recog_data
.operand_loc
[i
],
1729 recog_op_alt
[i
][alt
].class,
1731 else if (GET_CODE (recog_data
.operand
[i
]) == MEM
)
1732 replaced
= replace_oldest_value_mem (recog_data
.operand
[i
],
1735 else if (GET_CODE (recog_data
.operand
[i
]) == MEM
)
1736 replaced
= replace_oldest_value_mem (recog_data
.operand
[i
],
1739 /* If we performed any replacement, update match_dups. */
1747 new = *recog_data
.operand_loc
[i
];
1748 recog_data
.operand
[i
] = new;
1749 for (j
= 0; j
< recog_data
.n_dups
; j
++)
1750 if (recog_data
.dup_num
[j
] == i
)
1751 *recog_data
.dup_loc
[j
] = new;
1756 /* Clobber call-clobbered registers. */
1757 if (GET_CODE (insn
) == CALL_INSN
)
1758 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
1759 if (TEST_HARD_REG_BIT (regs_invalidated_by_call
, i
))
1760 kill_value_regno (i
, vd
);
1762 /* Notice stores. */
1763 note_stores (PATTERN (insn
), kill_set_value
, vd
);
1765 /* Notice copies. */
1766 if (set
&& REG_P (SET_DEST (set
)) && REG_P (SET_SRC (set
)))
1767 copy_value (SET_DEST (set
), SET_SRC (set
), vd
);
1769 if (insn
== bb
->end
)
1776 /* Main entry point for the forward copy propagation optimization. */
1779 copyprop_hardreg_forward ()
1781 struct value_data
*all_vd
;
1783 basic_block bb
, bbp
= 0;
1785 need_refresh
= false;
1787 all_vd
= xmalloc (sizeof (struct value_data
) * last_basic_block
);
1791 /* If a block has a single predecessor, that we've already
1792 processed, begin with the value data that was live at
1793 the end of the predecessor block. */
1794 /* ??? Ought to use more intelligent queueing of blocks. */
1796 for (bbp
= bb
; bbp
&& bbp
!= bb
->pred
->src
; bbp
= bbp
->prev_bb
);
1798 && ! bb
->pred
->pred_next
1799 && ! (bb
->pred
->flags
& (EDGE_ABNORMAL_CALL
| EDGE_EH
))
1800 && bb
->pred
->src
!= ENTRY_BLOCK_PTR
1802 all_vd
[bb
->index
] = all_vd
[bb
->pred
->src
->index
];
1804 init_value_data (all_vd
+ bb
->index
);
1806 if (copyprop_hardreg_forward_1 (bb
, all_vd
+ bb
->index
))
1807 need_refresh
= true;
1813 fputs ("\n\n", rtl_dump_file
);
1815 /* ??? Irritatingly, delete_noop_moves does not take a set of blocks
1816 to scan, so we have to do a life update with no initial set of
1817 blocks Just In Case. */
1818 delete_noop_moves (get_insns ());
1819 update_life_info (NULL
, UPDATE_LIFE_GLOBAL_RM_NOTES
,
1821 | PROP_SCAN_DEAD_CODE
1822 | PROP_KILL_DEAD_CODE
);
1828 /* Dump the value chain data to stderr. */
1831 debug_value_data (vd
)
1832 struct value_data
*vd
;
1837 CLEAR_HARD_REG_SET (set
);
1839 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
1840 if (vd
->e
[i
].oldest_regno
== i
)
1842 if (vd
->e
[i
].mode
== VOIDmode
)
1844 if (vd
->e
[i
].next_regno
!= INVALID_REGNUM
)
1845 fprintf (stderr
, "[%u] Bad next_regno for empty chain (%u)\n",
1846 i
, vd
->e
[i
].next_regno
);
1850 SET_HARD_REG_BIT (set
, i
);
1851 fprintf (stderr
, "[%u %s] ", i
, GET_MODE_NAME (vd
->e
[i
].mode
));
1853 for (j
= vd
->e
[i
].next_regno
;
1854 j
!= INVALID_REGNUM
;
1855 j
= vd
->e
[j
].next_regno
)
1857 if (TEST_HARD_REG_BIT (set
, j
))
1859 fprintf (stderr
, "[%u] Loop in regno chain\n", j
);
1863 if (vd
->e
[j
].oldest_regno
!= i
)
1865 fprintf (stderr
, "[%u] Bad oldest_regno (%u)\n",
1866 j
, vd
->e
[j
].oldest_regno
);
1869 SET_HARD_REG_BIT (set
, j
);
1870 fprintf (stderr
, "[%u %s] ", j
, GET_MODE_NAME (vd
->e
[j
].mode
));
1872 fputc ('\n', stderr
);
1875 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
1876 if (! TEST_HARD_REG_BIT (set
, i
)
1877 && (vd
->e
[i
].mode
!= VOIDmode
1878 || vd
->e
[i
].oldest_regno
!= i
1879 || vd
->e
[i
].next_regno
!= INVALID_REGNUM
))
1880 fprintf (stderr
, "[%u] Non-empty reg in chain (%s %u %i)\n",
1881 i
, GET_MODE_NAME (vd
->e
[i
].mode
), vd
->e
[i
].oldest_regno
,
1882 vd
->e
[i
].next_regno
);
1885 #ifdef ENABLE_CHECKING
1887 validate_value_data (vd
)
1888 struct value_data
*vd
;
1893 CLEAR_HARD_REG_SET (set
);
1895 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
1896 if (vd
->e
[i
].oldest_regno
== i
)
1898 if (vd
->e
[i
].mode
== VOIDmode
)
1900 if (vd
->e
[i
].next_regno
!= INVALID_REGNUM
)
1901 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1902 i
, vd
->e
[i
].next_regno
);
1906 SET_HARD_REG_BIT (set
, i
);
1908 for (j
= vd
->e
[i
].next_regno
;
1909 j
!= INVALID_REGNUM
;
1910 j
= vd
->e
[j
].next_regno
)
1912 if (TEST_HARD_REG_BIT (set
, j
))
1913 internal_error ("validate_value_data: Loop in regno chain (%u)",
1915 if (vd
->e
[j
].oldest_regno
!= i
)
1916 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1917 j
, vd
->e
[j
].oldest_regno
);
1919 SET_HARD_REG_BIT (set
, j
);
1923 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; ++i
)
1924 if (! TEST_HARD_REG_BIT (set
, i
)
1925 && (vd
->e
[i
].mode
!= VOIDmode
1926 || vd
->e
[i
].oldest_regno
!= i
1927 || vd
->e
[i
].next_regno
!= INVALID_REGNUM
))
1928 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1929 i
, GET_MODE_NAME (vd
->e
[i
].mode
), vd
->e
[i
].oldest_regno
,
1930 vd
->e
[i
].next_regno
);