* lib/target-supports.exp (check_effective_target_vect_shift):
[official-gcc.git] / gcc / regrename.c
blobcd448c4465429dc57c73d925bae261a882915bf5
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
64 static const char * const scan_actions_name[] =
66 "terminate_all_read",
67 "terminate_overlapping_read",
68 "terminate_write",
69 "terminate_dead",
70 "mark_read",
71 "mark_write"
74 static struct obstack rename_obstack;
76 static void do_replace (struct du_chain *, int);
77 static void scan_rtx_reg (rtx, rtx *, enum reg_class,
78 enum scan_actions, enum op_type, int);
79 static void scan_rtx_address (rtx, rtx *, enum reg_class,
80 enum scan_actions, enum machine_mode);
81 static void scan_rtx (rtx, rtx *, enum reg_class, enum scan_actions,
82 enum op_type, int);
83 static struct du_chain *build_def_use (basic_block);
84 static void dump_def_use_chain (struct du_chain *);
85 static void note_sets (rtx, rtx, void *);
86 static void clear_dead_regs (HARD_REG_SET *, enum machine_mode, rtx);
87 static void merge_overlapping_regs (basic_block, HARD_REG_SET *,
88 struct du_chain *);
90 /* Called through note_stores from update_life. Find sets of registers, and
91 record them in *DATA (which is actually a HARD_REG_SET *). */
93 static void
94 note_sets (rtx x, rtx set ATTRIBUTE_UNUSED, void *data)
96 HARD_REG_SET *pset = (HARD_REG_SET *) data;
97 unsigned int regno;
98 int nregs;
100 if (GET_CODE (x) == SUBREG)
101 x = SUBREG_REG (x);
102 if (!REG_P (x))
103 return;
104 regno = REGNO (x);
105 nregs = hard_regno_nregs[regno][GET_MODE (x)];
107 /* There must not be pseudos at this point. */
108 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
110 while (nregs-- > 0)
111 SET_HARD_REG_BIT (*pset, regno + nregs);
114 /* Clear all registers from *PSET for which a note of kind KIND can be found
115 in the list NOTES. */
117 static void
118 clear_dead_regs (HARD_REG_SET *pset, enum machine_mode kind, rtx notes)
120 rtx note;
121 for (note = notes; note; note = XEXP (note, 1))
122 if (REG_NOTE_KIND (note) == kind && REG_P (XEXP (note, 0)))
124 rtx reg = XEXP (note, 0);
125 unsigned int regno = REGNO (reg);
126 int nregs = hard_regno_nregs[regno][GET_MODE (reg)];
128 /* There must not be pseudos at this point. */
129 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
131 while (nregs-- > 0)
132 CLEAR_HARD_REG_BIT (*pset, regno + nregs);
136 /* For a def-use chain CHAIN in basic block B, find which registers overlap
137 its lifetime and set the corresponding bits in *PSET. */
139 static void
140 merge_overlapping_regs (basic_block b, HARD_REG_SET *pset,
141 struct du_chain *chain)
143 struct du_chain *t = chain;
144 rtx insn;
145 HARD_REG_SET live;
147 REG_SET_TO_HARD_REG_SET (live, b->il.rtl->global_live_at_start);
148 insn = BB_HEAD (b);
149 while (t)
151 /* Search forward until the next reference to the register to be
152 renamed. */
153 while (insn != t->insn)
155 if (INSN_P (insn))
157 clear_dead_regs (&live, REG_DEAD, REG_NOTES (insn));
158 note_stores (PATTERN (insn), note_sets, (void *) &live);
159 /* Only record currently live regs if we are inside the
160 reg's live range. */
161 if (t != chain)
162 IOR_HARD_REG_SET (*pset, live);
163 clear_dead_regs (&live, REG_UNUSED, REG_NOTES (insn));
165 insn = NEXT_INSN (insn);
168 IOR_HARD_REG_SET (*pset, live);
170 /* For the last reference, also merge in all registers set in the
171 same insn.
172 @@@ We only have take earlyclobbered sets into account. */
173 if (! t->next_use)
174 note_stores (PATTERN (insn), note_sets, (void *) pset);
176 t = t->next_use;
180 /* Perform register renaming on the current function. */
182 void
183 regrename_optimize (void)
185 int tick[FIRST_PSEUDO_REGISTER];
186 int this_tick = 0;
187 basic_block bb;
188 char *first_obj;
190 memset (tick, 0, sizeof tick);
192 gcc_obstack_init (&rename_obstack);
193 first_obj = obstack_alloc (&rename_obstack, 0);
195 FOR_EACH_BB (bb)
197 struct du_chain *all_chains = 0;
198 HARD_REG_SET unavailable;
199 HARD_REG_SET regs_seen;
201 CLEAR_HARD_REG_SET (unavailable);
203 if (dump_file)
204 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
206 all_chains = build_def_use (bb);
208 if (dump_file)
209 dump_def_use_chain (all_chains);
211 CLEAR_HARD_REG_SET (unavailable);
212 /* Don't clobber traceback for noreturn functions. */
213 if (frame_pointer_needed)
215 int i;
217 for (i = hard_regno_nregs[FRAME_POINTER_REGNUM][Pmode]; i--;)
218 SET_HARD_REG_BIT (unavailable, FRAME_POINTER_REGNUM + i);
220 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
221 for (i = hard_regno_nregs[HARD_FRAME_POINTER_REGNUM][Pmode]; i--;)
222 SET_HARD_REG_BIT (unavailable, HARD_FRAME_POINTER_REGNUM + i);
223 #endif
226 CLEAR_HARD_REG_SET (regs_seen);
227 while (all_chains)
229 int new_reg, best_new_reg;
230 int n_uses;
231 struct du_chain *this = all_chains;
232 struct du_chain *tmp, *last;
233 HARD_REG_SET this_unavailable;
234 int reg = REGNO (*this->loc);
235 int i;
237 all_chains = this->next_chain;
239 best_new_reg = reg;
241 #if 0 /* This just disables optimization opportunities. */
242 /* Only rename once we've seen the reg more than once. */
243 if (! TEST_HARD_REG_BIT (regs_seen, reg))
245 SET_HARD_REG_BIT (regs_seen, reg);
246 continue;
248 #endif
250 if (fixed_regs[reg] || global_regs[reg]
251 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
252 || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
253 #else
254 || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
255 #endif
257 continue;
259 COPY_HARD_REG_SET (this_unavailable, unavailable);
261 /* Find last entry on chain (which has the need_caller_save bit),
262 count number of uses, and narrow the set of registers we can
263 use for renaming. */
264 n_uses = 0;
265 for (last = this; last->next_use; last = last->next_use)
267 n_uses++;
268 IOR_COMPL_HARD_REG_SET (this_unavailable,
269 reg_class_contents[last->cl]);
271 if (n_uses < 1)
272 continue;
274 IOR_COMPL_HARD_REG_SET (this_unavailable,
275 reg_class_contents[last->cl]);
277 if (this->need_caller_save_reg)
278 IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
280 merge_overlapping_regs (bb, &this_unavailable, this);
282 /* Now potential_regs is a reasonable approximation, let's
283 have a closer look at each register still in there. */
284 for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
286 int nregs = hard_regno_nregs[new_reg][GET_MODE (*this->loc)];
288 for (i = nregs - 1; i >= 0; --i)
289 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
290 || fixed_regs[new_reg + i]
291 || global_regs[new_reg + i]
292 /* Can't use regs which aren't saved by the prologue. */
293 || (! regs_ever_live[new_reg + i]
294 && ! call_used_regs[new_reg + i])
295 #ifdef LEAF_REGISTERS
296 /* We can't use a non-leaf register if we're in a
297 leaf function. */
298 || (current_function_is_leaf
299 && !LEAF_REGISTERS[new_reg + i])
300 #endif
301 #ifdef HARD_REGNO_RENAME_OK
302 || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
303 #endif
305 break;
306 if (i >= 0)
307 continue;
309 /* See whether it accepts all modes that occur in
310 definition and uses. */
311 for (tmp = this; tmp; tmp = tmp->next_use)
312 if (! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
313 || (tmp->need_caller_save_reg
314 && ! (HARD_REGNO_CALL_PART_CLOBBERED
315 (reg, GET_MODE (*tmp->loc)))
316 && (HARD_REGNO_CALL_PART_CLOBBERED
317 (new_reg, GET_MODE (*tmp->loc)))))
318 break;
319 if (! tmp)
321 if (tick[best_new_reg] > tick[new_reg])
322 best_new_reg = new_reg;
326 if (dump_file)
328 fprintf (dump_file, "Register %s in insn %d",
329 reg_names[reg], INSN_UID (last->insn));
330 if (last->need_caller_save_reg)
331 fprintf (dump_file, " crosses a call");
334 if (best_new_reg == reg)
336 tick[reg] = ++this_tick;
337 if (dump_file)
338 fprintf (dump_file, "; no available better choice\n");
339 continue;
342 do_replace (this, best_new_reg);
343 tick[best_new_reg] = ++this_tick;
344 regs_ever_live[best_new_reg] = 1;
346 if (dump_file)
347 fprintf (dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
350 obstack_free (&rename_obstack, first_obj);
353 obstack_free (&rename_obstack, NULL);
355 if (dump_file)
356 fputc ('\n', dump_file);
358 count_or_remove_death_notes (NULL, 1);
359 update_life_info (NULL, UPDATE_LIFE_LOCAL,
360 PROP_DEATH_NOTES);
363 static void
364 do_replace (struct du_chain *chain, int reg)
366 while (chain)
368 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
369 struct reg_attrs * attr = REG_ATTRS (*chain->loc);
371 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
372 if (regno >= FIRST_PSEUDO_REGISTER)
373 ORIGINAL_REGNO (*chain->loc) = regno;
374 REG_ATTRS (*chain->loc) = attr;
375 chain = chain->next_use;
380 static struct du_chain *open_chains;
381 static struct du_chain *closed_chains;
383 static void
384 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class cl,
385 enum scan_actions action, enum op_type type, int earlyclobber)
387 struct du_chain **p;
388 rtx x = *loc;
389 enum machine_mode mode = GET_MODE (x);
390 int this_regno = REGNO (x);
391 int this_nregs = hard_regno_nregs[this_regno][mode];
393 if (action == mark_write)
395 if (type == OP_OUT)
397 struct du_chain *this
398 = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
399 this->next_use = 0;
400 this->next_chain = open_chains;
401 this->loc = loc;
402 this->insn = insn;
403 this->cl = cl;
404 this->need_caller_save_reg = 0;
405 this->earlyclobber = earlyclobber;
406 open_chains = this;
408 return;
411 if ((type == OP_OUT && action != terminate_write)
412 || (type != OP_OUT && action == terminate_write))
413 return;
415 for (p = &open_chains; *p;)
417 struct du_chain *this = *p;
419 /* Check if the chain has been terminated if it has then skip to
420 the next chain.
422 This can happen when we've already appended the location to
423 the chain in Step 3, but are trying to hide in-out operands
424 from terminate_write in Step 5. */
426 if (*this->loc == cc0_rtx)
427 p = &this->next_chain;
428 else
430 int regno = REGNO (*this->loc);
431 int nregs = hard_regno_nregs[regno][GET_MODE (*this->loc)];
432 int exact_match = (regno == this_regno && nregs == this_nregs);
434 if (regno + nregs <= this_regno
435 || this_regno + this_nregs <= regno)
437 p = &this->next_chain;
438 continue;
441 if (action == mark_read)
443 gcc_assert (exact_match);
445 /* ??? Class NO_REGS can happen if the md file makes use of
446 EXTRA_CONSTRAINTS to match registers. Which is arguably
447 wrong, but there we are. Since we know not what this may
448 be replaced with, terminate the chain. */
449 if (cl != NO_REGS)
451 this = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
452 this->next_use = 0;
453 this->next_chain = (*p)->next_chain;
454 this->loc = loc;
455 this->insn = insn;
456 this->cl = cl;
457 this->need_caller_save_reg = 0;
458 while (*p)
459 p = &(*p)->next_use;
460 *p = this;
461 return;
465 if (action != terminate_overlapping_read || ! exact_match)
467 struct du_chain *next = this->next_chain;
469 /* Whether the terminated chain can be used for renaming
470 depends on the action and this being an exact match.
471 In either case, we remove this element from open_chains. */
473 if ((action == terminate_dead || action == terminate_write)
474 && exact_match)
476 this->next_chain = closed_chains;
477 closed_chains = this;
478 if (dump_file)
479 fprintf (dump_file,
480 "Closing chain %s at insn %d (%s)\n",
481 reg_names[REGNO (*this->loc)], INSN_UID (insn),
482 scan_actions_name[(int) action]);
484 else
486 if (dump_file)
487 fprintf (dump_file,
488 "Discarding chain %s at insn %d (%s)\n",
489 reg_names[REGNO (*this->loc)], INSN_UID (insn),
490 scan_actions_name[(int) action]);
492 *p = next;
494 else
495 p = &this->next_chain;
500 /* Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
501 BASE_REG_CLASS depending on how the register is being considered. */
503 static void
504 scan_rtx_address (rtx insn, rtx *loc, enum reg_class cl,
505 enum scan_actions action, enum machine_mode mode)
507 rtx x = *loc;
508 RTX_CODE code = GET_CODE (x);
509 const char *fmt;
510 int i, j;
512 if (action == mark_write)
513 return;
515 switch (code)
517 case PLUS:
519 rtx orig_op0 = XEXP (x, 0);
520 rtx orig_op1 = XEXP (x, 1);
521 RTX_CODE code0 = GET_CODE (orig_op0);
522 RTX_CODE code1 = GET_CODE (orig_op1);
523 rtx op0 = orig_op0;
524 rtx op1 = orig_op1;
525 rtx *locI = NULL;
526 rtx *locB = NULL;
527 rtx *locB_reg = NULL;
529 if (GET_CODE (op0) == SUBREG)
531 op0 = SUBREG_REG (op0);
532 code0 = GET_CODE (op0);
535 if (GET_CODE (op1) == SUBREG)
537 op1 = SUBREG_REG (op1);
538 code1 = GET_CODE (op1);
541 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
542 || code0 == ZERO_EXTEND || code1 == MEM)
544 locI = &XEXP (x, 0);
545 locB = &XEXP (x, 1);
547 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
548 || code1 == ZERO_EXTEND || code0 == MEM)
550 locI = &XEXP (x, 1);
551 locB = &XEXP (x, 0);
553 else if (code0 == CONST_INT || code0 == CONST
554 || code0 == SYMBOL_REF || code0 == LABEL_REF)
555 locB = &XEXP (x, 1);
556 else if (code1 == CONST_INT || code1 == CONST
557 || code1 == SYMBOL_REF || code1 == LABEL_REF)
558 locB = &XEXP (x, 0);
559 else if (code0 == REG && code1 == REG)
561 int index_op;
563 if (REG_OK_FOR_INDEX_P (op0)
564 && REG_MODE_OK_FOR_REG_BASE_P (op1, mode))
565 index_op = 0;
566 else if (REG_OK_FOR_INDEX_P (op1)
567 && REG_MODE_OK_FOR_REG_BASE_P (op0, mode))
568 index_op = 1;
569 else if (REG_MODE_OK_FOR_REG_BASE_P (op1, mode))
570 index_op = 0;
571 else if (REG_MODE_OK_FOR_REG_BASE_P (op0, mode))
572 index_op = 1;
573 else if (REG_OK_FOR_INDEX_P (op1))
574 index_op = 1;
575 else
576 index_op = 0;
578 locI = &XEXP (x, index_op);
579 locB_reg = &XEXP (x, !index_op);
581 else if (code0 == REG)
583 locI = &XEXP (x, 0);
584 locB = &XEXP (x, 1);
586 else if (code1 == REG)
588 locI = &XEXP (x, 1);
589 locB = &XEXP (x, 0);
592 if (locI)
593 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
594 if (locB)
595 scan_rtx_address (insn, locB, MODE_BASE_REG_CLASS (mode), action, mode);
596 if (locB_reg)
597 scan_rtx_address (insn, locB_reg, MODE_BASE_REG_REG_CLASS (mode),
598 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 MODE_BASE_REG_CLASS (GET_MODE (x)), 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_VECTOR:
656 case SYMBOL_REF:
657 case LABEL_REF:
658 case CC0:
659 case PC:
660 return;
662 case REG:
663 scan_rtx_reg (insn, loc, cl, action, type, earlyclobber);
664 return;
666 case MEM:
667 scan_rtx_address (insn, &XEXP (x, 0),
668 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
669 GET_MODE (x));
670 return;
672 case SET:
673 scan_rtx (insn, &SET_SRC (x), cl, action, OP_IN, 0);
674 scan_rtx (insn, &SET_DEST (x), cl, action,
675 GET_CODE (PATTERN (insn)) == COND_EXEC ? OP_INOUT : OP_OUT, 0);
676 return;
678 case STRICT_LOW_PART:
679 scan_rtx (insn, &XEXP (x, 0), cl, action, OP_INOUT, earlyclobber);
680 return;
682 case ZERO_EXTRACT:
683 case SIGN_EXTRACT:
684 scan_rtx (insn, &XEXP (x, 0), cl, action,
685 type == OP_IN ? OP_IN : OP_INOUT, earlyclobber);
686 scan_rtx (insn, &XEXP (x, 1), cl, action, OP_IN, 0);
687 scan_rtx (insn, &XEXP (x, 2), cl, action, OP_IN, 0);
688 return;
690 case POST_INC:
691 case PRE_INC:
692 case POST_DEC:
693 case PRE_DEC:
694 case POST_MODIFY:
695 case PRE_MODIFY:
696 /* Should only happen inside MEM. */
697 gcc_unreachable ();
699 case CLOBBER:
700 scan_rtx (insn, &SET_DEST (x), cl, action,
701 GET_CODE (PATTERN (insn)) == COND_EXEC ? OP_INOUT : OP_OUT, 0);
702 return;
704 case EXPR_LIST:
705 scan_rtx (insn, &XEXP (x, 0), cl, action, type, 0);
706 if (XEXP (x, 1))
707 scan_rtx (insn, &XEXP (x, 1), cl, action, type, 0);
708 return;
710 default:
711 break;
714 fmt = GET_RTX_FORMAT (code);
715 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
717 if (fmt[i] == 'e')
718 scan_rtx (insn, &XEXP (x, i), cl, action, type, 0);
719 else if (fmt[i] == 'E')
720 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
721 scan_rtx (insn, &XVECEXP (x, i, j), cl, action, type, 0);
725 /* Build def/use chain. */
727 static struct du_chain *
728 build_def_use (basic_block bb)
730 rtx insn;
732 open_chains = closed_chains = NULL;
734 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
736 if (INSN_P (insn))
738 int n_ops;
739 rtx note;
740 rtx old_operands[MAX_RECOG_OPERANDS];
741 rtx old_dups[MAX_DUP_OPERANDS];
742 int i, icode;
743 int alt;
744 int predicated;
746 /* Process the insn, determining its effect on the def-use
747 chains. We perform the following steps with the register
748 references in the insn:
749 (1) Any read that overlaps an open chain, but doesn't exactly
750 match, causes that chain to be closed. We can't deal
751 with overlaps yet.
752 (2) Any read outside an operand causes any chain it overlaps
753 with to be closed, since we can't replace it.
754 (3) Any read inside an operand is added if there's already
755 an open chain for it.
756 (4) For any REG_DEAD note we find, close open chains that
757 overlap it.
758 (5) For any write we find, close open chains that overlap it.
759 (6) For any write we find in an operand, make a new chain.
760 (7) For any REG_UNUSED, close any chains we just opened. */
762 icode = recog_memoized (insn);
763 extract_insn (insn);
764 if (! constrain_operands (1))
765 fatal_insn_not_found (insn);
766 preprocess_constraints ();
767 alt = which_alternative;
768 n_ops = recog_data.n_operands;
770 /* Simplify the code below by rewriting things to reflect
771 matching constraints. Also promote OP_OUT to OP_INOUT
772 in predicated instructions. */
774 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
775 for (i = 0; i < n_ops; ++i)
777 int matches = recog_op_alt[i][alt].matches;
778 if (matches >= 0)
779 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
780 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
781 || (predicated && recog_data.operand_type[i] == OP_OUT))
782 recog_data.operand_type[i] = OP_INOUT;
785 /* Step 1: Close chains for which we have overlapping reads. */
786 for (i = 0; i < n_ops; i++)
787 scan_rtx (insn, recog_data.operand_loc[i],
788 NO_REGS, terminate_overlapping_read,
789 recog_data.operand_type[i], 0);
791 /* Step 2: Close chains for which we have reads outside operands.
792 We do this by munging all operands into CC0, and closing
793 everything remaining. */
795 for (i = 0; i < n_ops; i++)
797 old_operands[i] = recog_data.operand[i];
798 /* Don't squash match_operator or match_parallel here, since
799 we don't know that all of the contained registers are
800 reachable by proper operands. */
801 if (recog_data.constraints[i][0] == '\0')
802 continue;
803 *recog_data.operand_loc[i] = cc0_rtx;
805 for (i = 0; i < recog_data.n_dups; i++)
807 int dup_num = recog_data.dup_num[i];
809 old_dups[i] = *recog_data.dup_loc[i];
810 *recog_data.dup_loc[i] = cc0_rtx;
812 /* For match_dup of match_operator or match_parallel, share
813 them, so that we don't miss changes in the dup. */
814 if (icode >= 0
815 && insn_data[icode].operand[dup_num].eliminable == 0)
816 old_dups[i] = recog_data.operand[dup_num];
819 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
820 OP_IN, 0);
822 for (i = 0; i < recog_data.n_dups; i++)
823 *recog_data.dup_loc[i] = old_dups[i];
824 for (i = 0; i < n_ops; i++)
825 *recog_data.operand_loc[i] = old_operands[i];
827 /* Step 2B: Can't rename function call argument registers. */
828 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
829 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
830 NO_REGS, terminate_all_read, OP_IN, 0);
832 /* Step 2C: Can't rename asm operands that were originally
833 hard registers. */
834 if (asm_noperands (PATTERN (insn)) > 0)
835 for (i = 0; i < n_ops; i++)
837 rtx *loc = recog_data.operand_loc[i];
838 rtx op = *loc;
840 if (REG_P (op)
841 && REGNO (op) == ORIGINAL_REGNO (op)
842 && (recog_data.operand_type[i] == OP_IN
843 || recog_data.operand_type[i] == OP_INOUT))
844 scan_rtx (insn, loc, NO_REGS, terminate_all_read, OP_IN, 0);
847 /* Step 3: Append to chains for reads inside operands. */
848 for (i = 0; i < n_ops + recog_data.n_dups; i++)
850 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
851 rtx *loc = (i < n_ops
852 ? recog_data.operand_loc[opn]
853 : recog_data.dup_loc[i - n_ops]);
854 enum reg_class cl = recog_op_alt[opn][alt].cl;
855 enum op_type type = recog_data.operand_type[opn];
857 /* Don't scan match_operand here, since we've no reg class
858 information to pass down. Any operands that we could
859 substitute in will be represented elsewhere. */
860 if (recog_data.constraints[opn][0] == '\0')
861 continue;
863 if (recog_op_alt[opn][alt].is_address)
864 scan_rtx_address (insn, loc, cl, mark_read, VOIDmode);
865 else
866 scan_rtx (insn, loc, cl, mark_read, type, 0);
869 /* Step 4: Close chains for registers that die here.
870 Also record updates for REG_INC notes. */
871 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
873 if (REG_NOTE_KIND (note) == REG_DEAD)
874 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
875 OP_IN, 0);
876 else if (REG_NOTE_KIND (note) == REG_INC)
877 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
878 OP_INOUT, 0);
881 /* Step 4B: If this is a call, any chain live at this point
882 requires a caller-saved reg. */
883 if (CALL_P (insn))
885 struct du_chain *p;
886 for (p = open_chains; p; p = p->next_chain)
887 p->need_caller_save_reg = 1;
890 /* Step 5: Close open chains that overlap writes. Similar to
891 step 2, we hide in-out operands, since we do not want to
892 close these chains. */
894 for (i = 0; i < n_ops; i++)
896 old_operands[i] = recog_data.operand[i];
897 if (recog_data.operand_type[i] == OP_INOUT)
898 *recog_data.operand_loc[i] = cc0_rtx;
900 for (i = 0; i < recog_data.n_dups; i++)
902 int opn = recog_data.dup_num[i];
903 old_dups[i] = *recog_data.dup_loc[i];
904 if (recog_data.operand_type[opn] == OP_INOUT)
905 *recog_data.dup_loc[i] = cc0_rtx;
908 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
910 for (i = 0; i < recog_data.n_dups; i++)
911 *recog_data.dup_loc[i] = old_dups[i];
912 for (i = 0; i < n_ops; i++)
913 *recog_data.operand_loc[i] = old_operands[i];
915 /* Step 6: Begin new chains for writes inside operands. */
916 /* ??? Many targets have output constraints on the SET_DEST
917 of a call insn, which is stupid, since these are certainly
918 ABI defined hard registers. Don't change calls at all.
919 Similarly take special care for asm statement that originally
920 referenced hard registers. */
921 if (asm_noperands (PATTERN (insn)) > 0)
923 for (i = 0; i < n_ops; i++)
924 if (recog_data.operand_type[i] == OP_OUT)
926 rtx *loc = recog_data.operand_loc[i];
927 rtx op = *loc;
928 enum reg_class cl = recog_op_alt[i][alt].cl;
930 if (REG_P (op)
931 && REGNO (op) == ORIGINAL_REGNO (op))
932 continue;
934 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
935 recog_op_alt[i][alt].earlyclobber);
938 else if (!CALL_P (insn))
939 for (i = 0; i < n_ops + recog_data.n_dups; i++)
941 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
942 rtx *loc = (i < n_ops
943 ? recog_data.operand_loc[opn]
944 : recog_data.dup_loc[i - n_ops]);
945 enum reg_class cl = recog_op_alt[opn][alt].cl;
947 if (recog_data.operand_type[opn] == OP_OUT)
948 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
949 recog_op_alt[opn][alt].earlyclobber);
952 /* Step 7: Close chains for registers that were never
953 really used here. */
954 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
955 if (REG_NOTE_KIND (note) == REG_UNUSED)
956 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
957 OP_IN, 0);
959 if (insn == BB_END (bb))
960 break;
963 /* Since we close every chain when we find a REG_DEAD note, anything that
964 is still open lives past the basic block, so it can't be renamed. */
965 return closed_chains;
968 /* Dump all def/use chains in CHAINS to DUMP_FILE. They are
969 printed in reverse order as that's how we build them. */
971 static void
972 dump_def_use_chain (struct du_chain *chains)
974 while (chains)
976 struct du_chain *this = chains;
977 int r = REGNO (*this->loc);
978 int nregs = hard_regno_nregs[r][GET_MODE (*this->loc)];
979 fprintf (dump_file, "Register %s (%d):", reg_names[r], nregs);
980 while (this)
982 fprintf (dump_file, " %d [%s]", INSN_UID (this->insn),
983 reg_class_names[this->cl]);
984 this = this->next_use;
986 fprintf (dump_file, "\n");
987 chains = chains->next_chain;
991 /* The following code does forward propagation of hard register copies.
992 The object is to eliminate as many dependencies as possible, so that
993 we have the most scheduling freedom. As a side effect, we also clean
994 up some silly register allocation decisions made by reload. This
995 code may be obsoleted by a new register allocator. */
997 /* For each register, we have a list of registers that contain the same
998 value. The OLDEST_REGNO field points to the head of the list, and
999 the NEXT_REGNO field runs through the list. The MODE field indicates
1000 what mode the data is known to be in; this field is VOIDmode when the
1001 register is not known to contain valid data. */
1003 struct value_data_entry
1005 enum machine_mode mode;
1006 unsigned int oldest_regno;
1007 unsigned int next_regno;
1010 struct value_data
1012 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
1013 unsigned int max_value_regs;
1016 static void kill_value_one_regno (unsigned, struct value_data *);
1017 static void kill_value_regno (unsigned, unsigned, struct value_data *);
1018 static void kill_value (rtx, struct value_data *);
1019 static void set_value_regno (unsigned, enum machine_mode, struct value_data *);
1020 static void init_value_data (struct value_data *);
1021 static void kill_clobbered_value (rtx, rtx, void *);
1022 static void kill_set_value (rtx, rtx, void *);
1023 static int kill_autoinc_value (rtx *, void *);
1024 static void copy_value (rtx, rtx, struct value_data *);
1025 static bool mode_change_ok (enum machine_mode, enum machine_mode,
1026 unsigned int);
1027 static rtx maybe_mode_change (enum machine_mode, enum machine_mode,
1028 enum machine_mode, unsigned int, unsigned int);
1029 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
1030 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx,
1031 struct value_data *);
1032 static bool replace_oldest_value_addr (rtx *, enum reg_class,
1033 enum machine_mode, rtx,
1034 struct value_data *);
1035 static bool replace_oldest_value_mem (rtx, rtx, struct value_data *);
1036 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
1037 extern void debug_value_data (struct value_data *);
1038 #ifdef ENABLE_CHECKING
1039 static void validate_value_data (struct value_data *);
1040 #endif
1042 /* Kill register REGNO. This involves removing it from any value
1043 lists, and resetting the value mode to VOIDmode. This is only a
1044 helper function; it does not handle any hard registers overlapping
1045 with REGNO. */
1047 static void
1048 kill_value_one_regno (unsigned int regno, struct value_data *vd)
1050 unsigned int i, next;
1052 if (vd->e[regno].oldest_regno != regno)
1054 for (i = vd->e[regno].oldest_regno;
1055 vd->e[i].next_regno != regno;
1056 i = vd->e[i].next_regno)
1057 continue;
1058 vd->e[i].next_regno = vd->e[regno].next_regno;
1060 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
1062 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
1063 vd->e[i].oldest_regno = next;
1066 vd->e[regno].mode = VOIDmode;
1067 vd->e[regno].oldest_regno = regno;
1068 vd->e[regno].next_regno = INVALID_REGNUM;
1070 #ifdef ENABLE_CHECKING
1071 validate_value_data (vd);
1072 #endif
1075 /* Kill the value in register REGNO for NREGS, and any other registers
1076 whose values overlap. */
1078 static void
1079 kill_value_regno (unsigned int regno, unsigned int nregs,
1080 struct value_data *vd)
1082 unsigned int j;
1084 /* Kill the value we're told to kill. */
1085 for (j = 0; j < nregs; ++j)
1086 kill_value_one_regno (regno + j, vd);
1088 /* Kill everything that overlapped what we're told to kill. */
1089 if (regno < vd->max_value_regs)
1090 j = 0;
1091 else
1092 j = regno - vd->max_value_regs;
1093 for (; j < regno; ++j)
1095 unsigned int i, n;
1096 if (vd->e[j].mode == VOIDmode)
1097 continue;
1098 n = hard_regno_nregs[j][vd->e[j].mode];
1099 if (j + n > regno)
1100 for (i = 0; i < n; ++i)
1101 kill_value_one_regno (j + i, vd);
1105 /* Kill X. This is a convenience function wrapping kill_value_regno
1106 so that we mind the mode the register is in. */
1108 static void
1109 kill_value (rtx x, struct value_data *vd)
1111 rtx orig_rtx = x;
1113 if (GET_CODE (x) == SUBREG)
1115 x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
1116 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
1117 if (x == NULL_RTX)
1118 x = SUBREG_REG (orig_rtx);
1120 if (REG_P (x))
1122 unsigned int regno = REGNO (x);
1123 unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
1125 kill_value_regno (regno, n, vd);
1129 /* Remember that REGNO is valid in MODE. */
1131 static void
1132 set_value_regno (unsigned int regno, enum machine_mode mode,
1133 struct value_data *vd)
1135 unsigned int nregs;
1137 vd->e[regno].mode = mode;
1139 nregs = hard_regno_nregs[regno][mode];
1140 if (nregs > vd->max_value_regs)
1141 vd->max_value_regs = nregs;
1144 /* Initialize VD such that there are no known relationships between regs. */
1146 static void
1147 init_value_data (struct value_data *vd)
1149 int i;
1150 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1152 vd->e[i].mode = VOIDmode;
1153 vd->e[i].oldest_regno = i;
1154 vd->e[i].next_regno = INVALID_REGNUM;
1156 vd->max_value_regs = 0;
1159 /* Called through note_stores. If X is clobbered, kill its value. */
1161 static void
1162 kill_clobbered_value (rtx x, rtx set, void *data)
1164 struct value_data *vd = data;
1165 if (GET_CODE (set) == CLOBBER)
1166 kill_value (x, vd);
1169 /* Called through note_stores. If X is set, not clobbered, kill its
1170 current value and install it as the root of its own value list. */
1172 static void
1173 kill_set_value (rtx x, rtx set, void *data)
1175 struct value_data *vd = data;
1176 if (GET_CODE (set) != CLOBBER)
1178 kill_value (x, vd);
1179 if (REG_P (x))
1180 set_value_regno (REGNO (x), GET_MODE (x), vd);
1184 /* Called through for_each_rtx. Kill any register used as the base of an
1185 auto-increment expression, and install that register as the root of its
1186 own value list. */
1188 static int
1189 kill_autoinc_value (rtx *px, void *data)
1191 rtx x = *px;
1192 struct value_data *vd = data;
1194 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
1196 x = XEXP (x, 0);
1197 kill_value (x, vd);
1198 set_value_regno (REGNO (x), Pmode, vd);
1199 return -1;
1202 return 0;
1205 /* Assert that SRC has been copied to DEST. Adjust the data structures
1206 to reflect that SRC contains an older copy of the shared value. */
1208 static void
1209 copy_value (rtx dest, rtx src, struct value_data *vd)
1211 unsigned int dr = REGNO (dest);
1212 unsigned int sr = REGNO (src);
1213 unsigned int dn, sn;
1214 unsigned int i;
1216 /* ??? At present, it's possible to see noop sets. It'd be nice if
1217 this were cleaned up beforehand... */
1218 if (sr == dr)
1219 return;
1221 /* Do not propagate copies to the stack pointer, as that can leave
1222 memory accesses with no scheduling dependency on the stack update. */
1223 if (dr == STACK_POINTER_REGNUM)
1224 return;
1226 /* Likewise with the frame pointer, if we're using one. */
1227 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
1228 return;
1230 /* Do not propagate copies to fixed or global registers, patterns
1231 can be relying to see particular fixed register or users can
1232 expect the chosen global register in asm. */
1233 if (fixed_regs[dr] || global_regs[dr])
1234 return;
1236 /* If SRC and DEST overlap, don't record anything. */
1237 dn = hard_regno_nregs[dr][GET_MODE (dest)];
1238 sn = hard_regno_nregs[sr][GET_MODE (dest)];
1239 if ((dr > sr && dr < sr + sn)
1240 || (sr > dr && sr < dr + dn))
1241 return;
1243 /* If SRC had no assigned mode (i.e. we didn't know it was live)
1244 assign it now and assume the value came from an input argument
1245 or somesuch. */
1246 if (vd->e[sr].mode == VOIDmode)
1247 set_value_regno (sr, vd->e[dr].mode, vd);
1249 /* If we are narrowing the input to a smaller number of hard regs,
1250 and it is in big endian, we are really extracting a high part.
1251 Since we generally associate a low part of a value with the value itself,
1252 we must not do the same for the high part.
1253 Note we can still get low parts for the same mode combination through
1254 a two-step copy involving differently sized hard regs.
1255 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
1256 (set (reg:DI r0) (reg:DI fr0))
1257 (set (reg:SI fr2) (reg:SI r0))
1258 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
1259 (set (reg:SI fr2) (reg:SI fr0))
1260 loads the high part of (reg:DI fr0) into fr2.
1262 We can't properly represent the latter case in our tables, so don't
1263 record anything then. */
1264 else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
1265 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
1266 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
1267 return;
1269 /* If SRC had been assigned a mode narrower than the copy, we can't
1270 link DEST into the chain, because not all of the pieces of the
1271 copy came from oldest_regno. */
1272 else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
1273 return;
1275 /* Link DR at the end of the value chain used by SR. */
1277 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
1279 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
1280 continue;
1281 vd->e[i].next_regno = dr;
1283 #ifdef ENABLE_CHECKING
1284 validate_value_data (vd);
1285 #endif
1288 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
1290 static bool
1291 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
1292 unsigned int regno ATTRIBUTE_UNUSED)
1294 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
1295 return false;
1297 #ifdef CANNOT_CHANGE_MODE_CLASS
1298 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
1299 #endif
1301 return true;
1304 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
1305 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
1306 in NEW_MODE.
1307 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
1309 static rtx
1310 maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
1311 enum machine_mode new_mode, unsigned int regno,
1312 unsigned int copy_regno ATTRIBUTE_UNUSED)
1314 if (orig_mode == new_mode)
1315 return gen_rtx_raw_REG (new_mode, regno);
1316 else if (mode_change_ok (orig_mode, new_mode, regno))
1318 int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
1319 int use_nregs = hard_regno_nregs[copy_regno][new_mode];
1320 int copy_offset
1321 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
1322 int offset
1323 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
1324 int byteoffset = offset % UNITS_PER_WORD;
1325 int wordoffset = offset - byteoffset;
1327 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
1328 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
1329 return gen_rtx_raw_REG (new_mode,
1330 regno + subreg_regno_offset (regno, orig_mode,
1331 offset,
1332 new_mode));
1334 return NULL_RTX;
1337 /* Find the oldest copy of the value contained in REGNO that is in
1338 register class CL and has mode MODE. If found, return an rtx
1339 of that oldest register, otherwise return NULL. */
1341 static rtx
1342 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
1344 unsigned int regno = REGNO (reg);
1345 enum machine_mode mode = GET_MODE (reg);
1346 unsigned int i;
1348 /* If we are accessing REG in some mode other that what we set it in,
1349 make sure that the replacement is valid. In particular, consider
1350 (set (reg:DI r11) (...))
1351 (set (reg:SI r9) (reg:SI r11))
1352 (set (reg:SI r10) (...))
1353 (set (...) (reg:DI r9))
1354 Replacing r9 with r11 is invalid. */
1355 if (mode != vd->e[regno].mode)
1357 if (hard_regno_nregs[regno][mode]
1358 > hard_regno_nregs[regno][vd->e[regno].mode])
1359 return NULL_RTX;
1362 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
1364 enum machine_mode oldmode = vd->e[i].mode;
1365 rtx new;
1366 unsigned int last;
1368 for (last = i; last < i + hard_regno_nregs[i][mode]; last++)
1369 if (!TEST_HARD_REG_BIT (reg_class_contents[cl], last))
1370 return NULL_RTX;
1372 new = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
1373 if (new)
1375 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (reg);
1376 REG_ATTRS (new) = REG_ATTRS (reg);
1377 return new;
1381 return NULL_RTX;
1384 /* If possible, replace the register at *LOC with the oldest register
1385 in register class CL. Return true if successfully replaced. */
1387 static bool
1388 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx insn,
1389 struct value_data *vd)
1391 rtx new = find_oldest_value_reg (cl, *loc, vd);
1392 if (new)
1394 if (dump_file)
1395 fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
1396 INSN_UID (insn), REGNO (*loc), REGNO (new));
1398 *loc = new;
1399 return true;
1401 return false;
1404 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1405 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
1406 BASE_REG_CLASS depending on how the register is being considered. */
1408 static bool
1409 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
1410 enum machine_mode mode, rtx insn,
1411 struct value_data *vd)
1413 rtx x = *loc;
1414 RTX_CODE code = GET_CODE (x);
1415 const char *fmt;
1416 int i, j;
1417 bool changed = false;
1419 switch (code)
1421 case PLUS:
1423 rtx orig_op0 = XEXP (x, 0);
1424 rtx orig_op1 = XEXP (x, 1);
1425 RTX_CODE code0 = GET_CODE (orig_op0);
1426 RTX_CODE code1 = GET_CODE (orig_op1);
1427 rtx op0 = orig_op0;
1428 rtx op1 = orig_op1;
1429 rtx *locI = NULL;
1430 rtx *locB = NULL;
1431 rtx *locB_reg = NULL;
1433 if (GET_CODE (op0) == SUBREG)
1435 op0 = SUBREG_REG (op0);
1436 code0 = GET_CODE (op0);
1439 if (GET_CODE (op1) == SUBREG)
1441 op1 = SUBREG_REG (op1);
1442 code1 = GET_CODE (op1);
1445 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1446 || code0 == ZERO_EXTEND || code1 == MEM)
1448 locI = &XEXP (x, 0);
1449 locB = &XEXP (x, 1);
1451 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1452 || code1 == ZERO_EXTEND || code0 == MEM)
1454 locI = &XEXP (x, 1);
1455 locB = &XEXP (x, 0);
1457 else if (code0 == CONST_INT || code0 == CONST
1458 || code0 == SYMBOL_REF || code0 == LABEL_REF)
1459 locB = &XEXP (x, 1);
1460 else if (code1 == CONST_INT || code1 == CONST
1461 || code1 == SYMBOL_REF || code1 == LABEL_REF)
1462 locB = &XEXP (x, 0);
1463 else if (code0 == REG && code1 == REG)
1465 int index_op;
1467 if (REG_OK_FOR_INDEX_P (op0)
1468 && REG_MODE_OK_FOR_REG_BASE_P (op1, mode))
1469 index_op = 0;
1470 else if (REG_OK_FOR_INDEX_P (op1)
1471 && REG_MODE_OK_FOR_REG_BASE_P (op0, mode))
1472 index_op = 1;
1473 else if (REG_MODE_OK_FOR_REG_BASE_P (op1, mode))
1474 index_op = 0;
1475 else if (REG_MODE_OK_FOR_REG_BASE_P (op0, mode))
1476 index_op = 1;
1477 else if (REG_OK_FOR_INDEX_P (op1))
1478 index_op = 1;
1479 else
1480 index_op = 0;
1482 locI = &XEXP (x, index_op);
1483 locB_reg = &XEXP (x, !index_op);
1485 else if (code0 == REG)
1487 locI = &XEXP (x, 0);
1488 locB = &XEXP (x, 1);
1490 else if (code1 == REG)
1492 locI = &XEXP (x, 1);
1493 locB = &XEXP (x, 0);
1496 if (locI)
1497 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
1498 insn, vd);
1499 if (locB)
1500 changed |= replace_oldest_value_addr (locB,
1501 MODE_BASE_REG_CLASS (mode),
1502 mode, insn, vd);
1503 if (locB_reg)
1504 changed |= replace_oldest_value_addr (locB_reg,
1505 MODE_BASE_REG_REG_CLASS (mode),
1506 mode, insn, vd);
1507 return changed;
1510 case POST_INC:
1511 case POST_DEC:
1512 case POST_MODIFY:
1513 case PRE_INC:
1514 case PRE_DEC:
1515 case PRE_MODIFY:
1516 return false;
1518 case MEM:
1519 return replace_oldest_value_mem (x, insn, vd);
1521 case REG:
1522 return replace_oldest_value_reg (loc, cl, insn, vd);
1524 default:
1525 break;
1528 fmt = GET_RTX_FORMAT (code);
1529 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1531 if (fmt[i] == 'e')
1532 changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode,
1533 insn, vd);
1534 else if (fmt[i] == 'E')
1535 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1536 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
1537 mode, insn, vd);
1540 return changed;
1543 /* Similar to replace_oldest_value_reg, but X contains a memory. */
1545 static bool
1546 replace_oldest_value_mem (rtx x, rtx insn, struct value_data *vd)
1548 return replace_oldest_value_addr (&XEXP (x, 0),
1549 MODE_BASE_REG_CLASS (GET_MODE (x)),
1550 GET_MODE (x), insn, vd);
1553 /* Perform the forward copy propagation on basic block BB. */
1555 static bool
1556 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
1558 bool changed = false;
1559 rtx insn;
1561 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1563 int n_ops, i, alt, predicated;
1564 bool is_asm;
1565 rtx set;
1567 if (! INSN_P (insn))
1569 if (insn == BB_END (bb))
1570 break;
1571 else
1572 continue;
1575 set = single_set (insn);
1576 extract_insn (insn);
1577 if (! constrain_operands (1))
1578 fatal_insn_not_found (insn);
1579 preprocess_constraints ();
1580 alt = which_alternative;
1581 n_ops = recog_data.n_operands;
1582 is_asm = asm_noperands (PATTERN (insn)) >= 0;
1584 /* Simplify the code below by rewriting things to reflect
1585 matching constraints. Also promote OP_OUT to OP_INOUT
1586 in predicated instructions. */
1588 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1589 for (i = 0; i < n_ops; ++i)
1591 int matches = recog_op_alt[i][alt].matches;
1592 if (matches >= 0)
1593 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
1594 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1595 || (predicated && recog_data.operand_type[i] == OP_OUT))
1596 recog_data.operand_type[i] = OP_INOUT;
1599 /* For each earlyclobber operand, zap the value data. */
1600 for (i = 0; i < n_ops; i++)
1601 if (recog_op_alt[i][alt].earlyclobber)
1602 kill_value (recog_data.operand[i], vd);
1604 /* Within asms, a clobber cannot overlap inputs or outputs.
1605 I wouldn't think this were true for regular insns, but
1606 scan_rtx treats them like that... */
1607 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1609 /* Kill all auto-incremented values. */
1610 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
1611 for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
1613 /* Kill all early-clobbered operands. */
1614 for (i = 0; i < n_ops; i++)
1615 if (recog_op_alt[i][alt].earlyclobber)
1616 kill_value (recog_data.operand[i], vd);
1618 /* Special-case plain move instructions, since we may well
1619 be able to do the move from a different register class. */
1620 if (set && REG_P (SET_SRC (set)))
1622 rtx src = SET_SRC (set);
1623 unsigned int regno = REGNO (src);
1624 enum machine_mode mode = GET_MODE (src);
1625 unsigned int i;
1626 rtx new;
1628 /* If we are accessing SRC in some mode other that what we
1629 set it in, make sure that the replacement is valid. */
1630 if (mode != vd->e[regno].mode)
1632 if (hard_regno_nregs[regno][mode]
1633 > hard_regno_nregs[regno][vd->e[regno].mode])
1634 goto no_move_special_case;
1637 /* If the destination is also a register, try to find a source
1638 register in the same class. */
1639 if (REG_P (SET_DEST (set)))
1641 new = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
1642 if (new && validate_change (insn, &SET_SRC (set), new, 0))
1644 if (dump_file)
1645 fprintf (dump_file,
1646 "insn %u: replaced reg %u with %u\n",
1647 INSN_UID (insn), regno, REGNO (new));
1648 changed = true;
1649 goto did_replacement;
1653 /* Otherwise, try all valid registers and see if its valid. */
1654 for (i = vd->e[regno].oldest_regno; i != regno;
1655 i = vd->e[i].next_regno)
1657 new = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
1658 mode, i, regno);
1659 if (new != NULL_RTX)
1661 if (validate_change (insn, &SET_SRC (set), new, 0))
1663 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (src);
1664 REG_ATTRS (new) = REG_ATTRS (src);
1665 if (dump_file)
1666 fprintf (dump_file,
1667 "insn %u: replaced reg %u with %u\n",
1668 INSN_UID (insn), regno, REGNO (new));
1669 changed = true;
1670 goto did_replacement;
1675 no_move_special_case:
1677 /* For each input operand, replace a hard register with the
1678 eldest live copy that's in an appropriate register class. */
1679 for (i = 0; i < n_ops; i++)
1681 bool replaced = false;
1683 /* Don't scan match_operand here, since we've no reg class
1684 information to pass down. Any operands that we could
1685 substitute in will be represented elsewhere. */
1686 if (recog_data.constraints[i][0] == '\0')
1687 continue;
1689 /* Don't replace in asms intentionally referencing hard regs. */
1690 if (is_asm && REG_P (recog_data.operand[i])
1691 && (REGNO (recog_data.operand[i])
1692 == ORIGINAL_REGNO (recog_data.operand[i])))
1693 continue;
1695 if (recog_data.operand_type[i] == OP_IN)
1697 if (recog_op_alt[i][alt].is_address)
1698 replaced
1699 = replace_oldest_value_addr (recog_data.operand_loc[i],
1700 recog_op_alt[i][alt].cl,
1701 VOIDmode, insn, vd);
1702 else if (REG_P (recog_data.operand[i]))
1703 replaced
1704 = replace_oldest_value_reg (recog_data.operand_loc[i],
1705 recog_op_alt[i][alt].cl,
1706 insn, vd);
1707 else if (MEM_P (recog_data.operand[i]))
1708 replaced = replace_oldest_value_mem (recog_data.operand[i],
1709 insn, vd);
1711 else if (MEM_P (recog_data.operand[i]))
1712 replaced = replace_oldest_value_mem (recog_data.operand[i],
1713 insn, vd);
1715 /* If we performed any replacement, update match_dups. */
1716 if (replaced)
1718 int j;
1719 rtx new;
1721 changed = true;
1723 new = *recog_data.operand_loc[i];
1724 recog_data.operand[i] = new;
1725 for (j = 0; j < recog_data.n_dups; j++)
1726 if (recog_data.dup_num[j] == i)
1727 *recog_data.dup_loc[j] = new;
1731 did_replacement:
1732 /* Clobber call-clobbered registers. */
1733 if (CALL_P (insn))
1734 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1735 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1736 kill_value_regno (i, 1, vd);
1738 /* Notice stores. */
1739 note_stores (PATTERN (insn), kill_set_value, vd);
1741 /* Notice copies. */
1742 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1743 copy_value (SET_DEST (set), SET_SRC (set), vd);
1745 if (insn == BB_END (bb))
1746 break;
1749 return changed;
1752 /* Main entry point for the forward copy propagation optimization. */
1754 void
1755 copyprop_hardreg_forward (void)
1757 struct value_data *all_vd;
1758 bool need_refresh;
1759 basic_block bb;
1760 sbitmap visited;
1762 need_refresh = false;
1764 all_vd = xmalloc (sizeof (struct value_data) * last_basic_block);
1766 visited = sbitmap_alloc (last_basic_block - (INVALID_BLOCK + 1));
1767 sbitmap_zero (visited);
1769 FOR_EACH_BB (bb)
1771 SET_BIT (visited, bb->index - (INVALID_BLOCK + 1));
1773 /* If a block has a single predecessor, that we've already
1774 processed, begin with the value data that was live at
1775 the end of the predecessor block. */
1776 /* ??? Ought to use more intelligent queuing of blocks. */
1777 if (single_pred_p (bb)
1778 && TEST_BIT (visited,
1779 single_pred (bb)->index - (INVALID_BLOCK + 1))
1780 && ! (single_pred_edge (bb)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1781 all_vd[bb->index] = all_vd[single_pred (bb)->index];
1782 else
1783 init_value_data (all_vd + bb->index);
1785 if (copyprop_hardreg_forward_1 (bb, all_vd + bb->index))
1786 need_refresh = true;
1789 sbitmap_free (visited);
1791 if (need_refresh)
1793 if (dump_file)
1794 fputs ("\n\n", dump_file);
1796 /* ??? Irritatingly, delete_noop_moves does not take a set of blocks
1797 to scan, so we have to do a life update with no initial set of
1798 blocks Just In Case. */
1799 delete_noop_moves ();
1800 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
1801 PROP_DEATH_NOTES
1802 | PROP_SCAN_DEAD_CODE
1803 | PROP_KILL_DEAD_CODE);
1806 free (all_vd);
1809 /* Dump the value chain data to stderr. */
1811 void
1812 debug_value_data (struct value_data *vd)
1814 HARD_REG_SET set;
1815 unsigned int i, j;
1817 CLEAR_HARD_REG_SET (set);
1819 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1820 if (vd->e[i].oldest_regno == i)
1822 if (vd->e[i].mode == VOIDmode)
1824 if (vd->e[i].next_regno != INVALID_REGNUM)
1825 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1826 i, vd->e[i].next_regno);
1827 continue;
1830 SET_HARD_REG_BIT (set, i);
1831 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1833 for (j = vd->e[i].next_regno;
1834 j != INVALID_REGNUM;
1835 j = vd->e[j].next_regno)
1837 if (TEST_HARD_REG_BIT (set, j))
1839 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1840 return;
1843 if (vd->e[j].oldest_regno != i)
1845 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1846 j, vd->e[j].oldest_regno);
1847 return;
1849 SET_HARD_REG_BIT (set, j);
1850 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1852 fputc ('\n', stderr);
1855 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1856 if (! TEST_HARD_REG_BIT (set, i)
1857 && (vd->e[i].mode != VOIDmode
1858 || vd->e[i].oldest_regno != i
1859 || vd->e[i].next_regno != INVALID_REGNUM))
1860 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1861 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1862 vd->e[i].next_regno);
1865 #ifdef ENABLE_CHECKING
1866 static void
1867 validate_value_data (struct value_data *vd)
1869 HARD_REG_SET set;
1870 unsigned int i, j;
1872 CLEAR_HARD_REG_SET (set);
1874 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1875 if (vd->e[i].oldest_regno == i)
1877 if (vd->e[i].mode == VOIDmode)
1879 if (vd->e[i].next_regno != INVALID_REGNUM)
1880 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1881 i, vd->e[i].next_regno);
1882 continue;
1885 SET_HARD_REG_BIT (set, i);
1887 for (j = vd->e[i].next_regno;
1888 j != INVALID_REGNUM;
1889 j = vd->e[j].next_regno)
1891 if (TEST_HARD_REG_BIT (set, j))
1892 internal_error ("validate_value_data: Loop in regno chain (%u)",
1894 if (vd->e[j].oldest_regno != i)
1895 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1896 j, vd->e[j].oldest_regno);
1898 SET_HARD_REG_BIT (set, j);
1902 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1903 if (! TEST_HARD_REG_BIT (set, i)
1904 && (vd->e[i].mode != VOIDmode
1905 || vd->e[i].oldest_regno != i
1906 || vd->e[i].next_regno != INVALID_REGNUM))
1907 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1908 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1909 vd->e[i].next_regno);
1911 #endif
1913 static bool
1914 gate_handle_regrename (void)
1916 return (optimize > 0 && (flag_rename_registers || flag_cprop_registers));
1920 /* Run the regrename and cprop passes. */
1921 static void
1922 rest_of_handle_regrename (void)
1924 if (flag_rename_registers)
1925 regrename_optimize ();
1926 if (flag_cprop_registers)
1927 copyprop_hardreg_forward ();
1930 struct tree_opt_pass pass_regrename =
1932 "rnreg", /* name */
1933 gate_handle_regrename, /* gate */
1934 rest_of_handle_regrename, /* execute */
1935 NULL, /* sub */
1936 NULL, /* next */
1937 0, /* static_pass_number */
1938 TV_RENAME_REGISTERS, /* tv_id */
1939 0, /* properties_required */
1940 0, /* properties_provided */
1941 0, /* properties_destroyed */
1942 0, /* todo_flags_start */
1943 TODO_dump_func, /* todo_flags_finish */
1944 'n' /* letter */