Check in tree-dce enh to trunk
[official-gcc.git] / gcc / regrename.c
blobcfc0881be1fb5a37ab4d5ca1a17ae75118037880
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_DEFER_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);
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);
361 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
362 if (regno >= FIRST_PSEUDO_REGISTER)
363 ORIGINAL_REGNO (*chain->loc) = regno;
364 REG_ATTRS (*chain->loc) = attr;
365 df_insn_rescan (chain->insn);
366 chain = chain->next_use;
371 static struct du_chain *open_chains;
372 static struct du_chain *closed_chains;
374 static void
375 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class cl,
376 enum scan_actions action, enum op_type type, int earlyclobber)
378 struct du_chain **p;
379 rtx x = *loc;
380 enum machine_mode mode = GET_MODE (x);
381 int this_regno = REGNO (x);
382 int this_nregs = hard_regno_nregs[this_regno][mode];
384 if (action == mark_write)
386 if (type == OP_OUT)
388 struct du_chain *this
389 = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
390 this->next_use = 0;
391 this->next_chain = open_chains;
392 this->loc = loc;
393 this->insn = insn;
394 this->cl = cl;
395 this->need_caller_save_reg = 0;
396 this->earlyclobber = earlyclobber;
397 open_chains = this;
399 return;
402 if ((type == OP_OUT) != (action == terminate_write || action == mark_access))
403 return;
405 for (p = &open_chains; *p;)
407 struct du_chain *this = *p;
409 /* Check if the chain has been terminated if it has then skip to
410 the next chain.
412 This can happen when we've already appended the location to
413 the chain in Step 3, but are trying to hide in-out operands
414 from terminate_write in Step 5. */
416 if (*this->loc == cc0_rtx)
417 p = &this->next_chain;
418 else
420 int regno = REGNO (*this->loc);
421 int nregs = hard_regno_nregs[regno][GET_MODE (*this->loc)];
422 int exact_match = (regno == this_regno && nregs == this_nregs);
424 if (regno + nregs <= this_regno
425 || this_regno + this_nregs <= regno)
427 p = &this->next_chain;
428 continue;
431 if (action == mark_read || action == mark_access)
433 gcc_assert (exact_match);
435 /* ??? Class NO_REGS can happen if the md file makes use of
436 EXTRA_CONSTRAINTS to match registers. Which is arguably
437 wrong, but there we are. Since we know not what this may
438 be replaced with, terminate the chain. */
439 if (cl != NO_REGS)
441 this = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
442 this->next_use = 0;
443 this->next_chain = (*p)->next_chain;
444 this->loc = loc;
445 this->insn = insn;
446 this->cl = cl;
447 this->need_caller_save_reg = 0;
448 while (*p)
449 p = &(*p)->next_use;
450 *p = this;
451 return;
455 if (action != terminate_overlapping_read || ! exact_match)
457 struct du_chain *next = this->next_chain;
459 /* Whether the terminated chain can be used for renaming
460 depends on the action and this being an exact match.
461 In either case, we remove this element from open_chains. */
463 if ((action == terminate_dead || action == terminate_write)
464 && exact_match)
466 this->next_chain = closed_chains;
467 closed_chains = this;
468 if (dump_file)
469 fprintf (dump_file,
470 "Closing chain %s at insn %d (%s)\n",
471 reg_names[REGNO (*this->loc)], INSN_UID (insn),
472 scan_actions_name[(int) action]);
474 else
476 if (dump_file)
477 fprintf (dump_file,
478 "Discarding chain %s at insn %d (%s)\n",
479 reg_names[REGNO (*this->loc)], INSN_UID (insn),
480 scan_actions_name[(int) action]);
482 *p = next;
484 else
485 p = &this->next_chain;
490 /* Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
491 BASE_REG_CLASS depending on how the register is being considered. */
493 static void
494 scan_rtx_address (rtx insn, rtx *loc, enum reg_class cl,
495 enum scan_actions action, enum machine_mode mode)
497 rtx x = *loc;
498 RTX_CODE code = GET_CODE (x);
499 const char *fmt;
500 int i, j;
502 if (action == mark_write || action == mark_access)
503 return;
505 switch (code)
507 case PLUS:
509 rtx orig_op0 = XEXP (x, 0);
510 rtx orig_op1 = XEXP (x, 1);
511 RTX_CODE code0 = GET_CODE (orig_op0);
512 RTX_CODE code1 = GET_CODE (orig_op1);
513 rtx op0 = orig_op0;
514 rtx op1 = orig_op1;
515 rtx *locI = NULL;
516 rtx *locB = NULL;
517 enum rtx_code index_code = SCRATCH;
519 if (GET_CODE (op0) == SUBREG)
521 op0 = SUBREG_REG (op0);
522 code0 = GET_CODE (op0);
525 if (GET_CODE (op1) == SUBREG)
527 op1 = SUBREG_REG (op1);
528 code1 = GET_CODE (op1);
531 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
532 || code0 == ZERO_EXTEND || code1 == MEM)
534 locI = &XEXP (x, 0);
535 locB = &XEXP (x, 1);
536 index_code = GET_CODE (*locI);
538 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
539 || code1 == ZERO_EXTEND || code0 == MEM)
541 locI = &XEXP (x, 1);
542 locB = &XEXP (x, 0);
543 index_code = GET_CODE (*locI);
545 else if (code0 == CONST_INT || code0 == CONST
546 || code0 == SYMBOL_REF || code0 == LABEL_REF)
548 locB = &XEXP (x, 1);
549 index_code = GET_CODE (XEXP (x, 0));
551 else if (code1 == CONST_INT || code1 == CONST
552 || code1 == SYMBOL_REF || code1 == LABEL_REF)
554 locB = &XEXP (x, 0);
555 index_code = GET_CODE (XEXP (x, 1));
557 else if (code0 == REG && code1 == REG)
559 int index_op;
560 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
562 if (REGNO_OK_FOR_INDEX_P (regno1)
563 && regno_ok_for_base_p (regno0, mode, PLUS, REG))
564 index_op = 1;
565 else if (REGNO_OK_FOR_INDEX_P (regno0)
566 && regno_ok_for_base_p (regno1, mode, PLUS, REG))
567 index_op = 0;
568 else if (regno_ok_for_base_p (regno0, mode, PLUS, REG)
569 || REGNO_OK_FOR_INDEX_P (regno1))
570 index_op = 1;
571 else if (regno_ok_for_base_p (regno1, mode, PLUS, REG))
572 index_op = 0;
573 else
574 index_op = 1;
576 locI = &XEXP (x, index_op);
577 locB = &XEXP (x, !index_op);
578 index_code = GET_CODE (*locI);
580 else if (code0 == REG)
582 locI = &XEXP (x, 0);
583 locB = &XEXP (x, 1);
584 index_code = GET_CODE (*locI);
586 else if (code1 == REG)
588 locI = &XEXP (x, 1);
589 locB = &XEXP (x, 0);
590 index_code = GET_CODE (*locI);
593 if (locI)
594 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
595 if (locB)
596 scan_rtx_address (insn, locB, base_reg_class (mode, PLUS, index_code),
597 action, mode);
599 return;
602 case POST_INC:
603 case POST_DEC:
604 case POST_MODIFY:
605 case PRE_INC:
606 case PRE_DEC:
607 case PRE_MODIFY:
608 #ifndef AUTO_INC_DEC
609 /* If the target doesn't claim to handle autoinc, this must be
610 something special, like a stack push. Kill this chain. */
611 action = terminate_all_read;
612 #endif
613 break;
615 case MEM:
616 scan_rtx_address (insn, &XEXP (x, 0),
617 base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
618 GET_MODE (x));
619 return;
621 case REG:
622 scan_rtx_reg (insn, loc, cl, action, OP_IN, 0);
623 return;
625 default:
626 break;
629 fmt = GET_RTX_FORMAT (code);
630 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
632 if (fmt[i] == 'e')
633 scan_rtx_address (insn, &XEXP (x, i), cl, action, mode);
634 else if (fmt[i] == 'E')
635 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
636 scan_rtx_address (insn, &XVECEXP (x, i, j), cl, action, mode);
640 static void
641 scan_rtx (rtx insn, rtx *loc, enum reg_class cl,
642 enum scan_actions action, enum op_type type, int earlyclobber)
644 const char *fmt;
645 rtx x = *loc;
646 enum rtx_code code = GET_CODE (x);
647 int i, j;
649 code = GET_CODE (x);
650 switch (code)
652 case CONST:
653 case CONST_INT:
654 case CONST_DOUBLE:
655 case CONST_FIXED:
656 case CONST_VECTOR:
657 case SYMBOL_REF:
658 case LABEL_REF:
659 case CC0:
660 case PC:
661 return;
663 case REG:
664 scan_rtx_reg (insn, loc, cl, action, type, earlyclobber);
665 return;
667 case MEM:
668 scan_rtx_address (insn, &XEXP (x, 0),
669 base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
670 GET_MODE (x));
671 return;
673 case SET:
674 scan_rtx (insn, &SET_SRC (x), cl, action, OP_IN, 0);
675 scan_rtx (insn, &SET_DEST (x), cl, action,
676 GET_CODE (PATTERN (insn)) == COND_EXEC ? OP_INOUT : OP_OUT, 0);
677 return;
679 case STRICT_LOW_PART:
680 scan_rtx (insn, &XEXP (x, 0), cl, action, OP_INOUT, earlyclobber);
681 return;
683 case ZERO_EXTRACT:
684 case SIGN_EXTRACT:
685 scan_rtx (insn, &XEXP (x, 0), cl, action,
686 type == OP_IN ? OP_IN : OP_INOUT, earlyclobber);
687 scan_rtx (insn, &XEXP (x, 1), cl, action, OP_IN, 0);
688 scan_rtx (insn, &XEXP (x, 2), cl, action, OP_IN, 0);
689 return;
691 case POST_INC:
692 case PRE_INC:
693 case POST_DEC:
694 case PRE_DEC:
695 case POST_MODIFY:
696 case PRE_MODIFY:
697 /* Should only happen inside MEM. */
698 gcc_unreachable ();
700 case CLOBBER:
701 scan_rtx (insn, &SET_DEST (x), cl, action,
702 GET_CODE (PATTERN (insn)) == COND_EXEC ? OP_INOUT : OP_OUT, 0);
703 return;
705 case EXPR_LIST:
706 scan_rtx (insn, &XEXP (x, 0), cl, action, type, 0);
707 if (XEXP (x, 1))
708 scan_rtx (insn, &XEXP (x, 1), cl, action, type, 0);
709 return;
711 default:
712 break;
715 fmt = GET_RTX_FORMAT (code);
716 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
718 if (fmt[i] == 'e')
719 scan_rtx (insn, &XEXP (x, i), cl, action, type, 0);
720 else if (fmt[i] == 'E')
721 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
722 scan_rtx (insn, &XVECEXP (x, i, j), cl, action, type, 0);
726 /* Build def/use chain. */
728 static struct du_chain *
729 build_def_use (basic_block bb)
731 rtx insn;
733 open_chains = closed_chains = NULL;
735 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
737 if (INSN_P (insn))
739 int n_ops;
740 rtx note;
741 rtx old_operands[MAX_RECOG_OPERANDS];
742 rtx old_dups[MAX_DUP_OPERANDS];
743 int i, icode;
744 int alt;
745 int predicated;
747 /* Process the insn, determining its effect on the def-use
748 chains. We perform the following steps with the register
749 references in the insn:
750 (1) Any read that overlaps an open chain, but doesn't exactly
751 match, causes that chain to be closed. We can't deal
752 with overlaps yet.
753 (2) Any read outside an operand causes any chain it overlaps
754 with to be closed, since we can't replace it.
755 (3) Any read inside an operand is added if there's already
756 an open chain for it.
757 (4) For any REG_DEAD note we find, close open chains that
758 overlap it.
759 (5) For any write we find, close open chains that overlap it.
760 (6) For any write we find in an operand, make a new chain.
761 (7) For any REG_UNUSED, close any chains we just opened. */
763 icode = recog_memoized (insn);
764 extract_insn (insn);
765 if (! constrain_operands (1))
766 fatal_insn_not_found (insn);
767 preprocess_constraints ();
768 alt = which_alternative;
769 n_ops = recog_data.n_operands;
771 /* Simplify the code below by rewriting things to reflect
772 matching constraints. Also promote OP_OUT to OP_INOUT
773 in predicated instructions. */
775 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
776 for (i = 0; i < n_ops; ++i)
778 int matches = recog_op_alt[i][alt].matches;
779 if (matches >= 0)
780 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
781 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
782 || (predicated && recog_data.operand_type[i] == OP_OUT))
783 recog_data.operand_type[i] = OP_INOUT;
786 /* Step 1: Close chains for which we have overlapping reads. */
787 for (i = 0; i < n_ops; i++)
788 scan_rtx (insn, recog_data.operand_loc[i],
789 NO_REGS, terminate_overlapping_read,
790 recog_data.operand_type[i], 0);
792 /* Step 2: Close chains for which we have reads outside operands.
793 We do this by munging all operands into CC0, and closing
794 everything remaining. */
796 for (i = 0; i < n_ops; i++)
798 old_operands[i] = recog_data.operand[i];
799 /* Don't squash match_operator or match_parallel here, since
800 we don't know that all of the contained registers are
801 reachable by proper operands. */
802 if (recog_data.constraints[i][0] == '\0')
803 continue;
804 *recog_data.operand_loc[i] = cc0_rtx;
806 for (i = 0; i < recog_data.n_dups; i++)
808 old_dups[i] = *recog_data.dup_loc[i];
809 *recog_data.dup_loc[i] = cc0_rtx;
812 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
813 OP_IN, 0);
815 for (i = 0; i < recog_data.n_dups; i++)
816 *recog_data.dup_loc[i] = copy_rtx (old_dups[i]);
817 for (i = 0; i < n_ops; i++)
818 *recog_data.operand_loc[i] = old_operands[i];
819 if (recog_data.n_dups)
820 df_insn_rescan (insn);
822 /* Step 2B: Can't rename function call argument registers. */
823 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
824 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
825 NO_REGS, terminate_all_read, OP_IN, 0);
827 /* Step 2C: Can't rename asm operands that were originally
828 hard registers. */
829 if (asm_noperands (PATTERN (insn)) > 0)
830 for (i = 0; i < n_ops; i++)
832 rtx *loc = recog_data.operand_loc[i];
833 rtx op = *loc;
835 if (REG_P (op)
836 && REGNO (op) == ORIGINAL_REGNO (op)
837 && (recog_data.operand_type[i] == OP_IN
838 || recog_data.operand_type[i] == OP_INOUT))
839 scan_rtx (insn, loc, NO_REGS, terminate_all_read, OP_IN, 0);
842 /* Step 3: Append to chains for reads inside operands. */
843 for (i = 0; i < n_ops + recog_data.n_dups; i++)
845 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
846 rtx *loc = (i < n_ops
847 ? recog_data.operand_loc[opn]
848 : recog_data.dup_loc[i - n_ops]);
849 enum reg_class cl = recog_op_alt[opn][alt].cl;
850 enum op_type type = recog_data.operand_type[opn];
852 /* Don't scan match_operand here, since we've no reg class
853 information to pass down. Any operands that we could
854 substitute in will be represented elsewhere. */
855 if (recog_data.constraints[opn][0] == '\0')
856 continue;
858 if (recog_op_alt[opn][alt].is_address)
859 scan_rtx_address (insn, loc, cl, mark_read, VOIDmode);
860 else
861 scan_rtx (insn, loc, cl, mark_read, type, 0);
864 /* Step 3B: Record updates for regs in REG_INC notes, and
865 source regs in REG_FRAME_RELATED_EXPR notes. */
866 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
867 if (REG_NOTE_KIND (note) == REG_INC
868 || REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
869 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
870 OP_INOUT, 0);
872 /* Step 4: Close chains for registers that die here. */
873 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
874 if (REG_NOTE_KIND (note) == REG_DEAD)
875 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
876 OP_IN, 0);
878 /* Step 4B: If this is a call, any chain live at this point
879 requires a caller-saved reg. */
880 if (CALL_P (insn))
882 struct du_chain *p;
883 for (p = open_chains; p; p = p->next_chain)
884 p->need_caller_save_reg = 1;
887 /* Step 5: Close open chains that overlap writes. Similar to
888 step 2, we hide in-out operands, since we do not want to
889 close these chains. */
891 for (i = 0; i < n_ops; i++)
893 old_operands[i] = recog_data.operand[i];
894 if (recog_data.operand_type[i] == OP_INOUT)
895 *recog_data.operand_loc[i] = cc0_rtx;
897 for (i = 0; i < recog_data.n_dups; i++)
899 int opn = recog_data.dup_num[i];
900 old_dups[i] = *recog_data.dup_loc[i];
901 if (recog_data.operand_type[opn] == OP_INOUT)
902 *recog_data.dup_loc[i] = cc0_rtx;
905 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
907 for (i = 0; i < recog_data.n_dups; i++)
908 *recog_data.dup_loc[i] = old_dups[i];
909 for (i = 0; i < n_ops; i++)
910 *recog_data.operand_loc[i] = old_operands[i];
912 /* Step 6: Begin new chains for writes inside operands. */
913 /* ??? Many targets have output constraints on the SET_DEST
914 of a call insn, which is stupid, since these are certainly
915 ABI defined hard registers. Don't change calls at all.
916 Similarly take special care for asm statement that originally
917 referenced hard registers. */
918 if (asm_noperands (PATTERN (insn)) > 0)
920 for (i = 0; i < n_ops; i++)
921 if (recog_data.operand_type[i] == OP_OUT)
923 rtx *loc = recog_data.operand_loc[i];
924 rtx op = *loc;
925 enum reg_class cl = recog_op_alt[i][alt].cl;
927 if (REG_P (op)
928 && REGNO (op) == ORIGINAL_REGNO (op))
929 continue;
931 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
932 recog_op_alt[i][alt].earlyclobber);
935 else if (!CALL_P (insn))
936 for (i = 0; i < n_ops + recog_data.n_dups; i++)
938 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
939 rtx *loc = (i < n_ops
940 ? recog_data.operand_loc[opn]
941 : recog_data.dup_loc[i - n_ops]);
942 enum reg_class cl = recog_op_alt[opn][alt].cl;
944 if (recog_data.operand_type[opn] == OP_OUT)
945 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
946 recog_op_alt[opn][alt].earlyclobber);
949 /* Step 6B: Record destination regs in REG_FRAME_RELATED_EXPR
950 notes for update. */
951 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
952 if (REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
953 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_access,
954 OP_INOUT, 0);
956 /* Step 7: Close chains for registers that were never
957 really used here. */
958 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
959 if (REG_NOTE_KIND (note) == REG_UNUSED)
960 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
961 OP_IN, 0);
963 if (insn == BB_END (bb))
964 break;
967 /* Since we close every chain when we find a REG_DEAD note, anything that
968 is still open lives past the basic block, so it can't be renamed. */
969 return closed_chains;
972 /* Dump all def/use chains in CHAINS to DUMP_FILE. They are
973 printed in reverse order as that's how we build them. */
975 static void
976 dump_def_use_chain (struct du_chain *chains)
978 while (chains)
980 struct du_chain *this = chains;
981 int r = REGNO (*this->loc);
982 int nregs = hard_regno_nregs[r][GET_MODE (*this->loc)];
983 fprintf (dump_file, "Register %s (%d):", reg_names[r], nregs);
984 while (this)
986 fprintf (dump_file, " %d [%s]", INSN_UID (this->insn),
987 reg_class_names[this->cl]);
988 this = this->next_use;
990 fprintf (dump_file, "\n");
991 chains = chains->next_chain;
995 /* The following code does forward propagation of hard register copies.
996 The object is to eliminate as many dependencies as possible, so that
997 we have the most scheduling freedom. As a side effect, we also clean
998 up some silly register allocation decisions made by reload. This
999 code may be obsoleted by a new register allocator. */
1001 /* For each register, we have a list of registers that contain the same
1002 value. The OLDEST_REGNO field points to the head of the list, and
1003 the NEXT_REGNO field runs through the list. The MODE field indicates
1004 what mode the data is known to be in; this field is VOIDmode when the
1005 register is not known to contain valid data. */
1007 struct value_data_entry
1009 enum machine_mode mode;
1010 unsigned int oldest_regno;
1011 unsigned int next_regno;
1014 struct value_data
1016 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
1017 unsigned int max_value_regs;
1020 static void kill_value_one_regno (unsigned, struct value_data *);
1021 static void kill_value_regno (unsigned, unsigned, struct value_data *);
1022 static void kill_value (rtx, struct value_data *);
1023 static void set_value_regno (unsigned, enum machine_mode, struct value_data *);
1024 static void init_value_data (struct value_data *);
1025 static void kill_clobbered_value (rtx, const_rtx, void *);
1026 static void kill_set_value (rtx, const_rtx, void *);
1027 static int kill_autoinc_value (rtx *, void *);
1028 static void copy_value (rtx, rtx, struct value_data *);
1029 static bool mode_change_ok (enum machine_mode, enum machine_mode,
1030 unsigned int);
1031 static rtx maybe_mode_change (enum machine_mode, enum machine_mode,
1032 enum machine_mode, unsigned int, unsigned int);
1033 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
1034 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx,
1035 struct value_data *);
1036 static bool replace_oldest_value_addr (rtx *, enum reg_class,
1037 enum machine_mode, rtx,
1038 struct value_data *);
1039 static bool replace_oldest_value_mem (rtx, rtx, struct value_data *);
1040 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
1041 extern void debug_value_data (struct value_data *);
1042 #ifdef ENABLE_CHECKING
1043 static void validate_value_data (struct value_data *);
1044 #endif
1046 /* Kill register REGNO. This involves removing it from any value
1047 lists, and resetting the value mode to VOIDmode. This is only a
1048 helper function; it does not handle any hard registers overlapping
1049 with REGNO. */
1051 static void
1052 kill_value_one_regno (unsigned int regno, struct value_data *vd)
1054 unsigned int i, next;
1056 if (vd->e[regno].oldest_regno != regno)
1058 for (i = vd->e[regno].oldest_regno;
1059 vd->e[i].next_regno != regno;
1060 i = vd->e[i].next_regno)
1061 continue;
1062 vd->e[i].next_regno = vd->e[regno].next_regno;
1064 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
1066 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
1067 vd->e[i].oldest_regno = next;
1070 vd->e[regno].mode = VOIDmode;
1071 vd->e[regno].oldest_regno = regno;
1072 vd->e[regno].next_regno = INVALID_REGNUM;
1074 #ifdef ENABLE_CHECKING
1075 validate_value_data (vd);
1076 #endif
1079 /* Kill the value in register REGNO for NREGS, and any other registers
1080 whose values overlap. */
1082 static void
1083 kill_value_regno (unsigned int regno, unsigned int nregs,
1084 struct value_data *vd)
1086 unsigned int j;
1088 /* Kill the value we're told to kill. */
1089 for (j = 0; j < nregs; ++j)
1090 kill_value_one_regno (regno + j, vd);
1092 /* Kill everything that overlapped what we're told to kill. */
1093 if (regno < vd->max_value_regs)
1094 j = 0;
1095 else
1096 j = regno - vd->max_value_regs;
1097 for (; j < regno; ++j)
1099 unsigned int i, n;
1100 if (vd->e[j].mode == VOIDmode)
1101 continue;
1102 n = hard_regno_nregs[j][vd->e[j].mode];
1103 if (j + n > regno)
1104 for (i = 0; i < n; ++i)
1105 kill_value_one_regno (j + i, vd);
1109 /* Kill X. This is a convenience function wrapping kill_value_regno
1110 so that we mind the mode the register is in. */
1112 static void
1113 kill_value (rtx x, struct value_data *vd)
1115 rtx orig_rtx = x;
1117 if (GET_CODE (x) == SUBREG)
1119 x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
1120 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
1121 if (x == NULL_RTX)
1122 x = SUBREG_REG (orig_rtx);
1124 if (REG_P (x))
1126 unsigned int regno = REGNO (x);
1127 unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
1129 kill_value_regno (regno, n, vd);
1133 /* Remember that REGNO is valid in MODE. */
1135 static void
1136 set_value_regno (unsigned int regno, enum machine_mode mode,
1137 struct value_data *vd)
1139 unsigned int nregs;
1141 vd->e[regno].mode = mode;
1143 nregs = hard_regno_nregs[regno][mode];
1144 if (nregs > vd->max_value_regs)
1145 vd->max_value_regs = nregs;
1148 /* Initialize VD such that there are no known relationships between regs. */
1150 static void
1151 init_value_data (struct value_data *vd)
1153 int i;
1154 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1156 vd->e[i].mode = VOIDmode;
1157 vd->e[i].oldest_regno = i;
1158 vd->e[i].next_regno = INVALID_REGNUM;
1160 vd->max_value_regs = 0;
1163 /* Called through note_stores. If X is clobbered, kill its value. */
1165 static void
1166 kill_clobbered_value (rtx x, const_rtx set, void *data)
1168 struct value_data *vd = data;
1169 if (GET_CODE (set) == CLOBBER)
1170 kill_value (x, vd);
1173 /* Called through note_stores. If X is set, not clobbered, kill its
1174 current value and install it as the root of its own value list. */
1176 static void
1177 kill_set_value (rtx x, const_rtx set, void *data)
1179 struct value_data *vd = data;
1180 if (GET_CODE (set) != CLOBBER)
1182 kill_value (x, vd);
1183 if (REG_P (x))
1184 set_value_regno (REGNO (x), GET_MODE (x), vd);
1188 /* Called through for_each_rtx. Kill any register used as the base of an
1189 auto-increment expression, and install that register as the root of its
1190 own value list. */
1192 static int
1193 kill_autoinc_value (rtx *px, void *data)
1195 rtx x = *px;
1196 struct value_data *vd = data;
1198 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
1200 x = XEXP (x, 0);
1201 kill_value (x, vd);
1202 set_value_regno (REGNO (x), Pmode, vd);
1203 return -1;
1206 return 0;
1209 /* Assert that SRC has been copied to DEST. Adjust the data structures
1210 to reflect that SRC contains an older copy of the shared value. */
1212 static void
1213 copy_value (rtx dest, rtx src, struct value_data *vd)
1215 unsigned int dr = REGNO (dest);
1216 unsigned int sr = REGNO (src);
1217 unsigned int dn, sn;
1218 unsigned int i;
1220 /* ??? At present, it's possible to see noop sets. It'd be nice if
1221 this were cleaned up beforehand... */
1222 if (sr == dr)
1223 return;
1225 /* Do not propagate copies to the stack pointer, as that can leave
1226 memory accesses with no scheduling dependency on the stack update. */
1227 if (dr == STACK_POINTER_REGNUM)
1228 return;
1230 /* Likewise with the frame pointer, if we're using one. */
1231 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
1232 return;
1234 /* Do not propagate copies to fixed or global registers, patterns
1235 can be relying to see particular fixed register or users can
1236 expect the chosen global register in asm. */
1237 if (fixed_regs[dr] || global_regs[dr])
1238 return;
1240 /* If SRC and DEST overlap, don't record anything. */
1241 dn = hard_regno_nregs[dr][GET_MODE (dest)];
1242 sn = hard_regno_nregs[sr][GET_MODE (dest)];
1243 if ((dr > sr && dr < sr + sn)
1244 || (sr > dr && sr < dr + dn))
1245 return;
1247 /* If SRC had no assigned mode (i.e. we didn't know it was live)
1248 assign it now and assume the value came from an input argument
1249 or somesuch. */
1250 if (vd->e[sr].mode == VOIDmode)
1251 set_value_regno (sr, vd->e[dr].mode, vd);
1253 /* If we are narrowing the input to a smaller number of hard regs,
1254 and it is in big endian, we are really extracting a high part.
1255 Since we generally associate a low part of a value with the value itself,
1256 we must not do the same for the high part.
1257 Note we can still get low parts for the same mode combination through
1258 a two-step copy involving differently sized hard regs.
1259 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
1260 (set (reg:DI r0) (reg:DI fr0))
1261 (set (reg:SI fr2) (reg:SI r0))
1262 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
1263 (set (reg:SI fr2) (reg:SI fr0))
1264 loads the high part of (reg:DI fr0) into fr2.
1266 We can't properly represent the latter case in our tables, so don't
1267 record anything then. */
1268 else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
1269 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
1270 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
1271 return;
1273 /* If SRC had been assigned a mode narrower than the copy, we can't
1274 link DEST into the chain, because not all of the pieces of the
1275 copy came from oldest_regno. */
1276 else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
1277 return;
1279 /* Link DR at the end of the value chain used by SR. */
1281 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
1283 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
1284 continue;
1285 vd->e[i].next_regno = dr;
1287 #ifdef ENABLE_CHECKING
1288 validate_value_data (vd);
1289 #endif
1292 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
1294 static bool
1295 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
1296 unsigned int regno ATTRIBUTE_UNUSED)
1298 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
1299 return false;
1301 #ifdef CANNOT_CHANGE_MODE_CLASS
1302 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
1303 #endif
1305 return true;
1308 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
1309 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
1310 in NEW_MODE.
1311 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
1313 static rtx
1314 maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
1315 enum machine_mode new_mode, unsigned int regno,
1316 unsigned int copy_regno ATTRIBUTE_UNUSED)
1318 if (orig_mode == new_mode)
1319 return gen_rtx_raw_REG (new_mode, regno);
1320 else if (mode_change_ok (orig_mode, new_mode, regno))
1322 int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
1323 int use_nregs = hard_regno_nregs[copy_regno][new_mode];
1324 int copy_offset
1325 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
1326 int offset
1327 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
1328 int byteoffset = offset % UNITS_PER_WORD;
1329 int wordoffset = offset - byteoffset;
1331 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
1332 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
1333 return gen_rtx_raw_REG (new_mode,
1334 regno + subreg_regno_offset (regno, orig_mode,
1335 offset,
1336 new_mode));
1338 return NULL_RTX;
1341 /* Find the oldest copy of the value contained in REGNO that is in
1342 register class CL and has mode MODE. If found, return an rtx
1343 of that oldest register, otherwise return NULL. */
1345 static rtx
1346 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
1348 unsigned int regno = REGNO (reg);
1349 enum machine_mode mode = GET_MODE (reg);
1350 unsigned int i;
1352 /* If we are accessing REG in some mode other that what we set it in,
1353 make sure that the replacement is valid. In particular, consider
1354 (set (reg:DI r11) (...))
1355 (set (reg:SI r9) (reg:SI r11))
1356 (set (reg:SI r10) (...))
1357 (set (...) (reg:DI r9))
1358 Replacing r9 with r11 is invalid. */
1359 if (mode != vd->e[regno].mode)
1361 if (hard_regno_nregs[regno][mode]
1362 > hard_regno_nregs[regno][vd->e[regno].mode])
1363 return NULL_RTX;
1366 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
1368 enum machine_mode oldmode = vd->e[i].mode;
1369 rtx new;
1371 if (!in_hard_reg_set_p (reg_class_contents[cl], mode, i))
1372 return NULL_RTX;
1374 new = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
1375 if (new)
1377 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (reg);
1378 REG_ATTRS (new) = REG_ATTRS (reg);
1379 return new;
1383 return NULL_RTX;
1386 /* If possible, replace the register at *LOC with the oldest register
1387 in register class CL. Return true if successfully replaced. */
1389 static bool
1390 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx insn,
1391 struct value_data *vd)
1393 rtx new = find_oldest_value_reg (cl, *loc, vd);
1394 if (new)
1396 if (dump_file)
1397 fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
1398 INSN_UID (insn), REGNO (*loc), REGNO (new));
1400 validate_change (insn, loc, new, 1);
1401 return true;
1403 return false;
1406 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1407 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
1408 BASE_REG_CLASS depending on how the register is being considered. */
1410 static bool
1411 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
1412 enum machine_mode mode, rtx insn,
1413 struct value_data *vd)
1415 rtx x = *loc;
1416 RTX_CODE code = GET_CODE (x);
1417 const char *fmt;
1418 int i, j;
1419 bool changed = false;
1421 switch (code)
1423 case PLUS:
1425 rtx orig_op0 = XEXP (x, 0);
1426 rtx orig_op1 = XEXP (x, 1);
1427 RTX_CODE code0 = GET_CODE (orig_op0);
1428 RTX_CODE code1 = GET_CODE (orig_op1);
1429 rtx op0 = orig_op0;
1430 rtx op1 = orig_op1;
1431 rtx *locI = NULL;
1432 rtx *locB = NULL;
1433 enum rtx_code index_code = SCRATCH;
1435 if (GET_CODE (op0) == SUBREG)
1437 op0 = SUBREG_REG (op0);
1438 code0 = GET_CODE (op0);
1441 if (GET_CODE (op1) == SUBREG)
1443 op1 = SUBREG_REG (op1);
1444 code1 = GET_CODE (op1);
1447 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1448 || code0 == ZERO_EXTEND || code1 == MEM)
1450 locI = &XEXP (x, 0);
1451 locB = &XEXP (x, 1);
1452 index_code = GET_CODE (*locI);
1454 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1455 || code1 == ZERO_EXTEND || code0 == MEM)
1457 locI = &XEXP (x, 1);
1458 locB = &XEXP (x, 0);
1459 index_code = GET_CODE (*locI);
1461 else if (code0 == CONST_INT || code0 == CONST
1462 || code0 == SYMBOL_REF || code0 == LABEL_REF)
1464 locB = &XEXP (x, 1);
1465 index_code = GET_CODE (XEXP (x, 0));
1467 else if (code1 == CONST_INT || code1 == CONST
1468 || code1 == SYMBOL_REF || code1 == LABEL_REF)
1470 locB = &XEXP (x, 0);
1471 index_code = GET_CODE (XEXP (x, 1));
1473 else if (code0 == REG && code1 == REG)
1475 int index_op;
1476 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
1478 if (REGNO_OK_FOR_INDEX_P (regno1)
1479 && regno_ok_for_base_p (regno0, mode, PLUS, REG))
1480 index_op = 1;
1481 else if (REGNO_OK_FOR_INDEX_P (regno0)
1482 && regno_ok_for_base_p (regno1, mode, PLUS, REG))
1483 index_op = 0;
1484 else if (regno_ok_for_base_p (regno0, mode, PLUS, REG)
1485 || REGNO_OK_FOR_INDEX_P (regno1))
1486 index_op = 1;
1487 else if (regno_ok_for_base_p (regno1, mode, PLUS, REG))
1488 index_op = 0;
1489 else
1490 index_op = 1;
1492 locI = &XEXP (x, index_op);
1493 locB = &XEXP (x, !index_op);
1494 index_code = GET_CODE (*locI);
1496 else if (code0 == REG)
1498 locI = &XEXP (x, 0);
1499 locB = &XEXP (x, 1);
1500 index_code = GET_CODE (*locI);
1502 else if (code1 == REG)
1504 locI = &XEXP (x, 1);
1505 locB = &XEXP (x, 0);
1506 index_code = GET_CODE (*locI);
1509 if (locI)
1510 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
1511 insn, vd);
1512 if (locB)
1513 changed |= replace_oldest_value_addr (locB,
1514 base_reg_class (mode, PLUS,
1515 index_code),
1516 mode, insn, vd);
1517 return changed;
1520 case POST_INC:
1521 case POST_DEC:
1522 case POST_MODIFY:
1523 case PRE_INC:
1524 case PRE_DEC:
1525 case PRE_MODIFY:
1526 return false;
1528 case MEM:
1529 return replace_oldest_value_mem (x, insn, vd);
1531 case REG:
1532 return replace_oldest_value_reg (loc, cl, insn, vd);
1534 default:
1535 break;
1538 fmt = GET_RTX_FORMAT (code);
1539 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1541 if (fmt[i] == 'e')
1542 changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode,
1543 insn, vd);
1544 else if (fmt[i] == 'E')
1545 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1546 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
1547 mode, insn, vd);
1550 return changed;
1553 /* Similar to replace_oldest_value_reg, but X contains a memory. */
1555 static bool
1556 replace_oldest_value_mem (rtx x, rtx insn, struct value_data *vd)
1558 return replace_oldest_value_addr (&XEXP (x, 0),
1559 base_reg_class (GET_MODE (x), MEM,
1560 SCRATCH),
1561 GET_MODE (x), insn, vd);
1564 /* Perform the forward copy propagation on basic block BB. */
1566 static bool
1567 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
1569 bool changed = false;
1570 rtx insn;
1572 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1574 int n_ops, i, alt, predicated;
1575 bool is_asm, any_replacements;
1576 rtx set;
1577 bool replaced[MAX_RECOG_OPERANDS];
1579 if (! INSN_P (insn))
1581 if (insn == BB_END (bb))
1582 break;
1583 else
1584 continue;
1587 set = single_set (insn);
1588 extract_insn (insn);
1589 if (! constrain_operands (1))
1590 fatal_insn_not_found (insn);
1591 preprocess_constraints ();
1592 alt = which_alternative;
1593 n_ops = recog_data.n_operands;
1594 is_asm = asm_noperands (PATTERN (insn)) >= 0;
1596 /* Simplify the code below by rewriting things to reflect
1597 matching constraints. Also promote OP_OUT to OP_INOUT
1598 in predicated instructions. */
1600 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1601 for (i = 0; i < n_ops; ++i)
1603 int matches = recog_op_alt[i][alt].matches;
1604 if (matches >= 0)
1605 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
1606 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1607 || (predicated && recog_data.operand_type[i] == OP_OUT))
1608 recog_data.operand_type[i] = OP_INOUT;
1611 /* For each earlyclobber operand, zap the value data. */
1612 for (i = 0; i < n_ops; i++)
1613 if (recog_op_alt[i][alt].earlyclobber)
1614 kill_value (recog_data.operand[i], vd);
1616 /* Within asms, a clobber cannot overlap inputs or outputs.
1617 I wouldn't think this were true for regular insns, but
1618 scan_rtx treats them like that... */
1619 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1621 /* Kill all auto-incremented values. */
1622 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
1623 for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
1625 /* Kill all early-clobbered operands. */
1626 for (i = 0; i < n_ops; i++)
1627 if (recog_op_alt[i][alt].earlyclobber)
1628 kill_value (recog_data.operand[i], vd);
1630 /* Special-case plain move instructions, since we may well
1631 be able to do the move from a different register class. */
1632 if (set && REG_P (SET_SRC (set)))
1634 rtx src = SET_SRC (set);
1635 unsigned int regno = REGNO (src);
1636 enum machine_mode mode = GET_MODE (src);
1637 unsigned int i;
1638 rtx new;
1640 /* If we are accessing SRC in some mode other that what we
1641 set it in, make sure that the replacement is valid. */
1642 if (mode != vd->e[regno].mode)
1644 if (hard_regno_nregs[regno][mode]
1645 > hard_regno_nregs[regno][vd->e[regno].mode])
1646 goto no_move_special_case;
1649 /* If the destination is also a register, try to find a source
1650 register in the same class. */
1651 if (REG_P (SET_DEST (set)))
1653 new = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
1654 if (new && validate_change (insn, &SET_SRC (set), new, 0))
1656 if (dump_file)
1657 fprintf (dump_file,
1658 "insn %u: replaced reg %u with %u\n",
1659 INSN_UID (insn), regno, REGNO (new));
1660 changed = true;
1661 goto did_replacement;
1665 /* Otherwise, try all valid registers and see if its valid. */
1666 for (i = vd->e[regno].oldest_regno; i != regno;
1667 i = vd->e[i].next_regno)
1669 new = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
1670 mode, i, regno);
1671 if (new != NULL_RTX)
1673 if (validate_change (insn, &SET_SRC (set), new, 0))
1675 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (src);
1676 REG_ATTRS (new) = REG_ATTRS (src);
1677 if (dump_file)
1678 fprintf (dump_file,
1679 "insn %u: replaced reg %u with %u\n",
1680 INSN_UID (insn), regno, REGNO (new));
1681 changed = true;
1682 goto did_replacement;
1687 no_move_special_case:
1689 any_replacements = false;
1691 /* For each input operand, replace a hard register with the
1692 eldest live copy that's in an appropriate register class. */
1693 for (i = 0; i < n_ops; i++)
1695 replaced[i] = false;
1697 /* Don't scan match_operand here, since we've no reg class
1698 information to pass down. Any operands that we could
1699 substitute in will be represented elsewhere. */
1700 if (recog_data.constraints[i][0] == '\0')
1701 continue;
1703 /* Don't replace in asms intentionally referencing hard regs. */
1704 if (is_asm && REG_P (recog_data.operand[i])
1705 && (REGNO (recog_data.operand[i])
1706 == ORIGINAL_REGNO (recog_data.operand[i])))
1707 continue;
1709 if (recog_data.operand_type[i] == OP_IN)
1711 if (recog_op_alt[i][alt].is_address)
1712 replaced[i]
1713 = replace_oldest_value_addr (recog_data.operand_loc[i],
1714 recog_op_alt[i][alt].cl,
1715 VOIDmode, insn, vd);
1716 else if (REG_P (recog_data.operand[i]))
1717 replaced[i]
1718 = replace_oldest_value_reg (recog_data.operand_loc[i],
1719 recog_op_alt[i][alt].cl,
1720 insn, vd);
1721 else if (MEM_P (recog_data.operand[i]))
1722 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
1723 insn, vd);
1725 else if (MEM_P (recog_data.operand[i]))
1726 replaced[i] = replace_oldest_value_mem (recog_data.operand[i],
1727 insn, vd);
1729 /* If we performed any replacement, update match_dups. */
1730 if (replaced[i])
1732 int j;
1733 rtx new;
1735 new = *recog_data.operand_loc[i];
1736 recog_data.operand[i] = new;
1737 for (j = 0; j < recog_data.n_dups; j++)
1738 if (recog_data.dup_num[j] == i)
1739 validate_unshare_change (insn, recog_data.dup_loc[j], new, 1);
1741 any_replacements = true;
1745 if (any_replacements)
1747 if (! apply_change_group ())
1749 for (i = 0; i < n_ops; i++)
1750 if (replaced[i])
1752 rtx old = *recog_data.operand_loc[i];
1753 recog_data.operand[i] = old;
1756 if (dump_file)
1757 fprintf (dump_file,
1758 "insn %u: reg replacements not verified\n",
1759 INSN_UID (insn));
1761 else
1762 changed = true;
1765 did_replacement:
1766 /* Clobber call-clobbered registers. */
1767 if (CALL_P (insn))
1768 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1769 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1770 kill_value_regno (i, 1, vd);
1772 /* Notice stores. */
1773 note_stores (PATTERN (insn), kill_set_value, vd);
1775 /* Notice copies. */
1776 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1777 copy_value (SET_DEST (set), SET_SRC (set), vd);
1779 if (insn == BB_END (bb))
1780 break;
1783 return changed;
1786 /* Main entry point for the forward copy propagation optimization. */
1788 static void
1789 copyprop_hardreg_forward (void)
1791 struct value_data *all_vd;
1792 basic_block bb;
1793 sbitmap visited;
1795 all_vd = XNEWVEC (struct value_data, last_basic_block);
1797 visited = sbitmap_alloc (last_basic_block);
1798 sbitmap_zero (visited);
1800 FOR_EACH_BB (bb)
1802 SET_BIT (visited, bb->index);
1804 /* If a block has a single predecessor, that we've already
1805 processed, begin with the value data that was live at
1806 the end of the predecessor block. */
1807 /* ??? Ought to use more intelligent queuing of blocks. */
1808 if (single_pred_p (bb)
1809 && TEST_BIT (visited, single_pred (bb)->index)
1810 && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1811 all_vd[bb->index] = all_vd[single_pred (bb)->index];
1812 else
1813 init_value_data (all_vd + bb->index);
1815 copyprop_hardreg_forward_1 (bb, all_vd + bb->index);
1818 sbitmap_free (visited);
1819 free (all_vd);
1822 /* Dump the value chain data to stderr. */
1824 void
1825 debug_value_data (struct value_data *vd)
1827 HARD_REG_SET set;
1828 unsigned int i, j;
1830 CLEAR_HARD_REG_SET (set);
1832 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1833 if (vd->e[i].oldest_regno == i)
1835 if (vd->e[i].mode == VOIDmode)
1837 if (vd->e[i].next_regno != INVALID_REGNUM)
1838 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1839 i, vd->e[i].next_regno);
1840 continue;
1843 SET_HARD_REG_BIT (set, i);
1844 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1846 for (j = vd->e[i].next_regno;
1847 j != INVALID_REGNUM;
1848 j = vd->e[j].next_regno)
1850 if (TEST_HARD_REG_BIT (set, j))
1852 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1853 return;
1856 if (vd->e[j].oldest_regno != i)
1858 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1859 j, vd->e[j].oldest_regno);
1860 return;
1862 SET_HARD_REG_BIT (set, j);
1863 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1865 fputc ('\n', stderr);
1868 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1869 if (! TEST_HARD_REG_BIT (set, i)
1870 && (vd->e[i].mode != VOIDmode
1871 || vd->e[i].oldest_regno != i
1872 || vd->e[i].next_regno != INVALID_REGNUM))
1873 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1874 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1875 vd->e[i].next_regno);
1878 #ifdef ENABLE_CHECKING
1879 static void
1880 validate_value_data (struct value_data *vd)
1882 HARD_REG_SET set;
1883 unsigned int i, j;
1885 CLEAR_HARD_REG_SET (set);
1887 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1888 if (vd->e[i].oldest_regno == i)
1890 if (vd->e[i].mode == VOIDmode)
1892 if (vd->e[i].next_regno != INVALID_REGNUM)
1893 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1894 i, vd->e[i].next_regno);
1895 continue;
1898 SET_HARD_REG_BIT (set, i);
1900 for (j = vd->e[i].next_regno;
1901 j != INVALID_REGNUM;
1902 j = vd->e[j].next_regno)
1904 if (TEST_HARD_REG_BIT (set, j))
1905 internal_error ("validate_value_data: Loop in regno chain (%u)",
1907 if (vd->e[j].oldest_regno != i)
1908 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1909 j, vd->e[j].oldest_regno);
1911 SET_HARD_REG_BIT (set, j);
1915 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1916 if (! TEST_HARD_REG_BIT (set, i)
1917 && (vd->e[i].mode != VOIDmode
1918 || vd->e[i].oldest_regno != i
1919 || vd->e[i].next_regno != INVALID_REGNUM))
1920 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1921 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1922 vd->e[i].next_regno);
1924 #endif
1926 static bool
1927 gate_handle_regrename (void)
1929 return (optimize > 0 && (flag_rename_registers));
1933 /* Run the regrename and cprop passes. */
1934 static unsigned int
1935 rest_of_handle_regrename (void)
1937 regrename_optimize ();
1938 return 0;
1941 struct rtl_opt_pass pass_regrename =
1944 RTL_PASS,
1945 "rnreg", /* name */
1946 gate_handle_regrename, /* gate */
1947 rest_of_handle_regrename, /* execute */
1948 NULL, /* sub */
1949 NULL, /* next */
1950 0, /* static_pass_number */
1951 TV_RENAME_REGISTERS, /* tv_id */
1952 0, /* properties_required */
1953 0, /* properties_provided */
1954 0, /* properties_destroyed */
1955 0, /* todo_flags_start */
1956 TODO_df_finish | TODO_verify_rtl_sharing |
1957 TODO_dump_func /* todo_flags_finish */
1961 static bool
1962 gate_handle_cprop (void)
1964 return (optimize > 0 && (flag_cprop_registers));
1968 /* Run the regrename and cprop passes. */
1969 static unsigned int
1970 rest_of_handle_cprop (void)
1972 copyprop_hardreg_forward ();
1973 return 0;
1976 struct rtl_opt_pass pass_cprop_hardreg =
1979 RTL_PASS,
1980 "cprop_hardreg", /* name */
1981 gate_handle_cprop, /* gate */
1982 rest_of_handle_cprop, /* execute */
1983 NULL, /* sub */
1984 NULL, /* next */
1985 0, /* static_pass_number */
1986 TV_RENAME_REGISTERS, /* tv_id */
1987 0, /* properties_required */
1988 0, /* properties_provided */
1989 0, /* properties_destroyed */
1990 0, /* todo_flags_start */
1991 TODO_dump_func | TODO_verify_rtl_sharing /* todo_flags_finish */