configure: Regenerate for new libtool.
[official-gcc.git] / gcc / regrename.c
blobb01c2e6b15bd83e394cbed15247fdad859c2a04f
1 /* Register renaming for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "insn-config.h"
28 #include "regs.h"
29 #include "addresses.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
32 #include "reload.h"
33 #include "output.h"
34 #include "function.h"
35 #include "recog.h"
36 #include "flags.h"
37 #include "toplev.h"
38 #include "obstack.h"
39 #include "timevar.h"
40 #include "tree-pass.h"
41 #include "df.h"
43 struct du_chain
45 struct du_chain *next_chain;
46 struct du_chain *next_use;
48 rtx insn;
49 rtx *loc;
50 ENUM_BITFIELD(reg_class) cl : 16;
51 unsigned int need_caller_save_reg:1;
52 unsigned int earlyclobber:1;
55 enum scan_actions
57 terminate_all_read,
58 terminate_overlapping_read,
59 terminate_write,
60 terminate_dead,
61 mark_read,
62 mark_write,
63 /* mark_access is for marking the destination regs in
64 REG_FRAME_RELATED_EXPR notes (as if they were read) so that the
65 note is updated properly. */
66 mark_access
69 static const char * const scan_actions_name[] =
71 "terminate_all_read",
72 "terminate_overlapping_read",
73 "terminate_write",
74 "terminate_dead",
75 "mark_read",
76 "mark_write",
77 "mark_access"
80 static struct obstack rename_obstack;
82 static void do_replace (struct du_chain *, int);
83 static void scan_rtx_reg (rtx, rtx *, enum reg_class,
84 enum scan_actions, enum op_type, int);
85 static void scan_rtx_address (rtx, rtx *, enum reg_class,
86 enum scan_actions, enum machine_mode);
87 static void scan_rtx (rtx, rtx *, enum reg_class, enum scan_actions,
88 enum op_type, int);
89 static struct du_chain *build_def_use (basic_block);
90 static void dump_def_use_chain (struct du_chain *);
91 static void note_sets (rtx, const_rtx, void *);
92 static void clear_dead_regs (HARD_REG_SET *, enum reg_note, rtx);
93 static void merge_overlapping_regs (basic_block, HARD_REG_SET *,
94 struct du_chain *);
96 /* Called through note_stores. Find sets of registers, and
97 record them in *DATA (which is actually a HARD_REG_SET *). */
99 static void
100 note_sets (rtx x, const_rtx set ATTRIBUTE_UNUSED, void *data)
102 HARD_REG_SET *pset = (HARD_REG_SET *) data;
104 if (GET_CODE (x) == SUBREG)
105 x = SUBREG_REG (x);
106 if (!REG_P (x))
107 return;
108 /* There must not be pseudos at this point. */
109 gcc_assert (HARD_REGISTER_P (x));
110 add_to_hard_reg_set (pset, GET_MODE (x), REGNO (x));
113 /* Clear all registers from *PSET for which a note of kind KIND can be found
114 in the list NOTES. */
116 static void
117 clear_dead_regs (HARD_REG_SET *pset, enum reg_note kind, rtx notes)
119 rtx note;
120 for (note = notes; note; note = XEXP (note, 1))
121 if (REG_NOTE_KIND (note) == kind && REG_P (XEXP (note, 0)))
123 rtx reg = XEXP (note, 0);
124 /* There must not be pseudos at this point. */
125 gcc_assert (HARD_REGISTER_P (reg));
126 remove_from_hard_reg_set (pset, GET_MODE (reg), REGNO (reg));
130 /* For a def-use chain CHAIN in basic block B, find which registers overlap
131 its lifetime and set the corresponding bits in *PSET. */
133 static void
134 merge_overlapping_regs (basic_block b, HARD_REG_SET *pset,
135 struct du_chain *chain)
137 struct du_chain *t = chain;
138 rtx insn;
139 HARD_REG_SET live;
141 REG_SET_TO_HARD_REG_SET (live, df_get_live_in (b));
142 insn = BB_HEAD (b);
143 while (t)
145 /* Search forward until the next reference to the register to be
146 renamed. */
147 while (insn != t->insn)
149 if (INSN_P (insn))
151 clear_dead_regs (&live, REG_DEAD, REG_NOTES (insn));
152 note_stores (PATTERN (insn), note_sets, (void *) &live);
153 /* Only record currently live regs if we are inside the
154 reg's live range. */
155 if (t != chain)
156 IOR_HARD_REG_SET (*pset, live);
157 clear_dead_regs (&live, REG_UNUSED, REG_NOTES (insn));
159 insn = NEXT_INSN (insn);
162 IOR_HARD_REG_SET (*pset, live);
164 /* For the last reference, also merge in all registers set in the
165 same insn.
166 @@@ We only have take earlyclobbered sets into account. */
167 if (! t->next_use)
168 note_stores (PATTERN (insn), note_sets, (void *) pset);
170 t = t->next_use;
174 /* Perform register renaming on the current function. */
176 static void
177 regrename_optimize (void)
179 int tick[FIRST_PSEUDO_REGISTER];
180 int this_tick = 0;
181 basic_block bb;
182 char *first_obj;
184 df_set_flags (DF_LR_RUN_DCE);
185 df_note_add_problem ();
186 df_analyze ();
187 df_set_flags (DF_DEFER_INSN_RESCAN);
189 memset (tick, 0, sizeof tick);
191 gcc_obstack_init (&rename_obstack);
192 first_obj = XOBNEWVAR (&rename_obstack, char, 0);
194 FOR_EACH_BB (bb)
196 struct du_chain *all_chains = 0;
197 HARD_REG_SET unavailable;
198 HARD_REG_SET regs_seen;
200 CLEAR_HARD_REG_SET (unavailable);
202 if (dump_file)
203 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
205 all_chains = build_def_use (bb);
207 if (dump_file)
208 dump_def_use_chain (all_chains);
210 CLEAR_HARD_REG_SET (unavailable);
211 /* Don't clobber traceback for noreturn functions. */
212 if (frame_pointer_needed)
214 add_to_hard_reg_set (&unavailable, Pmode, FRAME_POINTER_REGNUM);
215 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
216 add_to_hard_reg_set (&unavailable, Pmode, HARD_FRAME_POINTER_REGNUM);
217 #endif
220 CLEAR_HARD_REG_SET (regs_seen);
221 while (all_chains)
223 int new_reg, best_new_reg;
224 int n_uses;
225 struct du_chain *this_du = all_chains;
226 struct du_chain *tmp, *last;
227 HARD_REG_SET this_unavailable;
228 int reg = REGNO (*this_du->loc);
229 int i;
231 all_chains = this_du->next_chain;
233 best_new_reg = reg;
235 #if 0 /* This just disables optimization opportunities. */
236 /* Only rename once we've seen the reg more than once. */
237 if (! TEST_HARD_REG_BIT (regs_seen, reg))
239 SET_HARD_REG_BIT (regs_seen, reg);
240 continue;
242 #endif
244 if (fixed_regs[reg] || global_regs[reg]
245 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
246 || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
247 #else
248 || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
249 #endif
251 continue;
253 COPY_HARD_REG_SET (this_unavailable, unavailable);
255 /* Find last entry on chain (which has the need_caller_save bit),
256 count number of uses, and narrow the set of registers we can
257 use for renaming. */
258 n_uses = 0;
259 for (last = this_du; last->next_use; last = last->next_use)
261 n_uses++;
262 IOR_COMPL_HARD_REG_SET (this_unavailable,
263 reg_class_contents[last->cl]);
265 if (n_uses < 1)
266 continue;
268 IOR_COMPL_HARD_REG_SET (this_unavailable,
269 reg_class_contents[last->cl]);
271 if (this_du->need_caller_save_reg)
272 IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
274 merge_overlapping_regs (bb, &this_unavailable, this_du);
276 /* Now potential_regs is a reasonable approximation, let's
277 have a closer look at each register still in there. */
278 for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
280 int nregs = hard_regno_nregs[new_reg][GET_MODE (*this_du->loc)];
282 for (i = nregs - 1; i >= 0; --i)
283 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
284 || fixed_regs[new_reg + i]
285 || global_regs[new_reg + i]
286 /* Can't use regs which aren't saved by the prologue. */
287 || (! df_regs_ever_live_p (new_reg + i)
288 && ! call_used_regs[new_reg + i])
289 #ifdef LEAF_REGISTERS
290 /* We can't use a non-leaf register if we're in a
291 leaf function. */
292 || (current_function_is_leaf
293 && !LEAF_REGISTERS[new_reg + i])
294 #endif
295 #ifdef HARD_REGNO_RENAME_OK
296 || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
297 #endif
299 break;
300 if (i >= 0)
301 continue;
303 /* See whether it accepts all modes that occur in
304 definition and uses. */
305 for (tmp = this_du; tmp; tmp = tmp->next_use)
306 if (! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
307 || (tmp->need_caller_save_reg
308 && ! (HARD_REGNO_CALL_PART_CLOBBERED
309 (reg, GET_MODE (*tmp->loc)))
310 && (HARD_REGNO_CALL_PART_CLOBBERED
311 (new_reg, GET_MODE (*tmp->loc)))))
312 break;
313 if (! tmp)
315 if (tick[best_new_reg] > tick[new_reg])
316 best_new_reg = new_reg;
320 if (dump_file)
322 fprintf (dump_file, "Register %s in insn %d",
323 reg_names[reg], INSN_UID (last->insn));
324 if (last->need_caller_save_reg)
325 fprintf (dump_file, " crosses a call");
328 if (best_new_reg == reg)
330 tick[reg] = ++this_tick;
331 if (dump_file)
332 fprintf (dump_file, "; no available better choice\n");
333 continue;
336 do_replace (this_du, best_new_reg);
337 tick[best_new_reg] = ++this_tick;
338 df_set_regs_ever_live (best_new_reg, true);
340 if (dump_file)
341 fprintf (dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
344 obstack_free (&rename_obstack, first_obj);
347 obstack_free (&rename_obstack, NULL);
349 if (dump_file)
350 fputc ('\n', dump_file);
353 static void
354 do_replace (struct du_chain *chain, int reg)
356 while (chain)
358 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
359 struct reg_attrs * attr = REG_ATTRS (*chain->loc);
360 int reg_ptr = REG_POINTER (*chain->loc);
362 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
363 if (regno >= FIRST_PSEUDO_REGISTER)
364 ORIGINAL_REGNO (*chain->loc) = regno;
365 REG_ATTRS (*chain->loc) = attr;
366 REG_POINTER (*chain->loc) = reg_ptr;
367 df_insn_rescan (chain->insn);
368 chain = chain->next_use;
373 static struct du_chain *open_chains;
374 static struct du_chain *closed_chains;
376 static void
377 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class cl,
378 enum scan_actions action, enum op_type type, int earlyclobber)
380 struct du_chain **p;
381 rtx x = *loc;
382 enum machine_mode mode = GET_MODE (x);
383 int this_regno = REGNO (x);
384 int this_nregs = hard_regno_nregs[this_regno][mode];
386 if (action == mark_write)
388 if (type == OP_OUT)
390 struct du_chain *this_du = XOBNEW (&rename_obstack, struct du_chain);
391 this_du->next_use = 0;
392 this_du->next_chain = open_chains;
393 this_du->loc = loc;
394 this_du->insn = insn;
395 this_du->cl = cl;
396 this_du->need_caller_save_reg = 0;
397 this_du->earlyclobber = earlyclobber;
398 open_chains = this_du;
400 return;
403 if ((type == OP_OUT) != (action == terminate_write || action == mark_access))
404 return;
406 for (p = &open_chains; *p;)
408 struct du_chain *this_du = *p;
410 /* Check if the chain has been terminated if it has then skip to
411 the next chain.
413 This can happen when we've already appended the location to
414 the chain in Step 3, but are trying to hide in-out operands
415 from terminate_write in Step 5. */
417 if (*this_du->loc == cc0_rtx)
418 p = &this_du->next_chain;
419 else
421 int regno = REGNO (*this_du->loc);
422 int nregs = hard_regno_nregs[regno][GET_MODE (*this_du->loc)];
423 int exact_match = (regno == this_regno && nregs == this_nregs);
425 if (regno + nregs <= this_regno
426 || this_regno + this_nregs <= regno)
428 p = &this_du->next_chain;
429 continue;
432 if (action == mark_read || action == mark_access)
434 gcc_assert (exact_match);
436 /* ??? Class NO_REGS can happen if the md file makes use of
437 EXTRA_CONSTRAINTS to match registers. Which is arguably
438 wrong, but there we are. Since we know not what this may
439 be replaced with, terminate the chain. */
440 if (cl != NO_REGS)
442 this_du = XOBNEW (&rename_obstack, struct du_chain);
443 this_du->next_use = 0;
444 this_du->next_chain = (*p)->next_chain;
445 this_du->loc = loc;
446 this_du->insn = insn;
447 this_du->cl = cl;
448 this_du->need_caller_save_reg = 0;
449 while (*p)
450 p = &(*p)->next_use;
451 *p = this_du;
452 return;
456 if (action != terminate_overlapping_read || ! exact_match)
458 struct du_chain *next = this_du->next_chain;
460 /* Whether the terminated chain can be used for renaming
461 depends on the action and this being an exact match.
462 In either case, we remove this element from open_chains. */
464 if ((action == terminate_dead || action == terminate_write)
465 && exact_match)
467 this_du->next_chain = closed_chains;
468 closed_chains = this_du;
469 if (dump_file)
470 fprintf (dump_file,
471 "Closing chain %s at insn %d (%s)\n",
472 reg_names[REGNO (*this_du->loc)], INSN_UID (insn),
473 scan_actions_name[(int) action]);
475 else
477 if (dump_file)
478 fprintf (dump_file,
479 "Discarding chain %s at insn %d (%s)\n",
480 reg_names[REGNO (*this_du->loc)], INSN_UID (insn),
481 scan_actions_name[(int) action]);
483 *p = next;
485 else
486 p = &this_du->next_chain;
491 /* Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
492 BASE_REG_CLASS depending on how the register is being considered. */
494 static void
495 scan_rtx_address (rtx insn, rtx *loc, enum reg_class cl,
496 enum scan_actions action, enum machine_mode mode)
498 rtx x = *loc;
499 RTX_CODE code = GET_CODE (x);
500 const char *fmt;
501 int i, j;
503 if (action == mark_write || action == mark_access)
504 return;
506 switch (code)
508 case PLUS:
510 rtx orig_op0 = XEXP (x, 0);
511 rtx orig_op1 = XEXP (x, 1);
512 RTX_CODE code0 = GET_CODE (orig_op0);
513 RTX_CODE code1 = GET_CODE (orig_op1);
514 rtx op0 = orig_op0;
515 rtx op1 = orig_op1;
516 rtx *locI = NULL;
517 rtx *locB = NULL;
518 enum rtx_code index_code = SCRATCH;
520 if (GET_CODE (op0) == SUBREG)
522 op0 = SUBREG_REG (op0);
523 code0 = GET_CODE (op0);
526 if (GET_CODE (op1) == SUBREG)
528 op1 = SUBREG_REG (op1);
529 code1 = GET_CODE (op1);
532 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
533 || code0 == ZERO_EXTEND || code1 == MEM)
535 locI = &XEXP (x, 0);
536 locB = &XEXP (x, 1);
537 index_code = GET_CODE (*locI);
539 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
540 || code1 == ZERO_EXTEND || code0 == MEM)
542 locI = &XEXP (x, 1);
543 locB = &XEXP (x, 0);
544 index_code = GET_CODE (*locI);
546 else if (code0 == CONST_INT || code0 == CONST
547 || code0 == SYMBOL_REF || code0 == LABEL_REF)
549 locB = &XEXP (x, 1);
550 index_code = GET_CODE (XEXP (x, 0));
552 else if (code1 == CONST_INT || code1 == CONST
553 || code1 == SYMBOL_REF || code1 == LABEL_REF)
555 locB = &XEXP (x, 0);
556 index_code = GET_CODE (XEXP (x, 1));
558 else if (code0 == REG && code1 == REG)
560 int index_op;
561 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
563 if (REGNO_OK_FOR_INDEX_P (regno1)
564 && regno_ok_for_base_p (regno0, mode, PLUS, REG))
565 index_op = 1;
566 else if (REGNO_OK_FOR_INDEX_P (regno0)
567 && regno_ok_for_base_p (regno1, mode, PLUS, REG))
568 index_op = 0;
569 else if (regno_ok_for_base_p (regno0, mode, PLUS, REG)
570 || REGNO_OK_FOR_INDEX_P (regno1))
571 index_op = 1;
572 else if (regno_ok_for_base_p (regno1, mode, PLUS, REG))
573 index_op = 0;
574 else
575 index_op = 1;
577 locI = &XEXP (x, index_op);
578 locB = &XEXP (x, !index_op);
579 index_code = GET_CODE (*locI);
581 else if (code0 == REG)
583 locI = &XEXP (x, 0);
584 locB = &XEXP (x, 1);
585 index_code = GET_CODE (*locI);
587 else if (code1 == REG)
589 locI = &XEXP (x, 1);
590 locB = &XEXP (x, 0);
591 index_code = GET_CODE (*locI);
594 if (locI)
595 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
596 if (locB)
597 scan_rtx_address (insn, locB, base_reg_class (mode, PLUS, index_code),
598 action, mode);
600 return;
603 case POST_INC:
604 case POST_DEC:
605 case POST_MODIFY:
606 case PRE_INC:
607 case PRE_DEC:
608 case PRE_MODIFY:
609 #ifndef AUTO_INC_DEC
610 /* If the target doesn't claim to handle autoinc, this must be
611 something special, like a stack push. Kill this chain. */
612 action = terminate_all_read;
613 #endif
614 break;
616 case MEM:
617 scan_rtx_address (insn, &XEXP (x, 0),
618 base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
619 GET_MODE (x));
620 return;
622 case REG:
623 scan_rtx_reg (insn, loc, cl, action, OP_IN, 0);
624 return;
626 default:
627 break;
630 fmt = GET_RTX_FORMAT (code);
631 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
633 if (fmt[i] == 'e')
634 scan_rtx_address (insn, &XEXP (x, i), cl, action, mode);
635 else if (fmt[i] == 'E')
636 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
637 scan_rtx_address (insn, &XVECEXP (x, i, j), cl, action, mode);
641 static void
642 scan_rtx (rtx insn, rtx *loc, enum reg_class cl,
643 enum scan_actions action, enum op_type type, int earlyclobber)
645 const char *fmt;
646 rtx x = *loc;
647 enum rtx_code code = GET_CODE (x);
648 int i, j;
650 code = GET_CODE (x);
651 switch (code)
653 case CONST:
654 case CONST_INT:
655 case CONST_DOUBLE:
656 case CONST_FIXED:
657 case CONST_VECTOR:
658 case SYMBOL_REF:
659 case LABEL_REF:
660 case CC0:
661 case PC:
662 return;
664 case REG:
665 scan_rtx_reg (insn, loc, cl, action, type, earlyclobber);
666 return;
668 case MEM:
669 scan_rtx_address (insn, &XEXP (x, 0),
670 base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
671 GET_MODE (x));
672 return;
674 case SET:
675 scan_rtx (insn, &SET_SRC (x), cl, action, OP_IN, 0);
676 scan_rtx (insn, &SET_DEST (x), cl, action,
677 GET_CODE (PATTERN (insn)) == COND_EXEC ? OP_INOUT : OP_OUT, 0);
678 return;
680 case STRICT_LOW_PART:
681 scan_rtx (insn, &XEXP (x, 0), cl, action, OP_INOUT, earlyclobber);
682 return;
684 case ZERO_EXTRACT:
685 case SIGN_EXTRACT:
686 scan_rtx (insn, &XEXP (x, 0), cl, action,
687 type == OP_IN ? OP_IN : OP_INOUT, earlyclobber);
688 scan_rtx (insn, &XEXP (x, 1), cl, action, OP_IN, 0);
689 scan_rtx (insn, &XEXP (x, 2), cl, action, OP_IN, 0);
690 return;
692 case POST_INC:
693 case PRE_INC:
694 case POST_DEC:
695 case PRE_DEC:
696 case POST_MODIFY:
697 case PRE_MODIFY:
698 /* Should only happen inside MEM. */
699 gcc_unreachable ();
701 case CLOBBER:
702 scan_rtx (insn, &SET_DEST (x), cl, action,
703 GET_CODE (PATTERN (insn)) == COND_EXEC ? OP_INOUT : OP_OUT, 0);
704 return;
706 case EXPR_LIST:
707 scan_rtx (insn, &XEXP (x, 0), cl, action, type, 0);
708 if (XEXP (x, 1))
709 scan_rtx (insn, &XEXP (x, 1), cl, action, type, 0);
710 return;
712 default:
713 break;
716 fmt = GET_RTX_FORMAT (code);
717 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
719 if (fmt[i] == 'e')
720 scan_rtx (insn, &XEXP (x, i), cl, 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), cl, action, type, 0);
727 /* Build def/use chain. */
729 static struct du_chain *
730 build_def_use (basic_block bb)
732 rtx insn;
734 open_chains = closed_chains = NULL;
736 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
738 if (INSN_P (insn))
740 int n_ops;
741 rtx note;
742 rtx old_operands[MAX_RECOG_OPERANDS];
743 rtx old_dups[MAX_DUP_OPERANDS];
744 int i, icode;
745 int alt;
746 int predicated;
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
753 with overlaps yet.
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
759 overlap it.
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);
765 extract_insn (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;
780 if (matches >= 0)
781 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
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')
804 continue;
805 *recog_data.operand_loc[i] = cc0_rtx;
807 for (i = 0; i < recog_data.n_dups; i++)
809 old_dups[i] = *recog_data.dup_loc[i];
810 *recog_data.dup_loc[i] = cc0_rtx;
813 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
814 OP_IN, 0);
816 for (i = 0; i < recog_data.n_dups; i++)
817 *recog_data.dup_loc[i] = old_dups[i];
818 for (i = 0; i < n_ops; i++)
819 *recog_data.operand_loc[i] = old_operands[i];
820 if (recog_data.n_dups)
821 df_insn_rescan (insn);
823 /* Step 2B: Can't rename function call argument registers. */
824 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
825 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
826 NO_REGS, terminate_all_read, OP_IN, 0);
828 /* Step 2C: Can't rename asm operands that were originally
829 hard registers. */
830 if (asm_noperands (PATTERN (insn)) > 0)
831 for (i = 0; i < n_ops; i++)
833 rtx *loc = recog_data.operand_loc[i];
834 rtx op = *loc;
836 if (REG_P (op)
837 && REGNO (op) == ORIGINAL_REGNO (op)
838 && (recog_data.operand_type[i] == OP_IN
839 || recog_data.operand_type[i] == OP_INOUT))
840 scan_rtx (insn, loc, NO_REGS, terminate_all_read, OP_IN, 0);
843 /* Step 3: Append to chains for reads inside operands. */
844 for (i = 0; i < n_ops + recog_data.n_dups; i++)
846 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
847 rtx *loc = (i < n_ops
848 ? recog_data.operand_loc[opn]
849 : recog_data.dup_loc[i - n_ops]);
850 enum reg_class cl = recog_op_alt[opn][alt].cl;
851 enum op_type type = recog_data.operand_type[opn];
853 /* Don't scan match_operand here, since we've no reg class
854 information to pass down. Any operands that we could
855 substitute in will be represented elsewhere. */
856 if (recog_data.constraints[opn][0] == '\0')
857 continue;
859 if (recog_op_alt[opn][alt].is_address)
860 scan_rtx_address (insn, loc, cl, mark_read, VOIDmode);
861 else
862 scan_rtx (insn, loc, cl, mark_read, type, 0);
865 /* Step 3B: Record updates for regs in REG_INC notes, and
866 source regs in REG_FRAME_RELATED_EXPR notes. */
867 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
868 if (REG_NOTE_KIND (note) == REG_INC
869 || REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
870 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
871 OP_INOUT, 0);
873 /* Step 4: Close chains for registers that die here. */
874 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,
877 OP_IN, 0);
879 /* Step 4B: If this is a call, any chain live at this point
880 requires a caller-saved reg. */
881 if (CALL_P (insn))
883 struct du_chain *p;
884 for (p = open_chains; p; p = p->next_chain)
885 p->need_caller_save_reg = 1;
888 /* Step 5: Close open chains that overlap writes. Similar to
889 step 2, we hide in-out operands, since we do not want to
890 close these chains. */
892 for (i = 0; i < n_ops; i++)
894 old_operands[i] = recog_data.operand[i];
895 if (recog_data.operand_type[i] == OP_INOUT)
896 *recog_data.operand_loc[i] = cc0_rtx;
898 for (i = 0; i < recog_data.n_dups; i++)
900 int opn = recog_data.dup_num[i];
901 old_dups[i] = *recog_data.dup_loc[i];
902 if (recog_data.operand_type[opn] == OP_INOUT)
903 *recog_data.dup_loc[i] = cc0_rtx;
906 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
908 for (i = 0; i < recog_data.n_dups; i++)
909 *recog_data.dup_loc[i] = old_dups[i];
910 for (i = 0; i < n_ops; i++)
911 *recog_data.operand_loc[i] = old_operands[i];
913 /* Step 6: Begin new chains for writes inside operands. */
914 /* ??? Many targets have output constraints on the SET_DEST
915 of a call insn, which is stupid, since these are certainly
916 ABI defined hard registers. Don't change calls at all.
917 Similarly take special care for asm statement that originally
918 referenced hard registers. */
919 if (asm_noperands (PATTERN (insn)) > 0)
921 for (i = 0; i < n_ops; i++)
922 if (recog_data.operand_type[i] == OP_OUT)
924 rtx *loc = recog_data.operand_loc[i];
925 rtx op = *loc;
926 enum reg_class cl = recog_op_alt[i][alt].cl;
928 if (REG_P (op)
929 && REGNO (op) == ORIGINAL_REGNO (op))
930 continue;
932 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
933 recog_op_alt[i][alt].earlyclobber);
936 else if (!CALL_P (insn))
937 for (i = 0; i < n_ops + recog_data.n_dups; i++)
939 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
940 rtx *loc = (i < n_ops
941 ? recog_data.operand_loc[opn]
942 : recog_data.dup_loc[i - n_ops]);
943 enum reg_class cl = recog_op_alt[opn][alt].cl;
945 if (recog_data.operand_type[opn] == OP_OUT)
946 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
947 recog_op_alt[opn][alt].earlyclobber);
950 /* Step 6B: Record destination regs in REG_FRAME_RELATED_EXPR
951 notes for update. */
952 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
953 if (REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
954 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_access,
955 OP_INOUT, 0);
957 /* Step 7: Close chains for registers that were never
958 really used here. */
959 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
960 if (REG_NOTE_KIND (note) == REG_UNUSED)
961 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
962 OP_IN, 0);
964 if (insn == BB_END (bb))
965 break;
968 /* Since we close every chain when we find a REG_DEAD note, anything that
969 is still open lives past the basic block, so it can't be renamed. */
970 return closed_chains;
973 /* Dump all def/use chains in CHAINS to DUMP_FILE. They are
974 printed in reverse order as that's how we build them. */
976 static void
977 dump_def_use_chain (struct du_chain *chains)
979 while (chains)
981 struct du_chain *this_du = chains;
982 int r = REGNO (*this_du->loc);
983 int nregs = hard_regno_nregs[r][GET_MODE (*this_du->loc)];
984 fprintf (dump_file, "Register %s (%d):", reg_names[r], nregs);
985 while (this_du)
987 fprintf (dump_file, " %d [%s]", INSN_UID (this_du->insn),
988 reg_class_names[this_du->cl]);
989 this_du = this_du->next_use;
991 fprintf (dump_file, "\n");
992 chains = chains->next_chain;
996 /* The following code does forward propagation of hard register copies.
997 The object is to eliminate as many dependencies as possible, so that
998 we have the most scheduling freedom. As a side effect, we also clean
999 up some silly register allocation decisions made by reload. This
1000 code may be obsoleted by a new register allocator. */
1002 /* For each register, we have a list of registers that contain the same
1003 value. The OLDEST_REGNO field points to the head of the list, and
1004 the NEXT_REGNO field runs through the list. The MODE field indicates
1005 what mode the data is known to be in; this field is VOIDmode when the
1006 register is not known to contain valid data. */
1008 struct value_data_entry
1010 enum machine_mode mode;
1011 unsigned int oldest_regno;
1012 unsigned int next_regno;
1015 struct value_data
1017 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
1018 unsigned int max_value_regs;
1021 static void kill_value_one_regno (unsigned, struct value_data *);
1022 static void kill_value_regno (unsigned, unsigned, struct value_data *);
1023 static void kill_value (rtx, struct value_data *);
1024 static void set_value_regno (unsigned, enum machine_mode, struct value_data *);
1025 static void init_value_data (struct value_data *);
1026 static void kill_clobbered_value (rtx, const_rtx, void *);
1027 static void kill_set_value (rtx, const_rtx, void *);
1028 static int kill_autoinc_value (rtx *, void *);
1029 static void copy_value (rtx, rtx, struct value_data *);
1030 static bool mode_change_ok (enum machine_mode, enum machine_mode,
1031 unsigned int);
1032 static rtx maybe_mode_change (enum machine_mode, enum machine_mode,
1033 enum machine_mode, unsigned int, unsigned int);
1034 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
1035 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx,
1036 struct value_data *);
1037 static bool replace_oldest_value_addr (rtx *, enum reg_class,
1038 enum machine_mode, rtx,
1039 struct value_data *);
1040 static bool replace_oldest_value_mem (rtx, rtx, struct value_data *);
1041 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
1042 extern void debug_value_data (struct value_data *);
1043 #ifdef ENABLE_CHECKING
1044 static void validate_value_data (struct value_data *);
1045 #endif
1047 /* Kill register REGNO. This involves removing it from any value
1048 lists, and resetting the value mode to VOIDmode. This is only a
1049 helper function; it does not handle any hard registers overlapping
1050 with REGNO. */
1052 static void
1053 kill_value_one_regno (unsigned int regno, struct value_data *vd)
1055 unsigned int i, next;
1057 if (vd->e[regno].oldest_regno != regno)
1059 for (i = vd->e[regno].oldest_regno;
1060 vd->e[i].next_regno != regno;
1061 i = vd->e[i].next_regno)
1062 continue;
1063 vd->e[i].next_regno = vd->e[regno].next_regno;
1065 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
1067 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
1068 vd->e[i].oldest_regno = next;
1071 vd->e[regno].mode = VOIDmode;
1072 vd->e[regno].oldest_regno = regno;
1073 vd->e[regno].next_regno = INVALID_REGNUM;
1075 #ifdef ENABLE_CHECKING
1076 validate_value_data (vd);
1077 #endif
1080 /* Kill the value in register REGNO for NREGS, and any other registers
1081 whose values overlap. */
1083 static void
1084 kill_value_regno (unsigned int regno, unsigned int nregs,
1085 struct value_data *vd)
1087 unsigned int j;
1089 /* Kill the value we're told to kill. */
1090 for (j = 0; j < nregs; ++j)
1091 kill_value_one_regno (regno + j, vd);
1093 /* Kill everything that overlapped what we're told to kill. */
1094 if (regno < vd->max_value_regs)
1095 j = 0;
1096 else
1097 j = regno - vd->max_value_regs;
1098 for (; j < regno; ++j)
1100 unsigned int i, n;
1101 if (vd->e[j].mode == VOIDmode)
1102 continue;
1103 n = hard_regno_nregs[j][vd->e[j].mode];
1104 if (j + n > regno)
1105 for (i = 0; i < n; ++i)
1106 kill_value_one_regno (j + i, vd);
1110 /* Kill X. This is a convenience function wrapping kill_value_regno
1111 so that we mind the mode the register is in. */
1113 static void
1114 kill_value (rtx x, struct value_data *vd)
1116 rtx orig_rtx = x;
1118 if (GET_CODE (x) == SUBREG)
1120 x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
1121 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
1122 if (x == NULL_RTX)
1123 x = SUBREG_REG (orig_rtx);
1125 if (REG_P (x))
1127 unsigned int regno = REGNO (x);
1128 unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
1130 kill_value_regno (regno, n, vd);
1134 /* Remember that REGNO is valid in MODE. */
1136 static void
1137 set_value_regno (unsigned int regno, enum machine_mode mode,
1138 struct value_data *vd)
1140 unsigned int nregs;
1142 vd->e[regno].mode = mode;
1144 nregs = hard_regno_nregs[regno][mode];
1145 if (nregs > vd->max_value_regs)
1146 vd->max_value_regs = nregs;
1149 /* Initialize VD such that there are no known relationships between regs. */
1151 static void
1152 init_value_data (struct value_data *vd)
1154 int i;
1155 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1157 vd->e[i].mode = VOIDmode;
1158 vd->e[i].oldest_regno = i;
1159 vd->e[i].next_regno = INVALID_REGNUM;
1161 vd->max_value_regs = 0;
1164 /* Called through note_stores. If X is clobbered, kill its value. */
1166 static void
1167 kill_clobbered_value (rtx x, const_rtx set, void *data)
1169 struct value_data *const vd = (struct value_data *) data;
1170 if (GET_CODE (set) == CLOBBER)
1171 kill_value (x, vd);
1174 /* Called through note_stores. If X is set, not clobbered, kill its
1175 current value and install it as the root of its own value list. */
1177 static void
1178 kill_set_value (rtx x, const_rtx set, void *data)
1180 struct value_data *const vd = (struct value_data *) data;
1181 if (GET_CODE (set) != CLOBBER)
1183 kill_value (x, vd);
1184 if (REG_P (x))
1185 set_value_regno (REGNO (x), GET_MODE (x), vd);
1189 /* Called through for_each_rtx. Kill any register used as the base of an
1190 auto-increment expression, and install that register as the root of its
1191 own value list. */
1193 static int
1194 kill_autoinc_value (rtx *px, void *data)
1196 rtx x = *px;
1197 struct value_data *const vd = (struct value_data *) data;
1199 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
1201 x = XEXP (x, 0);
1202 kill_value (x, vd);
1203 set_value_regno (REGNO (x), Pmode, vd);
1204 return -1;
1207 return 0;
1210 /* Assert that SRC has been copied to DEST. Adjust the data structures
1211 to reflect that SRC contains an older copy of the shared value. */
1213 static void
1214 copy_value (rtx dest, rtx src, struct value_data *vd)
1216 unsigned int dr = REGNO (dest);
1217 unsigned int sr = REGNO (src);
1218 unsigned int dn, sn;
1219 unsigned int i;
1221 /* ??? At present, it's possible to see noop sets. It'd be nice if
1222 this were cleaned up beforehand... */
1223 if (sr == dr)
1224 return;
1226 /* Do not propagate copies to the stack pointer, as that can leave
1227 memory accesses with no scheduling dependency on the stack update. */
1228 if (dr == STACK_POINTER_REGNUM)
1229 return;
1231 /* Likewise with the frame pointer, if we're using one. */
1232 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
1233 return;
1235 /* Do not propagate copies to fixed or global registers, patterns
1236 can be relying to see particular fixed register or users can
1237 expect the chosen global register in asm. */
1238 if (fixed_regs[dr] || global_regs[dr])
1239 return;
1241 /* If SRC and DEST overlap, don't record anything. */
1242 dn = hard_regno_nregs[dr][GET_MODE (dest)];
1243 sn = hard_regno_nregs[sr][GET_MODE (dest)];
1244 if ((dr > sr && dr < sr + sn)
1245 || (sr > dr && sr < dr + dn))
1246 return;
1248 /* If SRC had no assigned mode (i.e. we didn't know it was live)
1249 assign it now and assume the value came from an input argument
1250 or somesuch. */
1251 if (vd->e[sr].mode == VOIDmode)
1252 set_value_regno (sr, vd->e[dr].mode, vd);
1254 /* If we are narrowing the input to a smaller number of hard regs,
1255 and it is in big endian, we are really extracting a high part.
1256 Since we generally associate a low part of a value with the value itself,
1257 we must not do the same for the high part.
1258 Note we can still get low parts for the same mode combination through
1259 a two-step copy involving differently sized hard regs.
1260 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
1261 (set (reg:DI r0) (reg:DI fr0))
1262 (set (reg:SI fr2) (reg:SI r0))
1263 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
1264 (set (reg:SI fr2) (reg:SI fr0))
1265 loads the high part of (reg:DI fr0) into fr2.
1267 We can't properly represent the latter case in our tables, so don't
1268 record anything then. */
1269 else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
1270 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
1271 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
1272 return;
1274 /* If SRC had been assigned a mode narrower than the copy, we can't
1275 link DEST into the chain, because not all of the pieces of the
1276 copy came from oldest_regno. */
1277 else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
1278 return;
1280 /* Link DR at the end of the value chain used by SR. */
1282 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
1284 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
1285 continue;
1286 vd->e[i].next_regno = dr;
1288 #ifdef ENABLE_CHECKING
1289 validate_value_data (vd);
1290 #endif
1293 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
1295 static bool
1296 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
1297 unsigned int regno ATTRIBUTE_UNUSED)
1299 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
1300 return false;
1302 #ifdef CANNOT_CHANGE_MODE_CLASS
1303 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
1304 #endif
1306 return true;
1309 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
1310 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
1311 in NEW_MODE.
1312 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
1314 static rtx
1315 maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
1316 enum machine_mode new_mode, unsigned int regno,
1317 unsigned int copy_regno ATTRIBUTE_UNUSED)
1319 if (GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (orig_mode)
1320 && GET_MODE_SIZE (copy_mode) < GET_MODE_SIZE (new_mode))
1321 return NULL_RTX;
1323 if (orig_mode == new_mode)
1324 return gen_rtx_raw_REG (new_mode, regno);
1325 else if (mode_change_ok (orig_mode, new_mode, regno))
1327 int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
1328 int use_nregs = hard_regno_nregs[copy_regno][new_mode];
1329 int copy_offset
1330 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
1331 int offset
1332 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
1333 int byteoffset = offset % UNITS_PER_WORD;
1334 int wordoffset = offset - byteoffset;
1336 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
1337 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
1338 return gen_rtx_raw_REG (new_mode,
1339 regno + subreg_regno_offset (regno, orig_mode,
1340 offset,
1341 new_mode));
1343 return NULL_RTX;
1346 /* Find the oldest copy of the value contained in REGNO that is in
1347 register class CL and has mode MODE. If found, return an rtx
1348 of that oldest register, otherwise return NULL. */
1350 static rtx
1351 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
1353 unsigned int regno = REGNO (reg);
1354 enum machine_mode mode = GET_MODE (reg);
1355 unsigned int i;
1357 /* If we are accessing REG in some mode other that what we set it in,
1358 make sure that the replacement is valid. In particular, consider
1359 (set (reg:DI r11) (...))
1360 (set (reg:SI r9) (reg:SI r11))
1361 (set (reg:SI r10) (...))
1362 (set (...) (reg:DI r9))
1363 Replacing r9 with r11 is invalid. */
1364 if (mode != vd->e[regno].mode)
1366 if (hard_regno_nregs[regno][mode]
1367 > hard_regno_nregs[regno][vd->e[regno].mode])
1368 return NULL_RTX;
1371 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
1373 enum machine_mode oldmode = vd->e[i].mode;
1374 rtx new_rtx;
1376 if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
1377 return NULL_RTX;
1379 new_rtx = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
1380 if (new_rtx)
1382 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (reg);
1383 REG_ATTRS (new_rtx) = REG_ATTRS (reg);
1384 return new_rtx;
1388 return NULL_RTX;
1391 /* If possible, replace the register at *LOC with the oldest register
1392 in register class CL. Return true if successfully replaced. */
1394 static bool
1395 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx insn,
1396 struct value_data *vd)
1398 rtx new_rtx = find_oldest_value_reg (cl, *loc, vd);
1399 if (new_rtx)
1401 if (dump_file)
1402 fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
1403 INSN_UID (insn), REGNO (*loc), REGNO (new_rtx));
1405 validate_change (insn, loc, new_rtx, 1);
1406 return true;
1408 return false;
1411 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1412 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
1413 BASE_REG_CLASS depending on how the register is being considered. */
1415 static bool
1416 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
1417 enum machine_mode mode, rtx insn,
1418 struct value_data *vd)
1420 rtx x = *loc;
1421 RTX_CODE code = GET_CODE (x);
1422 const char *fmt;
1423 int i, j;
1424 bool changed = false;
1426 switch (code)
1428 case PLUS:
1430 rtx orig_op0 = XEXP (x, 0);
1431 rtx orig_op1 = XEXP (x, 1);
1432 RTX_CODE code0 = GET_CODE (orig_op0);
1433 RTX_CODE code1 = GET_CODE (orig_op1);
1434 rtx op0 = orig_op0;
1435 rtx op1 = orig_op1;
1436 rtx *locI = NULL;
1437 rtx *locB = NULL;
1438 enum rtx_code index_code = SCRATCH;
1440 if (GET_CODE (op0) == SUBREG)
1442 op0 = SUBREG_REG (op0);
1443 code0 = GET_CODE (op0);
1446 if (GET_CODE (op1) == SUBREG)
1448 op1 = SUBREG_REG (op1);
1449 code1 = GET_CODE (op1);
1452 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1453 || code0 == ZERO_EXTEND || code1 == MEM)
1455 locI = &XEXP (x, 0);
1456 locB = &XEXP (x, 1);
1457 index_code = GET_CODE (*locI);
1459 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1460 || code1 == ZERO_EXTEND || code0 == MEM)
1462 locI = &XEXP (x, 1);
1463 locB = &XEXP (x, 0);
1464 index_code = GET_CODE (*locI);
1466 else if (code0 == CONST_INT || code0 == CONST
1467 || code0 == SYMBOL_REF || code0 == LABEL_REF)
1469 locB = &XEXP (x, 1);
1470 index_code = GET_CODE (XEXP (x, 0));
1472 else if (code1 == CONST_INT || code1 == CONST
1473 || code1 == SYMBOL_REF || code1 == LABEL_REF)
1475 locB = &XEXP (x, 0);
1476 index_code = GET_CODE (XEXP (x, 1));
1478 else if (code0 == REG && code1 == REG)
1480 int index_op;
1481 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
1483 if (REGNO_OK_FOR_INDEX_P (regno1)
1484 && regno_ok_for_base_p (regno0, mode, PLUS, REG))
1485 index_op = 1;
1486 else if (REGNO_OK_FOR_INDEX_P (regno0)
1487 && regno_ok_for_base_p (regno1, mode, PLUS, REG))
1488 index_op = 0;
1489 else if (regno_ok_for_base_p (regno0, mode, PLUS, REG)
1490 || REGNO_OK_FOR_INDEX_P (regno1))
1491 index_op = 1;
1492 else if (regno_ok_for_base_p (regno1, mode, PLUS, REG))
1493 index_op = 0;
1494 else
1495 index_op = 1;
1497 locI = &XEXP (x, index_op);
1498 locB = &XEXP (x, !index_op);
1499 index_code = GET_CODE (*locI);
1501 else if (code0 == REG)
1503 locI = &XEXP (x, 0);
1504 locB = &XEXP (x, 1);
1505 index_code = GET_CODE (*locI);
1507 else if (code1 == REG)
1509 locI = &XEXP (x, 1);
1510 locB = &XEXP (x, 0);
1511 index_code = GET_CODE (*locI);
1514 if (locI)
1515 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
1516 insn, vd);
1517 if (locB)
1518 changed |= replace_oldest_value_addr (locB,
1519 base_reg_class (mode, PLUS,
1520 index_code),
1521 mode, insn, vd);
1522 return changed;
1525 case POST_INC:
1526 case POST_DEC:
1527 case POST_MODIFY:
1528 case PRE_INC:
1529 case PRE_DEC:
1530 case PRE_MODIFY:
1531 return false;
1533 case MEM:
1534 return replace_oldest_value_mem (x, insn, vd);
1536 case REG:
1537 return replace_oldest_value_reg (loc, cl, insn, vd);
1539 default:
1540 break;
1543 fmt = GET_RTX_FORMAT (code);
1544 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1546 if (fmt[i] == 'e')
1547 changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode,
1548 insn, vd);
1549 else if (fmt[i] == 'E')
1550 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1551 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
1552 mode, insn, vd);
1555 return changed;
1558 /* Similar to replace_oldest_value_reg, but X contains a memory. */
1560 static bool
1561 replace_oldest_value_mem (rtx x, rtx insn, struct value_data *vd)
1563 return replace_oldest_value_addr (&XEXP (x, 0),
1564 base_reg_class (GET_MODE (x), MEM,
1565 SCRATCH),
1566 GET_MODE (x), insn, vd);
1569 /* Perform the forward copy propagation on basic block BB. */
1571 static bool
1572 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
1574 bool changed = false;
1575 rtx insn;
1577 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1579 int n_ops, i, alt, predicated;
1580 bool is_asm, any_replacements;
1581 rtx set;
1582 bool replaced[MAX_RECOG_OPERANDS];
1584 if (! INSN_P (insn))
1586 if (insn == BB_END (bb))
1587 break;
1588 else
1589 continue;
1592 set = single_set (insn);
1593 extract_insn (insn);
1594 if (! constrain_operands (1))
1595 fatal_insn_not_found (insn);
1596 preprocess_constraints ();
1597 alt = which_alternative;
1598 n_ops = recog_data.n_operands;
1599 is_asm = asm_noperands (PATTERN (insn)) >= 0;
1601 /* Simplify the code below by rewriting things to reflect
1602 matching constraints. Also promote OP_OUT to OP_INOUT
1603 in predicated instructions. */
1605 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1606 for (i = 0; i < n_ops; ++i)
1608 int matches = recog_op_alt[i][alt].matches;
1609 if (matches >= 0)
1610 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
1611 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1612 || (predicated && recog_data.operand_type[i] == OP_OUT))
1613 recog_data.operand_type[i] = OP_INOUT;
1616 /* For each earlyclobber operand, zap the value data. */
1617 for (i = 0; i < n_ops; i++)
1618 if (recog_op_alt[i][alt].earlyclobber)
1619 kill_value (recog_data.operand[i], vd);
1621 /* Within asms, a clobber cannot overlap inputs or outputs.
1622 I wouldn't think this were true for regular insns, but
1623 scan_rtx treats them like that... */
1624 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1626 /* Kill all auto-incremented values. */
1627 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
1628 for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
1630 /* Kill all early-clobbered operands. */
1631 for (i = 0; i < n_ops; i++)
1632 if (recog_op_alt[i][alt].earlyclobber)
1633 kill_value (recog_data.operand[i], vd);
1635 /* Special-case plain move instructions, since we may well
1636 be able to do the move from a different register class. */
1637 if (set && REG_P (SET_SRC (set)))
1639 rtx src = SET_SRC (set);
1640 unsigned int regno = REGNO (src);
1641 enum machine_mode mode = GET_MODE (src);
1642 unsigned int i;
1643 rtx new_rtx;
1645 /* If we are accessing SRC in some mode other that what we
1646 set it in, make sure that the replacement is valid. */
1647 if (mode != vd->e[regno].mode)
1649 if (hard_regno_nregs[regno][mode]
1650 > hard_regno_nregs[regno][vd->e[regno].mode])
1651 goto no_move_special_case;
1654 /* If the destination is also a register, try to find a source
1655 register in the same class. */
1656 if (REG_P (SET_DEST (set)))
1658 new_rtx = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
1659 if (new_rtx && validate_change (insn, &SET_SRC (set), new_rtx, 0))
1661 if (dump_file)
1662 fprintf (dump_file,
1663 "insn %u: replaced reg %u with %u\n",
1664 INSN_UID (insn), regno, REGNO (new_rtx));
1665 changed = true;
1666 goto did_replacement;
1670 /* Otherwise, try all valid registers and see if its valid. */
1671 for (i = vd->e[regno].oldest_regno; i != regno;
1672 i = vd->e[i].next_regno)
1674 new_rtx = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
1675 mode, i, regno);
1676 if (new_rtx != NULL_RTX)
1678 if (validate_change (insn, &SET_SRC (set), new_rtx, 0))
1680 ORIGINAL_REGNO (new_rtx) = ORIGINAL_REGNO (src);
1681 REG_ATTRS (new_rtx) = REG_ATTRS (src);
1682 if (dump_file)
1683 fprintf (dump_file,
1684 "insn %u: replaced reg %u with %u\n",
1685 INSN_UID (insn), regno, REGNO (new_rtx));
1686 changed = true;
1687 goto did_replacement;
1692 no_move_special_case:
1694 any_replacements = false;
1696 /* For each input operand, replace a hard register with the
1697 eldest live copy that's in an appropriate register class. */
1698 for (i = 0; i < n_ops; i++)
1700 replaced[i] = false;
1702 /* Don't scan match_operand here, since we've no reg class
1703 information to pass down. Any operands that we could
1704 substitute in will be represented elsewhere. */
1705 if (recog_data.constraints[i][0] == '\0')
1706 continue;
1708 /* Don't replace in asms intentionally referencing hard regs. */
1709 if (is_asm && REG_P (recog_data.operand[i])
1710 && (REGNO (recog_data.operand[i])
1711 == ORIGINAL_REGNO (recog_data.operand[i])))
1712 continue;
1714 if (recog_data.operand_type[i] == OP_IN)
1716 if (recog_op_alt[i][alt].is_address)
1717 replaced[i]
1718 = replace_oldest_value_addr (recog_data.operand_loc[i],
1719 recog_op_alt[i][alt].cl,
1720 VOIDmode, insn, vd);
1721 else if (REG_P (recog_data.operand[i]))
1722 replaced[i]
1723 = replace_oldest_value_reg (recog_data.operand_loc[i],
1724 recog_op_alt[i][alt].cl,
1725 insn, vd);
1726 else if (MEM_P (recog_data.operand[i]))
1727 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
1728 insn, vd);
1730 else if (MEM_P (recog_data.operand[i]))
1731 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
1732 insn, vd);
1734 /* If we performed any replacement, update match_dups. */
1735 if (replaced[i])
1737 int j;
1738 rtx new_rtx;
1740 new_rtx = *recog_data.operand_loc[i];
1741 recog_data.operand[i] = new_rtx;
1742 for (j = 0; j < recog_data.n_dups; j++)
1743 if (recog_data.dup_num[j] == i)
1744 validate_unshare_change (insn, recog_data.dup_loc[j], new_rtx, 1);
1746 any_replacements = true;
1750 if (any_replacements)
1752 if (! apply_change_group ())
1754 for (i = 0; i < n_ops; i++)
1755 if (replaced[i])
1757 rtx old = *recog_data.operand_loc[i];
1758 recog_data.operand[i] = old;
1761 if (dump_file)
1762 fprintf (dump_file,
1763 "insn %u: reg replacements not verified\n",
1764 INSN_UID (insn));
1766 else
1767 changed = true;
1770 did_replacement:
1771 /* Clobber call-clobbered registers. */
1772 if (CALL_P (insn))
1773 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1774 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1775 kill_value_regno (i, 1, vd);
1777 /* Notice stores. */
1778 note_stores (PATTERN (insn), kill_set_value, vd);
1780 /* Notice copies. */
1781 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1782 copy_value (SET_DEST (set), SET_SRC (set), vd);
1784 if (insn == BB_END (bb))
1785 break;
1788 return changed;
1791 /* Main entry point for the forward copy propagation optimization. */
1793 static void
1794 copyprop_hardreg_forward (void)
1796 struct value_data *all_vd;
1797 basic_block bb;
1798 sbitmap visited;
1800 all_vd = XNEWVEC (struct value_data, last_basic_block);
1802 visited = sbitmap_alloc (last_basic_block);
1803 sbitmap_zero (visited);
1805 FOR_EACH_BB (bb)
1807 SET_BIT (visited, bb->index);
1809 /* If a block has a single predecessor, that we've already
1810 processed, begin with the value data that was live at
1811 the end of the predecessor block. */
1812 /* ??? Ought to use more intelligent queuing of blocks. */
1813 if (single_pred_p (bb)
1814 && TEST_BIT (visited, single_pred (bb)->index)
1815 && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1816 all_vd[bb->index] = all_vd[single_pred (bb)->index];
1817 else
1818 init_value_data (all_vd + bb->index);
1820 copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
1823 sbitmap_free (visited);
1824 free (all_vd);
1827 /* Dump the value chain data to stderr. */
1829 void
1830 debug_value_data (struct value_data *vd)
1832 HARD_REG_SET set;
1833 unsigned int i, j;
1835 CLEAR_HARD_REG_SET (set);
1837 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1838 if (vd->e[i].oldest_regno == i)
1840 if (vd->e[i].mode == VOIDmode)
1842 if (vd->e[i].next_regno != INVALID_REGNUM)
1843 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1844 i, vd->e[i].next_regno);
1845 continue;
1848 SET_HARD_REG_BIT (set, i);
1849 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1851 for (j = vd->e[i].next_regno;
1852 j != INVALID_REGNUM;
1853 j = vd->e[j].next_regno)
1855 if (TEST_HARD_REG_BIT (set, j))
1857 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1858 return;
1861 if (vd->e[j].oldest_regno != i)
1863 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1864 j, vd->e[j].oldest_regno);
1865 return;
1867 SET_HARD_REG_BIT (set, j);
1868 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1870 fputc ('\n', stderr);
1873 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1874 if (! TEST_HARD_REG_BIT (set, i)
1875 && (vd->e[i].mode != VOIDmode
1876 || vd->e[i].oldest_regno != i
1877 || vd->e[i].next_regno != INVALID_REGNUM))
1878 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1879 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1880 vd->e[i].next_regno);
1883 #ifdef ENABLE_CHECKING
1884 static void
1885 validate_value_data (struct value_data *vd)
1887 HARD_REG_SET set;
1888 unsigned int i, j;
1890 CLEAR_HARD_REG_SET (set);
1892 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1893 if (vd->e[i].oldest_regno == i)
1895 if (vd->e[i].mode == VOIDmode)
1897 if (vd->e[i].next_regno != INVALID_REGNUM)
1898 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1899 i, vd->e[i].next_regno);
1900 continue;
1903 SET_HARD_REG_BIT (set, i);
1905 for (j = vd->e[i].next_regno;
1906 j != INVALID_REGNUM;
1907 j = vd->e[j].next_regno)
1909 if (TEST_HARD_REG_BIT (set, j))
1910 internal_error ("validate_value_data: Loop in regno chain (%u)",
1912 if (vd->e[j].oldest_regno != i)
1913 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1914 j, vd->e[j].oldest_regno);
1916 SET_HARD_REG_BIT (set, j);
1920 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1921 if (! TEST_HARD_REG_BIT (set, i)
1922 && (vd->e[i].mode != VOIDmode
1923 || vd->e[i].oldest_regno != i
1924 || vd->e[i].next_regno != INVALID_REGNUM))
1925 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1926 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1927 vd->e[i].next_regno);
1929 #endif
1931 static bool
1932 gate_handle_regrename (void)
1934 return (optimize > 0 && (flag_rename_registers));
1938 /* Run the regrename and cprop passes. */
1939 static unsigned int
1940 rest_of_handle_regrename (void)
1942 regrename_optimize ();
1943 return 0;
1946 struct rtl_opt_pass pass_regrename =
1949 RTL_PASS,
1950 "rnreg", /* name */
1951 gate_handle_regrename, /* gate */
1952 rest_of_handle_regrename, /* execute */
1953 NULL, /* sub */
1954 NULL, /* next */
1955 0, /* static_pass_number */
1956 TV_RENAME_REGISTERS, /* tv_id */
1957 0, /* properties_required */
1958 0, /* properties_provided */
1959 0, /* properties_destroyed */
1960 0, /* todo_flags_start */
1961 TODO_df_finish | TODO_verify_rtl_sharing |
1962 TODO_dump_func /* todo_flags_finish */
1966 static bool
1967 gate_handle_cprop (void)
1969 return (optimize > 0 && (flag_cprop_registers));
1973 /* Run the regrename and cprop passes. */
1974 static unsigned int
1975 rest_of_handle_cprop (void)
1977 copyprop_hardreg_forward ();
1978 return 0;
1981 struct rtl_opt_pass pass_cprop_hardreg =
1984 RTL_PASS,
1985 "cprop_hardreg", /* name */
1986 gate_handle_cprop, /* gate */
1987 rest_of_handle_cprop, /* execute */
1988 NULL, /* sub */
1989 NULL, /* next */
1990 0, /* static_pass_number */
1991 TV_RENAME_REGISTERS, /* tv_id */
1992 0, /* properties_required */
1993 0, /* properties_provided */
1994 0, /* properties_destroyed */
1995 0, /* todo_flags_start */
1996 TODO_dump_func | TODO_verify_rtl_sharing /* todo_flags_finish */