1 /* Register renaming for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 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;
54 ENUM_BITFIELD(reg_class) class : 16;
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 (struct du_chain *, int);
82 static void scan_rtx_reg (rtx, rtx *, enum reg_class,
83 enum scan_actions, enum op_type, int);
84 static void scan_rtx_address (rtx, rtx *, enum reg_class,
85 enum scan_actions, enum machine_mode);
86 static void scan_rtx (rtx, rtx *, enum reg_class, enum scan_actions,
88 static struct du_chain *build_def_use (basic_block);
89 static void dump_def_use_chain (struct du_chain *);
90 static void note_sets (rtx, rtx, void *);
91 static void clear_dead_regs (HARD_REG_SET *, enum machine_mode, rtx);
92 static void merge_overlapping_regs (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 (rtx x, rtx set ATTRIBUTE_UNUSED, void *data)
101 HARD_REG_SET *pset = (HARD_REG_SET *) data;
105 if (GET_CODE (x) == SUBREG)
107 if (GET_CODE (x) != REG)
111 nregs = HARD_REGNO_NREGS (regno, GET_MODE (x));
113 /* There must not be pseudos at this point. */
114 if (regno + nregs > FIRST_PSEUDO_REGISTER)
118 SET_HARD_REG_BIT (*pset, regno + nregs);
121 /* Clear all registers from *PSET for which a note of kind KIND can be found
122 in the list NOTES. */
125 clear_dead_regs (HARD_REG_SET *pset, enum machine_mode kind, rtx notes)
128 for (note = notes; note; note = XEXP (note, 1))
129 if (REG_NOTE_KIND (note) == kind && REG_P (XEXP (note, 0)))
131 rtx reg = XEXP (note, 0);
132 unsigned int regno = REGNO (reg);
133 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
135 /* There must not be pseudos at this point. */
136 if (regno + nregs > FIRST_PSEUDO_REGISTER)
140 CLEAR_HARD_REG_BIT (*pset, regno + nregs);
144 /* For a def-use chain CHAIN in basic block B, find which registers overlap
145 its lifetime and set the corresponding bits in *PSET. */
148 merge_overlapping_regs (basic_block b, HARD_REG_SET *pset,
149 struct du_chain *chain)
151 struct du_chain *t = chain;
155 REG_SET_TO_HARD_REG_SET (live, b->global_live_at_start);
159 /* Search forward until the next reference to the register to be
161 while (insn != t->insn)
165 clear_dead_regs (&live, REG_DEAD, REG_NOTES (insn));
166 note_stores (PATTERN (insn), note_sets, (void *) &live);
167 /* Only record currently live regs if we are inside the
170 IOR_HARD_REG_SET (*pset, live);
171 clear_dead_regs (&live, REG_UNUSED, REG_NOTES (insn));
173 insn = NEXT_INSN (insn);
176 IOR_HARD_REG_SET (*pset, live);
178 /* For the last reference, also merge in all registers set in the
180 @@@ We only have take earlyclobbered sets into account. */
182 note_stores (PATTERN (insn), note_sets, (void *) pset);
188 /* Perform register renaming on the current function. */
191 regrename_optimize (void)
193 int tick[FIRST_PSEUDO_REGISTER];
198 memset (tick, 0, sizeof tick);
200 gcc_obstack_init (&rename_obstack);
201 first_obj = obstack_alloc (&rename_obstack, 0);
205 struct du_chain *all_chains = 0;
206 HARD_REG_SET unavailable;
207 HARD_REG_SET regs_seen;
209 CLEAR_HARD_REG_SET (unavailable);
212 fprintf (rtl_dump_file, "\nBasic block %d:\n", bb->index);
214 all_chains = build_def_use (bb);
217 dump_def_use_chain (all_chains);
219 CLEAR_HARD_REG_SET (unavailable);
220 /* Don't clobber traceback for noreturn functions. */
221 if (frame_pointer_needed)
225 for (i = HARD_REGNO_NREGS (FRAME_POINTER_REGNUM, Pmode); i--;)
226 SET_HARD_REG_BIT (unavailable, FRAME_POINTER_REGNUM + i);
228 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
229 for (i = HARD_REGNO_NREGS (HARD_FRAME_POINTER_REGNUM, Pmode); i--;)
230 SET_HARD_REG_BIT (unavailable, HARD_FRAME_POINTER_REGNUM + i);
234 CLEAR_HARD_REG_SET (regs_seen);
237 int new_reg, best_new_reg;
239 struct du_chain *this = all_chains;
240 struct du_chain *tmp, *last;
241 HARD_REG_SET this_unavailable;
242 int reg = REGNO (*this->loc);
245 all_chains = this->next_chain;
249 #if 0 /* This just disables optimization opportunities. */
250 /* Only rename once we've seen the reg more than once. */
251 if (! TEST_HARD_REG_BIT (regs_seen, reg))
253 SET_HARD_REG_BIT (regs_seen, reg);
258 if (fixed_regs[reg] || global_regs[reg]
259 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
260 || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
262 || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
267 COPY_HARD_REG_SET (this_unavailable, unavailable);
269 /* Find last entry on chain (which has the need_caller_save bit),
270 count number of uses, and narrow the set of registers we can
273 for (last = this; last->next_use; last = last->next_use)
276 IOR_COMPL_HARD_REG_SET (this_unavailable,
277 reg_class_contents[last->class]);
282 IOR_COMPL_HARD_REG_SET (this_unavailable,
283 reg_class_contents[last->class]);
285 if (this->need_caller_save_reg)
286 IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
288 merge_overlapping_regs (bb, &this_unavailable, this);
290 /* Now potential_regs is a reasonable approximation, let's
291 have a closer look at each register still in there. */
292 for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
294 int nregs = HARD_REGNO_NREGS (new_reg, GET_MODE (*this->loc));
296 for (i = nregs - 1; i >= 0; --i)
297 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
298 || fixed_regs[new_reg + i]
299 || global_regs[new_reg + i]
300 /* Can't use regs which aren't saved by the prologue. */
301 || (! regs_ever_live[new_reg + i]
302 && ! call_used_regs[new_reg + i])
303 #ifdef LEAF_REGISTERS
304 /* We can't use a non-leaf register if we're in a
306 || (current_function_is_leaf
307 && !LEAF_REGISTERS[new_reg + i])
309 #ifdef HARD_REGNO_RENAME_OK
310 || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
317 /* See whether it accepts all modes that occur in
318 definition and uses. */
319 for (tmp = this; tmp; tmp = tmp->next_use)
320 if (! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
321 || (tmp->need_caller_save_reg
322 && ! (HARD_REGNO_CALL_PART_CLOBBERED
323 (reg, GET_MODE (*tmp->loc)))
324 && (HARD_REGNO_CALL_PART_CLOBBERED
325 (new_reg, GET_MODE (*tmp->loc)))))
329 if (tick[best_new_reg] > tick[new_reg])
330 best_new_reg = new_reg;
336 fprintf (rtl_dump_file, "Register %s in insn %d",
337 reg_names[reg], INSN_UID (last->insn));
338 if (last->need_caller_save_reg)
339 fprintf (rtl_dump_file, " crosses a call");
342 if (best_new_reg == reg)
344 tick[reg] = ++this_tick;
346 fprintf (rtl_dump_file, "; no available better choice\n");
350 do_replace (this, best_new_reg);
351 tick[best_new_reg] = ++this_tick;
354 fprintf (rtl_dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
357 obstack_free (&rename_obstack, first_obj);
360 obstack_free (&rename_obstack, NULL);
363 fputc ('\n', rtl_dump_file);
365 count_or_remove_death_notes (NULL, 1);
366 update_life_info (NULL, UPDATE_LIFE_LOCAL,
367 PROP_REG_INFO | PROP_DEATH_NOTES);
371 do_replace (struct du_chain *chain, int reg)
375 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
376 struct reg_attrs * attr = REG_ATTRS (*chain->loc);
378 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
379 if (regno >= FIRST_PSEUDO_REGISTER)
380 ORIGINAL_REGNO (*chain->loc) = regno;
381 REG_ATTRS (*chain->loc) = attr;
382 chain = chain->next_use;
387 static struct du_chain *open_chains;
388 static struct du_chain *closed_chains;
391 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class class,
392 enum scan_actions action, enum op_type type, int earlyclobber)
396 enum machine_mode mode = GET_MODE (x);
397 int this_regno = REGNO (x);
398 int this_nregs = HARD_REGNO_NREGS (this_regno, mode);
400 if (action == mark_write)
404 struct du_chain *this
405 = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
407 this->next_chain = open_chains;
411 this->need_caller_save_reg = 0;
412 this->earlyclobber = earlyclobber;
418 if ((type == OP_OUT && action != terminate_write)
419 || (type != OP_OUT && action == terminate_write))
422 for (p = &open_chains; *p;)
424 struct du_chain *this = *p;
426 /* Check if the chain has been terminated if it has then skip to
429 This can happen when we've already appended the location to
430 the chain in Step 3, but are trying to hide in-out operands
431 from terminate_write in Step 5. */
433 if (*this->loc == cc0_rtx)
434 p = &this->next_chain;
437 int regno = REGNO (*this->loc);
438 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (*this->loc));
439 int exact_match = (regno == this_regno && nregs == this_nregs);
441 if (regno + nregs <= this_regno
442 || this_regno + this_nregs <= regno)
444 p = &this->next_chain;
448 if (action == mark_read)
453 /* ??? Class NO_REGS can happen if the md file makes use of
454 EXTRA_CONSTRAINTS to match registers. Which is arguably
455 wrong, but there we are. Since we know not what this may
456 be replaced with, terminate the chain. */
457 if (class != NO_REGS)
459 this = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
461 this->next_chain = (*p)->next_chain;
465 this->need_caller_save_reg = 0;
473 if (action != terminate_overlapping_read || ! exact_match)
475 struct du_chain *next = this->next_chain;
477 /* Whether the terminated chain can be used for renaming
478 depends on the action and this being an exact match.
479 In either case, we remove this element from open_chains. */
481 if ((action == terminate_dead || action == terminate_write)
484 this->next_chain = closed_chains;
485 closed_chains = this;
487 fprintf (rtl_dump_file,
488 "Closing chain %s at insn %d (%s)\n",
489 reg_names[REGNO (*this->loc)], INSN_UID (insn),
490 scan_actions_name[(int) action]);
495 fprintf (rtl_dump_file,
496 "Discarding chain %s at insn %d (%s)\n",
497 reg_names[REGNO (*this->loc)], INSN_UID (insn),
498 scan_actions_name[(int) action]);
503 p = &this->next_chain;
508 /* Adapted from find_reloads_address_1. CLASS is INDEX_REG_CLASS or
509 BASE_REG_CLASS depending on how the register is being considered. */
512 scan_rtx_address (rtx insn, rtx *loc, enum reg_class class,
513 enum scan_actions action, enum machine_mode mode)
516 RTX_CODE code = GET_CODE (x);
520 if (action == mark_write)
527 rtx orig_op0 = XEXP (x, 0);
528 rtx orig_op1 = XEXP (x, 1);
529 RTX_CODE code0 = GET_CODE (orig_op0);
530 RTX_CODE code1 = GET_CODE (orig_op1);
536 if (GET_CODE (op0) == SUBREG)
538 op0 = SUBREG_REG (op0);
539 code0 = GET_CODE (op0);
542 if (GET_CODE (op1) == SUBREG)
544 op1 = SUBREG_REG (op1);
545 code1 = GET_CODE (op1);
548 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
549 || code0 == ZERO_EXTEND || code1 == MEM)
554 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
555 || code1 == ZERO_EXTEND || code0 == MEM)
560 else if (code0 == CONST_INT || code0 == CONST
561 || code0 == SYMBOL_REF || code0 == LABEL_REF)
563 else if (code1 == CONST_INT || code1 == CONST
564 || code1 == SYMBOL_REF || code1 == LABEL_REF)
566 else if (code0 == REG && code1 == REG)
570 if (REG_OK_FOR_INDEX_P (op0)
571 && REG_MODE_OK_FOR_BASE_P (op1, mode))
573 else if (REG_OK_FOR_INDEX_P (op1)
574 && REG_MODE_OK_FOR_BASE_P (op0, mode))
576 else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
578 else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
580 else if (REG_OK_FOR_INDEX_P (op1))
585 locI = &XEXP (x, index_op);
586 locB = &XEXP (x, !index_op);
588 else if (code0 == REG)
593 else if (code1 == REG)
600 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
602 scan_rtx_address (insn, locB, MODE_BASE_REG_CLASS (mode), action, mode);
613 /* If the target doesn't claim to handle autoinc, this must be
614 something special, like a stack push. Kill this chain. */
615 action = terminate_all_read;
620 scan_rtx_address (insn, &XEXP (x, 0),
621 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
626 scan_rtx_reg (insn, loc, class, action, OP_IN, 0);
633 fmt = GET_RTX_FORMAT (code);
634 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
637 scan_rtx_address (insn, &XEXP (x, i), class, action, mode);
638 else if (fmt[i] == 'E')
639 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
640 scan_rtx_address (insn, &XVECEXP (x, i, j), class, action, mode);
645 scan_rtx (rtx insn, rtx *loc, enum reg_class class,
646 enum scan_actions action, enum op_type type, int earlyclobber)
650 enum rtx_code code = GET_CODE (x);
667 scan_rtx_reg (insn, loc, class, action, type, earlyclobber);
671 scan_rtx_address (insn, &XEXP (x, 0),
672 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
677 scan_rtx (insn, &SET_SRC (x), class, action, OP_IN, 0);
678 scan_rtx (insn, &SET_DEST (x), class, action, OP_OUT, 0);
681 case STRICT_LOW_PART:
682 scan_rtx (insn, &XEXP (x, 0), class, action, OP_INOUT, earlyclobber);
687 scan_rtx (insn, &XEXP (x, 0), class, action,
688 type == OP_IN ? OP_IN : OP_INOUT, earlyclobber);
689 scan_rtx (insn, &XEXP (x, 1), class, action, OP_IN, 0);
690 scan_rtx (insn, &XEXP (x, 2), class, action, OP_IN, 0);
699 /* Should only happen inside MEM. */
703 scan_rtx (insn, &SET_DEST (x), class, action, OP_OUT, 1);
707 scan_rtx (insn, &XEXP (x, 0), class, action, type, 0);
709 scan_rtx (insn, &XEXP (x, 1), class, action, type, 0);
716 fmt = GET_RTX_FORMAT (code);
717 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
720 scan_rtx (insn, &XEXP (x, i), class, action, type, 0);
721 else if (fmt[i] == 'E')
722 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
723 scan_rtx (insn, &XVECEXP (x, i, j), class, action, type, 0);
727 /* Build def/use chain. */
729 static struct du_chain *
730 build_def_use (basic_block bb)
734 open_chains = closed_chains = NULL;
736 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
742 rtx old_operands[MAX_RECOG_OPERANDS];
743 rtx old_dups[MAX_DUP_OPERANDS];
748 /* Process the insn, determining its effect on the def-use
749 chains. We perform the following steps with the register
750 references in the insn:
751 (1) Any read that overlaps an open chain, but doesn't exactly
752 match, causes that chain to be closed. We can't deal
754 (2) Any read outside an operand causes any chain it overlaps
755 with to be closed, since we can't replace it.
756 (3) Any read inside an operand is added if there's already
757 an open chain for it.
758 (4) For any REG_DEAD note we find, close open chains that
760 (5) For any write we find, close open chains that overlap it.
761 (6) For any write we find in an operand, make a new chain.
762 (7) For any REG_UNUSED, close any chains we just opened. */
764 icode = recog_memoized (insn);
766 if (! constrain_operands (1))
767 fatal_insn_not_found (insn);
768 preprocess_constraints ();
769 alt = which_alternative;
770 n_ops = recog_data.n_operands;
772 /* Simplify the code below by rewriting things to reflect
773 matching constraints. Also promote OP_OUT to OP_INOUT
774 in predicated instructions. */
776 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
777 for (i = 0; i < n_ops; ++i)
779 int matches = recog_op_alt[i][alt].matches;
781 recog_op_alt[i][alt].class = recog_op_alt[matches][alt].class;
782 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
783 || (predicated && recog_data.operand_type[i] == OP_OUT))
784 recog_data.operand_type[i] = OP_INOUT;
787 /* Step 1: Close chains for which we have overlapping reads. */
788 for (i = 0; i < n_ops; i++)
789 scan_rtx (insn, recog_data.operand_loc[i],
790 NO_REGS, terminate_overlapping_read,
791 recog_data.operand_type[i], 0);
793 /* Step 2: Close chains for which we have reads outside operands.
794 We do this by munging all operands into CC0, and closing
795 everything remaining. */
797 for (i = 0; i < n_ops; i++)
799 old_operands[i] = recog_data.operand[i];
800 /* Don't squash match_operator or match_parallel here, since
801 we don't know that all of the contained registers are
802 reachable by proper operands. */
803 if (recog_data.constraints[i][0] == '\0')
805 *recog_data.operand_loc[i] = cc0_rtx;
807 for (i = 0; i < recog_data.n_dups; i++)
809 int dup_num = recog_data.dup_num[i];
811 old_dups[i] = *recog_data.dup_loc[i];
812 *recog_data.dup_loc[i] = cc0_rtx;
814 /* For match_dup of match_operator or match_parallel, share
815 them, so that we don't miss changes in the dup. */
817 && insn_data[icode].operand[dup_num].eliminable == 0)
818 old_dups[i] = recog_data.operand[dup_num];
821 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
824 for (i = 0; i < recog_data.n_dups; i++)
825 *recog_data.dup_loc[i] = old_dups[i];
826 for (i = 0; i < n_ops; i++)
827 *recog_data.operand_loc[i] = old_operands[i];
829 /* Step 2B: Can't rename function call argument registers. */
830 if (GET_CODE (insn) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (insn))
831 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
832 NO_REGS, terminate_all_read, OP_IN, 0);
834 /* Step 2C: Can't rename asm operands that were originally
836 if (asm_noperands (PATTERN (insn)) > 0)
837 for (i = 0; i < n_ops; i++)
839 rtx *loc = recog_data.operand_loc[i];
842 if (GET_CODE (op) == REG
843 && REGNO (op) == ORIGINAL_REGNO (op)
844 && (recog_data.operand_type[i] == OP_IN
845 || recog_data.operand_type[i] == OP_INOUT))
846 scan_rtx (insn, loc, NO_REGS, terminate_all_read, OP_IN, 0);
849 /* Step 3: Append to chains for reads inside operands. */
850 for (i = 0; i < n_ops + recog_data.n_dups; i++)
852 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
853 rtx *loc = (i < n_ops
854 ? recog_data.operand_loc[opn]
855 : recog_data.dup_loc[i - n_ops]);
856 enum reg_class class = recog_op_alt[opn][alt].class;
857 enum op_type type = recog_data.operand_type[opn];
859 /* Don't scan match_operand here, since we've no reg class
860 information to pass down. Any operands that we could
861 substitute in will be represented elsewhere. */
862 if (recog_data.constraints[opn][0] == '\0')
865 if (recog_op_alt[opn][alt].is_address)
866 scan_rtx_address (insn, loc, class, mark_read, VOIDmode);
868 scan_rtx (insn, loc, class, mark_read, type, 0);
871 /* Step 4: Close chains for registers that die here.
872 Also record updates for REG_INC notes. */
873 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
875 if (REG_NOTE_KIND (note) == REG_DEAD)
876 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
878 else if (REG_NOTE_KIND (note) == REG_INC)
879 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
883 /* Step 4B: If this is a call, any chain live at this point
884 requires a caller-saved reg. */
885 if (GET_CODE (insn) == CALL_INSN)
888 for (p = open_chains; p; p = p->next_chain)
889 p->need_caller_save_reg = 1;
892 /* Step 5: Close open chains that overlap writes. Similar to
893 step 2, we hide in-out operands, since we do not want to
894 close these chains. */
896 for (i = 0; i < n_ops; i++)
898 old_operands[i] = recog_data.operand[i];
899 if (recog_data.operand_type[i] == OP_INOUT)
900 *recog_data.operand_loc[i] = cc0_rtx;
902 for (i = 0; i < recog_data.n_dups; i++)
904 int opn = recog_data.dup_num[i];
905 old_dups[i] = *recog_data.dup_loc[i];
906 if (recog_data.operand_type[opn] == OP_INOUT)
907 *recog_data.dup_loc[i] = cc0_rtx;
910 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
912 for (i = 0; i < recog_data.n_dups; i++)
913 *recog_data.dup_loc[i] = old_dups[i];
914 for (i = 0; i < n_ops; i++)
915 *recog_data.operand_loc[i] = old_operands[i];
917 /* Step 6: Begin new chains for writes inside operands. */
918 /* ??? Many targets have output constraints on the SET_DEST
919 of a call insn, which is stupid, since these are certainly
920 ABI defined hard registers. Don't change calls at all.
921 Similarly take special care for asm statement that originally
922 referenced hard registers. */
923 if (asm_noperands (PATTERN (insn)) > 0)
925 for (i = 0; i < n_ops; i++)
926 if (recog_data.operand_type[i] == OP_OUT)
928 rtx *loc = recog_data.operand_loc[i];
930 enum reg_class class = recog_op_alt[i][alt].class;
932 if (GET_CODE (op) == REG
933 && REGNO (op) == ORIGINAL_REGNO (op))
936 scan_rtx (insn, loc, class, mark_write, OP_OUT,
937 recog_op_alt[i][alt].earlyclobber);
940 else if (GET_CODE (insn) != CALL_INSN)
941 for (i = 0; i < n_ops + recog_data.n_dups; i++)
943 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
944 rtx *loc = (i < n_ops
945 ? recog_data.operand_loc[opn]
946 : recog_data.dup_loc[i - n_ops]);
947 enum reg_class class = recog_op_alt[opn][alt].class;
949 if (recog_data.operand_type[opn] == OP_OUT)
950 scan_rtx (insn, loc, class, mark_write, OP_OUT,
951 recog_op_alt[opn][alt].earlyclobber);
954 /* Step 7: Close chains for registers that were never
956 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
957 if (REG_NOTE_KIND (note) == REG_UNUSED)
958 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
961 if (insn == BB_END (bb))
965 /* Since we close every chain when we find a REG_DEAD note, anything that
966 is still open lives past the basic block, so it can't be renamed. */
967 return closed_chains;
970 /* Dump all def/use chains in CHAINS to RTL_DUMP_FILE. They are
971 printed in reverse order as that's how we build them. */
974 dump_def_use_chain (struct du_chain *chains)
978 struct du_chain *this = chains;
979 int r = REGNO (*this->loc);
980 int nregs = HARD_REGNO_NREGS (r, GET_MODE (*this->loc));
981 fprintf (rtl_dump_file, "Register %s (%d):", reg_names[r], nregs);
984 fprintf (rtl_dump_file, " %d [%s]", INSN_UID (this->insn),
985 reg_class_names[this->class]);
986 this = this->next_use;
988 fprintf (rtl_dump_file, "\n");
989 chains = chains->next_chain;
993 /* The following code does forward propagation of hard register copies.
994 The object is to eliminate as many dependencies as possible, so that
995 we have the most scheduling freedom. As a side effect, we also clean
996 up some silly register allocation decisions made by reload. This
997 code may be obsoleted by a new register allocator. */
999 /* For each register, we have a list of registers that contain the same
1000 value. The OLDEST_REGNO field points to the head of the list, and
1001 the NEXT_REGNO field runs through the list. The MODE field indicates
1002 what mode the data is known to be in; this field is VOIDmode when the
1003 register is not known to contain valid data. */
1005 struct value_data_entry
1007 enum machine_mode mode;
1008 unsigned int oldest_regno;
1009 unsigned int next_regno;
1014 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
1015 unsigned int max_value_regs;
1018 static void kill_value_regno (unsigned, struct value_data *);
1019 static void kill_value (rtx, struct value_data *);
1020 static void set_value_regno (unsigned, enum machine_mode, struct value_data *);
1021 static void init_value_data (struct value_data *);
1022 static void kill_clobbered_value (rtx, rtx, void *);
1023 static void kill_set_value (rtx, rtx, void *);
1024 static int kill_autoinc_value (rtx *, void *);
1025 static void copy_value (rtx, rtx, struct value_data *);
1026 static bool mode_change_ok (enum machine_mode, enum machine_mode,
1028 static rtx maybe_mode_change (enum machine_mode, enum machine_mode,
1029 enum machine_mode, unsigned int, unsigned int);
1030 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
1031 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx,
1032 struct value_data *);
1033 static bool replace_oldest_value_addr (rtx *, enum reg_class,
1034 enum machine_mode, rtx,
1035 struct value_data *);
1036 static bool replace_oldest_value_mem (rtx, rtx, struct value_data *);
1037 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
1038 extern void debug_value_data (struct value_data *);
1039 #ifdef ENABLE_CHECKING
1040 static void validate_value_data (struct value_data *);
1043 /* Kill register REGNO. This involves removing it from any value lists,
1044 and resetting the value mode to VOIDmode. */
1047 kill_value_regno (unsigned int regno, struct value_data *vd)
1049 unsigned int i, next;
1051 if (vd->e[regno].oldest_regno != regno)
1053 for (i = vd->e[regno].oldest_regno;
1054 vd->e[i].next_regno != regno;
1055 i = vd->e[i].next_regno)
1057 vd->e[i].next_regno = vd->e[regno].next_regno;
1059 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
1061 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
1062 vd->e[i].oldest_regno = next;
1065 vd->e[regno].mode = VOIDmode;
1066 vd->e[regno].oldest_regno = regno;
1067 vd->e[regno].next_regno = INVALID_REGNUM;
1069 #ifdef ENABLE_CHECKING
1070 validate_value_data (vd);
1074 /* Kill X. This is a convenience function for kill_value_regno
1075 so that we mind the mode the register is in. */
1078 kill_value (rtx x, struct value_data *vd)
1080 /* SUBREGS are supposed to have been eliminated by now. But some
1081 ports, e.g. i386 sse, use them to smuggle vector type information
1082 through to instruction selection. Each such SUBREG should simplify,
1083 so if we get a NULL we've done something wrong elsewhere. */
1085 if (GET_CODE (x) == SUBREG)
1086 x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
1087 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
1090 unsigned int regno = REGNO (x);
1091 unsigned int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
1094 /* Kill the value we're told to kill. */
1095 for (i = 0; i < n; ++i)
1096 kill_value_regno (regno + i, vd);
1098 /* Kill everything that overlapped what we're told to kill. */
1099 if (regno < vd->max_value_regs)
1102 j = regno - vd->max_value_regs;
1103 for (; j < regno; ++j)
1105 if (vd->e[j].mode == VOIDmode)
1107 n = HARD_REGNO_NREGS (j, vd->e[j].mode);
1109 for (i = 0; i < n; ++i)
1110 kill_value_regno (j + i, vd);
1115 /* Remember that REGNO is valid in MODE. */
1118 set_value_regno (unsigned int regno, enum machine_mode mode,
1119 struct value_data *vd)
1123 vd->e[regno].mode = mode;
1125 nregs = HARD_REGNO_NREGS (regno, mode);
1126 if (nregs > vd->max_value_regs)
1127 vd->max_value_regs = nregs;
1130 /* Initialize VD such that there are no known relationships between regs. */
1133 init_value_data (struct value_data *vd)
1136 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1138 vd->e[i].mode = VOIDmode;
1139 vd->e[i].oldest_regno = i;
1140 vd->e[i].next_regno = INVALID_REGNUM;
1142 vd->max_value_regs = 0;
1145 /* Called through note_stores. If X is clobbered, kill its value. */
1148 kill_clobbered_value (rtx x, rtx set, void *data)
1150 struct value_data *vd = data;
1151 if (GET_CODE (set) == CLOBBER)
1155 /* Called through note_stores. If X is set, not clobbered, kill its
1156 current value and install it as the root of its own value list. */
1159 kill_set_value (rtx x, rtx set, void *data)
1161 struct value_data *vd = data;
1162 if (GET_CODE (set) != CLOBBER)
1166 set_value_regno (REGNO (x), GET_MODE (x), vd);
1170 /* Called through for_each_rtx. Kill any register used as the base of an
1171 auto-increment expression, and install that register as the root of its
1175 kill_autoinc_value (rtx *px, void *data)
1178 struct value_data *vd = data;
1180 if (GET_RTX_CLASS (GET_CODE (x)) == 'a')
1184 set_value_regno (REGNO (x), Pmode, vd);
1191 /* Assert that SRC has been copied to DEST. Adjust the data structures
1192 to reflect that SRC contains an older copy of the shared value. */
1195 copy_value (rtx dest, rtx src, struct value_data *vd)
1197 unsigned int dr = REGNO (dest);
1198 unsigned int sr = REGNO (src);
1199 unsigned int dn, sn;
1202 /* ??? At present, it's possible to see noop sets. It'd be nice if
1203 this were cleaned up beforehand... */
1207 /* Do not propagate copies to the stack pointer, as that can leave
1208 memory accesses with no scheduling dependency on the stack update. */
1209 if (dr == STACK_POINTER_REGNUM)
1212 /* Likewise with the frame pointer, if we're using one. */
1213 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
1216 /* If SRC and DEST overlap, don't record anything. */
1217 dn = HARD_REGNO_NREGS (dr, GET_MODE (dest));
1218 sn = HARD_REGNO_NREGS (sr, GET_MODE (dest));
1219 if ((dr > sr && dr < sr + sn)
1220 || (sr > dr && sr < dr + dn))
1223 /* If SRC had no assigned mode (i.e. we didn't know it was live)
1224 assign it now and assume the value came from an input argument
1226 if (vd->e[sr].mode == VOIDmode)
1227 set_value_regno (sr, vd->e[dr].mode, vd);
1229 /* If we are narrowing the input to a smaller number of hard regs,
1230 and it is in big endian, we are really extracting a high part.
1231 Since we generally associate a low part of a value with the value itself,
1232 we must not do the same for the high part.
1233 Note we can still get low parts for the same mode combination through
1234 a two-step copy involving differently sized hard regs.
1235 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
1236 (set (reg:DI r0) (reg:DI fr0))
1237 (set (reg:SI fr2) (reg:SI r0))
1238 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
1239 (set (reg:SI fr2) (reg:SI fr0))
1240 loads the high part of (reg:DI fr0) into fr2.
1242 We can't properly represent the latter case in our tables, so don't
1243 record anything then. */
1244 else if (sn < (unsigned int) HARD_REGNO_NREGS (sr, vd->e[sr].mode)
1245 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
1246 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
1249 /* If SRC had been assigned a mode narrower than the copy, we can't
1250 link DEST into the chain, because not all of the pieces of the
1251 copy came from oldest_regno. */
1252 else if (sn > (unsigned int) HARD_REGNO_NREGS (sr, vd->e[sr].mode))
1255 /* Link DR at the end of the value chain used by SR. */
1257 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
1259 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
1261 vd->e[i].next_regno = dr;
1263 #ifdef ENABLE_CHECKING
1264 validate_value_data (vd);
1268 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
1271 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
1272 unsigned int regno ATTRIBUTE_UNUSED)
1274 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
1277 #ifdef CANNOT_CHANGE_MODE_CLASS
1278 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
1284 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
1285 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
1287 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
1290 maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
1291 enum machine_mode new_mode, unsigned int regno,
1292 unsigned int copy_regno ATTRIBUTE_UNUSED)
1294 if (orig_mode == new_mode)
1295 return gen_rtx_raw_REG (new_mode, regno);
1296 else if (mode_change_ok (orig_mode, new_mode, regno))
1298 int copy_nregs = HARD_REGNO_NREGS (copy_regno, copy_mode);
1299 int use_nregs = HARD_REGNO_NREGS (copy_regno, new_mode);
1301 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
1303 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
1304 int byteoffset = offset % UNITS_PER_WORD;
1305 int wordoffset = offset - byteoffset;
1307 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
1308 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
1309 return gen_rtx_raw_REG (new_mode,
1310 regno + subreg_regno_offset (regno, orig_mode,
1317 /* Find the oldest copy of the value contained in REGNO that is in
1318 register class CLASS and has mode MODE. If found, return an rtx
1319 of that oldest register, otherwise return NULL. */
1322 find_oldest_value_reg (enum reg_class class, rtx reg, struct value_data *vd)
1324 unsigned int regno = REGNO (reg);
1325 enum machine_mode mode = GET_MODE (reg);
1328 /* If we are accessing REG in some mode other that what we set it in,
1329 make sure that the replacement is valid. In particular, consider
1330 (set (reg:DI r11) (...))
1331 (set (reg:SI r9) (reg:SI r11))
1332 (set (reg:SI r10) (...))
1333 (set (...) (reg:DI r9))
1334 Replacing r9 with r11 is invalid. */
1335 if (mode != vd->e[regno].mode)
1337 if (HARD_REGNO_NREGS (regno, mode)
1338 > HARD_REGNO_NREGS (regno, vd->e[regno].mode))
1342 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
1344 enum machine_mode oldmode = vd->e[i].mode;
1348 for (last = i; last < i + HARD_REGNO_NREGS (i, mode); last++)
1349 if (!TEST_HARD_REG_BIT (reg_class_contents[class], last))
1352 new = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
1355 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (reg);
1356 REG_ATTRS (new) = REG_ATTRS (reg);
1364 /* If possible, replace the register at *LOC with the oldest register
1365 in register class CLASS. Return true if successfully replaced. */
1368 replace_oldest_value_reg (rtx *loc, enum reg_class class, rtx insn,
1369 struct value_data *vd)
1371 rtx new = find_oldest_value_reg (class, *loc, vd);
1375 fprintf (rtl_dump_file, "insn %u: replaced reg %u with %u\n",
1376 INSN_UID (insn), REGNO (*loc), REGNO (new));
1384 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1385 Adapted from find_reloads_address_1. CLASS is INDEX_REG_CLASS or
1386 BASE_REG_CLASS depending on how the register is being considered. */
1389 replace_oldest_value_addr (rtx *loc, enum reg_class class,
1390 enum machine_mode mode, rtx insn,
1391 struct value_data *vd)
1394 RTX_CODE code = GET_CODE (x);
1397 bool changed = false;
1403 rtx orig_op0 = XEXP (x, 0);
1404 rtx orig_op1 = XEXP (x, 1);
1405 RTX_CODE code0 = GET_CODE (orig_op0);
1406 RTX_CODE code1 = GET_CODE (orig_op1);
1412 if (GET_CODE (op0) == SUBREG)
1414 op0 = SUBREG_REG (op0);
1415 code0 = GET_CODE (op0);
1418 if (GET_CODE (op1) == SUBREG)
1420 op1 = SUBREG_REG (op1);
1421 code1 = GET_CODE (op1);
1424 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1425 || code0 == ZERO_EXTEND || code1 == MEM)
1427 locI = &XEXP (x, 0);
1428 locB = &XEXP (x, 1);
1430 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1431 || code1 == ZERO_EXTEND || code0 == MEM)
1433 locI = &XEXP (x, 1);
1434 locB = &XEXP (x, 0);
1436 else if (code0 == CONST_INT || code0 == CONST
1437 || code0 == SYMBOL_REF || code0 == LABEL_REF)
1438 locB = &XEXP (x, 1);
1439 else if (code1 == CONST_INT || code1 == CONST
1440 || code1 == SYMBOL_REF || code1 == LABEL_REF)
1441 locB = &XEXP (x, 0);
1442 else if (code0 == REG && code1 == REG)
1446 if (REG_OK_FOR_INDEX_P (op0)
1447 && REG_MODE_OK_FOR_BASE_P (op1, mode))
1449 else if (REG_OK_FOR_INDEX_P (op1)
1450 && REG_MODE_OK_FOR_BASE_P (op0, mode))
1452 else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
1454 else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
1456 else if (REG_OK_FOR_INDEX_P (op1))
1461 locI = &XEXP (x, index_op);
1462 locB = &XEXP (x, !index_op);
1464 else if (code0 == REG)
1466 locI = &XEXP (x, 0);
1467 locB = &XEXP (x, 1);
1469 else if (code1 == REG)
1471 locI = &XEXP (x, 1);
1472 locB = &XEXP (x, 0);
1476 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
1479 changed |= replace_oldest_value_addr (locB,
1480 MODE_BASE_REG_CLASS (mode),
1494 return replace_oldest_value_mem (x, insn, vd);
1497 return replace_oldest_value_reg (loc, class, insn, vd);
1503 fmt = GET_RTX_FORMAT (code);
1504 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1507 changed |= replace_oldest_value_addr (&XEXP (x, i), class, mode,
1509 else if (fmt[i] == 'E')
1510 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1511 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), class,
1518 /* Similar to replace_oldest_value_reg, but X contains a memory. */
1521 replace_oldest_value_mem (rtx x, rtx insn, struct value_data *vd)
1523 return replace_oldest_value_addr (&XEXP (x, 0),
1524 MODE_BASE_REG_CLASS (GET_MODE (x)),
1525 GET_MODE (x), insn, vd);
1528 /* Perform the forward copy propagation on basic block BB. */
1531 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
1533 bool changed = false;
1536 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1538 int n_ops, i, alt, predicated;
1542 if (! INSN_P (insn))
1544 if (insn == BB_END (bb))
1550 set = single_set (insn);
1551 extract_insn (insn);
1552 if (! constrain_operands (1))
1553 fatal_insn_not_found (insn);
1554 preprocess_constraints ();
1555 alt = which_alternative;
1556 n_ops = recog_data.n_operands;
1557 is_asm = asm_noperands (PATTERN (insn)) >= 0;
1559 /* Simplify the code below by rewriting things to reflect
1560 matching constraints. Also promote OP_OUT to OP_INOUT
1561 in predicated instructions. */
1563 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1564 for (i = 0; i < n_ops; ++i)
1566 int matches = recog_op_alt[i][alt].matches;
1568 recog_op_alt[i][alt].class = recog_op_alt[matches][alt].class;
1569 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1570 || (predicated && recog_data.operand_type[i] == OP_OUT))
1571 recog_data.operand_type[i] = OP_INOUT;
1574 /* For each earlyclobber operand, zap the value data. */
1575 for (i = 0; i < n_ops; i++)
1576 if (recog_op_alt[i][alt].earlyclobber)
1577 kill_value (recog_data.operand[i], vd);
1579 /* Within asms, a clobber cannot overlap inputs or outputs.
1580 I wouldn't think this were true for regular insns, but
1581 scan_rtx treats them like that... */
1582 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1584 /* Kill all auto-incremented values. */
1585 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
1586 for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
1588 /* Kill all early-clobbered operands. */
1589 for (i = 0; i < n_ops; i++)
1590 if (recog_op_alt[i][alt].earlyclobber)
1591 kill_value (recog_data.operand[i], vd);
1593 /* Special-case plain move instructions, since we may well
1594 be able to do the move from a different register class. */
1595 if (set && REG_P (SET_SRC (set)))
1597 rtx src = SET_SRC (set);
1598 unsigned int regno = REGNO (src);
1599 enum machine_mode mode = GET_MODE (src);
1603 /* If we are accessing SRC in some mode other that what we
1604 set it in, make sure that the replacement is valid. */
1605 if (mode != vd->e[regno].mode)
1607 if (HARD_REGNO_NREGS (regno, mode)
1608 > HARD_REGNO_NREGS (regno, vd->e[regno].mode))
1609 goto no_move_special_case;
1612 /* If the destination is also a register, try to find a source
1613 register in the same class. */
1614 if (REG_P (SET_DEST (set)))
1616 new = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
1617 if (new && validate_change (insn, &SET_SRC (set), new, 0))
1620 fprintf (rtl_dump_file,
1621 "insn %u: replaced reg %u with %u\n",
1622 INSN_UID (insn), regno, REGNO (new));
1624 goto did_replacement;
1628 /* Otherwise, try all valid registers and see if its valid. */
1629 for (i = vd->e[regno].oldest_regno; i != regno;
1630 i = vd->e[i].next_regno)
1632 new = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
1634 if (new != NULL_RTX)
1636 if (validate_change (insn, &SET_SRC (set), new, 0))
1638 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (src);
1639 REG_ATTRS (new) = REG_ATTRS (src);
1641 fprintf (rtl_dump_file,
1642 "insn %u: replaced reg %u with %u\n",
1643 INSN_UID (insn), regno, REGNO (new));
1645 goto did_replacement;
1650 no_move_special_case:
1652 /* For each input operand, replace a hard register with the
1653 eldest live copy that's in an appropriate register class. */
1654 for (i = 0; i < n_ops; i++)
1656 bool replaced = false;
1658 /* Don't scan match_operand here, since we've no reg class
1659 information to pass down. Any operands that we could
1660 substitute in will be represented elsewhere. */
1661 if (recog_data.constraints[i][0] == '\0')
1664 /* Don't replace in asms intentionally referencing hard regs. */
1665 if (is_asm && GET_CODE (recog_data.operand[i]) == REG
1666 && (REGNO (recog_data.operand[i])
1667 == ORIGINAL_REGNO (recog_data.operand[i])))
1670 if (recog_data.operand_type[i] == OP_IN)
1672 if (recog_op_alt[i][alt].is_address)
1674 = replace_oldest_value_addr (recog_data.operand_loc[i],
1675 recog_op_alt[i][alt].class,
1676 VOIDmode, insn, vd);
1677 else if (REG_P (recog_data.operand[i]))
1679 = replace_oldest_value_reg (recog_data.operand_loc[i],
1680 recog_op_alt[i][alt].class,
1682 else if (GET_CODE (recog_data.operand[i]) == MEM)
1683 replaced = replace_oldest_value_mem (recog_data.operand[i],
1686 else if (GET_CODE (recog_data.operand[i]) == MEM)
1687 replaced = replace_oldest_value_mem (recog_data.operand[i],
1690 /* If we performed any replacement, update match_dups. */
1698 new = *recog_data.operand_loc[i];
1699 recog_data.operand[i] = new;
1700 for (j = 0; j < recog_data.n_dups; j++)
1701 if (recog_data.dup_num[j] == i)
1702 *recog_data.dup_loc[j] = new;
1707 /* Clobber call-clobbered registers. */
1708 if (GET_CODE (insn) == CALL_INSN)
1709 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1710 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1711 kill_value_regno (i, vd);
1713 /* Notice stores. */
1714 note_stores (PATTERN (insn), kill_set_value, vd);
1716 /* Notice copies. */
1717 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1718 copy_value (SET_DEST (set), SET_SRC (set), vd);
1720 if (insn == BB_END (bb))
1727 /* Main entry point for the forward copy propagation optimization. */
1730 copyprop_hardreg_forward (void)
1732 struct value_data *all_vd;
1734 basic_block bb, bbp = 0;
1736 need_refresh = false;
1738 all_vd = xmalloc (sizeof (struct value_data) * last_basic_block);
1742 /* If a block has a single predecessor, that we've already
1743 processed, begin with the value data that was live at
1744 the end of the predecessor block. */
1745 /* ??? Ought to use more intelligent queuing of blocks. */
1747 for (bbp = bb; bbp && bbp != bb->pred->src; bbp = bbp->prev_bb);
1749 && ! bb->pred->pred_next
1750 && ! (bb->pred->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1751 && bb->pred->src != ENTRY_BLOCK_PTR
1753 all_vd[bb->index] = all_vd[bb->pred->src->index];
1755 init_value_data (all_vd + bb->index);
1757 if (copyprop_hardreg_forward_1 (bb, all_vd + bb->index))
1758 need_refresh = true;
1764 fputs ("\n\n", rtl_dump_file);
1766 /* ??? Irritatingly, delete_noop_moves does not take a set of blocks
1767 to scan, so we have to do a life update with no initial set of
1768 blocks Just In Case. */
1769 delete_noop_moves (get_insns ());
1770 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
1772 | PROP_SCAN_DEAD_CODE
1773 | PROP_KILL_DEAD_CODE);
1779 /* Dump the value chain data to stderr. */
1782 debug_value_data (struct value_data *vd)
1787 CLEAR_HARD_REG_SET (set);
1789 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1790 if (vd->e[i].oldest_regno == i)
1792 if (vd->e[i].mode == VOIDmode)
1794 if (vd->e[i].next_regno != INVALID_REGNUM)
1795 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1796 i, vd->e[i].next_regno);
1800 SET_HARD_REG_BIT (set, i);
1801 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1803 for (j = vd->e[i].next_regno;
1804 j != INVALID_REGNUM;
1805 j = vd->e[j].next_regno)
1807 if (TEST_HARD_REG_BIT (set, j))
1809 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1813 if (vd->e[j].oldest_regno != i)
1815 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1816 j, vd->e[j].oldest_regno);
1819 SET_HARD_REG_BIT (set, j);
1820 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1822 fputc ('\n', stderr);
1825 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1826 if (! TEST_HARD_REG_BIT (set, i)
1827 && (vd->e[i].mode != VOIDmode
1828 || vd->e[i].oldest_regno != i
1829 || vd->e[i].next_regno != INVALID_REGNUM))
1830 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1831 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1832 vd->e[i].next_regno);
1835 #ifdef ENABLE_CHECKING
1837 validate_value_data (struct value_data *vd)
1842 CLEAR_HARD_REG_SET (set);
1844 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1845 if (vd->e[i].oldest_regno == i)
1847 if (vd->e[i].mode == VOIDmode)
1849 if (vd->e[i].next_regno != INVALID_REGNUM)
1850 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1851 i, vd->e[i].next_regno);
1855 SET_HARD_REG_BIT (set, i);
1857 for (j = vd->e[i].next_regno;
1858 j != INVALID_REGNUM;
1859 j = vd->e[j].next_regno)
1861 if (TEST_HARD_REG_BIT (set, j))
1862 internal_error ("validate_value_data: Loop in regno chain (%u)",
1864 if (vd->e[j].oldest_regno != i)
1865 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1866 j, vd->e[j].oldest_regno);
1868 SET_HARD_REG_BIT (set, j);
1872 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1873 if (! TEST_HARD_REG_BIT (set, i)
1874 && (vd->e[i].mode != VOIDmode
1875 || vd->e[i].oldest_regno != i
1876 || vd->e[i].next_regno != INVALID_REGNUM))
1877 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1878 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1879 vd->e[i].next_regno);