* expr.c (force_operand): Use convert_to_mode for conversions.
[official-gcc.git] / gcc / regrename.c
blob06b1c5b748055586da9a6f2dcbbb66d8b3058db3
1 /* Register renaming for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005
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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "insn-config.h"
29 #include "regs.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"
42 struct du_chain
44 struct du_chain *next_chain;
45 struct du_chain *next_use;
47 rtx insn;
48 rtx *loc;
49 ENUM_BITFIELD(reg_class) cl : 16;
50 unsigned int need_caller_save_reg:1;
51 unsigned int earlyclobber:1;
54 enum scan_actions
56 terminate_all_read,
57 terminate_overlapping_read,
58 terminate_write,
59 terminate_dead,
60 mark_read,
61 mark_write,
62 /* mark_access is for marking the destination regs in
63 REG_FRAME_RELATED_EXPR notes (as if they were read) so that the
64 note is updated properly. */
65 mark_access
68 static const char * const scan_actions_name[] =
70 "terminate_all_read",
71 "terminate_overlapping_read",
72 "terminate_write",
73 "terminate_dead",
74 "mark_read",
75 "mark_write",
76 "mark_access"
79 static struct obstack rename_obstack;
81 static void do_replace (struct du_chain *, int);
82 static void scan_rtx_reg (rtx, rtx *, enum reg_class,
83 enum scan_actions, enum op_type, int);
84 static void scan_rtx_address (rtx, rtx *, enum reg_class,
85 enum scan_actions, enum machine_mode);
86 static void scan_rtx (rtx, rtx *, enum reg_class, enum scan_actions,
87 enum op_type, int);
88 static struct du_chain *build_def_use (basic_block);
89 static void dump_def_use_chain (struct du_chain *);
90 static void note_sets (rtx, rtx, void *);
91 static void clear_dead_regs (HARD_REG_SET *, enum machine_mode, rtx);
92 static void merge_overlapping_regs (basic_block, HARD_REG_SET *,
93 struct du_chain *);
95 /* Called through note_stores from update_life. Find sets of registers, and
96 record them in *DATA (which is actually a HARD_REG_SET *). */
98 static void
99 note_sets (rtx x, rtx set ATTRIBUTE_UNUSED, void *data)
101 HARD_REG_SET *pset = (HARD_REG_SET *) data;
102 unsigned int regno;
103 int nregs;
105 if (GET_CODE (x) == SUBREG)
106 x = SUBREG_REG (x);
107 if (!REG_P (x))
108 return;
109 regno = REGNO (x);
110 nregs = hard_regno_nregs[regno][GET_MODE (x)];
112 /* There must not be pseudos at this point. */
113 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
115 while (nregs-- > 0)
116 SET_HARD_REG_BIT (*pset, regno + nregs);
119 /* Clear all registers from *PSET for which a note of kind KIND can be found
120 in the list NOTES. */
122 static void
123 clear_dead_regs (HARD_REG_SET *pset, enum machine_mode kind, rtx notes)
125 rtx note;
126 for (note = notes; note; note = XEXP (note, 1))
127 if (REG_NOTE_KIND (note) == kind && REG_P (XEXP (note, 0)))
129 rtx reg = XEXP (note, 0);
130 unsigned int regno = REGNO (reg);
131 int nregs = hard_regno_nregs[regno][GET_MODE (reg)];
133 /* There must not be pseudos at this point. */
134 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
136 while (nregs-- > 0)
137 CLEAR_HARD_REG_BIT (*pset, regno + nregs);
141 /* For a def-use chain CHAIN in basic block B, find which registers overlap
142 its lifetime and set the corresponding bits in *PSET. */
144 static void
145 merge_overlapping_regs (basic_block b, HARD_REG_SET *pset,
146 struct du_chain *chain)
148 struct du_chain *t = chain;
149 rtx insn;
150 HARD_REG_SET live;
152 REG_SET_TO_HARD_REG_SET (live, b->il.rtl->global_live_at_start);
153 insn = BB_HEAD (b);
154 while (t)
156 /* Search forward until the next reference to the register to be
157 renamed. */
158 while (insn != t->insn)
160 if (INSN_P (insn))
162 clear_dead_regs (&live, REG_DEAD, REG_NOTES (insn));
163 note_stores (PATTERN (insn), note_sets, (void *) &live);
164 /* Only record currently live regs if we are inside the
165 reg's live range. */
166 if (t != chain)
167 IOR_HARD_REG_SET (*pset, live);
168 clear_dead_regs (&live, REG_UNUSED, REG_NOTES (insn));
170 insn = NEXT_INSN (insn);
173 IOR_HARD_REG_SET (*pset, live);
175 /* For the last reference, also merge in all registers set in the
176 same insn.
177 @@@ We only have take earlyclobbered sets into account. */
178 if (! t->next_use)
179 note_stores (PATTERN (insn), note_sets, (void *) pset);
181 t = t->next_use;
185 /* Perform register renaming on the current function. */
187 void
188 regrename_optimize (void)
190 int tick[FIRST_PSEUDO_REGISTER];
191 int this_tick = 0;
192 basic_block bb;
193 char *first_obj;
195 memset (tick, 0, sizeof tick);
197 gcc_obstack_init (&rename_obstack);
198 first_obj = obstack_alloc (&rename_obstack, 0);
200 FOR_EACH_BB (bb)
202 struct du_chain *all_chains = 0;
203 HARD_REG_SET unavailable;
204 HARD_REG_SET regs_seen;
206 CLEAR_HARD_REG_SET (unavailable);
208 if (dump_file)
209 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
211 all_chains = build_def_use (bb);
213 if (dump_file)
214 dump_def_use_chain (all_chains);
216 CLEAR_HARD_REG_SET (unavailable);
217 /* Don't clobber traceback for noreturn functions. */
218 if (frame_pointer_needed)
220 int i;
222 for (i = hard_regno_nregs[FRAME_POINTER_REGNUM][Pmode]; i--;)
223 SET_HARD_REG_BIT (unavailable, FRAME_POINTER_REGNUM + i);
225 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
226 for (i = hard_regno_nregs[HARD_FRAME_POINTER_REGNUM][Pmode]; i--;)
227 SET_HARD_REG_BIT (unavailable, HARD_FRAME_POINTER_REGNUM + i);
228 #endif
231 CLEAR_HARD_REG_SET (regs_seen);
232 while (all_chains)
234 int new_reg, best_new_reg;
235 int n_uses;
236 struct du_chain *this = all_chains;
237 struct du_chain *tmp, *last;
238 HARD_REG_SET this_unavailable;
239 int reg = REGNO (*this->loc);
240 int i;
242 all_chains = this->next_chain;
244 best_new_reg = reg;
246 #if 0 /* This just disables optimization opportunities. */
247 /* Only rename once we've seen the reg more than once. */
248 if (! TEST_HARD_REG_BIT (regs_seen, reg))
250 SET_HARD_REG_BIT (regs_seen, reg);
251 continue;
253 #endif
255 if (fixed_regs[reg] || global_regs[reg]
256 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
257 || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
258 #else
259 || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
260 #endif
262 continue;
264 COPY_HARD_REG_SET (this_unavailable, unavailable);
266 /* Find last entry on chain (which has the need_caller_save bit),
267 count number of uses, and narrow the set of registers we can
268 use for renaming. */
269 n_uses = 0;
270 for (last = this; last->next_use; last = last->next_use)
272 n_uses++;
273 IOR_COMPL_HARD_REG_SET (this_unavailable,
274 reg_class_contents[last->cl]);
276 if (n_uses < 1)
277 continue;
279 IOR_COMPL_HARD_REG_SET (this_unavailable,
280 reg_class_contents[last->cl]);
282 if (this->need_caller_save_reg)
283 IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
285 merge_overlapping_regs (bb, &this_unavailable, this);
287 /* Now potential_regs is a reasonable approximation, let's
288 have a closer look at each register still in there. */
289 for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
291 int nregs = hard_regno_nregs[new_reg][GET_MODE (*this->loc)];
293 for (i = nregs - 1; i >= 0; --i)
294 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
295 || fixed_regs[new_reg + i]
296 || global_regs[new_reg + i]
297 /* Can't use regs which aren't saved by the prologue. */
298 || (! regs_ever_live[new_reg + i]
299 && ! call_used_regs[new_reg + i])
300 #ifdef LEAF_REGISTERS
301 /* We can't use a non-leaf register if we're in a
302 leaf function. */
303 || (current_function_is_leaf
304 && !LEAF_REGISTERS[new_reg + i])
305 #endif
306 #ifdef HARD_REGNO_RENAME_OK
307 || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
308 #endif
310 break;
311 if (i >= 0)
312 continue;
314 /* See whether it accepts all modes that occur in
315 definition and uses. */
316 for (tmp = this; tmp; tmp = tmp->next_use)
317 if (! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
318 || (tmp->need_caller_save_reg
319 && ! (HARD_REGNO_CALL_PART_CLOBBERED
320 (reg, GET_MODE (*tmp->loc)))
321 && (HARD_REGNO_CALL_PART_CLOBBERED
322 (new_reg, GET_MODE (*tmp->loc)))))
323 break;
324 if (! tmp)
326 if (tick[best_new_reg] > tick[new_reg])
327 best_new_reg = new_reg;
331 if (dump_file)
333 fprintf (dump_file, "Register %s in insn %d",
334 reg_names[reg], INSN_UID (last->insn));
335 if (last->need_caller_save_reg)
336 fprintf (dump_file, " crosses a call");
339 if (best_new_reg == reg)
341 tick[reg] = ++this_tick;
342 if (dump_file)
343 fprintf (dump_file, "; no available better choice\n");
344 continue;
347 do_replace (this, best_new_reg);
348 tick[best_new_reg] = ++this_tick;
349 regs_ever_live[best_new_reg] = 1;
351 if (dump_file)
352 fprintf (dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
355 obstack_free (&rename_obstack, first_obj);
358 obstack_free (&rename_obstack, NULL);
360 if (dump_file)
361 fputc ('\n', dump_file);
363 count_or_remove_death_notes (NULL, 1);
364 update_life_info (NULL, UPDATE_LIFE_LOCAL,
365 PROP_DEATH_NOTES);
368 static void
369 do_replace (struct du_chain *chain, int reg)
371 while (chain)
373 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
374 struct reg_attrs * attr = REG_ATTRS (*chain->loc);
376 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
377 if (regno >= FIRST_PSEUDO_REGISTER)
378 ORIGINAL_REGNO (*chain->loc) = regno;
379 REG_ATTRS (*chain->loc) = attr;
380 chain = chain->next_use;
385 static struct du_chain *open_chains;
386 static struct du_chain *closed_chains;
388 static void
389 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class cl,
390 enum scan_actions action, enum op_type type, int earlyclobber)
392 struct du_chain **p;
393 rtx x = *loc;
394 enum machine_mode mode = GET_MODE (x);
395 int this_regno = REGNO (x);
396 int this_nregs = hard_regno_nregs[this_regno][mode];
398 if (action == mark_write)
400 if (type == OP_OUT)
402 struct du_chain *this
403 = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
404 this->next_use = 0;
405 this->next_chain = open_chains;
406 this->loc = loc;
407 this->insn = insn;
408 this->cl = cl;
409 this->need_caller_save_reg = 0;
410 this->earlyclobber = earlyclobber;
411 open_chains = this;
413 return;
416 if ((type == OP_OUT) != (action == terminate_write || action == mark_access))
417 return;
419 for (p = &open_chains; *p;)
421 struct du_chain *this = *p;
423 /* Check if the chain has been terminated if it has then skip to
424 the next chain.
426 This can happen when we've already appended the location to
427 the chain in Step 3, but are trying to hide in-out operands
428 from terminate_write in Step 5. */
430 if (*this->loc == cc0_rtx)
431 p = &this->next_chain;
432 else
434 int regno = REGNO (*this->loc);
435 int nregs = hard_regno_nregs[regno][GET_MODE (*this->loc)];
436 int exact_match = (regno == this_regno && nregs == this_nregs);
438 if (regno + nregs <= this_regno
439 || this_regno + this_nregs <= regno)
441 p = &this->next_chain;
442 continue;
445 if (action == mark_read || action == mark_access)
447 gcc_assert (exact_match);
449 /* ??? Class NO_REGS can happen if the md file makes use of
450 EXTRA_CONSTRAINTS to match registers. Which is arguably
451 wrong, but there we are. Since we know not what this may
452 be replaced with, terminate the chain. */
453 if (cl != NO_REGS)
455 this = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
456 this->next_use = 0;
457 this->next_chain = (*p)->next_chain;
458 this->loc = loc;
459 this->insn = insn;
460 this->cl = cl;
461 this->need_caller_save_reg = 0;
462 while (*p)
463 p = &(*p)->next_use;
464 *p = this;
465 return;
469 if (action != terminate_overlapping_read || ! exact_match)
471 struct du_chain *next = this->next_chain;
473 /* Whether the terminated chain can be used for renaming
474 depends on the action and this being an exact match.
475 In either case, we remove this element from open_chains. */
477 if ((action == terminate_dead || action == terminate_write)
478 && exact_match)
480 this->next_chain = closed_chains;
481 closed_chains = this;
482 if (dump_file)
483 fprintf (dump_file,
484 "Closing chain %s at insn %d (%s)\n",
485 reg_names[REGNO (*this->loc)], INSN_UID (insn),
486 scan_actions_name[(int) action]);
488 else
490 if (dump_file)
491 fprintf (dump_file,
492 "Discarding chain %s at insn %d (%s)\n",
493 reg_names[REGNO (*this->loc)], INSN_UID (insn),
494 scan_actions_name[(int) action]);
496 *p = next;
498 else
499 p = &this->next_chain;
504 /* Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
505 BASE_REG_CLASS depending on how the register is being considered. */
507 static void
508 scan_rtx_address (rtx insn, rtx *loc, enum reg_class cl,
509 enum scan_actions action, enum machine_mode mode)
511 rtx x = *loc;
512 RTX_CODE code = GET_CODE (x);
513 const char *fmt;
514 int i, j;
516 if (action == mark_write || action == mark_access)
517 return;
519 switch (code)
521 case PLUS:
523 rtx orig_op0 = XEXP (x, 0);
524 rtx orig_op1 = XEXP (x, 1);
525 RTX_CODE code0 = GET_CODE (orig_op0);
526 RTX_CODE code1 = GET_CODE (orig_op1);
527 rtx op0 = orig_op0;
528 rtx op1 = orig_op1;
529 rtx *locI = NULL;
530 rtx *locB = NULL;
531 rtx *locB_reg = NULL;
533 if (GET_CODE (op0) == SUBREG)
535 op0 = SUBREG_REG (op0);
536 code0 = GET_CODE (op0);
539 if (GET_CODE (op1) == SUBREG)
541 op1 = SUBREG_REG (op1);
542 code1 = GET_CODE (op1);
545 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
546 || code0 == ZERO_EXTEND || code1 == MEM)
548 locI = &XEXP (x, 0);
549 locB = &XEXP (x, 1);
551 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
552 || code1 == ZERO_EXTEND || code0 == MEM)
554 locI = &XEXP (x, 1);
555 locB = &XEXP (x, 0);
557 else if (code0 == CONST_INT || code0 == CONST
558 || code0 == SYMBOL_REF || code0 == LABEL_REF)
559 locB = &XEXP (x, 1);
560 else if (code1 == CONST_INT || code1 == CONST
561 || code1 == SYMBOL_REF || code1 == LABEL_REF)
562 locB = &XEXP (x, 0);
563 else if (code0 == REG && code1 == REG)
565 int index_op;
567 if (REG_OK_FOR_INDEX_P (op0)
568 && REG_MODE_OK_FOR_REG_BASE_P (op1, mode))
569 index_op = 0;
570 else if (REG_OK_FOR_INDEX_P (op1)
571 && REG_MODE_OK_FOR_REG_BASE_P (op0, mode))
572 index_op = 1;
573 else if (REG_MODE_OK_FOR_REG_BASE_P (op1, mode))
574 index_op = 0;
575 else if (REG_MODE_OK_FOR_REG_BASE_P (op0, mode))
576 index_op = 1;
577 else if (REG_OK_FOR_INDEX_P (op1))
578 index_op = 1;
579 else
580 index_op = 0;
582 locI = &XEXP (x, index_op);
583 locB_reg = &XEXP (x, !index_op);
585 else if (code0 == REG)
587 locI = &XEXP (x, 0);
588 locB = &XEXP (x, 1);
590 else if (code1 == REG)
592 locI = &XEXP (x, 1);
593 locB = &XEXP (x, 0);
596 if (locI)
597 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
598 if (locB)
599 scan_rtx_address (insn, locB, MODE_BASE_REG_CLASS (mode), action, mode);
600 if (locB_reg)
601 scan_rtx_address (insn, locB_reg, MODE_BASE_REG_REG_CLASS (mode),
602 action, mode);
603 return;
606 case POST_INC:
607 case POST_DEC:
608 case POST_MODIFY:
609 case PRE_INC:
610 case PRE_DEC:
611 case PRE_MODIFY:
612 #ifndef AUTO_INC_DEC
613 /* If the target doesn't claim to handle autoinc, this must be
614 something special, like a stack push. Kill this chain. */
615 action = terminate_all_read;
616 #endif
617 break;
619 case MEM:
620 scan_rtx_address (insn, &XEXP (x, 0),
621 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
622 GET_MODE (x));
623 return;
625 case REG:
626 scan_rtx_reg (insn, loc, cl, action, OP_IN, 0);
627 return;
629 default:
630 break;
633 fmt = GET_RTX_FORMAT (code);
634 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
636 if (fmt[i] == 'e')
637 scan_rtx_address (insn, &XEXP (x, i), cl, action, mode);
638 else if (fmt[i] == 'E')
639 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
640 scan_rtx_address (insn, &XVECEXP (x, i, j), cl, action, mode);
644 static void
645 scan_rtx (rtx insn, rtx *loc, enum reg_class cl,
646 enum scan_actions action, enum op_type type, int earlyclobber)
648 const char *fmt;
649 rtx x = *loc;
650 enum rtx_code code = GET_CODE (x);
651 int i, j;
653 code = GET_CODE (x);
654 switch (code)
656 case CONST:
657 case CONST_INT:
658 case CONST_DOUBLE:
659 case CONST_VECTOR:
660 case SYMBOL_REF:
661 case LABEL_REF:
662 case CC0:
663 case PC:
664 return;
666 case REG:
667 scan_rtx_reg (insn, loc, cl, action, type, earlyclobber);
668 return;
670 case MEM:
671 scan_rtx_address (insn, &XEXP (x, 0),
672 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
673 GET_MODE (x));
674 return;
676 case SET:
677 scan_rtx (insn, &SET_SRC (x), cl, action, OP_IN, 0);
678 scan_rtx (insn, &SET_DEST (x), cl, action,
679 GET_CODE (PATTERN (insn)) == COND_EXEC ? OP_INOUT : OP_OUT, 0);
680 return;
682 case STRICT_LOW_PART:
683 scan_rtx (insn, &XEXP (x, 0), cl, action, OP_INOUT, earlyclobber);
684 return;
686 case ZERO_EXTRACT:
687 case SIGN_EXTRACT:
688 scan_rtx (insn, &XEXP (x, 0), cl, action,
689 type == OP_IN ? OP_IN : OP_INOUT, earlyclobber);
690 scan_rtx (insn, &XEXP (x, 1), cl, action, OP_IN, 0);
691 scan_rtx (insn, &XEXP (x, 2), cl, action, OP_IN, 0);
692 return;
694 case POST_INC:
695 case PRE_INC:
696 case POST_DEC:
697 case PRE_DEC:
698 case POST_MODIFY:
699 case PRE_MODIFY:
700 /* Should only happen inside MEM. */
701 gcc_unreachable ();
703 case CLOBBER:
704 scan_rtx (insn, &SET_DEST (x), cl, action,
705 GET_CODE (PATTERN (insn)) == COND_EXEC ? OP_INOUT : OP_OUT, 0);
706 return;
708 case EXPR_LIST:
709 scan_rtx (insn, &XEXP (x, 0), cl, action, type, 0);
710 if (XEXP (x, 1))
711 scan_rtx (insn, &XEXP (x, 1), cl, action, type, 0);
712 return;
714 default:
715 break;
718 fmt = GET_RTX_FORMAT (code);
719 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
721 if (fmt[i] == 'e')
722 scan_rtx (insn, &XEXP (x, i), cl, action, type, 0);
723 else if (fmt[i] == 'E')
724 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
725 scan_rtx (insn, &XVECEXP (x, i, j), cl, action, type, 0);
729 /* Build def/use chain. */
731 static struct du_chain *
732 build_def_use (basic_block bb)
734 rtx insn;
736 open_chains = closed_chains = NULL;
738 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
740 if (INSN_P (insn))
742 int n_ops;
743 rtx note;
744 rtx old_operands[MAX_RECOG_OPERANDS];
745 rtx old_dups[MAX_DUP_OPERANDS];
746 int i, icode;
747 int alt;
748 int predicated;
750 /* Process the insn, determining its effect on the def-use
751 chains. We perform the following steps with the register
752 references in the insn:
753 (1) Any read that overlaps an open chain, but doesn't exactly
754 match, causes that chain to be closed. We can't deal
755 with overlaps yet.
756 (2) Any read outside an operand causes any chain it overlaps
757 with to be closed, since we can't replace it.
758 (3) Any read inside an operand is added if there's already
759 an open chain for it.
760 (4) For any REG_DEAD note we find, close open chains that
761 overlap it.
762 (5) For any write we find, close open chains that overlap it.
763 (6) For any write we find in an operand, make a new chain.
764 (7) For any REG_UNUSED, close any chains we just opened. */
766 icode = recog_memoized (insn);
767 extract_insn (insn);
768 if (! constrain_operands (1))
769 fatal_insn_not_found (insn);
770 preprocess_constraints ();
771 alt = which_alternative;
772 n_ops = recog_data.n_operands;
774 /* Simplify the code below by rewriting things to reflect
775 matching constraints. Also promote OP_OUT to OP_INOUT
776 in predicated instructions. */
778 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
779 for (i = 0; i < n_ops; ++i)
781 int matches = recog_op_alt[i][alt].matches;
782 if (matches >= 0)
783 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
784 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
785 || (predicated && recog_data.operand_type[i] == OP_OUT))
786 recog_data.operand_type[i] = OP_INOUT;
789 /* Step 1: Close chains for which we have overlapping reads. */
790 for (i = 0; i < n_ops; i++)
791 scan_rtx (insn, recog_data.operand_loc[i],
792 NO_REGS, terminate_overlapping_read,
793 recog_data.operand_type[i], 0);
795 /* Step 2: Close chains for which we have reads outside operands.
796 We do this by munging all operands into CC0, and closing
797 everything remaining. */
799 for (i = 0; i < n_ops; i++)
801 old_operands[i] = recog_data.operand[i];
802 /* Don't squash match_operator or match_parallel here, since
803 we don't know that all of the contained registers are
804 reachable by proper operands. */
805 if (recog_data.constraints[i][0] == '\0')
806 continue;
807 *recog_data.operand_loc[i] = cc0_rtx;
809 for (i = 0; i < recog_data.n_dups; i++)
811 int dup_num = recog_data.dup_num[i];
813 old_dups[i] = *recog_data.dup_loc[i];
814 *recog_data.dup_loc[i] = cc0_rtx;
816 /* For match_dup of match_operator or match_parallel, share
817 them, so that we don't miss changes in the dup. */
818 if (icode >= 0
819 && insn_data[icode].operand[dup_num].eliminable == 0)
820 old_dups[i] = recog_data.operand[dup_num];
823 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
824 OP_IN, 0);
826 for (i = 0; i < recog_data.n_dups; i++)
827 *recog_data.dup_loc[i] = old_dups[i];
828 for (i = 0; i < n_ops; i++)
829 *recog_data.operand_loc[i] = old_operands[i];
831 /* Step 2B: Can't rename function call argument registers. */
832 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
833 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
834 NO_REGS, terminate_all_read, OP_IN, 0);
836 /* Step 2C: Can't rename asm operands that were originally
837 hard registers. */
838 if (asm_noperands (PATTERN (insn)) > 0)
839 for (i = 0; i < n_ops; i++)
841 rtx *loc = recog_data.operand_loc[i];
842 rtx op = *loc;
844 if (REG_P (op)
845 && REGNO (op) == ORIGINAL_REGNO (op)
846 && (recog_data.operand_type[i] == OP_IN
847 || recog_data.operand_type[i] == OP_INOUT))
848 scan_rtx (insn, loc, NO_REGS, terminate_all_read, OP_IN, 0);
851 /* Step 3: Append to chains for reads inside operands. */
852 for (i = 0; i < n_ops + recog_data.n_dups; i++)
854 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
855 rtx *loc = (i < n_ops
856 ? recog_data.operand_loc[opn]
857 : recog_data.dup_loc[i - n_ops]);
858 enum reg_class cl = recog_op_alt[opn][alt].cl;
859 enum op_type type = recog_data.operand_type[opn];
861 /* Don't scan match_operand here, since we've no reg class
862 information to pass down. Any operands that we could
863 substitute in will be represented elsewhere. */
864 if (recog_data.constraints[opn][0] == '\0')
865 continue;
867 if (recog_op_alt[opn][alt].is_address)
868 scan_rtx_address (insn, loc, cl, mark_read, VOIDmode);
869 else
870 scan_rtx (insn, loc, cl, mark_read, type, 0);
873 /* Step 3B: Record updates for regs in REG_INC notes, and
874 source regs in REG_FRAME_RELATED_EXPR notes. */
875 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
876 if (REG_NOTE_KIND (note) == REG_INC
877 || REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
878 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
879 OP_INOUT, 0);
881 /* Step 4: Close chains for registers that die here. */
882 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
883 if (REG_NOTE_KIND (note) == REG_DEAD)
884 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
885 OP_IN, 0);
887 /* Step 4B: If this is a call, any chain live at this point
888 requires a caller-saved reg. */
889 if (CALL_P (insn))
891 struct du_chain *p;
892 for (p = open_chains; p; p = p->next_chain)
893 p->need_caller_save_reg = 1;
896 /* Step 5: Close open chains that overlap writes. Similar to
897 step 2, we hide in-out operands, since we do not want to
898 close these chains. */
900 for (i = 0; i < n_ops; i++)
902 old_operands[i] = recog_data.operand[i];
903 if (recog_data.operand_type[i] == OP_INOUT)
904 *recog_data.operand_loc[i] = cc0_rtx;
906 for (i = 0; i < recog_data.n_dups; i++)
908 int opn = recog_data.dup_num[i];
909 old_dups[i] = *recog_data.dup_loc[i];
910 if (recog_data.operand_type[opn] == OP_INOUT)
911 *recog_data.dup_loc[i] = cc0_rtx;
914 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
916 for (i = 0; i < recog_data.n_dups; i++)
917 *recog_data.dup_loc[i] = old_dups[i];
918 for (i = 0; i < n_ops; i++)
919 *recog_data.operand_loc[i] = old_operands[i];
921 /* Step 6: Begin new chains for writes inside operands. */
922 /* ??? Many targets have output constraints on the SET_DEST
923 of a call insn, which is stupid, since these are certainly
924 ABI defined hard registers. Don't change calls at all.
925 Similarly take special care for asm statement that originally
926 referenced hard registers. */
927 if (asm_noperands (PATTERN (insn)) > 0)
929 for (i = 0; i < n_ops; i++)
930 if (recog_data.operand_type[i] == OP_OUT)
932 rtx *loc = recog_data.operand_loc[i];
933 rtx op = *loc;
934 enum reg_class cl = recog_op_alt[i][alt].cl;
936 if (REG_P (op)
937 && REGNO (op) == ORIGINAL_REGNO (op))
938 continue;
940 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
941 recog_op_alt[i][alt].earlyclobber);
944 else if (!CALL_P (insn))
945 for (i = 0; i < n_ops + recog_data.n_dups; i++)
947 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
948 rtx *loc = (i < n_ops
949 ? recog_data.operand_loc[opn]
950 : recog_data.dup_loc[i - n_ops]);
951 enum reg_class cl = recog_op_alt[opn][alt].cl;
953 if (recog_data.operand_type[opn] == OP_OUT)
954 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
955 recog_op_alt[opn][alt].earlyclobber);
958 /* Step 6B: Record destination regs in REG_FRAME_RELATED_EXPR
959 notes for update. */
960 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
961 if (REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
962 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_access,
963 OP_INOUT, 0);
965 /* Step 7: Close chains for registers that were never
966 really used here. */
967 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
968 if (REG_NOTE_KIND (note) == REG_UNUSED)
969 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
970 OP_IN, 0);
972 if (insn == BB_END (bb))
973 break;
976 /* Since we close every chain when we find a REG_DEAD note, anything that
977 is still open lives past the basic block, so it can't be renamed. */
978 return closed_chains;
981 /* Dump all def/use chains in CHAINS to DUMP_FILE. They are
982 printed in reverse order as that's how we build them. */
984 static void
985 dump_def_use_chain (struct du_chain *chains)
987 while (chains)
989 struct du_chain *this = chains;
990 int r = REGNO (*this->loc);
991 int nregs = hard_regno_nregs[r][GET_MODE (*this->loc)];
992 fprintf (dump_file, "Register %s (%d):", reg_names[r], nregs);
993 while (this)
995 fprintf (dump_file, " %d [%s]", INSN_UID (this->insn),
996 reg_class_names[this->cl]);
997 this = this->next_use;
999 fprintf (dump_file, "\n");
1000 chains = chains->next_chain;
1004 /* The following code does forward propagation of hard register copies.
1005 The object is to eliminate as many dependencies as possible, so that
1006 we have the most scheduling freedom. As a side effect, we also clean
1007 up some silly register allocation decisions made by reload. This
1008 code may be obsoleted by a new register allocator. */
1010 /* For each register, we have a list of registers that contain the same
1011 value. The OLDEST_REGNO field points to the head of the list, and
1012 the NEXT_REGNO field runs through the list. The MODE field indicates
1013 what mode the data is known to be in; this field is VOIDmode when the
1014 register is not known to contain valid data. */
1016 struct value_data_entry
1018 enum machine_mode mode;
1019 unsigned int oldest_regno;
1020 unsigned int next_regno;
1023 struct value_data
1025 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
1026 unsigned int max_value_regs;
1029 static void kill_value_one_regno (unsigned, struct value_data *);
1030 static void kill_value_regno (unsigned, unsigned, struct value_data *);
1031 static void kill_value (rtx, struct value_data *);
1032 static void set_value_regno (unsigned, enum machine_mode, struct value_data *);
1033 static void init_value_data (struct value_data *);
1034 static void kill_clobbered_value (rtx, rtx, void *);
1035 static void kill_set_value (rtx, rtx, void *);
1036 static int kill_autoinc_value (rtx *, void *);
1037 static void copy_value (rtx, rtx, struct value_data *);
1038 static bool mode_change_ok (enum machine_mode, enum machine_mode,
1039 unsigned int);
1040 static rtx maybe_mode_change (enum machine_mode, enum machine_mode,
1041 enum machine_mode, unsigned int, unsigned int);
1042 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
1043 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx,
1044 struct value_data *);
1045 static bool replace_oldest_value_addr (rtx *, enum reg_class,
1046 enum machine_mode, rtx,
1047 struct value_data *);
1048 static bool replace_oldest_value_mem (rtx, rtx, struct value_data *);
1049 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
1050 extern void debug_value_data (struct value_data *);
1051 #ifdef ENABLE_CHECKING
1052 static void validate_value_data (struct value_data *);
1053 #endif
1055 /* Kill register REGNO. This involves removing it from any value
1056 lists, and resetting the value mode to VOIDmode. This is only a
1057 helper function; it does not handle any hard registers overlapping
1058 with REGNO. */
1060 static void
1061 kill_value_one_regno (unsigned int regno, struct value_data *vd)
1063 unsigned int i, next;
1065 if (vd->e[regno].oldest_regno != regno)
1067 for (i = vd->e[regno].oldest_regno;
1068 vd->e[i].next_regno != regno;
1069 i = vd->e[i].next_regno)
1070 continue;
1071 vd->e[i].next_regno = vd->e[regno].next_regno;
1073 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
1075 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
1076 vd->e[i].oldest_regno = next;
1079 vd->e[regno].mode = VOIDmode;
1080 vd->e[regno].oldest_regno = regno;
1081 vd->e[regno].next_regno = INVALID_REGNUM;
1083 #ifdef ENABLE_CHECKING
1084 validate_value_data (vd);
1085 #endif
1088 /* Kill the value in register REGNO for NREGS, and any other registers
1089 whose values overlap. */
1091 static void
1092 kill_value_regno (unsigned int regno, unsigned int nregs,
1093 struct value_data *vd)
1095 unsigned int j;
1097 /* Kill the value we're told to kill. */
1098 for (j = 0; j < nregs; ++j)
1099 kill_value_one_regno (regno + j, vd);
1101 /* Kill everything that overlapped what we're told to kill. */
1102 if (regno < vd->max_value_regs)
1103 j = 0;
1104 else
1105 j = regno - vd->max_value_regs;
1106 for (; j < regno; ++j)
1108 unsigned int i, n;
1109 if (vd->e[j].mode == VOIDmode)
1110 continue;
1111 n = hard_regno_nregs[j][vd->e[j].mode];
1112 if (j + n > regno)
1113 for (i = 0; i < n; ++i)
1114 kill_value_one_regno (j + i, vd);
1118 /* Kill X. This is a convenience function wrapping kill_value_regno
1119 so that we mind the mode the register is in. */
1121 static void
1122 kill_value (rtx x, struct value_data *vd)
1124 rtx orig_rtx = x;
1126 if (GET_CODE (x) == SUBREG)
1128 x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
1129 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
1130 if (x == NULL_RTX)
1131 x = SUBREG_REG (orig_rtx);
1133 if (REG_P (x))
1135 unsigned int regno = REGNO (x);
1136 unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
1138 kill_value_regno (regno, n, vd);
1142 /* Remember that REGNO is valid in MODE. */
1144 static void
1145 set_value_regno (unsigned int regno, enum machine_mode mode,
1146 struct value_data *vd)
1148 unsigned int nregs;
1150 vd->e[regno].mode = mode;
1152 nregs = hard_regno_nregs[regno][mode];
1153 if (nregs > vd->max_value_regs)
1154 vd->max_value_regs = nregs;
1157 /* Initialize VD such that there are no known relationships between regs. */
1159 static void
1160 init_value_data (struct value_data *vd)
1162 int i;
1163 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1165 vd->e[i].mode = VOIDmode;
1166 vd->e[i].oldest_regno = i;
1167 vd->e[i].next_regno = INVALID_REGNUM;
1169 vd->max_value_regs = 0;
1172 /* Called through note_stores. If X is clobbered, kill its value. */
1174 static void
1175 kill_clobbered_value (rtx x, rtx set, void *data)
1177 struct value_data *vd = data;
1178 if (GET_CODE (set) == CLOBBER)
1179 kill_value (x, vd);
1182 /* Called through note_stores. If X is set, not clobbered, kill its
1183 current value and install it as the root of its own value list. */
1185 static void
1186 kill_set_value (rtx x, rtx set, void *data)
1188 struct value_data *vd = data;
1189 if (GET_CODE (set) != CLOBBER)
1191 kill_value (x, vd);
1192 if (REG_P (x))
1193 set_value_regno (REGNO (x), GET_MODE (x), vd);
1197 /* Called through for_each_rtx. Kill any register used as the base of an
1198 auto-increment expression, and install that register as the root of its
1199 own value list. */
1201 static int
1202 kill_autoinc_value (rtx *px, void *data)
1204 rtx x = *px;
1205 struct value_data *vd = data;
1207 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
1209 x = XEXP (x, 0);
1210 kill_value (x, vd);
1211 set_value_regno (REGNO (x), Pmode, vd);
1212 return -1;
1215 return 0;
1218 /* Assert that SRC has been copied to DEST. Adjust the data structures
1219 to reflect that SRC contains an older copy of the shared value. */
1221 static void
1222 copy_value (rtx dest, rtx src, struct value_data *vd)
1224 unsigned int dr = REGNO (dest);
1225 unsigned int sr = REGNO (src);
1226 unsigned int dn, sn;
1227 unsigned int i;
1229 /* ??? At present, it's possible to see noop sets. It'd be nice if
1230 this were cleaned up beforehand... */
1231 if (sr == dr)
1232 return;
1234 /* Do not propagate copies to the stack pointer, as that can leave
1235 memory accesses with no scheduling dependency on the stack update. */
1236 if (dr == STACK_POINTER_REGNUM)
1237 return;
1239 /* Likewise with the frame pointer, if we're using one. */
1240 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
1241 return;
1243 /* Do not propagate copies to fixed or global registers, patterns
1244 can be relying to see particular fixed register or users can
1245 expect the chosen global register in asm. */
1246 if (fixed_regs[dr] || global_regs[dr])
1247 return;
1249 /* If SRC and DEST overlap, don't record anything. */
1250 dn = hard_regno_nregs[dr][GET_MODE (dest)];
1251 sn = hard_regno_nregs[sr][GET_MODE (dest)];
1252 if ((dr > sr && dr < sr + sn)
1253 || (sr > dr && sr < dr + dn))
1254 return;
1256 /* If SRC had no assigned mode (i.e. we didn't know it was live)
1257 assign it now and assume the value came from an input argument
1258 or somesuch. */
1259 if (vd->e[sr].mode == VOIDmode)
1260 set_value_regno (sr, vd->e[dr].mode, vd);
1262 /* If we are narrowing the input to a smaller number of hard regs,
1263 and it is in big endian, we are really extracting a high part.
1264 Since we generally associate a low part of a value with the value itself,
1265 we must not do the same for the high part.
1266 Note we can still get low parts for the same mode combination through
1267 a two-step copy involving differently sized hard regs.
1268 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
1269 (set (reg:DI r0) (reg:DI fr0))
1270 (set (reg:SI fr2) (reg:SI r0))
1271 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
1272 (set (reg:SI fr2) (reg:SI fr0))
1273 loads the high part of (reg:DI fr0) into fr2.
1275 We can't properly represent the latter case in our tables, so don't
1276 record anything then. */
1277 else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
1278 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
1279 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
1280 return;
1282 /* If SRC had been assigned a mode narrower than the copy, we can't
1283 link DEST into the chain, because not all of the pieces of the
1284 copy came from oldest_regno. */
1285 else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
1286 return;
1288 /* Link DR at the end of the value chain used by SR. */
1290 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
1292 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
1293 continue;
1294 vd->e[i].next_regno = dr;
1296 #ifdef ENABLE_CHECKING
1297 validate_value_data (vd);
1298 #endif
1301 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
1303 static bool
1304 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
1305 unsigned int regno ATTRIBUTE_UNUSED)
1307 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
1308 return false;
1310 #ifdef CANNOT_CHANGE_MODE_CLASS
1311 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
1312 #endif
1314 return true;
1317 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
1318 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
1319 in NEW_MODE.
1320 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
1322 static rtx
1323 maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
1324 enum machine_mode new_mode, unsigned int regno,
1325 unsigned int copy_regno ATTRIBUTE_UNUSED)
1327 if (orig_mode == new_mode)
1328 return gen_rtx_raw_REG (new_mode, regno);
1329 else if (mode_change_ok (orig_mode, new_mode, regno))
1331 int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
1332 int use_nregs = hard_regno_nregs[copy_regno][new_mode];
1333 int copy_offset
1334 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
1335 int offset
1336 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
1337 int byteoffset = offset % UNITS_PER_WORD;
1338 int wordoffset = offset - byteoffset;
1340 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
1341 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
1342 return gen_rtx_raw_REG (new_mode,
1343 regno + subreg_regno_offset (regno, orig_mode,
1344 offset,
1345 new_mode));
1347 return NULL_RTX;
1350 /* Find the oldest copy of the value contained in REGNO that is in
1351 register class CL and has mode MODE. If found, return an rtx
1352 of that oldest register, otherwise return NULL. */
1354 static rtx
1355 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
1357 unsigned int regno = REGNO (reg);
1358 enum machine_mode mode = GET_MODE (reg);
1359 unsigned int i;
1361 /* If we are accessing REG in some mode other that what we set it in,
1362 make sure that the replacement is valid. In particular, consider
1363 (set (reg:DI r11) (...))
1364 (set (reg:SI r9) (reg:SI r11))
1365 (set (reg:SI r10) (...))
1366 (set (...) (reg:DI r9))
1367 Replacing r9 with r11 is invalid. */
1368 if (mode != vd->e[regno].mode)
1370 if (hard_regno_nregs[regno][mode]
1371 > hard_regno_nregs[regno][vd->e[regno].mode])
1372 return NULL_RTX;
1375 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
1377 enum machine_mode oldmode = vd->e[i].mode;
1378 rtx new;
1379 unsigned int last;
1381 for (last = i; last < i + hard_regno_nregs[i][mode]; last++)
1382 if (!TEST_HARD_REG_BIT (reg_class_contents[cl], last))
1383 return NULL_RTX;
1385 new = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
1386 if (new)
1388 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (reg);
1389 REG_ATTRS (new) = REG_ATTRS (reg);
1390 return new;
1394 return NULL_RTX;
1397 /* If possible, replace the register at *LOC with the oldest register
1398 in register class CL. Return true if successfully replaced. */
1400 static bool
1401 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx insn,
1402 struct value_data *vd)
1404 rtx new = find_oldest_value_reg (cl, *loc, vd);
1405 if (new)
1407 if (dump_file)
1408 fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
1409 INSN_UID (insn), REGNO (*loc), REGNO (new));
1411 *loc = new;
1412 return true;
1414 return false;
1417 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1418 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
1419 BASE_REG_CLASS depending on how the register is being considered. */
1421 static bool
1422 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
1423 enum machine_mode mode, rtx insn,
1424 struct value_data *vd)
1426 rtx x = *loc;
1427 RTX_CODE code = GET_CODE (x);
1428 const char *fmt;
1429 int i, j;
1430 bool changed = false;
1432 switch (code)
1434 case PLUS:
1436 rtx orig_op0 = XEXP (x, 0);
1437 rtx orig_op1 = XEXP (x, 1);
1438 RTX_CODE code0 = GET_CODE (orig_op0);
1439 RTX_CODE code1 = GET_CODE (orig_op1);
1440 rtx op0 = orig_op0;
1441 rtx op1 = orig_op1;
1442 rtx *locI = NULL;
1443 rtx *locB = NULL;
1444 rtx *locB_reg = NULL;
1446 if (GET_CODE (op0) == SUBREG)
1448 op0 = SUBREG_REG (op0);
1449 code0 = GET_CODE (op0);
1452 if (GET_CODE (op1) == SUBREG)
1454 op1 = SUBREG_REG (op1);
1455 code1 = GET_CODE (op1);
1458 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1459 || code0 == ZERO_EXTEND || code1 == MEM)
1461 locI = &XEXP (x, 0);
1462 locB = &XEXP (x, 1);
1464 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1465 || code1 == ZERO_EXTEND || code0 == MEM)
1467 locI = &XEXP (x, 1);
1468 locB = &XEXP (x, 0);
1470 else if (code0 == CONST_INT || code0 == CONST
1471 || code0 == SYMBOL_REF || code0 == LABEL_REF)
1472 locB = &XEXP (x, 1);
1473 else if (code1 == CONST_INT || code1 == CONST
1474 || code1 == SYMBOL_REF || code1 == LABEL_REF)
1475 locB = &XEXP (x, 0);
1476 else if (code0 == REG && code1 == REG)
1478 int index_op;
1480 if (REG_OK_FOR_INDEX_P (op0)
1481 && REG_MODE_OK_FOR_REG_BASE_P (op1, mode))
1482 index_op = 0;
1483 else if (REG_OK_FOR_INDEX_P (op1)
1484 && REG_MODE_OK_FOR_REG_BASE_P (op0, mode))
1485 index_op = 1;
1486 else if (REG_MODE_OK_FOR_REG_BASE_P (op1, mode))
1487 index_op = 0;
1488 else if (REG_MODE_OK_FOR_REG_BASE_P (op0, mode))
1489 index_op = 1;
1490 else if (REG_OK_FOR_INDEX_P (op1))
1491 index_op = 1;
1492 else
1493 index_op = 0;
1495 locI = &XEXP (x, index_op);
1496 locB_reg = &XEXP (x, !index_op);
1498 else if (code0 == REG)
1500 locI = &XEXP (x, 0);
1501 locB = &XEXP (x, 1);
1503 else if (code1 == REG)
1505 locI = &XEXP (x, 1);
1506 locB = &XEXP (x, 0);
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 MODE_BASE_REG_CLASS (mode),
1515 mode, insn, vd);
1516 if (locB_reg)
1517 changed |= replace_oldest_value_addr (locB_reg,
1518 MODE_BASE_REG_REG_CLASS (mode),
1519 mode, insn, vd);
1520 return changed;
1523 case POST_INC:
1524 case POST_DEC:
1525 case POST_MODIFY:
1526 case PRE_INC:
1527 case PRE_DEC:
1528 case PRE_MODIFY:
1529 return false;
1531 case MEM:
1532 return replace_oldest_value_mem (x, insn, vd);
1534 case REG:
1535 return replace_oldest_value_reg (loc, cl, insn, vd);
1537 default:
1538 break;
1541 fmt = GET_RTX_FORMAT (code);
1542 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1544 if (fmt[i] == 'e')
1545 changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode,
1546 insn, vd);
1547 else if (fmt[i] == 'E')
1548 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1549 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
1550 mode, insn, vd);
1553 return changed;
1556 /* Similar to replace_oldest_value_reg, but X contains a memory. */
1558 static bool
1559 replace_oldest_value_mem (rtx x, rtx insn, struct value_data *vd)
1561 return replace_oldest_value_addr (&XEXP (x, 0),
1562 MODE_BASE_REG_CLASS (GET_MODE (x)),
1563 GET_MODE (x), insn, vd);
1566 /* Perform the forward copy propagation on basic block BB. */
1568 static bool
1569 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
1571 bool changed = false;
1572 rtx insn;
1574 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1576 int n_ops, i, alt, predicated;
1577 bool is_asm;
1578 rtx set;
1580 if (! INSN_P (insn))
1582 if (insn == BB_END (bb))
1583 break;
1584 else
1585 continue;
1588 set = single_set (insn);
1589 extract_insn (insn);
1590 if (! constrain_operands (1))
1591 fatal_insn_not_found (insn);
1592 preprocess_constraints ();
1593 alt = which_alternative;
1594 n_ops = recog_data.n_operands;
1595 is_asm = asm_noperands (PATTERN (insn)) >= 0;
1597 /* Simplify the code below by rewriting things to reflect
1598 matching constraints. Also promote OP_OUT to OP_INOUT
1599 in predicated instructions. */
1601 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1602 for (i = 0; i < n_ops; ++i)
1604 int matches = recog_op_alt[i][alt].matches;
1605 if (matches >= 0)
1606 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
1607 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1608 || (predicated && recog_data.operand_type[i] == OP_OUT))
1609 recog_data.operand_type[i] = OP_INOUT;
1612 /* For each earlyclobber operand, zap the value data. */
1613 for (i = 0; i < n_ops; i++)
1614 if (recog_op_alt[i][alt].earlyclobber)
1615 kill_value (recog_data.operand[i], vd);
1617 /* Within asms, a clobber cannot overlap inputs or outputs.
1618 I wouldn't think this were true for regular insns, but
1619 scan_rtx treats them like that... */
1620 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1622 /* Kill all auto-incremented values. */
1623 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
1624 for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
1626 /* Kill all early-clobbered operands. */
1627 for (i = 0; i < n_ops; i++)
1628 if (recog_op_alt[i][alt].earlyclobber)
1629 kill_value (recog_data.operand[i], vd);
1631 /* Special-case plain move instructions, since we may well
1632 be able to do the move from a different register class. */
1633 if (set && REG_P (SET_SRC (set)))
1635 rtx src = SET_SRC (set);
1636 unsigned int regno = REGNO (src);
1637 enum machine_mode mode = GET_MODE (src);
1638 unsigned int i;
1639 rtx new;
1641 /* If we are accessing SRC in some mode other that what we
1642 set it in, make sure that the replacement is valid. */
1643 if (mode != vd->e[regno].mode)
1645 if (hard_regno_nregs[regno][mode]
1646 > hard_regno_nregs[regno][vd->e[regno].mode])
1647 goto no_move_special_case;
1650 /* If the destination is also a register, try to find a source
1651 register in the same class. */
1652 if (REG_P (SET_DEST (set)))
1654 new = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
1655 if (new && validate_change (insn, &SET_SRC (set), new, 0))
1657 if (dump_file)
1658 fprintf (dump_file,
1659 "insn %u: replaced reg %u with %u\n",
1660 INSN_UID (insn), regno, REGNO (new));
1661 changed = true;
1662 goto did_replacement;
1666 /* Otherwise, try all valid registers and see if its valid. */
1667 for (i = vd->e[regno].oldest_regno; i != regno;
1668 i = vd->e[i].next_regno)
1670 new = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
1671 mode, i, regno);
1672 if (new != NULL_RTX)
1674 if (validate_change (insn, &SET_SRC (set), new, 0))
1676 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (src);
1677 REG_ATTRS (new) = REG_ATTRS (src);
1678 if (dump_file)
1679 fprintf (dump_file,
1680 "insn %u: replaced reg %u with %u\n",
1681 INSN_UID (insn), regno, REGNO (new));
1682 changed = true;
1683 goto did_replacement;
1688 no_move_special_case:
1690 /* For each input operand, replace a hard register with the
1691 eldest live copy that's in an appropriate register class. */
1692 for (i = 0; i < n_ops; i++)
1694 bool replaced = false;
1696 /* Don't scan match_operand here, since we've no reg class
1697 information to pass down. Any operands that we could
1698 substitute in will be represented elsewhere. */
1699 if (recog_data.constraints[i][0] == '\0')
1700 continue;
1702 /* Don't replace in asms intentionally referencing hard regs. */
1703 if (is_asm && REG_P (recog_data.operand[i])
1704 && (REGNO (recog_data.operand[i])
1705 == ORIGINAL_REGNO (recog_data.operand[i])))
1706 continue;
1708 if (recog_data.operand_type[i] == OP_IN)
1710 if (recog_op_alt[i][alt].is_address)
1711 replaced
1712 = replace_oldest_value_addr (recog_data.operand_loc[i],
1713 recog_op_alt[i][alt].cl,
1714 VOIDmode, insn, vd);
1715 else if (REG_P (recog_data.operand[i]))
1716 replaced
1717 = replace_oldest_value_reg (recog_data.operand_loc[i],
1718 recog_op_alt[i][alt].cl,
1719 insn, vd);
1720 else if (MEM_P (recog_data.operand[i]))
1721 replaced = replace_oldest_value_mem (recog_data.operand[i],
1722 insn, vd);
1724 else if (MEM_P (recog_data.operand[i]))
1725 replaced = replace_oldest_value_mem (recog_data.operand[i],
1726 insn, vd);
1728 /* If we performed any replacement, update match_dups. */
1729 if (replaced)
1731 int j;
1732 rtx new;
1734 changed = true;
1736 new = *recog_data.operand_loc[i];
1737 recog_data.operand[i] = new;
1738 for (j = 0; j < recog_data.n_dups; j++)
1739 if (recog_data.dup_num[j] == i)
1740 *recog_data.dup_loc[j] = new;
1744 did_replacement:
1745 /* Clobber call-clobbered registers. */
1746 if (CALL_P (insn))
1747 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1748 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1749 kill_value_regno (i, 1, vd);
1751 /* Notice stores. */
1752 note_stores (PATTERN (insn), kill_set_value, vd);
1754 /* Notice copies. */
1755 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1756 copy_value (SET_DEST (set), SET_SRC (set), vd);
1758 if (insn == BB_END (bb))
1759 break;
1762 return changed;
1765 /* Main entry point for the forward copy propagation optimization. */
1767 void
1768 copyprop_hardreg_forward (void)
1770 struct value_data *all_vd;
1771 bool need_refresh;
1772 basic_block bb;
1773 sbitmap visited;
1775 need_refresh = false;
1777 all_vd = xmalloc (sizeof (struct value_data) * last_basic_block);
1779 visited = sbitmap_alloc (last_basic_block - (INVALID_BLOCK + 1));
1780 sbitmap_zero (visited);
1782 FOR_EACH_BB (bb)
1784 SET_BIT (visited, bb->index - (INVALID_BLOCK + 1));
1786 /* If a block has a single predecessor, that we've already
1787 processed, begin with the value data that was live at
1788 the end of the predecessor block. */
1789 /* ??? Ought to use more intelligent queuing of blocks. */
1790 if (single_pred_p (bb)
1791 && TEST_BIT (visited,
1792 single_pred (bb)->index - (INVALID_BLOCK + 1))
1793 && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1794 all_vd[bb->index] = all_vd[single_pred (bb)->index];
1795 else
1796 init_value_data (all_vd + bb->index);
1798 if (copyprop_hardreg_forward_1 (bb, all_vd + bb->index))
1799 need_refresh = true;
1802 sbitmap_free (visited);
1804 if (need_refresh)
1806 if (dump_file)
1807 fputs ("\n\n", dump_file);
1809 /* ??? Irritatingly, delete_noop_moves does not take a set of blocks
1810 to scan, so we have to do a life update with no initial set of
1811 blocks Just In Case. */
1812 delete_noop_moves ();
1813 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
1814 PROP_DEATH_NOTES
1815 | PROP_SCAN_DEAD_CODE
1816 | PROP_KILL_DEAD_CODE);
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 || flag_cprop_registers));
1933 /* Run the regrename and cprop passes. */
1934 static void
1935 rest_of_handle_regrename (void)
1937 if (flag_rename_registers)
1938 regrename_optimize ();
1939 if (flag_cprop_registers)
1940 copyprop_hardreg_forward ();
1943 struct tree_opt_pass pass_regrename =
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_dump_func, /* todo_flags_finish */
1957 'n' /* letter */