tree-ssa.doxy: Update for doxygen 1.5.
[official-gcc.git] / gcc / regrename.c
blob29f8e763d92279a3017a0a389f442bbf80440cef
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 machine_mode, 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 machine_mode 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_NO_INSN_RESCAN);
189 memset (tick, 0, sizeof tick);
191 gcc_obstack_init (&rename_obstack);
192 first_obj = obstack_alloc (&rename_obstack, 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 = all_chains;
226 struct du_chain *tmp, *last;
227 HARD_REG_SET this_unavailable;
228 int reg = REGNO (*this->loc);
229 int i;
231 all_chains = this->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; 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->need_caller_save_reg)
272 IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
274 merge_overlapping_regs (bb, &this_unavailable, this);
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->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; 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, 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);
348 df_clear_flags (DF_NO_INSN_RESCAN);
349 df_insn_rescan_all ();
351 if (dump_file)
352 fputc ('\n', dump_file);
355 static void
356 do_replace (struct du_chain *chain, int reg)
358 while (chain)
360 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
361 struct reg_attrs * attr = REG_ATTRS (*chain->loc);
363 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
364 if (regno >= FIRST_PSEUDO_REGISTER)
365 ORIGINAL_REGNO (*chain->loc) = regno;
366 REG_ATTRS (*chain->loc) = attr;
367 chain = chain->next_use;
372 static struct du_chain *open_chains;
373 static struct du_chain *closed_chains;
375 static void
376 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class cl,
377 enum scan_actions action, enum op_type type, int earlyclobber)
379 struct du_chain **p;
380 rtx x = *loc;
381 enum machine_mode mode = GET_MODE (x);
382 int this_regno = REGNO (x);
383 int this_nregs = hard_regno_nregs[this_regno][mode];
385 if (action == mark_write)
387 if (type == OP_OUT)
389 struct du_chain *this
390 = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
391 this->next_use = 0;
392 this->next_chain = open_chains;
393 this->loc = loc;
394 this->insn = insn;
395 this->cl = cl;
396 this->need_caller_save_reg = 0;
397 this->earlyclobber = earlyclobber;
398 open_chains = this;
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 = *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->loc == cc0_rtx)
418 p = &this->next_chain;
419 else
421 int regno = REGNO (*this->loc);
422 int nregs = hard_regno_nregs[regno][GET_MODE (*this->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->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 = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
443 this->next_use = 0;
444 this->next_chain = (*p)->next_chain;
445 this->loc = loc;
446 this->insn = insn;
447 this->cl = cl;
448 this->need_caller_save_reg = 0;
449 while (*p)
450 p = &(*p)->next_use;
451 *p = this;
452 return;
456 if (action != terminate_overlapping_read || ! exact_match)
458 struct du_chain *next = this->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->next_chain = closed_chains;
468 closed_chains = this;
469 if (dump_file)
470 fprintf (dump_file,
471 "Closing chain %s at insn %d (%s)\n",
472 reg_names[REGNO (*this->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->loc)], INSN_UID (insn),
481 scan_actions_name[(int) action]);
483 *p = next;
485 else
486 p = &this->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 (regno0)
564 && regno_ok_for_base_p (regno1, mode, PLUS, REG))
565 index_op = 0;
566 else if (REGNO_OK_FOR_INDEX_P (regno1)
567 && regno_ok_for_base_p (regno0, mode, PLUS, REG))
568 index_op = 1;
569 else if (regno_ok_for_base_p (regno1, mode, PLUS, REG))
570 index_op = 0;
571 else if (regno_ok_for_base_p (regno0, mode, PLUS, REG))
572 index_op = 1;
573 else if (REGNO_OK_FOR_INDEX_P (regno1))
574 index_op = 1;
575 else
576 index_op = 0;
578 locI = &XEXP (x, index_op);
579 locB = &XEXP (x, !index_op);
580 index_code = GET_CODE (*locI);
582 else if (code0 == REG)
584 locI = &XEXP (x, 0);
585 locB = &XEXP (x, 1);
586 index_code = GET_CODE (*locI);
588 else if (code1 == REG)
590 locI = &XEXP (x, 1);
591 locB = &XEXP (x, 0);
592 index_code = GET_CODE (*locI);
595 if (locI)
596 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
597 if (locB)
598 scan_rtx_address (insn, locB, base_reg_class (mode, PLUS, index_code),
599 action, mode);
601 return;
604 case POST_INC:
605 case POST_DEC:
606 case POST_MODIFY:
607 case PRE_INC:
608 case PRE_DEC:
609 case PRE_MODIFY:
610 #ifndef AUTO_INC_DEC
611 /* If the target doesn't claim to handle autoinc, this must be
612 something special, like a stack push. Kill this chain. */
613 action = terminate_all_read;
614 #endif
615 break;
617 case MEM:
618 scan_rtx_address (insn, &XEXP (x, 0),
619 base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
620 GET_MODE (x));
621 return;
623 case REG:
624 scan_rtx_reg (insn, loc, cl, action, OP_IN, 0);
625 return;
627 default:
628 break;
631 fmt = GET_RTX_FORMAT (code);
632 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
634 if (fmt[i] == 'e')
635 scan_rtx_address (insn, &XEXP (x, i), cl, action, mode);
636 else if (fmt[i] == 'E')
637 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
638 scan_rtx_address (insn, &XVECEXP (x, i, j), cl, action, mode);
642 static void
643 scan_rtx (rtx insn, rtx *loc, enum reg_class cl,
644 enum scan_actions action, enum op_type type, int earlyclobber)
646 const char *fmt;
647 rtx x = *loc;
648 enum rtx_code code = GET_CODE (x);
649 int i, j;
651 code = GET_CODE (x);
652 switch (code)
654 case CONST:
655 case CONST_INT:
656 case CONST_DOUBLE:
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 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. */
816 if (icode >= 0
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,
822 OP_IN, 0);
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 (CALL_P (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
835 hard registers. */
836 if (asm_noperands (PATTERN (insn)) > 0)
837 for (i = 0; i < n_ops; i++)
839 rtx *loc = recog_data.operand_loc[i];
840 rtx op = *loc;
842 if (REG_P (op)
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 cl = recog_op_alt[opn][alt].cl;
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')
863 continue;
865 if (recog_op_alt[opn][alt].is_address)
866 scan_rtx_address (insn, loc, cl, mark_read, VOIDmode);
867 else
868 scan_rtx (insn, loc, cl, mark_read, type, 0);
871 /* Step 3B: Record updates for regs in REG_INC notes, and
872 source regs in REG_FRAME_RELATED_EXPR notes. */
873 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
874 if (REG_NOTE_KIND (note) == REG_INC
875 || REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
876 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
877 OP_INOUT, 0);
879 /* Step 4: Close chains for registers that die here. */
880 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
881 if (REG_NOTE_KIND (note) == REG_DEAD)
882 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
883 OP_IN, 0);
885 /* Step 4B: If this is a call, any chain live at this point
886 requires a caller-saved reg. */
887 if (CALL_P (insn))
889 struct du_chain *p;
890 for (p = open_chains; p; p = p->next_chain)
891 p->need_caller_save_reg = 1;
894 /* Step 5: Close open chains that overlap writes. Similar to
895 step 2, we hide in-out operands, since we do not want to
896 close these chains. */
898 for (i = 0; i < n_ops; i++)
900 old_operands[i] = recog_data.operand[i];
901 if (recog_data.operand_type[i] == OP_INOUT)
902 *recog_data.operand_loc[i] = cc0_rtx;
904 for (i = 0; i < recog_data.n_dups; i++)
906 int opn = recog_data.dup_num[i];
907 old_dups[i] = *recog_data.dup_loc[i];
908 if (recog_data.operand_type[opn] == OP_INOUT)
909 *recog_data.dup_loc[i] = cc0_rtx;
912 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
914 for (i = 0; i < recog_data.n_dups; i++)
915 *recog_data.dup_loc[i] = old_dups[i];
916 for (i = 0; i < n_ops; i++)
917 *recog_data.operand_loc[i] = old_operands[i];
919 /* Step 6: Begin new chains for writes inside operands. */
920 /* ??? Many targets have output constraints on the SET_DEST
921 of a call insn, which is stupid, since these are certainly
922 ABI defined hard registers. Don't change calls at all.
923 Similarly take special care for asm statement that originally
924 referenced hard registers. */
925 if (asm_noperands (PATTERN (insn)) > 0)
927 for (i = 0; i < n_ops; i++)
928 if (recog_data.operand_type[i] == OP_OUT)
930 rtx *loc = recog_data.operand_loc[i];
931 rtx op = *loc;
932 enum reg_class cl = recog_op_alt[i][alt].cl;
934 if (REG_P (op)
935 && REGNO (op) == ORIGINAL_REGNO (op))
936 continue;
938 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
939 recog_op_alt[i][alt].earlyclobber);
942 else if (!CALL_P (insn))
943 for (i = 0; i < n_ops + recog_data.n_dups; i++)
945 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
946 rtx *loc = (i < n_ops
947 ? recog_data.operand_loc[opn]
948 : recog_data.dup_loc[i - n_ops]);
949 enum reg_class cl = recog_op_alt[opn][alt].cl;
951 if (recog_data.operand_type[opn] == OP_OUT)
952 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
953 recog_op_alt[opn][alt].earlyclobber);
956 /* Step 6B: Record destination regs in REG_FRAME_RELATED_EXPR
957 notes for update. */
958 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
959 if (REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
960 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_access,
961 OP_INOUT, 0);
963 /* Step 7: Close chains for registers that were never
964 really used here. */
965 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
966 if (REG_NOTE_KIND (note) == REG_UNUSED)
967 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
968 OP_IN, 0);
970 if (insn == BB_END (bb))
971 break;
974 /* Since we close every chain when we find a REG_DEAD note, anything that
975 is still open lives past the basic block, so it can't be renamed. */
976 return closed_chains;
979 /* Dump all def/use chains in CHAINS to DUMP_FILE. They are
980 printed in reverse order as that's how we build them. */
982 static void
983 dump_def_use_chain (struct du_chain *chains)
985 while (chains)
987 struct du_chain *this = chains;
988 int r = REGNO (*this->loc);
989 int nregs = hard_regno_nregs[r][GET_MODE (*this->loc)];
990 fprintf (dump_file, "Register %s (%d):", reg_names[r], nregs);
991 while (this)
993 fprintf (dump_file, " %d [%s]", INSN_UID (this->insn),
994 reg_class_names[this->cl]);
995 this = this->next_use;
997 fprintf (dump_file, "\n");
998 chains = chains->next_chain;
1002 /* The following code does forward propagation of hard register copies.
1003 The object is to eliminate as many dependencies as possible, so that
1004 we have the most scheduling freedom. As a side effect, we also clean
1005 up some silly register allocation decisions made by reload. This
1006 code may be obsoleted by a new register allocator. */
1008 /* For each register, we have a list of registers that contain the same
1009 value. The OLDEST_REGNO field points to the head of the list, and
1010 the NEXT_REGNO field runs through the list. The MODE field indicates
1011 what mode the data is known to be in; this field is VOIDmode when the
1012 register is not known to contain valid data. */
1014 struct value_data_entry
1016 enum machine_mode mode;
1017 unsigned int oldest_regno;
1018 unsigned int next_regno;
1021 struct value_data
1023 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
1024 unsigned int max_value_regs;
1027 static void kill_value_one_regno (unsigned, struct value_data *);
1028 static void kill_value_regno (unsigned, unsigned, struct value_data *);
1029 static void kill_value (rtx, struct value_data *);
1030 static void set_value_regno (unsigned, enum machine_mode, struct value_data *);
1031 static void init_value_data (struct value_data *);
1032 static void kill_clobbered_value (rtx, const_rtx, void *);
1033 static void kill_set_value (rtx, const_rtx, void *);
1034 static int kill_autoinc_value (rtx *, void *);
1035 static void copy_value (rtx, rtx, struct value_data *);
1036 static bool mode_change_ok (enum machine_mode, enum machine_mode,
1037 unsigned int);
1038 static rtx maybe_mode_change (enum machine_mode, enum machine_mode,
1039 enum machine_mode, unsigned int, unsigned int);
1040 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
1041 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx,
1042 struct value_data *);
1043 static bool replace_oldest_value_addr (rtx *, enum reg_class,
1044 enum machine_mode, rtx,
1045 struct value_data *);
1046 static bool replace_oldest_value_mem (rtx, rtx, struct value_data *);
1047 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
1048 extern void debug_value_data (struct value_data *);
1049 #ifdef ENABLE_CHECKING
1050 static void validate_value_data (struct value_data *);
1051 #endif
1053 /* Kill register REGNO. This involves removing it from any value
1054 lists, and resetting the value mode to VOIDmode. This is only a
1055 helper function; it does not handle any hard registers overlapping
1056 with REGNO. */
1058 static void
1059 kill_value_one_regno (unsigned int regno, struct value_data *vd)
1061 unsigned int i, next;
1063 if (vd->e[regno].oldest_regno != regno)
1065 for (i = vd->e[regno].oldest_regno;
1066 vd->e[i].next_regno != regno;
1067 i = vd->e[i].next_regno)
1068 continue;
1069 vd->e[i].next_regno = vd->e[regno].next_regno;
1071 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
1073 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
1074 vd->e[i].oldest_regno = next;
1077 vd->e[regno].mode = VOIDmode;
1078 vd->e[regno].oldest_regno = regno;
1079 vd->e[regno].next_regno = INVALID_REGNUM;
1081 #ifdef ENABLE_CHECKING
1082 validate_value_data (vd);
1083 #endif
1086 /* Kill the value in register REGNO for NREGS, and any other registers
1087 whose values overlap. */
1089 static void
1090 kill_value_regno (unsigned int regno, unsigned int nregs,
1091 struct value_data *vd)
1093 unsigned int j;
1095 /* Kill the value we're told to kill. */
1096 for (j = 0; j < nregs; ++j)
1097 kill_value_one_regno (regno + j, vd);
1099 /* Kill everything that overlapped what we're told to kill. */
1100 if (regno < vd->max_value_regs)
1101 j = 0;
1102 else
1103 j = regno - vd->max_value_regs;
1104 for (; j < regno; ++j)
1106 unsigned int i, n;
1107 if (vd->e[j].mode == VOIDmode)
1108 continue;
1109 n = hard_regno_nregs[j][vd->e[j].mode];
1110 if (j + n > regno)
1111 for (i = 0; i < n; ++i)
1112 kill_value_one_regno (j + i, vd);
1116 /* Kill X. This is a convenience function wrapping kill_value_regno
1117 so that we mind the mode the register is in. */
1119 static void
1120 kill_value (rtx x, struct value_data *vd)
1122 rtx orig_rtx = x;
1124 if (GET_CODE (x) == SUBREG)
1126 x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
1127 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
1128 if (x == NULL_RTX)
1129 x = SUBREG_REG (orig_rtx);
1131 if (REG_P (x))
1133 unsigned int regno = REGNO (x);
1134 unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
1136 kill_value_regno (regno, n, vd);
1140 /* Remember that REGNO is valid in MODE. */
1142 static void
1143 set_value_regno (unsigned int regno, enum machine_mode mode,
1144 struct value_data *vd)
1146 unsigned int nregs;
1148 vd->e[regno].mode = mode;
1150 nregs = hard_regno_nregs[regno][mode];
1151 if (nregs > vd->max_value_regs)
1152 vd->max_value_regs = nregs;
1155 /* Initialize VD such that there are no known relationships between regs. */
1157 static void
1158 init_value_data (struct value_data *vd)
1160 int i;
1161 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1163 vd->e[i].mode = VOIDmode;
1164 vd->e[i].oldest_regno = i;
1165 vd->e[i].next_regno = INVALID_REGNUM;
1167 vd->max_value_regs = 0;
1170 /* Called through note_stores. If X is clobbered, kill its value. */
1172 static void
1173 kill_clobbered_value (rtx x, const_rtx set, void *data)
1175 struct value_data *vd = data;
1176 if (GET_CODE (set) == CLOBBER)
1177 kill_value (x, vd);
1180 /* Called through note_stores. If X is set, not clobbered, kill its
1181 current value and install it as the root of its own value list. */
1183 static void
1184 kill_set_value (rtx x, const_rtx set, void *data)
1186 struct value_data *vd = data;
1187 if (GET_CODE (set) != CLOBBER)
1189 kill_value (x, vd);
1190 if (REG_P (x))
1191 set_value_regno (REGNO (x), GET_MODE (x), vd);
1195 /* Called through for_each_rtx. Kill any register used as the base of an
1196 auto-increment expression, and install that register as the root of its
1197 own value list. */
1199 static int
1200 kill_autoinc_value (rtx *px, void *data)
1202 rtx x = *px;
1203 struct value_data *vd = data;
1205 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
1207 x = XEXP (x, 0);
1208 kill_value (x, vd);
1209 set_value_regno (REGNO (x), Pmode, vd);
1210 return -1;
1213 return 0;
1216 /* Assert that SRC has been copied to DEST. Adjust the data structures
1217 to reflect that SRC contains an older copy of the shared value. */
1219 static void
1220 copy_value (rtx dest, rtx src, struct value_data *vd)
1222 unsigned int dr = REGNO (dest);
1223 unsigned int sr = REGNO (src);
1224 unsigned int dn, sn;
1225 unsigned int i;
1227 /* ??? At present, it's possible to see noop sets. It'd be nice if
1228 this were cleaned up beforehand... */
1229 if (sr == dr)
1230 return;
1232 /* Do not propagate copies to the stack pointer, as that can leave
1233 memory accesses with no scheduling dependency on the stack update. */
1234 if (dr == STACK_POINTER_REGNUM)
1235 return;
1237 /* Likewise with the frame pointer, if we're using one. */
1238 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
1239 return;
1241 /* Do not propagate copies to fixed or global registers, patterns
1242 can be relying to see particular fixed register or users can
1243 expect the chosen global register in asm. */
1244 if (fixed_regs[dr] || global_regs[dr])
1245 return;
1247 /* If SRC and DEST overlap, don't record anything. */
1248 dn = hard_regno_nregs[dr][GET_MODE (dest)];
1249 sn = hard_regno_nregs[sr][GET_MODE (dest)];
1250 if ((dr > sr && dr < sr + sn)
1251 || (sr > dr && sr < dr + dn))
1252 return;
1254 /* If SRC had no assigned mode (i.e. we didn't know it was live)
1255 assign it now and assume the value came from an input argument
1256 or somesuch. */
1257 if (vd->e[sr].mode == VOIDmode)
1258 set_value_regno (sr, vd->e[dr].mode, vd);
1260 /* If we are narrowing the input to a smaller number of hard regs,
1261 and it is in big endian, we are really extracting a high part.
1262 Since we generally associate a low part of a value with the value itself,
1263 we must not do the same for the high part.
1264 Note we can still get low parts for the same mode combination through
1265 a two-step copy involving differently sized hard regs.
1266 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
1267 (set (reg:DI r0) (reg:DI fr0))
1268 (set (reg:SI fr2) (reg:SI r0))
1269 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
1270 (set (reg:SI fr2) (reg:SI fr0))
1271 loads the high part of (reg:DI fr0) into fr2.
1273 We can't properly represent the latter case in our tables, so don't
1274 record anything then. */
1275 else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
1276 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
1277 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
1278 return;
1280 /* If SRC had been assigned a mode narrower than the copy, we can't
1281 link DEST into the chain, because not all of the pieces of the
1282 copy came from oldest_regno. */
1283 else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
1284 return;
1286 /* Link DR at the end of the value chain used by SR. */
1288 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
1290 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
1291 continue;
1292 vd->e[i].next_regno = dr;
1294 #ifdef ENABLE_CHECKING
1295 validate_value_data (vd);
1296 #endif
1299 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
1301 static bool
1302 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
1303 unsigned int regno ATTRIBUTE_UNUSED)
1305 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
1306 return false;
1308 #ifdef CANNOT_CHANGE_MODE_CLASS
1309 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
1310 #endif
1312 return true;
1315 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
1316 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
1317 in NEW_MODE.
1318 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
1320 static rtx
1321 maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
1322 enum machine_mode new_mode, unsigned int regno,
1323 unsigned int copy_regno ATTRIBUTE_UNUSED)
1325 if (orig_mode == new_mode)
1326 return gen_rtx_raw_REG (new_mode, regno);
1327 else if (mode_change_ok (orig_mode, new_mode, regno))
1329 int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
1330 int use_nregs = hard_regno_nregs[copy_regno][new_mode];
1331 int copy_offset
1332 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
1333 int offset
1334 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
1335 int byteoffset = offset % UNITS_PER_WORD;
1336 int wordoffset = offset - byteoffset;
1338 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
1339 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
1340 return gen_rtx_raw_REG (new_mode,
1341 regno + subreg_regno_offset (regno, orig_mode,
1342 offset,
1343 new_mode));
1345 return NULL_RTX;
1348 /* Find the oldest copy of the value contained in REGNO that is in
1349 register class CL and has mode MODE. If found, return an rtx
1350 of that oldest register, otherwise return NULL. */
1352 static rtx
1353 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
1355 unsigned int regno = REGNO (reg);
1356 enum machine_mode mode = GET_MODE (reg);
1357 unsigned int i;
1359 /* If we are accessing REG in some mode other that what we set it in,
1360 make sure that the replacement is valid. In particular, consider
1361 (set (reg:DI r11) (...))
1362 (set (reg:SI r9) (reg:SI r11))
1363 (set (reg:SI r10) (...))
1364 (set (...) (reg:DI r9))
1365 Replacing r9 with r11 is invalid. */
1366 if (mode != vd->e[regno].mode)
1368 if (hard_regno_nregs[regno][mode]
1369 > hard_regno_nregs[regno][vd->e[regno].mode])
1370 return NULL_RTX;
1373 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
1375 enum machine_mode oldmode = vd->e[i].mode;
1376 rtx new;
1378 if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
1379 return NULL_RTX;
1381 new = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
1382 if (new)
1384 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (reg);
1385 REG_ATTRS (new) = REG_ATTRS (reg);
1386 return new;
1390 return NULL_RTX;
1393 /* If possible, replace the register at *LOC with the oldest register
1394 in register class CL. Return true if successfully replaced. */
1396 static bool
1397 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx insn,
1398 struct value_data *vd)
1400 rtx new = find_oldest_value_reg (cl, *loc, vd);
1401 if (new)
1403 if (dump_file)
1404 fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
1405 INSN_UID (insn), REGNO (*loc), REGNO (new));
1407 validate_change (insn, loc, new, 1);
1408 return true;
1410 return false;
1413 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1414 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
1415 BASE_REG_CLASS depending on how the register is being considered. */
1417 static bool
1418 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
1419 enum machine_mode mode, rtx insn,
1420 struct value_data *vd)
1422 rtx x = *loc;
1423 RTX_CODE code = GET_CODE (x);
1424 const char *fmt;
1425 int i, j;
1426 bool changed = false;
1428 switch (code)
1430 case PLUS:
1432 rtx orig_op0 = XEXP (x, 0);
1433 rtx orig_op1 = XEXP (x, 1);
1434 RTX_CODE code0 = GET_CODE (orig_op0);
1435 RTX_CODE code1 = GET_CODE (orig_op1);
1436 rtx op0 = orig_op0;
1437 rtx op1 = orig_op1;
1438 rtx *locI = NULL;
1439 rtx *locB = NULL;
1440 enum rtx_code index_code = SCRATCH;
1442 if (GET_CODE (op0) == SUBREG)
1444 op0 = SUBREG_REG (op0);
1445 code0 = GET_CODE (op0);
1448 if (GET_CODE (op1) == SUBREG)
1450 op1 = SUBREG_REG (op1);
1451 code1 = GET_CODE (op1);
1454 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1455 || code0 == ZERO_EXTEND || code1 == MEM)
1457 locI = &XEXP (x, 0);
1458 locB = &XEXP (x, 1);
1459 index_code = GET_CODE (*locI);
1461 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1462 || code1 == ZERO_EXTEND || code0 == MEM)
1464 locI = &XEXP (x, 1);
1465 locB = &XEXP (x, 0);
1466 index_code = GET_CODE (*locI);
1468 else if (code0 == CONST_INT || code0 == CONST
1469 || code0 == SYMBOL_REF || code0 == LABEL_REF)
1471 locB = &XEXP (x, 1);
1472 index_code = GET_CODE (XEXP (x, 0));
1474 else if (code1 == CONST_INT || code1 == CONST
1475 || code1 == SYMBOL_REF || code1 == LABEL_REF)
1477 locB = &XEXP (x, 0);
1478 index_code = GET_CODE (XEXP (x, 1));
1480 else if (code0 == REG && code1 == REG)
1482 int index_op;
1483 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
1485 if (REGNO_OK_FOR_INDEX_P (regno0)
1486 && regno_ok_for_base_p (regno1, mode, PLUS, REG))
1487 index_op = 0;
1488 else if (REGNO_OK_FOR_INDEX_P (regno1)
1489 && regno_ok_for_base_p (regno0, mode, PLUS, REG))
1490 index_op = 1;
1491 else if (regno_ok_for_base_p (regno1, mode, PLUS, REG))
1492 index_op = 0;
1493 else if (regno_ok_for_base_p (regno0, mode, PLUS, REG))
1494 index_op = 1;
1495 else if (REGNO_OK_FOR_INDEX_P (regno1))
1496 index_op = 1;
1497 else
1498 index_op = 0;
1500 locI = &XEXP (x, index_op);
1501 locB = &XEXP (x, !index_op);
1502 index_code = GET_CODE (*locI);
1504 else if (code0 == REG)
1506 locI = &XEXP (x, 0);
1507 locB = &XEXP (x, 1);
1508 index_code = GET_CODE (*locI);
1510 else if (code1 == REG)
1512 locI = &XEXP (x, 1);
1513 locB = &XEXP (x, 0);
1514 index_code = GET_CODE (*locI);
1517 if (locI)
1518 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
1519 insn, vd);
1520 if (locB)
1521 changed |= replace_oldest_value_addr (locB,
1522 base_reg_class (mode, PLUS,
1523 index_code),
1524 mode, insn, vd);
1525 return changed;
1528 case POST_INC:
1529 case POST_DEC:
1530 case POST_MODIFY:
1531 case PRE_INC:
1532 case PRE_DEC:
1533 case PRE_MODIFY:
1534 return false;
1536 case MEM:
1537 return replace_oldest_value_mem (x, insn, vd);
1539 case REG:
1540 return replace_oldest_value_reg (loc, cl, insn, vd);
1542 default:
1543 break;
1546 fmt = GET_RTX_FORMAT (code);
1547 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1549 if (fmt[i] == 'e')
1550 changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode,
1551 insn, vd);
1552 else if (fmt[i] == 'E')
1553 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1554 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
1555 mode, insn, vd);
1558 return changed;
1561 /* Similar to replace_oldest_value_reg, but X contains a memory. */
1563 static bool
1564 replace_oldest_value_mem (rtx x, rtx insn, struct value_data *vd)
1566 return replace_oldest_value_addr (&XEXP (x, 0),
1567 base_reg_class (GET_MODE (x), MEM,
1568 SCRATCH),
1569 GET_MODE (x), insn, vd);
1572 /* Perform the forward copy propagation on basic block BB. */
1574 static bool
1575 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
1577 bool changed = false;
1578 rtx insn;
1580 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1582 int n_ops, i, alt, predicated;
1583 bool is_asm, any_replacements;
1584 rtx set;
1585 bool replaced[MAX_RECOG_OPERANDS];
1587 if (! INSN_P (insn))
1589 if (insn == BB_END (bb))
1590 break;
1591 else
1592 continue;
1595 set = single_set (insn);
1596 extract_insn (insn);
1597 if (! constrain_operands (1))
1598 fatal_insn_not_found (insn);
1599 preprocess_constraints ();
1600 alt = which_alternative;
1601 n_ops = recog_data.n_operands;
1602 is_asm = asm_noperands (PATTERN (insn)) >= 0;
1604 /* Simplify the code below by rewriting things to reflect
1605 matching constraints. Also promote OP_OUT to OP_INOUT
1606 in predicated instructions. */
1608 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1609 for (i = 0; i < n_ops; ++i)
1611 int matches = recog_op_alt[i][alt].matches;
1612 if (matches >= 0)
1613 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
1614 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1615 || (predicated && recog_data.operand_type[i] == OP_OUT))
1616 recog_data.operand_type[i] = OP_INOUT;
1619 /* For each earlyclobber operand, zap the value data. */
1620 for (i = 0; i < n_ops; i++)
1621 if (recog_op_alt[i][alt].earlyclobber)
1622 kill_value (recog_data.operand[i], vd);
1624 /* Within asms, a clobber cannot overlap inputs or outputs.
1625 I wouldn't think this were true for regular insns, but
1626 scan_rtx treats them like that... */
1627 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1629 /* Kill all auto-incremented values. */
1630 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
1631 for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
1633 /* Kill all early-clobbered operands. */
1634 for (i = 0; i < n_ops; i++)
1635 if (recog_op_alt[i][alt].earlyclobber)
1636 kill_value (recog_data.operand[i], vd);
1638 /* Special-case plain move instructions, since we may well
1639 be able to do the move from a different register class. */
1640 if (set && REG_P (SET_SRC (set)))
1642 rtx src = SET_SRC (set);
1643 unsigned int regno = REGNO (src);
1644 enum machine_mode mode = GET_MODE (src);
1645 unsigned int i;
1646 rtx new;
1648 /* If we are accessing SRC in some mode other that what we
1649 set it in, make sure that the replacement is valid. */
1650 if (mode != vd->e[regno].mode)
1652 if (hard_regno_nregs[regno][mode]
1653 > hard_regno_nregs[regno][vd->e[regno].mode])
1654 goto no_move_special_case;
1657 /* If the destination is also a register, try to find a source
1658 register in the same class. */
1659 if (REG_P (SET_DEST (set)))
1661 new = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
1662 if (new && validate_change (insn, &SET_SRC (set), new, 0))
1664 if (dump_file)
1665 fprintf (dump_file,
1666 "insn %u: replaced reg %u with %u\n",
1667 INSN_UID (insn), regno, REGNO (new));
1668 changed = true;
1669 goto did_replacement;
1673 /* Otherwise, try all valid registers and see if its valid. */
1674 for (i = vd->e[regno].oldest_regno; i != regno;
1675 i = vd->e[i].next_regno)
1677 new = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
1678 mode, i, regno);
1679 if (new != NULL_RTX)
1681 if (validate_change (insn, &SET_SRC (set), new, 0))
1683 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (src);
1684 REG_ATTRS (new) = REG_ATTRS (src);
1685 if (dump_file)
1686 fprintf (dump_file,
1687 "insn %u: replaced reg %u with %u\n",
1688 INSN_UID (insn), regno, REGNO (new));
1689 changed = true;
1690 goto did_replacement;
1695 no_move_special_case:
1697 any_replacements = false;
1699 /* For each input operand, replace a hard register with the
1700 eldest live copy that's in an appropriate register class. */
1701 for (i = 0; i < n_ops; i++)
1703 replaced[i] = false;
1705 /* Don't scan match_operand here, since we've no reg class
1706 information to pass down. Any operands that we could
1707 substitute in will be represented elsewhere. */
1708 if (recog_data.constraints[i][0] == '\0')
1709 continue;
1711 /* Don't replace in asms intentionally referencing hard regs. */
1712 if (is_asm && REG_P (recog_data.operand[i])
1713 && (REGNO (recog_data.operand[i])
1714 == ORIGINAL_REGNO (recog_data.operand[i])))
1715 continue;
1717 if (recog_data.operand_type[i] == OP_IN)
1719 if (recog_op_alt[i][alt].is_address)
1720 replaced[i]
1721 = replace_oldest_value_addr (recog_data.operand_loc[i],
1722 recog_op_alt[i][alt].cl,
1723 VOIDmode, insn, vd);
1724 else if (REG_P (recog_data.operand[i]))
1725 replaced[i]
1726 = replace_oldest_value_reg (recog_data.operand_loc[i],
1727 recog_op_alt[i][alt].cl,
1728 insn, vd);
1729 else if (MEM_P (recog_data.operand[i]))
1730 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
1731 insn, vd);
1733 else if (MEM_P (recog_data.operand[i]))
1734 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
1735 insn, vd);
1737 /* If we performed any replacement, update match_dups. */
1738 if (replaced[i])
1740 int j;
1741 rtx new;
1743 new = *recog_data.operand_loc[i];
1744 recog_data.operand[i] = new;
1745 for (j = 0; j < recog_data.n_dups; j++)
1746 if (recog_data.dup_num[j] == i)
1747 validate_change (insn, recog_data.dup_loc[j], new, 1);
1749 any_replacements = true;
1753 if (any_replacements)
1755 if (! apply_change_group ())
1757 for (i = 0; i < n_ops; i++)
1758 if (replaced[i])
1760 rtx old = *recog_data.operand_loc[i];
1761 recog_data.operand[i] = old;
1764 if (dump_file)
1765 fprintf (dump_file,
1766 "insn %u: reg replacements not verified\n",
1767 INSN_UID (insn));
1769 else
1770 changed = true;
1773 did_replacement:
1774 /* Clobber call-clobbered registers. */
1775 if (CALL_P (insn))
1776 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1777 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1778 kill_value_regno (i, 1, vd);
1780 /* Notice stores. */
1781 note_stores (PATTERN (insn), kill_set_value, vd);
1783 /* Notice copies. */
1784 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1785 copy_value (SET_DEST (set), SET_SRC (set), vd);
1787 if (insn == BB_END (bb))
1788 break;
1791 return changed;
1794 /* Main entry point for the forward copy propagation optimization. */
1796 static void
1797 copyprop_hardreg_forward (void)
1799 struct value_data *all_vd;
1800 basic_block bb;
1801 sbitmap visited;
1803 all_vd = XNEWVEC (struct value_data, last_basic_block);
1805 visited = sbitmap_alloc (last_basic_block);
1806 sbitmap_zero (visited);
1808 FOR_EACH_BB (bb)
1810 SET_BIT (visited, bb->index);
1812 /* If a block has a single predecessor, that we've already
1813 processed, begin with the value data that was live at
1814 the end of the predecessor block. */
1815 /* ??? Ought to use more intelligent queuing of blocks. */
1816 if (single_pred_p (bb)
1817 && TEST_BIT (visited, single_pred (bb)->index)
1818 && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1819 all_vd[bb->index] = all_vd[single_pred (bb)->index];
1820 else
1821 init_value_data (all_vd + bb->index);
1823 copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
1826 sbitmap_free (visited);
1827 free (all_vd);
1830 /* Dump the value chain data to stderr. */
1832 void
1833 debug_value_data (struct value_data *vd)
1835 HARD_REG_SET set;
1836 unsigned int i, j;
1838 CLEAR_HARD_REG_SET (set);
1840 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1841 if (vd->e[i].oldest_regno == i)
1843 if (vd->e[i].mode == VOIDmode)
1845 if (vd->e[i].next_regno != INVALID_REGNUM)
1846 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1847 i, vd->e[i].next_regno);
1848 continue;
1851 SET_HARD_REG_BIT (set, i);
1852 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1854 for (j = vd->e[i].next_regno;
1855 j != INVALID_REGNUM;
1856 j = vd->e[j].next_regno)
1858 if (TEST_HARD_REG_BIT (set, j))
1860 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1861 return;
1864 if (vd->e[j].oldest_regno != i)
1866 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1867 j, vd->e[j].oldest_regno);
1868 return;
1870 SET_HARD_REG_BIT (set, j);
1871 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1873 fputc ('\n', stderr);
1876 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1877 if (! TEST_HARD_REG_BIT (set, i)
1878 && (vd->e[i].mode != VOIDmode
1879 || vd->e[i].oldest_regno != i
1880 || vd->e[i].next_regno != INVALID_REGNUM))
1881 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1882 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1883 vd->e[i].next_regno);
1886 #ifdef ENABLE_CHECKING
1887 static void
1888 validate_value_data (struct value_data *vd)
1890 HARD_REG_SET set;
1891 unsigned int i, j;
1893 CLEAR_HARD_REG_SET (set);
1895 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1896 if (vd->e[i].oldest_regno == i)
1898 if (vd->e[i].mode == VOIDmode)
1900 if (vd->e[i].next_regno != INVALID_REGNUM)
1901 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1902 i, vd->e[i].next_regno);
1903 continue;
1906 SET_HARD_REG_BIT (set, i);
1908 for (j = vd->e[i].next_regno;
1909 j != INVALID_REGNUM;
1910 j = vd->e[j].next_regno)
1912 if (TEST_HARD_REG_BIT (set, j))
1913 internal_error ("validate_value_data: Loop in regno chain (%u)",
1915 if (vd->e[j].oldest_regno != i)
1916 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1917 j, vd->e[j].oldest_regno);
1919 SET_HARD_REG_BIT (set, j);
1923 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1924 if (! TEST_HARD_REG_BIT (set, i)
1925 && (vd->e[i].mode != VOIDmode
1926 || vd->e[i].oldest_regno != i
1927 || vd->e[i].next_regno != INVALID_REGNUM))
1928 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1929 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1930 vd->e[i].next_regno);
1932 #endif
1934 static bool
1935 gate_handle_regrename (void)
1937 return (optimize > 0 && (flag_rename_registers));
1941 /* Run the regrename and cprop passes. */
1942 static unsigned int
1943 rest_of_handle_regrename (void)
1945 regrename_optimize ();
1946 return 0;
1949 struct tree_opt_pass pass_regrename =
1951 "rnreg", /* name */
1952 gate_handle_regrename, /* gate */
1953 rest_of_handle_regrename, /* execute */
1954 NULL, /* sub */
1955 NULL, /* next */
1956 0, /* static_pass_number */
1957 TV_RENAME_REGISTERS, /* tv_id */
1958 0, /* properties_required */
1959 0, /* properties_provided */
1960 0, /* properties_destroyed */
1961 0, /* todo_flags_start */
1962 TODO_df_finish |
1963 TODO_dump_func, /* todo_flags_finish */
1964 'n' /* letter */
1967 static bool
1968 gate_handle_cprop (void)
1970 return (optimize > 0 && (flag_cprop_registers));
1974 /* Run the regrename and cprop passes. */
1975 static unsigned int
1976 rest_of_handle_cprop (void)
1978 copyprop_hardreg_forward ();
1979 return 0;
1982 struct tree_opt_pass pass_cprop_hardreg =
1984 "cprop_hardreg", /* name */
1985 gate_handle_cprop, /* gate */
1986 rest_of_handle_cprop, /* execute */
1987 NULL, /* sub */
1988 NULL, /* next */
1989 0, /* static_pass_number */
1990 TV_RENAME_REGISTERS, /* tv_id */
1991 0, /* properties_required */
1992 0, /* properties_provided */
1993 0, /* properties_destroyed */
1994 0, /* todo_flags_start */
1995 TODO_dump_func, /* todo_flags_finish */
1996 'n' /* letter */