PR target/16201
[official-gcc.git] / gcc / regrename.c
blob744f181bd18fd396a43ad71fcd3528e4ef895c9d
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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, 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"
40 struct du_chain
42 struct du_chain *next_chain;
43 struct du_chain *next_use;
45 rtx insn;
46 rtx *loc;
47 ENUM_BITFIELD(reg_class) cl : 16;
48 unsigned int need_caller_save_reg:1;
49 unsigned int earlyclobber:1;
52 enum scan_actions
54 terminate_all_read,
55 terminate_overlapping_read,
56 terminate_write,
57 terminate_dead,
58 mark_read,
59 mark_write
62 static const char * const scan_actions_name[] =
64 "terminate_all_read",
65 "terminate_overlapping_read",
66 "terminate_write",
67 "terminate_dead",
68 "mark_read",
69 "mark_write"
72 static struct obstack rename_obstack;
74 static void do_replace (struct du_chain *, int);
75 static void scan_rtx_reg (rtx, rtx *, enum reg_class,
76 enum scan_actions, enum op_type, int);
77 static void scan_rtx_address (rtx, rtx *, enum reg_class,
78 enum scan_actions, enum machine_mode);
79 static void scan_rtx (rtx, rtx *, enum reg_class, enum scan_actions,
80 enum op_type, int);
81 static struct du_chain *build_def_use (basic_block);
82 static void dump_def_use_chain (struct du_chain *);
83 static void note_sets (rtx, rtx, void *);
84 static void clear_dead_regs (HARD_REG_SET *, enum machine_mode, rtx);
85 static void merge_overlapping_regs (basic_block, HARD_REG_SET *,
86 struct du_chain *);
88 /* Called through note_stores from update_life. Find sets of registers, and
89 record them in *DATA (which is actually a HARD_REG_SET *). */
91 static void
92 note_sets (rtx x, rtx set ATTRIBUTE_UNUSED, void *data)
94 HARD_REG_SET *pset = (HARD_REG_SET *) data;
95 unsigned int regno;
96 int nregs;
98 if (GET_CODE (x) == SUBREG)
99 x = SUBREG_REG (x);
100 if (!REG_P (x))
101 return;
102 regno = REGNO (x);
103 nregs = hard_regno_nregs[regno][GET_MODE (x)];
105 /* There must not be pseudos at this point. */
106 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
108 while (nregs-- > 0)
109 SET_HARD_REG_BIT (*pset, regno + nregs);
112 /* Clear all registers from *PSET for which a note of kind KIND can be found
113 in the list NOTES. */
115 static void
116 clear_dead_regs (HARD_REG_SET *pset, enum machine_mode kind, rtx notes)
118 rtx note;
119 for (note = notes; note; note = XEXP (note, 1))
120 if (REG_NOTE_KIND (note) == kind && REG_P (XEXP (note, 0)))
122 rtx reg = XEXP (note, 0);
123 unsigned int regno = REGNO (reg);
124 int nregs = hard_regno_nregs[regno][GET_MODE (reg)];
126 /* There must not be pseudos at this point. */
127 gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
129 while (nregs-- > 0)
130 CLEAR_HARD_REG_BIT (*pset, regno + nregs);
134 /* For a def-use chain CHAIN in basic block B, find which registers overlap
135 its lifetime and set the corresponding bits in *PSET. */
137 static void
138 merge_overlapping_regs (basic_block b, HARD_REG_SET *pset,
139 struct du_chain *chain)
141 struct du_chain *t = chain;
142 rtx insn;
143 HARD_REG_SET live;
145 REG_SET_TO_HARD_REG_SET (live, b->global_live_at_start);
146 insn = BB_HEAD (b);
147 while (t)
149 /* Search forward until the next reference to the register to be
150 renamed. */
151 while (insn != t->insn)
153 if (INSN_P (insn))
155 clear_dead_regs (&live, REG_DEAD, REG_NOTES (insn));
156 note_stores (PATTERN (insn), note_sets, (void *) &live);
157 /* Only record currently live regs if we are inside the
158 reg's live range. */
159 if (t != chain)
160 IOR_HARD_REG_SET (*pset, live);
161 clear_dead_regs (&live, REG_UNUSED, REG_NOTES (insn));
163 insn = NEXT_INSN (insn);
166 IOR_HARD_REG_SET (*pset, live);
168 /* For the last reference, also merge in all registers set in the
169 same insn.
170 @@@ We only have take earlyclobbered sets into account. */
171 if (! t->next_use)
172 note_stores (PATTERN (insn), note_sets, (void *) pset);
174 t = t->next_use;
178 /* Perform register renaming on the current function. */
180 void
181 regrename_optimize (void)
183 int tick[FIRST_PSEUDO_REGISTER];
184 int this_tick = 0;
185 basic_block bb;
186 char *first_obj;
188 memset (tick, 0, sizeof tick);
190 gcc_obstack_init (&rename_obstack);
191 first_obj = obstack_alloc (&rename_obstack, 0);
193 FOR_EACH_BB (bb)
195 struct du_chain *all_chains = 0;
196 HARD_REG_SET unavailable;
197 HARD_REG_SET regs_seen;
199 CLEAR_HARD_REG_SET (unavailable);
201 if (dump_file)
202 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
204 all_chains = build_def_use (bb);
206 if (dump_file)
207 dump_def_use_chain (all_chains);
209 CLEAR_HARD_REG_SET (unavailable);
210 /* Don't clobber traceback for noreturn functions. */
211 if (frame_pointer_needed)
213 int i;
215 for (i = hard_regno_nregs[FRAME_POINTER_REGNUM][Pmode]; i--;)
216 SET_HARD_REG_BIT (unavailable, FRAME_POINTER_REGNUM + i);
218 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
219 for (i = hard_regno_nregs[HARD_FRAME_POINTER_REGNUM][Pmode]; i--;)
220 SET_HARD_REG_BIT (unavailable, HARD_FRAME_POINTER_REGNUM + i);
221 #endif
224 CLEAR_HARD_REG_SET (regs_seen);
225 while (all_chains)
227 int new_reg, best_new_reg;
228 int n_uses;
229 struct du_chain *this = all_chains;
230 struct du_chain *tmp, *last;
231 HARD_REG_SET this_unavailable;
232 int reg = REGNO (*this->loc);
233 int i;
235 all_chains = this->next_chain;
237 best_new_reg = reg;
239 #if 0 /* This just disables optimization opportunities. */
240 /* Only rename once we've seen the reg more than once. */
241 if (! TEST_HARD_REG_BIT (regs_seen, reg))
243 SET_HARD_REG_BIT (regs_seen, reg);
244 continue;
246 #endif
248 if (fixed_regs[reg] || global_regs[reg]
249 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
250 || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
251 #else
252 || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
253 #endif
255 continue;
257 COPY_HARD_REG_SET (this_unavailable, unavailable);
259 /* Find last entry on chain (which has the need_caller_save bit),
260 count number of uses, and narrow the set of registers we can
261 use for renaming. */
262 n_uses = 0;
263 for (last = this; last->next_use; last = last->next_use)
265 n_uses++;
266 IOR_COMPL_HARD_REG_SET (this_unavailable,
267 reg_class_contents[last->cl]);
269 if (n_uses < 1)
270 continue;
272 IOR_COMPL_HARD_REG_SET (this_unavailable,
273 reg_class_contents[last->cl]);
275 if (this->need_caller_save_reg)
276 IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
278 merge_overlapping_regs (bb, &this_unavailable, this);
280 /* Now potential_regs is a reasonable approximation, let's
281 have a closer look at each register still in there. */
282 for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
284 int nregs = hard_regno_nregs[new_reg][GET_MODE (*this->loc)];
286 for (i = nregs - 1; i >= 0; --i)
287 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
288 || fixed_regs[new_reg + i]
289 || global_regs[new_reg + i]
290 /* Can't use regs which aren't saved by the prologue. */
291 || (! regs_ever_live[new_reg + i]
292 && ! call_used_regs[new_reg + i])
293 #ifdef LEAF_REGISTERS
294 /* We can't use a non-leaf register if we're in a
295 leaf function. */
296 || (current_function_is_leaf
297 && !LEAF_REGISTERS[new_reg + i])
298 #endif
299 #ifdef HARD_REGNO_RENAME_OK
300 || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
301 #endif
303 break;
304 if (i >= 0)
305 continue;
307 /* See whether it accepts all modes that occur in
308 definition and uses. */
309 for (tmp = this; tmp; tmp = tmp->next_use)
310 if (! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
311 || (tmp->need_caller_save_reg
312 && ! (HARD_REGNO_CALL_PART_CLOBBERED
313 (reg, GET_MODE (*tmp->loc)))
314 && (HARD_REGNO_CALL_PART_CLOBBERED
315 (new_reg, GET_MODE (*tmp->loc)))))
316 break;
317 if (! tmp)
319 if (tick[best_new_reg] > tick[new_reg])
320 best_new_reg = new_reg;
324 if (dump_file)
326 fprintf (dump_file, "Register %s in insn %d",
327 reg_names[reg], INSN_UID (last->insn));
328 if (last->need_caller_save_reg)
329 fprintf (dump_file, " crosses a call");
332 if (best_new_reg == reg)
334 tick[reg] = ++this_tick;
335 if (dump_file)
336 fprintf (dump_file, "; no available better choice\n");
337 continue;
340 do_replace (this, best_new_reg);
341 tick[best_new_reg] = ++this_tick;
342 regs_ever_live[best_new_reg] = 1;
344 if (dump_file)
345 fprintf (dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
348 obstack_free (&rename_obstack, first_obj);
351 obstack_free (&rename_obstack, NULL);
353 if (dump_file)
354 fputc ('\n', dump_file);
356 count_or_remove_death_notes (NULL, 1);
357 update_life_info (NULL, UPDATE_LIFE_LOCAL,
358 PROP_DEATH_NOTES);
361 static void
362 do_replace (struct du_chain *chain, int reg)
364 while (chain)
366 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
367 struct reg_attrs * attr = REG_ATTRS (*chain->loc);
369 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
370 if (regno >= FIRST_PSEUDO_REGISTER)
371 ORIGINAL_REGNO (*chain->loc) = regno;
372 REG_ATTRS (*chain->loc) = attr;
373 chain = chain->next_use;
378 static struct du_chain *open_chains;
379 static struct du_chain *closed_chains;
381 static void
382 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class cl,
383 enum scan_actions action, enum op_type type, int earlyclobber)
385 struct du_chain **p;
386 rtx x = *loc;
387 enum machine_mode mode = GET_MODE (x);
388 int this_regno = REGNO (x);
389 int this_nregs = hard_regno_nregs[this_regno][mode];
391 if (action == mark_write)
393 if (type == OP_OUT)
395 struct du_chain *this
396 = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
397 this->next_use = 0;
398 this->next_chain = open_chains;
399 this->loc = loc;
400 this->insn = insn;
401 this->cl = cl;
402 this->need_caller_save_reg = 0;
403 this->earlyclobber = earlyclobber;
404 open_chains = this;
406 return;
409 if ((type == OP_OUT && action != terminate_write)
410 || (type != OP_OUT && action == terminate_write))
411 return;
413 for (p = &open_chains; *p;)
415 struct du_chain *this = *p;
417 /* Check if the chain has been terminated if it has then skip to
418 the next chain.
420 This can happen when we've already appended the location to
421 the chain in Step 3, but are trying to hide in-out operands
422 from terminate_write in Step 5. */
424 if (*this->loc == cc0_rtx)
425 p = &this->next_chain;
426 else
428 int regno = REGNO (*this->loc);
429 int nregs = hard_regno_nregs[regno][GET_MODE (*this->loc)];
430 int exact_match = (regno == this_regno && nregs == this_nregs);
432 if (regno + nregs <= this_regno
433 || this_regno + this_nregs <= regno)
435 p = &this->next_chain;
436 continue;
439 if (action == mark_read)
441 gcc_assert (exact_match);
443 /* ??? Class NO_REGS can happen if the md file makes use of
444 EXTRA_CONSTRAINTS to match registers. Which is arguably
445 wrong, but there we are. Since we know not what this may
446 be replaced with, terminate the chain. */
447 if (cl != NO_REGS)
449 this = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
450 this->next_use = 0;
451 this->next_chain = (*p)->next_chain;
452 this->loc = loc;
453 this->insn = insn;
454 this->cl = cl;
455 this->need_caller_save_reg = 0;
456 while (*p)
457 p = &(*p)->next_use;
458 *p = this;
459 return;
463 if (action != terminate_overlapping_read || ! exact_match)
465 struct du_chain *next = this->next_chain;
467 /* Whether the terminated chain can be used for renaming
468 depends on the action and this being an exact match.
469 In either case, we remove this element from open_chains. */
471 if ((action == terminate_dead || action == terminate_write)
472 && exact_match)
474 this->next_chain = closed_chains;
475 closed_chains = this;
476 if (dump_file)
477 fprintf (dump_file,
478 "Closing chain %s at insn %d (%s)\n",
479 reg_names[REGNO (*this->loc)], INSN_UID (insn),
480 scan_actions_name[(int) action]);
482 else
484 if (dump_file)
485 fprintf (dump_file,
486 "Discarding chain %s at insn %d (%s)\n",
487 reg_names[REGNO (*this->loc)], INSN_UID (insn),
488 scan_actions_name[(int) action]);
490 *p = next;
492 else
493 p = &this->next_chain;
498 /* Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
499 BASE_REG_CLASS depending on how the register is being considered. */
501 static void
502 scan_rtx_address (rtx insn, rtx *loc, enum reg_class cl,
503 enum scan_actions action, enum machine_mode mode)
505 rtx x = *loc;
506 RTX_CODE code = GET_CODE (x);
507 const char *fmt;
508 int i, j;
510 if (action == mark_write)
511 return;
513 switch (code)
515 case PLUS:
517 rtx orig_op0 = XEXP (x, 0);
518 rtx orig_op1 = XEXP (x, 1);
519 RTX_CODE code0 = GET_CODE (orig_op0);
520 RTX_CODE code1 = GET_CODE (orig_op1);
521 rtx op0 = orig_op0;
522 rtx op1 = orig_op1;
523 rtx *locI = NULL;
524 rtx *locB = NULL;
525 rtx *locB_reg = NULL;
527 if (GET_CODE (op0) == SUBREG)
529 op0 = SUBREG_REG (op0);
530 code0 = GET_CODE (op0);
533 if (GET_CODE (op1) == SUBREG)
535 op1 = SUBREG_REG (op1);
536 code1 = GET_CODE (op1);
539 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
540 || code0 == ZERO_EXTEND || code1 == MEM)
542 locI = &XEXP (x, 0);
543 locB = &XEXP (x, 1);
545 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
546 || code1 == ZERO_EXTEND || code0 == MEM)
548 locI = &XEXP (x, 1);
549 locB = &XEXP (x, 0);
551 else if (code0 == CONST_INT || code0 == CONST
552 || code0 == SYMBOL_REF || code0 == LABEL_REF)
553 locB = &XEXP (x, 1);
554 else if (code1 == CONST_INT || code1 == CONST
555 || code1 == SYMBOL_REF || code1 == LABEL_REF)
556 locB = &XEXP (x, 0);
557 else if (code0 == REG && code1 == REG)
559 int index_op;
561 if (REG_OK_FOR_INDEX_P (op0)
562 && REG_MODE_OK_FOR_REG_BASE_P (op1, mode))
563 index_op = 0;
564 else if (REG_OK_FOR_INDEX_P (op1)
565 && REG_MODE_OK_FOR_REG_BASE_P (op0, mode))
566 index_op = 1;
567 else if (REG_MODE_OK_FOR_REG_BASE_P (op1, mode))
568 index_op = 0;
569 else if (REG_MODE_OK_FOR_REG_BASE_P (op0, mode))
570 index_op = 1;
571 else if (REG_OK_FOR_INDEX_P (op1))
572 index_op = 1;
573 else
574 index_op = 0;
576 locI = &XEXP (x, index_op);
577 locB_reg = &XEXP (x, !index_op);
579 else if (code0 == REG)
581 locI = &XEXP (x, 0);
582 locB = &XEXP (x, 1);
584 else if (code1 == REG)
586 locI = &XEXP (x, 1);
587 locB = &XEXP (x, 0);
590 if (locI)
591 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
592 if (locB)
593 scan_rtx_address (insn, locB, MODE_BASE_REG_CLASS (mode), action, mode);
594 if (locB_reg)
595 scan_rtx_address (insn, locB_reg, MODE_BASE_REG_REG_CLASS (mode),
596 action, mode);
597 return;
600 case POST_INC:
601 case POST_DEC:
602 case POST_MODIFY:
603 case PRE_INC:
604 case PRE_DEC:
605 case PRE_MODIFY:
606 #ifndef AUTO_INC_DEC
607 /* If the target doesn't claim to handle autoinc, this must be
608 something special, like a stack push. Kill this chain. */
609 action = terminate_all_read;
610 #endif
611 break;
613 case MEM:
614 scan_rtx_address (insn, &XEXP (x, 0),
615 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
616 GET_MODE (x));
617 return;
619 case REG:
620 scan_rtx_reg (insn, loc, cl, action, OP_IN, 0);
621 return;
623 default:
624 break;
627 fmt = GET_RTX_FORMAT (code);
628 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
630 if (fmt[i] == 'e')
631 scan_rtx_address (insn, &XEXP (x, i), cl, action, mode);
632 else if (fmt[i] == 'E')
633 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
634 scan_rtx_address (insn, &XVECEXP (x, i, j), cl, action, mode);
638 static void
639 scan_rtx (rtx insn, rtx *loc, enum reg_class cl,
640 enum scan_actions action, enum op_type type, int earlyclobber)
642 const char *fmt;
643 rtx x = *loc;
644 enum rtx_code code = GET_CODE (x);
645 int i, j;
647 code = GET_CODE (x);
648 switch (code)
650 case CONST:
651 case CONST_INT:
652 case CONST_DOUBLE:
653 case CONST_VECTOR:
654 case SYMBOL_REF:
655 case LABEL_REF:
656 case CC0:
657 case PC:
658 return;
660 case REG:
661 scan_rtx_reg (insn, loc, cl, action, type, earlyclobber);
662 return;
664 case MEM:
665 scan_rtx_address (insn, &XEXP (x, 0),
666 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
667 GET_MODE (x));
668 return;
670 case SET:
671 scan_rtx (insn, &SET_SRC (x), cl, action, OP_IN, 0);
672 scan_rtx (insn, &SET_DEST (x), cl, action,
673 GET_CODE (PATTERN (insn)) == COND_EXEC ? OP_INOUT : OP_OUT, 0);
674 return;
676 case STRICT_LOW_PART:
677 scan_rtx (insn, &XEXP (x, 0), cl, action, OP_INOUT, earlyclobber);
678 return;
680 case ZERO_EXTRACT:
681 case SIGN_EXTRACT:
682 scan_rtx (insn, &XEXP (x, 0), cl, action,
683 type == OP_IN ? OP_IN : OP_INOUT, earlyclobber);
684 scan_rtx (insn, &XEXP (x, 1), cl, action, OP_IN, 0);
685 scan_rtx (insn, &XEXP (x, 2), cl, action, OP_IN, 0);
686 return;
688 case POST_INC:
689 case PRE_INC:
690 case POST_DEC:
691 case PRE_DEC:
692 case POST_MODIFY:
693 case PRE_MODIFY:
694 /* Should only happen inside MEM. */
695 gcc_unreachable ();
697 case CLOBBER:
698 scan_rtx (insn, &SET_DEST (x), cl, action,
699 GET_CODE (PATTERN (insn)) == COND_EXEC ? OP_INOUT : OP_OUT, 0);
700 return;
702 case EXPR_LIST:
703 scan_rtx (insn, &XEXP (x, 0), cl, action, type, 0);
704 if (XEXP (x, 1))
705 scan_rtx (insn, &XEXP (x, 1), cl, action, type, 0);
706 return;
708 default:
709 break;
712 fmt = GET_RTX_FORMAT (code);
713 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
715 if (fmt[i] == 'e')
716 scan_rtx (insn, &XEXP (x, i), cl, action, type, 0);
717 else if (fmt[i] == 'E')
718 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
719 scan_rtx (insn, &XVECEXP (x, i, j), cl, action, type, 0);
723 /* Build def/use chain. */
725 static struct du_chain *
726 build_def_use (basic_block bb)
728 rtx insn;
730 open_chains = closed_chains = NULL;
732 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
734 if (INSN_P (insn))
736 int n_ops;
737 rtx note;
738 rtx old_operands[MAX_RECOG_OPERANDS];
739 rtx old_dups[MAX_DUP_OPERANDS];
740 int i, icode;
741 int alt;
742 int predicated;
744 /* Process the insn, determining its effect on the def-use
745 chains. We perform the following steps with the register
746 references in the insn:
747 (1) Any read that overlaps an open chain, but doesn't exactly
748 match, causes that chain to be closed. We can't deal
749 with overlaps yet.
750 (2) Any read outside an operand causes any chain it overlaps
751 with to be closed, since we can't replace it.
752 (3) Any read inside an operand is added if there's already
753 an open chain for it.
754 (4) For any REG_DEAD note we find, close open chains that
755 overlap it.
756 (5) For any write we find, close open chains that overlap it.
757 (6) For any write we find in an operand, make a new chain.
758 (7) For any REG_UNUSED, close any chains we just opened. */
760 icode = recog_memoized (insn);
761 extract_insn (insn);
762 if (! constrain_operands (1))
763 fatal_insn_not_found (insn);
764 preprocess_constraints ();
765 alt = which_alternative;
766 n_ops = recog_data.n_operands;
768 /* Simplify the code below by rewriting things to reflect
769 matching constraints. Also promote OP_OUT to OP_INOUT
770 in predicated instructions. */
772 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
773 for (i = 0; i < n_ops; ++i)
775 int matches = recog_op_alt[i][alt].matches;
776 if (matches >= 0)
777 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
778 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
779 || (predicated && recog_data.operand_type[i] == OP_OUT))
780 recog_data.operand_type[i] = OP_INOUT;
783 /* Step 1: Close chains for which we have overlapping reads. */
784 for (i = 0; i < n_ops; i++)
785 scan_rtx (insn, recog_data.operand_loc[i],
786 NO_REGS, terminate_overlapping_read,
787 recog_data.operand_type[i], 0);
789 /* Step 2: Close chains for which we have reads outside operands.
790 We do this by munging all operands into CC0, and closing
791 everything remaining. */
793 for (i = 0; i < n_ops; i++)
795 old_operands[i] = recog_data.operand[i];
796 /* Don't squash match_operator or match_parallel here, since
797 we don't know that all of the contained registers are
798 reachable by proper operands. */
799 if (recog_data.constraints[i][0] == '\0')
800 continue;
801 *recog_data.operand_loc[i] = cc0_rtx;
803 for (i = 0; i < recog_data.n_dups; i++)
805 int dup_num = recog_data.dup_num[i];
807 old_dups[i] = *recog_data.dup_loc[i];
808 *recog_data.dup_loc[i] = cc0_rtx;
810 /* For match_dup of match_operator or match_parallel, share
811 them, so that we don't miss changes in the dup. */
812 if (icode >= 0
813 && insn_data[icode].operand[dup_num].eliminable == 0)
814 old_dups[i] = recog_data.operand[dup_num];
817 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
818 OP_IN, 0);
820 for (i = 0; i < recog_data.n_dups; i++)
821 *recog_data.dup_loc[i] = old_dups[i];
822 for (i = 0; i < n_ops; i++)
823 *recog_data.operand_loc[i] = old_operands[i];
825 /* Step 2B: Can't rename function call argument registers. */
826 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
827 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
828 NO_REGS, terminate_all_read, OP_IN, 0);
830 /* Step 2C: Can't rename asm operands that were originally
831 hard registers. */
832 if (asm_noperands (PATTERN (insn)) > 0)
833 for (i = 0; i < n_ops; i++)
835 rtx *loc = recog_data.operand_loc[i];
836 rtx op = *loc;
838 if (REG_P (op)
839 && REGNO (op) == ORIGINAL_REGNO (op)
840 && (recog_data.operand_type[i] == OP_IN
841 || recog_data.operand_type[i] == OP_INOUT))
842 scan_rtx (insn, loc, NO_REGS, terminate_all_read, OP_IN, 0);
845 /* Step 3: Append to chains for reads inside operands. */
846 for (i = 0; i < n_ops + recog_data.n_dups; i++)
848 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
849 rtx *loc = (i < n_ops
850 ? recog_data.operand_loc[opn]
851 : recog_data.dup_loc[i - n_ops]);
852 enum reg_class cl = recog_op_alt[opn][alt].cl;
853 enum op_type type = recog_data.operand_type[opn];
855 /* Don't scan match_operand here, since we've no reg class
856 information to pass down. Any operands that we could
857 substitute in will be represented elsewhere. */
858 if (recog_data.constraints[opn][0] == '\0')
859 continue;
861 if (recog_op_alt[opn][alt].is_address)
862 scan_rtx_address (insn, loc, cl, mark_read, VOIDmode);
863 else
864 scan_rtx (insn, loc, cl, mark_read, type, 0);
867 /* Step 4: Close chains for registers that die here.
868 Also record updates for REG_INC notes. */
869 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
871 if (REG_NOTE_KIND (note) == REG_DEAD)
872 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
873 OP_IN, 0);
874 else if (REG_NOTE_KIND (note) == REG_INC)
875 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
876 OP_INOUT, 0);
879 /* Step 4B: If this is a call, any chain live at this point
880 requires a caller-saved reg. */
881 if (CALL_P (insn))
883 struct du_chain *p;
884 for (p = open_chains; p; p = p->next_chain)
885 p->need_caller_save_reg = 1;
888 /* Step 5: Close open chains that overlap writes. Similar to
889 step 2, we hide in-out operands, since we do not want to
890 close these chains. */
892 for (i = 0; i < n_ops; i++)
894 old_operands[i] = recog_data.operand[i];
895 if (recog_data.operand_type[i] == OP_INOUT)
896 *recog_data.operand_loc[i] = cc0_rtx;
898 for (i = 0; i < recog_data.n_dups; i++)
900 int opn = recog_data.dup_num[i];
901 old_dups[i] = *recog_data.dup_loc[i];
902 if (recog_data.operand_type[opn] == OP_INOUT)
903 *recog_data.dup_loc[i] = cc0_rtx;
906 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
908 for (i = 0; i < recog_data.n_dups; i++)
909 *recog_data.dup_loc[i] = old_dups[i];
910 for (i = 0; i < n_ops; i++)
911 *recog_data.operand_loc[i] = old_operands[i];
913 /* Step 6: Begin new chains for writes inside operands. */
914 /* ??? Many targets have output constraints on the SET_DEST
915 of a call insn, which is stupid, since these are certainly
916 ABI defined hard registers. Don't change calls at all.
917 Similarly take special care for asm statement that originally
918 referenced hard registers. */
919 if (asm_noperands (PATTERN (insn)) > 0)
921 for (i = 0; i < n_ops; i++)
922 if (recog_data.operand_type[i] == OP_OUT)
924 rtx *loc = recog_data.operand_loc[i];
925 rtx op = *loc;
926 enum reg_class cl = recog_op_alt[i][alt].cl;
928 if (REG_P (op)
929 && REGNO (op) == ORIGINAL_REGNO (op))
930 continue;
932 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
933 recog_op_alt[i][alt].earlyclobber);
936 else if (!CALL_P (insn))
937 for (i = 0; i < n_ops + recog_data.n_dups; i++)
939 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
940 rtx *loc = (i < n_ops
941 ? recog_data.operand_loc[opn]
942 : recog_data.dup_loc[i - n_ops]);
943 enum reg_class cl = recog_op_alt[opn][alt].cl;
945 if (recog_data.operand_type[opn] == OP_OUT)
946 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
947 recog_op_alt[opn][alt].earlyclobber);
950 /* Step 7: Close chains for registers that were never
951 really used here. */
952 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
953 if (REG_NOTE_KIND (note) == REG_UNUSED)
954 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
955 OP_IN, 0);
957 if (insn == BB_END (bb))
958 break;
961 /* Since we close every chain when we find a REG_DEAD note, anything that
962 is still open lives past the basic block, so it can't be renamed. */
963 return closed_chains;
966 /* Dump all def/use chains in CHAINS to DUMP_FILE. They are
967 printed in reverse order as that's how we build them. */
969 static void
970 dump_def_use_chain (struct du_chain *chains)
972 while (chains)
974 struct du_chain *this = chains;
975 int r = REGNO (*this->loc);
976 int nregs = hard_regno_nregs[r][GET_MODE (*this->loc)];
977 fprintf (dump_file, "Register %s (%d):", reg_names[r], nregs);
978 while (this)
980 fprintf (dump_file, " %d [%s]", INSN_UID (this->insn),
981 reg_class_names[this->cl]);
982 this = this->next_use;
984 fprintf (dump_file, "\n");
985 chains = chains->next_chain;
989 /* The following code does forward propagation of hard register copies.
990 The object is to eliminate as many dependencies as possible, so that
991 we have the most scheduling freedom. As a side effect, we also clean
992 up some silly register allocation decisions made by reload. This
993 code may be obsoleted by a new register allocator. */
995 /* For each register, we have a list of registers that contain the same
996 value. The OLDEST_REGNO field points to the head of the list, and
997 the NEXT_REGNO field runs through the list. The MODE field indicates
998 what mode the data is known to be in; this field is VOIDmode when the
999 register is not known to contain valid data. */
1001 struct value_data_entry
1003 enum machine_mode mode;
1004 unsigned int oldest_regno;
1005 unsigned int next_regno;
1008 struct value_data
1010 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
1011 unsigned int max_value_regs;
1014 static void kill_value_one_regno (unsigned, struct value_data *);
1015 static void kill_value_regno (unsigned, unsigned, struct value_data *);
1016 static void kill_value (rtx, struct value_data *);
1017 static void set_value_regno (unsigned, enum machine_mode, struct value_data *);
1018 static void init_value_data (struct value_data *);
1019 static void kill_clobbered_value (rtx, rtx, void *);
1020 static void kill_set_value (rtx, rtx, void *);
1021 static int kill_autoinc_value (rtx *, void *);
1022 static void copy_value (rtx, rtx, struct value_data *);
1023 static bool mode_change_ok (enum machine_mode, enum machine_mode,
1024 unsigned int);
1025 static rtx maybe_mode_change (enum machine_mode, enum machine_mode,
1026 enum machine_mode, unsigned int, unsigned int);
1027 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
1028 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx,
1029 struct value_data *);
1030 static bool replace_oldest_value_addr (rtx *, enum reg_class,
1031 enum machine_mode, rtx,
1032 struct value_data *);
1033 static bool replace_oldest_value_mem (rtx, rtx, struct value_data *);
1034 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
1035 extern void debug_value_data (struct value_data *);
1036 #ifdef ENABLE_CHECKING
1037 static void validate_value_data (struct value_data *);
1038 #endif
1040 /* Kill register REGNO. This involves removing it from any value
1041 lists, and resetting the value mode to VOIDmode. This is only a
1042 helper function; it does not handle any hard registers overlapping
1043 with REGNO. */
1045 static void
1046 kill_value_one_regno (unsigned int regno, struct value_data *vd)
1048 unsigned int i, next;
1050 if (vd->e[regno].oldest_regno != regno)
1052 for (i = vd->e[regno].oldest_regno;
1053 vd->e[i].next_regno != regno;
1054 i = vd->e[i].next_regno)
1055 continue;
1056 vd->e[i].next_regno = vd->e[regno].next_regno;
1058 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
1060 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
1061 vd->e[i].oldest_regno = next;
1064 vd->e[regno].mode = VOIDmode;
1065 vd->e[regno].oldest_regno = regno;
1066 vd->e[regno].next_regno = INVALID_REGNUM;
1068 #ifdef ENABLE_CHECKING
1069 validate_value_data (vd);
1070 #endif
1073 /* Kill the value in register REGNO for NREGS, and any other registers
1074 whose values overlap. */
1076 static void
1077 kill_value_regno (unsigned int regno, unsigned int nregs,
1078 struct value_data *vd)
1080 unsigned int j;
1082 /* Kill the value we're told to kill. */
1083 for (j = 0; j < nregs; ++j)
1084 kill_value_one_regno (regno + j, vd);
1086 /* Kill everything that overlapped what we're told to kill. */
1087 if (regno < vd->max_value_regs)
1088 j = 0;
1089 else
1090 j = regno - vd->max_value_regs;
1091 for (; j < regno; ++j)
1093 unsigned int i, n;
1094 if (vd->e[j].mode == VOIDmode)
1095 continue;
1096 n = hard_regno_nregs[j][vd->e[j].mode];
1097 if (j + n > regno)
1098 for (i = 0; i < n; ++i)
1099 kill_value_one_regno (j + i, vd);
1103 /* Kill X. This is a convenience function wrapping kill_value_regno
1104 so that we mind the mode the register is in. */
1106 static void
1107 kill_value (rtx x, struct value_data *vd)
1109 rtx orig_rtx = x;
1111 if (GET_CODE (x) == SUBREG)
1113 x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
1114 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
1115 if (x == NULL_RTX)
1116 x = SUBREG_REG (orig_rtx);
1118 if (REG_P (x))
1120 unsigned int regno = REGNO (x);
1121 unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
1123 kill_value_regno (regno, n, vd);
1127 /* Remember that REGNO is valid in MODE. */
1129 static void
1130 set_value_regno (unsigned int regno, enum machine_mode mode,
1131 struct value_data *vd)
1133 unsigned int nregs;
1135 vd->e[regno].mode = mode;
1137 nregs = hard_regno_nregs[regno][mode];
1138 if (nregs > vd->max_value_regs)
1139 vd->max_value_regs = nregs;
1142 /* Initialize VD such that there are no known relationships between regs. */
1144 static void
1145 init_value_data (struct value_data *vd)
1147 int i;
1148 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1150 vd->e[i].mode = VOIDmode;
1151 vd->e[i].oldest_regno = i;
1152 vd->e[i].next_regno = INVALID_REGNUM;
1154 vd->max_value_regs = 0;
1157 /* Called through note_stores. If X is clobbered, kill its value. */
1159 static void
1160 kill_clobbered_value (rtx x, rtx set, void *data)
1162 struct value_data *vd = data;
1163 if (GET_CODE (set) == CLOBBER)
1164 kill_value (x, vd);
1167 /* Called through note_stores. If X is set, not clobbered, kill its
1168 current value and install it as the root of its own value list. */
1170 static void
1171 kill_set_value (rtx x, rtx set, void *data)
1173 struct value_data *vd = data;
1174 if (GET_CODE (set) != CLOBBER)
1176 kill_value (x, vd);
1177 if (REG_P (x))
1178 set_value_regno (REGNO (x), GET_MODE (x), vd);
1182 /* Called through for_each_rtx. Kill any register used as the base of an
1183 auto-increment expression, and install that register as the root of its
1184 own value list. */
1186 static int
1187 kill_autoinc_value (rtx *px, void *data)
1189 rtx x = *px;
1190 struct value_data *vd = data;
1192 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
1194 x = XEXP (x, 0);
1195 kill_value (x, vd);
1196 set_value_regno (REGNO (x), Pmode, vd);
1197 return -1;
1200 return 0;
1203 /* Assert that SRC has been copied to DEST. Adjust the data structures
1204 to reflect that SRC contains an older copy of the shared value. */
1206 static void
1207 copy_value (rtx dest, rtx src, struct value_data *vd)
1209 unsigned int dr = REGNO (dest);
1210 unsigned int sr = REGNO (src);
1211 unsigned int dn, sn;
1212 unsigned int i;
1214 /* ??? At present, it's possible to see noop sets. It'd be nice if
1215 this were cleaned up beforehand... */
1216 if (sr == dr)
1217 return;
1219 /* Do not propagate copies to the stack pointer, as that can leave
1220 memory accesses with no scheduling dependency on the stack update. */
1221 if (dr == STACK_POINTER_REGNUM)
1222 return;
1224 /* Likewise with the frame pointer, if we're using one. */
1225 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
1226 return;
1228 /* If SRC and DEST overlap, don't record anything. */
1229 dn = hard_regno_nregs[dr][GET_MODE (dest)];
1230 sn = hard_regno_nregs[sr][GET_MODE (dest)];
1231 if ((dr > sr && dr < sr + sn)
1232 || (sr > dr && sr < dr + dn))
1233 return;
1235 /* If SRC had no assigned mode (i.e. we didn't know it was live)
1236 assign it now and assume the value came from an input argument
1237 or somesuch. */
1238 if (vd->e[sr].mode == VOIDmode)
1239 set_value_regno (sr, vd->e[dr].mode, vd);
1241 /* If we are narrowing the input to a smaller number of hard regs,
1242 and it is in big endian, we are really extracting a high part.
1243 Since we generally associate a low part of a value with the value itself,
1244 we must not do the same for the high part.
1245 Note we can still get low parts for the same mode combination through
1246 a two-step copy involving differently sized hard regs.
1247 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
1248 (set (reg:DI r0) (reg:DI fr0))
1249 (set (reg:SI fr2) (reg:SI r0))
1250 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
1251 (set (reg:SI fr2) (reg:SI fr0))
1252 loads the high part of (reg:DI fr0) into fr2.
1254 We can't properly represent the latter case in our tables, so don't
1255 record anything then. */
1256 else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
1257 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
1258 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
1259 return;
1261 /* If SRC had been assigned a mode narrower than the copy, we can't
1262 link DEST into the chain, because not all of the pieces of the
1263 copy came from oldest_regno. */
1264 else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
1265 return;
1267 /* Link DR at the end of the value chain used by SR. */
1269 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
1271 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
1272 continue;
1273 vd->e[i].next_regno = dr;
1275 #ifdef ENABLE_CHECKING
1276 validate_value_data (vd);
1277 #endif
1280 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
1282 static bool
1283 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
1284 unsigned int regno ATTRIBUTE_UNUSED)
1286 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
1287 return false;
1289 #ifdef CANNOT_CHANGE_MODE_CLASS
1290 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
1291 #endif
1293 return true;
1296 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
1297 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
1298 in NEW_MODE.
1299 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
1301 static rtx
1302 maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
1303 enum machine_mode new_mode, unsigned int regno,
1304 unsigned int copy_regno ATTRIBUTE_UNUSED)
1306 if (orig_mode == new_mode)
1307 return gen_rtx_raw_REG (new_mode, regno);
1308 else if (mode_change_ok (orig_mode, new_mode, regno))
1310 int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
1311 int use_nregs = hard_regno_nregs[copy_regno][new_mode];
1312 int copy_offset
1313 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
1314 int offset
1315 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
1316 int byteoffset = offset % UNITS_PER_WORD;
1317 int wordoffset = offset - byteoffset;
1319 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
1320 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
1321 return gen_rtx_raw_REG (new_mode,
1322 regno + subreg_regno_offset (regno, orig_mode,
1323 offset,
1324 new_mode));
1326 return NULL_RTX;
1329 /* Find the oldest copy of the value contained in REGNO that is in
1330 register class CL and has mode MODE. If found, return an rtx
1331 of that oldest register, otherwise return NULL. */
1333 static rtx
1334 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
1336 unsigned int regno = REGNO (reg);
1337 enum machine_mode mode = GET_MODE (reg);
1338 unsigned int i;
1340 /* If we are accessing REG in some mode other that what we set it in,
1341 make sure that the replacement is valid. In particular, consider
1342 (set (reg:DI r11) (...))
1343 (set (reg:SI r9) (reg:SI r11))
1344 (set (reg:SI r10) (...))
1345 (set (...) (reg:DI r9))
1346 Replacing r9 with r11 is invalid. */
1347 if (mode != vd->e[regno].mode)
1349 if (hard_regno_nregs[regno][mode]
1350 > hard_regno_nregs[regno][vd->e[regno].mode])
1351 return NULL_RTX;
1354 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
1356 enum machine_mode oldmode = vd->e[i].mode;
1357 rtx new;
1358 unsigned int last;
1360 for (last = i; last < i + hard_regno_nregs[i][mode]; last++)
1361 if (!TEST_HARD_REG_BIT (reg_class_contents[cl], last))
1362 return NULL_RTX;
1364 new = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
1365 if (new)
1367 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (reg);
1368 REG_ATTRS (new) = REG_ATTRS (reg);
1369 return new;
1373 return NULL_RTX;
1376 /* If possible, replace the register at *LOC with the oldest register
1377 in register class CL. Return true if successfully replaced. */
1379 static bool
1380 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx insn,
1381 struct value_data *vd)
1383 rtx new = find_oldest_value_reg (cl, *loc, vd);
1384 if (new)
1386 if (dump_file)
1387 fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
1388 INSN_UID (insn), REGNO (*loc), REGNO (new));
1390 *loc = new;
1391 return true;
1393 return false;
1396 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1397 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
1398 BASE_REG_CLASS depending on how the register is being considered. */
1400 static bool
1401 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
1402 enum machine_mode mode, rtx insn,
1403 struct value_data *vd)
1405 rtx x = *loc;
1406 RTX_CODE code = GET_CODE (x);
1407 const char *fmt;
1408 int i, j;
1409 bool changed = false;
1411 switch (code)
1413 case PLUS:
1415 rtx orig_op0 = XEXP (x, 0);
1416 rtx orig_op1 = XEXP (x, 1);
1417 RTX_CODE code0 = GET_CODE (orig_op0);
1418 RTX_CODE code1 = GET_CODE (orig_op1);
1419 rtx op0 = orig_op0;
1420 rtx op1 = orig_op1;
1421 rtx *locI = NULL;
1422 rtx *locB = NULL;
1423 rtx *locB_reg = NULL;
1425 if (GET_CODE (op0) == SUBREG)
1427 op0 = SUBREG_REG (op0);
1428 code0 = GET_CODE (op0);
1431 if (GET_CODE (op1) == SUBREG)
1433 op1 = SUBREG_REG (op1);
1434 code1 = GET_CODE (op1);
1437 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1438 || code0 == ZERO_EXTEND || code1 == MEM)
1440 locI = &XEXP (x, 0);
1441 locB = &XEXP (x, 1);
1443 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1444 || code1 == ZERO_EXTEND || code0 == MEM)
1446 locI = &XEXP (x, 1);
1447 locB = &XEXP (x, 0);
1449 else if (code0 == CONST_INT || code0 == CONST
1450 || code0 == SYMBOL_REF || code0 == LABEL_REF)
1451 locB = &XEXP (x, 1);
1452 else if (code1 == CONST_INT || code1 == CONST
1453 || code1 == SYMBOL_REF || code1 == LABEL_REF)
1454 locB = &XEXP (x, 0);
1455 else if (code0 == REG && code1 == REG)
1457 int index_op;
1459 if (REG_OK_FOR_INDEX_P (op0)
1460 && REG_MODE_OK_FOR_REG_BASE_P (op1, mode))
1461 index_op = 0;
1462 else if (REG_OK_FOR_INDEX_P (op1)
1463 && REG_MODE_OK_FOR_REG_BASE_P (op0, mode))
1464 index_op = 1;
1465 else if (REG_MODE_OK_FOR_REG_BASE_P (op1, mode))
1466 index_op = 0;
1467 else if (REG_MODE_OK_FOR_REG_BASE_P (op0, mode))
1468 index_op = 1;
1469 else if (REG_OK_FOR_INDEX_P (op1))
1470 index_op = 1;
1471 else
1472 index_op = 0;
1474 locI = &XEXP (x, index_op);
1475 locB_reg = &XEXP (x, !index_op);
1477 else if (code0 == REG)
1479 locI = &XEXP (x, 0);
1480 locB = &XEXP (x, 1);
1482 else if (code1 == REG)
1484 locI = &XEXP (x, 1);
1485 locB = &XEXP (x, 0);
1488 if (locI)
1489 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
1490 insn, vd);
1491 if (locB)
1492 changed |= replace_oldest_value_addr (locB,
1493 MODE_BASE_REG_CLASS (mode),
1494 mode, insn, vd);
1495 if (locB_reg)
1496 changed |= replace_oldest_value_addr (locB_reg,
1497 MODE_BASE_REG_REG_CLASS (mode),
1498 mode, insn, vd);
1499 return changed;
1502 case POST_INC:
1503 case POST_DEC:
1504 case POST_MODIFY:
1505 case PRE_INC:
1506 case PRE_DEC:
1507 case PRE_MODIFY:
1508 return false;
1510 case MEM:
1511 return replace_oldest_value_mem (x, insn, vd);
1513 case REG:
1514 return replace_oldest_value_reg (loc, cl, insn, vd);
1516 default:
1517 break;
1520 fmt = GET_RTX_FORMAT (code);
1521 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1523 if (fmt[i] == 'e')
1524 changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode,
1525 insn, vd);
1526 else if (fmt[i] == 'E')
1527 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1528 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
1529 mode, insn, vd);
1532 return changed;
1535 /* Similar to replace_oldest_value_reg, but X contains a memory. */
1537 static bool
1538 replace_oldest_value_mem (rtx x, rtx insn, struct value_data *vd)
1540 return replace_oldest_value_addr (&XEXP (x, 0),
1541 MODE_BASE_REG_CLASS (GET_MODE (x)),
1542 GET_MODE (x), insn, vd);
1545 /* Perform the forward copy propagation on basic block BB. */
1547 static bool
1548 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
1550 bool changed = false;
1551 rtx insn;
1553 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1555 int n_ops, i, alt, predicated;
1556 bool is_asm;
1557 rtx set;
1559 if (! INSN_P (insn))
1561 if (insn == BB_END (bb))
1562 break;
1563 else
1564 continue;
1567 set = single_set (insn);
1568 extract_insn (insn);
1569 if (! constrain_operands (1))
1570 fatal_insn_not_found (insn);
1571 preprocess_constraints ();
1572 alt = which_alternative;
1573 n_ops = recog_data.n_operands;
1574 is_asm = asm_noperands (PATTERN (insn)) >= 0;
1576 /* Simplify the code below by rewriting things to reflect
1577 matching constraints. Also promote OP_OUT to OP_INOUT
1578 in predicated instructions. */
1580 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1581 for (i = 0; i < n_ops; ++i)
1583 int matches = recog_op_alt[i][alt].matches;
1584 if (matches >= 0)
1585 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
1586 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1587 || (predicated && recog_data.operand_type[i] == OP_OUT))
1588 recog_data.operand_type[i] = OP_INOUT;
1591 /* For each earlyclobber operand, zap the value data. */
1592 for (i = 0; i < n_ops; i++)
1593 if (recog_op_alt[i][alt].earlyclobber)
1594 kill_value (recog_data.operand[i], vd);
1596 /* Within asms, a clobber cannot overlap inputs or outputs.
1597 I wouldn't think this were true for regular insns, but
1598 scan_rtx treats them like that... */
1599 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1601 /* Kill all auto-incremented values. */
1602 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
1603 for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
1605 /* Kill all early-clobbered operands. */
1606 for (i = 0; i < n_ops; i++)
1607 if (recog_op_alt[i][alt].earlyclobber)
1608 kill_value (recog_data.operand[i], vd);
1610 /* Special-case plain move instructions, since we may well
1611 be able to do the move from a different register class. */
1612 if (set && REG_P (SET_SRC (set)))
1614 rtx src = SET_SRC (set);
1615 unsigned int regno = REGNO (src);
1616 enum machine_mode mode = GET_MODE (src);
1617 unsigned int i;
1618 rtx new;
1620 /* If we are accessing SRC in some mode other that what we
1621 set it in, make sure that the replacement is valid. */
1622 if (mode != vd->e[regno].mode)
1624 if (hard_regno_nregs[regno][mode]
1625 > hard_regno_nregs[regno][vd->e[regno].mode])
1626 goto no_move_special_case;
1629 /* If the destination is also a register, try to find a source
1630 register in the same class. */
1631 if (REG_P (SET_DEST (set)))
1633 new = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
1634 if (new && validate_change (insn, &SET_SRC (set), new, 0))
1636 if (dump_file)
1637 fprintf (dump_file,
1638 "insn %u: replaced reg %u with %u\n",
1639 INSN_UID (insn), regno, REGNO (new));
1640 changed = true;
1641 goto did_replacement;
1645 /* Otherwise, try all valid registers and see if its valid. */
1646 for (i = vd->e[regno].oldest_regno; i != regno;
1647 i = vd->e[i].next_regno)
1649 new = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
1650 mode, i, regno);
1651 if (new != NULL_RTX)
1653 if (validate_change (insn, &SET_SRC (set), new, 0))
1655 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (src);
1656 REG_ATTRS (new) = REG_ATTRS (src);
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;
1667 no_move_special_case:
1669 /* For each input operand, replace a hard register with the
1670 eldest live copy that's in an appropriate register class. */
1671 for (i = 0; i < n_ops; i++)
1673 bool replaced = false;
1675 /* Don't scan match_operand here, since we've no reg class
1676 information to pass down. Any operands that we could
1677 substitute in will be represented elsewhere. */
1678 if (recog_data.constraints[i][0] == '\0')
1679 continue;
1681 /* Don't replace in asms intentionally referencing hard regs. */
1682 if (is_asm && REG_P (recog_data.operand[i])
1683 && (REGNO (recog_data.operand[i])
1684 == ORIGINAL_REGNO (recog_data.operand[i])))
1685 continue;
1687 if (recog_data.operand_type[i] == OP_IN)
1689 if (recog_op_alt[i][alt].is_address)
1690 replaced
1691 = replace_oldest_value_addr (recog_data.operand_loc[i],
1692 recog_op_alt[i][alt].cl,
1693 VOIDmode, insn, vd);
1694 else if (REG_P (recog_data.operand[i]))
1695 replaced
1696 = replace_oldest_value_reg (recog_data.operand_loc[i],
1697 recog_op_alt[i][alt].cl,
1698 insn, vd);
1699 else if (MEM_P (recog_data.operand[i]))
1700 replaced = replace_oldest_value_mem (recog_data.operand[i],
1701 insn, vd);
1703 else if (MEM_P (recog_data.operand[i]))
1704 replaced = replace_oldest_value_mem (recog_data.operand[i],
1705 insn, vd);
1707 /* If we performed any replacement, update match_dups. */
1708 if (replaced)
1710 int j;
1711 rtx new;
1713 changed = true;
1715 new = *recog_data.operand_loc[i];
1716 recog_data.operand[i] = new;
1717 for (j = 0; j < recog_data.n_dups; j++)
1718 if (recog_data.dup_num[j] == i)
1719 *recog_data.dup_loc[j] = new;
1723 did_replacement:
1724 /* Clobber call-clobbered registers. */
1725 if (CALL_P (insn))
1726 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1727 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1728 kill_value_regno (i, 1, vd);
1730 /* Notice stores. */
1731 note_stores (PATTERN (insn), kill_set_value, vd);
1733 /* Notice copies. */
1734 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1735 copy_value (SET_DEST (set), SET_SRC (set), vd);
1737 if (insn == BB_END (bb))
1738 break;
1741 return changed;
1744 /* Main entry point for the forward copy propagation optimization. */
1746 void
1747 copyprop_hardreg_forward (void)
1749 struct value_data *all_vd;
1750 bool need_refresh;
1751 basic_block bb;
1752 sbitmap visited;
1754 need_refresh = false;
1756 all_vd = xmalloc (sizeof (struct value_data) * last_basic_block);
1758 visited = sbitmap_alloc (last_basic_block - (INVALID_BLOCK + 1));
1759 sbitmap_zero (visited);
1761 FOR_EACH_BB (bb)
1763 SET_BIT (visited, bb->index - (INVALID_BLOCK + 1));
1765 /* If a block has a single predecessor, that we've already
1766 processed, begin with the value data that was live at
1767 the end of the predecessor block. */
1768 /* ??? Ought to use more intelligent queuing of blocks. */
1769 if (EDGE_COUNT (bb->preds) == 1
1770 && TEST_BIT (visited,
1771 EDGE_PRED (bb, 0)->src->index - (INVALID_BLOCK + 1))
1772 && ! (EDGE_PRED (bb, 0)->flags & (EDGE_ABNORMAL_CALL | EDGE_EH)))
1773 all_vd[bb->index] = all_vd[EDGE_PRED (bb, 0)->src->index];
1774 else
1775 init_value_data (all_vd + bb->index);
1777 if (copyprop_hardreg_forward_1 (bb, all_vd + bb->index))
1778 need_refresh = true;
1781 sbitmap_free (visited);
1783 if (need_refresh)
1785 if (dump_file)
1786 fputs ("\n\n", dump_file);
1788 /* ??? Irritatingly, delete_noop_moves does not take a set of blocks
1789 to scan, so we have to do a life update with no initial set of
1790 blocks Just In Case. */
1791 delete_noop_moves ();
1792 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
1793 PROP_DEATH_NOTES
1794 | PROP_SCAN_DEAD_CODE
1795 | PROP_KILL_DEAD_CODE);
1798 free (all_vd);
1801 /* Dump the value chain data to stderr. */
1803 void
1804 debug_value_data (struct value_data *vd)
1806 HARD_REG_SET set;
1807 unsigned int i, j;
1809 CLEAR_HARD_REG_SET (set);
1811 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1812 if (vd->e[i].oldest_regno == i)
1814 if (vd->e[i].mode == VOIDmode)
1816 if (vd->e[i].next_regno != INVALID_REGNUM)
1817 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1818 i, vd->e[i].next_regno);
1819 continue;
1822 SET_HARD_REG_BIT (set, i);
1823 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1825 for (j = vd->e[i].next_regno;
1826 j != INVALID_REGNUM;
1827 j = vd->e[j].next_regno)
1829 if (TEST_HARD_REG_BIT (set, j))
1831 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1832 return;
1835 if (vd->e[j].oldest_regno != i)
1837 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1838 j, vd->e[j].oldest_regno);
1839 return;
1841 SET_HARD_REG_BIT (set, j);
1842 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1844 fputc ('\n', stderr);
1847 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1848 if (! TEST_HARD_REG_BIT (set, i)
1849 && (vd->e[i].mode != VOIDmode
1850 || vd->e[i].oldest_regno != i
1851 || vd->e[i].next_regno != INVALID_REGNUM))
1852 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1853 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1854 vd->e[i].next_regno);
1857 #ifdef ENABLE_CHECKING
1858 static void
1859 validate_value_data (struct value_data *vd)
1861 HARD_REG_SET set;
1862 unsigned int i, j;
1864 CLEAR_HARD_REG_SET (set);
1866 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1867 if (vd->e[i].oldest_regno == i)
1869 if (vd->e[i].mode == VOIDmode)
1871 if (vd->e[i].next_regno != INVALID_REGNUM)
1872 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1873 i, vd->e[i].next_regno);
1874 continue;
1877 SET_HARD_REG_BIT (set, i);
1879 for (j = vd->e[i].next_regno;
1880 j != INVALID_REGNUM;
1881 j = vd->e[j].next_regno)
1883 if (TEST_HARD_REG_BIT (set, j))
1884 internal_error ("validate_value_data: Loop in regno chain (%u)",
1886 if (vd->e[j].oldest_regno != i)
1887 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1888 j, vd->e[j].oldest_regno);
1890 SET_HARD_REG_BIT (set, j);
1894 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1895 if (! TEST_HARD_REG_BIT (set, i)
1896 && (vd->e[i].mode != VOIDmode
1897 || vd->e[i].oldest_regno != i
1898 || vd->e[i].next_regno != INVALID_REGNUM))
1899 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1900 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1901 vd->e[i].next_regno);
1903 #endif