2005-01-13 Michael Koch <konqueror@gmx.de>
[official-gcc.git] / gcc / regrename.c
blob0e276362449b636aef0b07dd5e7852392cbbfb2e
1 /* Register renaming for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
19 02111-1307, USA. */
21 #define REG_OK_STRICT
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "insn-config.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "reload.h"
34 #include "output.h"
35 #include "function.h"
36 #include "recog.h"
37 #include "flags.h"
38 #include "toplev.h"
39 #include "obstack.h"
41 #ifndef REG_MODE_OK_FOR_BASE_P
42 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
43 #endif
45 static const char *const reg_class_names[] = REG_CLASS_NAMES;
47 struct du_chain
49 struct du_chain *next_chain;
50 struct du_chain *next_use;
52 rtx insn;
53 rtx *loc;
54 ENUM_BITFIELD(reg_class) class : 16;
55 unsigned int need_caller_save_reg:1;
56 unsigned int earlyclobber:1;
59 enum scan_actions
61 terminate_all_read,
62 terminate_overlapping_read,
63 terminate_write,
64 terminate_dead,
65 mark_read,
66 mark_write
69 static const char * const scan_actions_name[] =
71 "terminate_all_read",
72 "terminate_overlapping_read",
73 "terminate_write",
74 "terminate_dead",
75 "mark_read",
76 "mark_write"
79 static struct obstack rename_obstack;
81 static void do_replace (struct du_chain *, int);
82 static void scan_rtx_reg (rtx, rtx *, enum reg_class,
83 enum scan_actions, enum op_type, int);
84 static void scan_rtx_address (rtx, rtx *, enum reg_class,
85 enum scan_actions, enum machine_mode);
86 static void scan_rtx (rtx, rtx *, enum reg_class, enum scan_actions,
87 enum op_type, int);
88 static struct du_chain *build_def_use (basic_block);
89 static void dump_def_use_chain (struct du_chain *);
90 static void note_sets (rtx, rtx, void *);
91 static void clear_dead_regs (HARD_REG_SET *, enum machine_mode, rtx);
92 static void merge_overlapping_regs (basic_block, HARD_REG_SET *,
93 struct du_chain *);
95 /* Called through note_stores from update_life. Find sets of registers, and
96 record them in *DATA (which is actually a HARD_REG_SET *). */
98 static void
99 note_sets (rtx x, rtx set ATTRIBUTE_UNUSED, void *data)
101 HARD_REG_SET *pset = (HARD_REG_SET *) data;
102 unsigned int regno;
103 int nregs;
104 if (!REG_P (x))
105 return;
106 regno = REGNO (x);
107 nregs = hard_regno_nregs[regno][GET_MODE (x)];
109 /* There must not be pseudos at this point. */
110 if (regno + nregs > FIRST_PSEUDO_REGISTER)
111 abort ();
113 while (nregs-- > 0)
114 SET_HARD_REG_BIT (*pset, regno + nregs);
117 /* Clear all registers from *PSET for which a note of kind KIND can be found
118 in the list NOTES. */
120 static void
121 clear_dead_regs (HARD_REG_SET *pset, enum machine_mode kind, rtx notes)
123 rtx note;
124 for (note = notes; note; note = XEXP (note, 1))
125 if (REG_NOTE_KIND (note) == kind && REG_P (XEXP (note, 0)))
127 rtx reg = XEXP (note, 0);
128 unsigned int regno = REGNO (reg);
129 int nregs = hard_regno_nregs[regno][GET_MODE (reg)];
131 /* There must not be pseudos at this point. */
132 if (regno + nregs > FIRST_PSEUDO_REGISTER)
133 abort ();
135 while (nregs-- > 0)
136 CLEAR_HARD_REG_BIT (*pset, regno + nregs);
140 /* For a def-use chain CHAIN in basic block B, find which registers overlap
141 its lifetime and set the corresponding bits in *PSET. */
143 static void
144 merge_overlapping_regs (basic_block b, HARD_REG_SET *pset,
145 struct du_chain *chain)
147 struct du_chain *t = chain;
148 rtx insn;
149 HARD_REG_SET live;
151 REG_SET_TO_HARD_REG_SET (live, b->global_live_at_start);
152 insn = BB_HEAD (b);
153 while (t)
155 /* Search forward until the next reference to the register to be
156 renamed. */
157 while (insn != t->insn)
159 if (INSN_P (insn))
161 clear_dead_regs (&live, REG_DEAD, REG_NOTES (insn));
162 note_stores (PATTERN (insn), note_sets, (void *) &live);
163 /* Only record currently live regs if we are inside the
164 reg's live range. */
165 if (t != chain)
166 IOR_HARD_REG_SET (*pset, live);
167 clear_dead_regs (&live, REG_UNUSED, REG_NOTES (insn));
169 insn = NEXT_INSN (insn);
172 IOR_HARD_REG_SET (*pset, live);
174 /* For the last reference, also merge in all registers set in the
175 same insn.
176 @@@ We only have take earlyclobbered sets into account. */
177 if (! t->next_use)
178 note_stores (PATTERN (insn), note_sets, (void *) pset);
180 t = t->next_use;
184 /* Perform register renaming on the current function. */
186 void
187 regrename_optimize (void)
189 int tick[FIRST_PSEUDO_REGISTER];
190 int this_tick = 0;
191 basic_block bb;
192 char *first_obj;
194 memset (tick, 0, sizeof tick);
196 gcc_obstack_init (&rename_obstack);
197 first_obj = obstack_alloc (&rename_obstack, 0);
199 FOR_EACH_BB (bb)
201 struct du_chain *all_chains = 0;
202 HARD_REG_SET unavailable;
203 HARD_REG_SET regs_seen;
205 CLEAR_HARD_REG_SET (unavailable);
207 if (dump_file)
208 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
210 all_chains = build_def_use (bb);
212 if (dump_file)
213 dump_def_use_chain (all_chains);
215 CLEAR_HARD_REG_SET (unavailable);
216 /* Don't clobber traceback for noreturn functions. */
217 if (frame_pointer_needed)
219 int i;
221 for (i = hard_regno_nregs[FRAME_POINTER_REGNUM][Pmode]; i--;)
222 SET_HARD_REG_BIT (unavailable, FRAME_POINTER_REGNUM + i);
224 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
225 for (i = hard_regno_nregs[HARD_FRAME_POINTER_REGNUM][Pmode]; i--;)
226 SET_HARD_REG_BIT (unavailable, HARD_FRAME_POINTER_REGNUM + i);
227 #endif
230 CLEAR_HARD_REG_SET (regs_seen);
231 while (all_chains)
233 int new_reg, best_new_reg;
234 int n_uses;
235 struct du_chain *this = all_chains;
236 struct du_chain *tmp, *last;
237 HARD_REG_SET this_unavailable;
238 int reg = REGNO (*this->loc);
239 int i;
241 all_chains = this->next_chain;
243 best_new_reg = reg;
245 #if 0 /* This just disables optimization opportunities. */
246 /* Only rename once we've seen the reg more than once. */
247 if (! TEST_HARD_REG_BIT (regs_seen, reg))
249 SET_HARD_REG_BIT (regs_seen, reg);
250 continue;
252 #endif
254 if (fixed_regs[reg] || global_regs[reg]
255 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
256 || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
257 #else
258 || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
259 #endif
261 continue;
263 COPY_HARD_REG_SET (this_unavailable, unavailable);
265 /* Find last entry on chain (which has the need_caller_save bit),
266 count number of uses, and narrow the set of registers we can
267 use for renaming. */
268 n_uses = 0;
269 for (last = this; last->next_use; last = last->next_use)
271 n_uses++;
272 IOR_COMPL_HARD_REG_SET (this_unavailable,
273 reg_class_contents[last->class]);
275 if (n_uses < 1)
276 continue;
278 IOR_COMPL_HARD_REG_SET (this_unavailable,
279 reg_class_contents[last->class]);
281 if (this->need_caller_save_reg)
282 IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
284 merge_overlapping_regs (bb, &this_unavailable, this);
286 /* Now potential_regs is a reasonable approximation, let's
287 have a closer look at each register still in there. */
288 for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
290 int nregs = hard_regno_nregs[new_reg][GET_MODE (*this->loc)];
292 for (i = nregs - 1; i >= 0; --i)
293 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
294 || fixed_regs[new_reg + i]
295 || global_regs[new_reg + i]
296 /* Can't use regs which aren't saved by the prologue. */
297 || (! regs_ever_live[new_reg + i]
298 && ! call_used_regs[new_reg + i])
299 #ifdef LEAF_REGISTERS
300 /* We can't use a non-leaf register if we're in a
301 leaf function. */
302 || (current_function_is_leaf
303 && !LEAF_REGISTERS[new_reg + i])
304 #endif
305 #ifdef HARD_REGNO_RENAME_OK
306 || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
307 #endif
309 break;
310 if (i >= 0)
311 continue;
313 /* See whether it accepts all modes that occur in
314 definition and uses. */
315 for (tmp = this; tmp; tmp = tmp->next_use)
316 if (! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
317 || (tmp->need_caller_save_reg
318 && ! (HARD_REGNO_CALL_PART_CLOBBERED
319 (reg, GET_MODE (*tmp->loc)))
320 && (HARD_REGNO_CALL_PART_CLOBBERED
321 (new_reg, GET_MODE (*tmp->loc)))))
322 break;
323 if (! tmp)
325 if (tick[best_new_reg] > tick[new_reg])
326 best_new_reg = new_reg;
330 if (dump_file)
332 fprintf (dump_file, "Register %s in insn %d",
333 reg_names[reg], INSN_UID (last->insn));
334 if (last->need_caller_save_reg)
335 fprintf (dump_file, " crosses a call");
338 if (best_new_reg == reg)
340 tick[reg] = ++this_tick;
341 if (dump_file)
342 fprintf (dump_file, "; no available better choice\n");
343 continue;
346 do_replace (this, best_new_reg);
347 tick[best_new_reg] = ++this_tick;
348 regs_ever_live[best_new_reg] = 1;
350 if (dump_file)
351 fprintf (dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
354 obstack_free (&rename_obstack, first_obj);
357 obstack_free (&rename_obstack, NULL);
359 if (dump_file)
360 fputc ('\n', dump_file);
362 count_or_remove_death_notes (NULL, 1);
363 update_life_info (NULL, UPDATE_LIFE_LOCAL,
364 PROP_DEATH_NOTES);
367 static void
368 do_replace (struct du_chain *chain, int reg)
370 while (chain)
372 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
373 struct reg_attrs * attr = REG_ATTRS (*chain->loc);
375 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
376 if (regno >= FIRST_PSEUDO_REGISTER)
377 ORIGINAL_REGNO (*chain->loc) = regno;
378 REG_ATTRS (*chain->loc) = attr;
379 chain = chain->next_use;
384 static struct du_chain *open_chains;
385 static struct du_chain *closed_chains;
387 static void
388 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class class,
389 enum scan_actions action, enum op_type type, int earlyclobber)
391 struct du_chain **p;
392 rtx x = *loc;
393 enum machine_mode mode = GET_MODE (x);
394 int this_regno = REGNO (x);
395 int this_nregs = hard_regno_nregs[this_regno][mode];
397 if (action == mark_write)
399 if (type == OP_OUT)
401 struct du_chain *this
402 = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
403 this->next_use = 0;
404 this->next_chain = open_chains;
405 this->loc = loc;
406 this->insn = insn;
407 this->class = class;
408 this->need_caller_save_reg = 0;
409 this->earlyclobber = earlyclobber;
410 open_chains = this;
412 return;
415 if ((type == OP_OUT && action != terminate_write)
416 || (type != OP_OUT && action == terminate_write))
417 return;
419 for (p = &open_chains; *p;)
421 struct du_chain *this = *p;
423 /* Check if the chain has been terminated if it has then skip to
424 the next chain.
426 This can happen when we've already appended the location to
427 the chain in Step 3, but are trying to hide in-out operands
428 from terminate_write in Step 5. */
430 if (*this->loc == cc0_rtx)
431 p = &this->next_chain;
432 else
434 int regno = REGNO (*this->loc);
435 int nregs = hard_regno_nregs[regno][GET_MODE (*this->loc)];
436 int exact_match = (regno == this_regno && nregs == this_nregs);
438 if (regno + nregs <= this_regno
439 || this_regno + this_nregs <= regno)
441 p = &this->next_chain;
442 continue;
445 if (action == mark_read)
447 if (! exact_match)
448 abort ();
450 /* ??? Class NO_REGS can happen if the md file makes use of
451 EXTRA_CONSTRAINTS to match registers. Which is arguably
452 wrong, but there we are. Since we know not what this may
453 be replaced with, terminate the chain. */
454 if (class != NO_REGS)
456 this = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
457 this->next_use = 0;
458 this->next_chain = (*p)->next_chain;
459 this->loc = loc;
460 this->insn = insn;
461 this->class = class;
462 this->need_caller_save_reg = 0;
463 while (*p)
464 p = &(*p)->next_use;
465 *p = this;
466 return;
470 if (action != terminate_overlapping_read || ! exact_match)
472 struct du_chain *next = this->next_chain;
474 /* Whether the terminated chain can be used for renaming
475 depends on the action and this being an exact match.
476 In either case, we remove this element from open_chains. */
478 if ((action == terminate_dead || action == terminate_write)
479 && exact_match)
481 this->next_chain = closed_chains;
482 closed_chains = this;
483 if (dump_file)
484 fprintf (dump_file,
485 "Closing chain %s at insn %d (%s)\n",
486 reg_names[REGNO (*this->loc)], INSN_UID (insn),
487 scan_actions_name[(int) action]);
489 else
491 if (dump_file)
492 fprintf (dump_file,
493 "Discarding chain %s at insn %d (%s)\n",
494 reg_names[REGNO (*this->loc)], INSN_UID (insn),
495 scan_actions_name[(int) action]);
497 *p = next;
499 else
500 p = &this->next_chain;
505 /* Adapted from find_reloads_address_1. CLASS is INDEX_REG_CLASS or
506 BASE_REG_CLASS depending on how the register is being considered. */
508 static void
509 scan_rtx_address (rtx insn, rtx *loc, enum reg_class class,
510 enum scan_actions action, enum machine_mode mode)
512 rtx x = *loc;
513 RTX_CODE code = GET_CODE (x);
514 const char *fmt;
515 int i, j;
517 if (action == mark_write)
518 return;
520 switch (code)
522 case PLUS:
524 rtx orig_op0 = XEXP (x, 0);
525 rtx orig_op1 = XEXP (x, 1);
526 RTX_CODE code0 = GET_CODE (orig_op0);
527 RTX_CODE code1 = GET_CODE (orig_op1);
528 rtx op0 = orig_op0;
529 rtx op1 = orig_op1;
530 rtx *locI = NULL;
531 rtx *locB = NULL;
533 if (GET_CODE (op0) == SUBREG)
535 op0 = SUBREG_REG (op0);
536 code0 = GET_CODE (op0);
539 if (GET_CODE (op1) == SUBREG)
541 op1 = SUBREG_REG (op1);
542 code1 = GET_CODE (op1);
545 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
546 || code0 == ZERO_EXTEND || code1 == MEM)
548 locI = &XEXP (x, 0);
549 locB = &XEXP (x, 1);
551 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
552 || code1 == ZERO_EXTEND || code0 == MEM)
554 locI = &XEXP (x, 1);
555 locB = &XEXP (x, 0);
557 else if (code0 == CONST_INT || code0 == CONST
558 || code0 == SYMBOL_REF || code0 == LABEL_REF)
559 locB = &XEXP (x, 1);
560 else if (code1 == CONST_INT || code1 == CONST
561 || code1 == SYMBOL_REF || code1 == LABEL_REF)
562 locB = &XEXP (x, 0);
563 else if (code0 == REG && code1 == REG)
565 int index_op;
567 if (REG_OK_FOR_INDEX_P (op0)
568 && REG_MODE_OK_FOR_BASE_P (op1, mode))
569 index_op = 0;
570 else if (REG_OK_FOR_INDEX_P (op1)
571 && REG_MODE_OK_FOR_BASE_P (op0, mode))
572 index_op = 1;
573 else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
574 index_op = 0;
575 else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
576 index_op = 1;
577 else if (REG_OK_FOR_INDEX_P (op1))
578 index_op = 1;
579 else
580 index_op = 0;
582 locI = &XEXP (x, index_op);
583 locB = &XEXP (x, !index_op);
585 else if (code0 == REG)
587 locI = &XEXP (x, 0);
588 locB = &XEXP (x, 1);
590 else if (code1 == REG)
592 locI = &XEXP (x, 1);
593 locB = &XEXP (x, 0);
596 if (locI)
597 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
598 if (locB)
599 scan_rtx_address (insn, locB, MODE_BASE_REG_CLASS (mode), action, mode);
600 return;
603 case POST_INC:
604 case POST_DEC:
605 case POST_MODIFY:
606 case PRE_INC:
607 case PRE_DEC:
608 case PRE_MODIFY:
609 #ifndef AUTO_INC_DEC
610 /* If the target doesn't claim to handle autoinc, this must be
611 something special, like a stack push. Kill this chain. */
612 action = terminate_all_read;
613 #endif
614 break;
616 case MEM:
617 scan_rtx_address (insn, &XEXP (x, 0),
618 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
619 GET_MODE (x));
620 return;
622 case REG:
623 scan_rtx_reg (insn, loc, class, action, OP_IN, 0);
624 return;
626 default:
627 break;
630 fmt = GET_RTX_FORMAT (code);
631 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
633 if (fmt[i] == 'e')
634 scan_rtx_address (insn, &XEXP (x, i), class, action, mode);
635 else if (fmt[i] == 'E')
636 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
637 scan_rtx_address (insn, &XVECEXP (x, i, j), class, action, mode);
641 static void
642 scan_rtx (rtx insn, rtx *loc, enum reg_class class,
643 enum scan_actions action, enum op_type type, int earlyclobber)
645 const char *fmt;
646 rtx x = *loc;
647 enum rtx_code code = GET_CODE (x);
648 int i, j;
650 code = GET_CODE (x);
651 switch (code)
653 case CONST:
654 case CONST_INT:
655 case CONST_DOUBLE:
656 case CONST_VECTOR:
657 case SYMBOL_REF:
658 case LABEL_REF:
659 case CC0:
660 case PC:
661 return;
663 case REG:
664 scan_rtx_reg (insn, loc, class, action, type, earlyclobber);
665 return;
667 case MEM:
668 scan_rtx_address (insn, &XEXP (x, 0),
669 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
670 GET_MODE (x));
671 return;
673 case SET:
674 scan_rtx (insn, &SET_SRC (x), class, action, OP_IN, 0);
675 scan_rtx (insn, &SET_DEST (x), class, action, OP_OUT, 0);
676 return;
678 case STRICT_LOW_PART:
679 scan_rtx (insn, &XEXP (x, 0), class, action, OP_INOUT, earlyclobber);
680 return;
682 case ZERO_EXTRACT:
683 case SIGN_EXTRACT:
684 scan_rtx (insn, &XEXP (x, 0), class, action,
685 type == OP_IN ? OP_IN : OP_INOUT, earlyclobber);
686 scan_rtx (insn, &XEXP (x, 1), class, action, OP_IN, 0);
687 scan_rtx (insn, &XEXP (x, 2), class, 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 abort ();
699 case CLOBBER:
700 scan_rtx (insn, &SET_DEST (x), class, action, OP_OUT, 1);
701 return;
703 case EXPR_LIST:
704 scan_rtx (insn, &XEXP (x, 0), class, action, type, 0);
705 if (XEXP (x, 1))
706 scan_rtx (insn, &XEXP (x, 1), class, action, type, 0);
707 return;
709 default:
710 break;
713 fmt = GET_RTX_FORMAT (code);
714 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
716 if (fmt[i] == 'e')
717 scan_rtx (insn, &XEXP (x, i), class, action, type, 0);
718 else if (fmt[i] == 'E')
719 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
720 scan_rtx (insn, &XVECEXP (x, i, j), class, action, type, 0);
724 /* Build def/use chain. */
726 static struct du_chain *
727 build_def_use (basic_block bb)
729 rtx insn;
731 open_chains = closed_chains = NULL;
733 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
735 if (INSN_P (insn))
737 int n_ops;
738 rtx note;
739 rtx old_operands[MAX_RECOG_OPERANDS];
740 rtx old_dups[MAX_DUP_OPERANDS];
741 int i, icode;
742 int alt;
743 int predicated;
745 /* Process the insn, determining its effect on the def-use
746 chains. We perform the following steps with the register
747 references in the insn:
748 (1) Any read that overlaps an open chain, but doesn't exactly
749 match, causes that chain to be closed. We can't deal
750 with overlaps yet.
751 (2) Any read outside an operand causes any chain it overlaps
752 with to be closed, since we can't replace it.
753 (3) Any read inside an operand is added if there's already
754 an open chain for it.
755 (4) For any REG_DEAD note we find, close open chains that
756 overlap it.
757 (5) For any write we find, close open chains that overlap it.
758 (6) For any write we find in an operand, make a new chain.
759 (7) For any REG_UNUSED, close any chains we just opened. */
761 icode = recog_memoized (insn);
762 extract_insn (insn);
763 if (! constrain_operands (1))
764 fatal_insn_not_found (insn);
765 preprocess_constraints ();
766 alt = which_alternative;
767 n_ops = recog_data.n_operands;
769 /* Simplify the code below by rewriting things to reflect
770 matching constraints. Also promote OP_OUT to OP_INOUT
771 in predicated instructions. */
773 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
774 for (i = 0; i < n_ops; ++i)
776 int matches = recog_op_alt[i][alt].matches;
777 if (matches >= 0)
778 recog_op_alt[i][alt].class = recog_op_alt[matches][alt].class;
779 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
780 || (predicated && recog_data.operand_type[i] == OP_OUT))
781 recog_data.operand_type[i] = OP_INOUT;
784 /* Step 1: Close chains for which we have overlapping reads. */
785 for (i = 0; i < n_ops; i++)
786 scan_rtx (insn, recog_data.operand_loc[i],
787 NO_REGS, terminate_overlapping_read,
788 recog_data.operand_type[i], 0);
790 /* Step 2: Close chains for which we have reads outside operands.
791 We do this by munging all operands into CC0, and closing
792 everything remaining. */
794 for (i = 0; i < n_ops; i++)
796 old_operands[i] = recog_data.operand[i];
797 /* Don't squash match_operator or match_parallel here, since
798 we don't know that all of the contained registers are
799 reachable by proper operands. */
800 if (recog_data.constraints[i][0] == '\0')
801 continue;
802 *recog_data.operand_loc[i] = cc0_rtx;
804 for (i = 0; i < recog_data.n_dups; i++)
806 int dup_num = recog_data.dup_num[i];
808 old_dups[i] = *recog_data.dup_loc[i];
809 *recog_data.dup_loc[i] = cc0_rtx;
811 /* For match_dup of match_operator or match_parallel, share
812 them, so that we don't miss changes in the dup. */
813 if (icode >= 0
814 && insn_data[icode].operand[dup_num].eliminable == 0)
815 old_dups[i] = recog_data.operand[dup_num];
818 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
819 OP_IN, 0);
821 for (i = 0; i < recog_data.n_dups; i++)
822 *recog_data.dup_loc[i] = old_dups[i];
823 for (i = 0; i < n_ops; i++)
824 *recog_data.operand_loc[i] = old_operands[i];
826 /* Step 2B: Can't rename function call argument registers. */
827 if (GET_CODE (insn) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (insn))
828 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
829 NO_REGS, terminate_all_read, OP_IN, 0);
831 /* Step 2C: Can't rename asm operands that were originally
832 hard registers. */
833 if (asm_noperands (PATTERN (insn)) > 0)
834 for (i = 0; i < n_ops; i++)
836 rtx *loc = recog_data.operand_loc[i];
837 rtx op = *loc;
839 if (REG_P (op)
840 && REGNO (op) == ORIGINAL_REGNO (op)
841 && (recog_data.operand_type[i] == OP_IN
842 || recog_data.operand_type[i] == OP_INOUT))
843 scan_rtx (insn, loc, NO_REGS, terminate_all_read, OP_IN, 0);
846 /* Step 3: Append to chains for reads inside operands. */
847 for (i = 0; i < n_ops + recog_data.n_dups; i++)
849 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
850 rtx *loc = (i < n_ops
851 ? recog_data.operand_loc[opn]
852 : recog_data.dup_loc[i - n_ops]);
853 enum reg_class class = recog_op_alt[opn][alt].class;
854 enum op_type type = recog_data.operand_type[opn];
856 /* Don't scan match_operand here, since we've no reg class
857 information to pass down. Any operands that we could
858 substitute in will be represented elsewhere. */
859 if (recog_data.constraints[opn][0] == '\0')
860 continue;
862 if (recog_op_alt[opn][alt].is_address)
863 scan_rtx_address (insn, loc, class, mark_read, VOIDmode);
864 else
865 scan_rtx (insn, loc, class, mark_read, type, 0);
868 /* Step 4: Close chains for registers that die here.
869 Also record updates for REG_INC notes. */
870 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
872 if (REG_NOTE_KIND (note) == REG_DEAD)
873 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
874 OP_IN, 0);
875 else if (REG_NOTE_KIND (note) == REG_INC)
876 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
877 OP_INOUT, 0);
880 /* Step 4B: If this is a call, any chain live at this point
881 requires a caller-saved reg. */
882 if (GET_CODE (insn) == CALL_INSN)
884 struct du_chain *p;
885 for (p = open_chains; p; p = p->next_chain)
886 p->need_caller_save_reg = 1;
889 /* Step 5: Close open chains that overlap writes. Similar to
890 step 2, we hide in-out operands, since we do not want to
891 close these chains. */
893 for (i = 0; i < n_ops; i++)
895 old_operands[i] = recog_data.operand[i];
896 if (recog_data.operand_type[i] == OP_INOUT)
897 *recog_data.operand_loc[i] = cc0_rtx;
899 for (i = 0; i < recog_data.n_dups; i++)
901 int opn = recog_data.dup_num[i];
902 old_dups[i] = *recog_data.dup_loc[i];
903 if (recog_data.operand_type[opn] == OP_INOUT)
904 *recog_data.dup_loc[i] = cc0_rtx;
907 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
909 for (i = 0; i < recog_data.n_dups; i++)
910 *recog_data.dup_loc[i] = old_dups[i];
911 for (i = 0; i < n_ops; i++)
912 *recog_data.operand_loc[i] = old_operands[i];
914 /* Step 6: Begin new chains for writes inside operands. */
915 /* ??? Many targets have output constraints on the SET_DEST
916 of a call insn, which is stupid, since these are certainly
917 ABI defined hard registers. Don't change calls at all.
918 Similarly take special care for asm statement that originally
919 referenced hard registers. */
920 if (asm_noperands (PATTERN (insn)) > 0)
922 for (i = 0; i < n_ops; i++)
923 if (recog_data.operand_type[i] == OP_OUT)
925 rtx *loc = recog_data.operand_loc[i];
926 rtx op = *loc;
927 enum reg_class class = recog_op_alt[i][alt].class;
929 if (REG_P (op)
930 && REGNO (op) == ORIGINAL_REGNO (op))
931 continue;
933 scan_rtx (insn, loc, class, mark_write, OP_OUT,
934 recog_op_alt[i][alt].earlyclobber);
937 else if (GET_CODE (insn) != CALL_INSN)
938 for (i = 0; i < n_ops + recog_data.n_dups; i++)
940 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
941 rtx *loc = (i < n_ops
942 ? recog_data.operand_loc[opn]
943 : recog_data.dup_loc[i - n_ops]);
944 enum reg_class class = recog_op_alt[opn][alt].class;
946 if (recog_data.operand_type[opn] == OP_OUT)
947 scan_rtx (insn, loc, class, mark_write, OP_OUT,
948 recog_op_alt[opn][alt].earlyclobber);
951 /* Step 7: Close chains for registers that were never
952 really used here. */
953 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
954 if (REG_NOTE_KIND (note) == REG_UNUSED)
955 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
956 OP_IN, 0);
958 if (insn == BB_END (bb))
959 break;
962 /* Since we close every chain when we find a REG_DEAD note, anything that
963 is still open lives past the basic block, so it can't be renamed. */
964 return closed_chains;
967 /* Dump all def/use chains in CHAINS to DUMP_FILE. They are
968 printed in reverse order as that's how we build them. */
970 static void
971 dump_def_use_chain (struct du_chain *chains)
973 while (chains)
975 struct du_chain *this = chains;
976 int r = REGNO (*this->loc);
977 int nregs = hard_regno_nregs[r][GET_MODE (*this->loc)];
978 fprintf (dump_file, "Register %s (%d):", reg_names[r], nregs);
979 while (this)
981 fprintf (dump_file, " %d [%s]", INSN_UID (this->insn),
982 reg_class_names[this->class]);
983 this = this->next_use;
985 fprintf (dump_file, "\n");
986 chains = chains->next_chain;
990 /* The following code does forward propagation of hard register copies.
991 The object is to eliminate as many dependencies as possible, so that
992 we have the most scheduling freedom. As a side effect, we also clean
993 up some silly register allocation decisions made by reload. This
994 code may be obsoleted by a new register allocator. */
996 /* For each register, we have a list of registers that contain the same
997 value. The OLDEST_REGNO field points to the head of the list, and
998 the NEXT_REGNO field runs through the list. The MODE field indicates
999 what mode the data is known to be in; this field is VOIDmode when the
1000 register is not known to contain valid data. */
1002 struct value_data_entry
1004 enum machine_mode mode;
1005 unsigned int oldest_regno;
1006 unsigned int next_regno;
1009 struct value_data
1011 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
1012 unsigned int max_value_regs;
1015 static void kill_value_regno (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 lists,
1041 and resetting the value mode to VOIDmode. */
1043 static void
1044 kill_value_regno (unsigned int regno, struct value_data *vd)
1046 unsigned int i, next;
1048 if (vd->e[regno].oldest_regno != regno)
1050 for (i = vd->e[regno].oldest_regno;
1051 vd->e[i].next_regno != regno;
1052 i = vd->e[i].next_regno)
1053 continue;
1054 vd->e[i].next_regno = vd->e[regno].next_regno;
1056 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
1058 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
1059 vd->e[i].oldest_regno = next;
1062 vd->e[regno].mode = VOIDmode;
1063 vd->e[regno].oldest_regno = regno;
1064 vd->e[regno].next_regno = INVALID_REGNUM;
1066 #ifdef ENABLE_CHECKING
1067 validate_value_data (vd);
1068 #endif
1071 /* Kill X. This is a convenience function for kill_value_regno
1072 so that we mind the mode the register is in. */
1074 static void
1075 kill_value (rtx x, struct value_data *vd)
1077 /* SUBREGS are supposed to have been eliminated by now. But some
1078 ports, e.g. i386 sse, use them to smuggle vector type information
1079 through to instruction selection. Each such SUBREG should simplify,
1080 so if we get a NULL we've done something wrong elsewhere. */
1082 if (GET_CODE (x) == SUBREG)
1083 x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
1084 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
1085 if (REG_P (x))
1087 unsigned int regno = REGNO (x);
1088 unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
1089 unsigned int i, j;
1091 /* Kill the value we're told to kill. */
1092 for (i = 0; i < n; ++i)
1093 kill_value_regno (regno + i, vd);
1095 /* Kill everything that overlapped what we're told to kill. */
1096 if (regno < vd->max_value_regs)
1097 j = 0;
1098 else
1099 j = regno - vd->max_value_regs;
1100 for (; j < regno; ++j)
1102 if (vd->e[j].mode == VOIDmode)
1103 continue;
1104 n = hard_regno_nregs[j][vd->e[j].mode];
1105 if (j + n > regno)
1106 for (i = 0; i < n; ++i)
1107 kill_value_regno (j + i, vd);
1112 /* Remember that REGNO is valid in MODE. */
1114 static void
1115 set_value_regno (unsigned int regno, enum machine_mode mode,
1116 struct value_data *vd)
1118 unsigned int nregs;
1120 vd->e[regno].mode = mode;
1122 nregs = hard_regno_nregs[regno][mode];
1123 if (nregs > vd->max_value_regs)
1124 vd->max_value_regs = nregs;
1127 /* Initialize VD such that there are no known relationships between regs. */
1129 static void
1130 init_value_data (struct value_data *vd)
1132 int i;
1133 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1135 vd->e[i].mode = VOIDmode;
1136 vd->e[i].oldest_regno = i;
1137 vd->e[i].next_regno = INVALID_REGNUM;
1139 vd->max_value_regs = 0;
1142 /* Called through note_stores. If X is clobbered, kill its value. */
1144 static void
1145 kill_clobbered_value (rtx x, rtx set, void *data)
1147 struct value_data *vd = data;
1148 if (GET_CODE (set) == CLOBBER)
1149 kill_value (x, vd);
1152 /* Called through note_stores. If X is set, not clobbered, kill its
1153 current value and install it as the root of its own value list. */
1155 static void
1156 kill_set_value (rtx x, rtx set, void *data)
1158 struct value_data *vd = data;
1159 if (GET_CODE (set) != CLOBBER)
1161 kill_value (x, vd);
1162 if (REG_P (x))
1163 set_value_regno (REGNO (x), GET_MODE (x), vd);
1167 /* Called through for_each_rtx. Kill any register used as the base of an
1168 auto-increment expression, and install that register as the root of its
1169 own value list. */
1171 static int
1172 kill_autoinc_value (rtx *px, void *data)
1174 rtx x = *px;
1175 struct value_data *vd = data;
1177 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
1179 x = XEXP (x, 0);
1180 kill_value (x, vd);
1181 set_value_regno (REGNO (x), Pmode, vd);
1182 return -1;
1185 return 0;
1188 /* Assert that SRC has been copied to DEST. Adjust the data structures
1189 to reflect that SRC contains an older copy of the shared value. */
1191 static void
1192 copy_value (rtx dest, rtx src, struct value_data *vd)
1194 unsigned int dr = REGNO (dest);
1195 unsigned int sr = REGNO (src);
1196 unsigned int dn, sn;
1197 unsigned int i;
1199 /* ??? At present, it's possible to see noop sets. It'd be nice if
1200 this were cleaned up beforehand... */
1201 if (sr == dr)
1202 return;
1204 /* Do not propagate copies to the stack pointer, as that can leave
1205 memory accesses with no scheduling dependency on the stack update. */
1206 if (dr == STACK_POINTER_REGNUM)
1207 return;
1209 /* Likewise with the frame pointer, if we're using one. */
1210 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
1211 return;
1213 /* If SRC and DEST overlap, don't record anything. */
1214 dn = hard_regno_nregs[dr][GET_MODE (dest)];
1215 sn = hard_regno_nregs[sr][GET_MODE (dest)];
1216 if ((dr > sr && dr < sr + sn)
1217 || (sr > dr && sr < dr + dn))
1218 return;
1220 /* If SRC had no assigned mode (i.e. we didn't know it was live)
1221 assign it now and assume the value came from an input argument
1222 or somesuch. */
1223 if (vd->e[sr].mode == VOIDmode)
1224 set_value_regno (sr, vd->e[dr].mode, vd);
1226 /* If we are narrowing the input to a smaller number of hard regs,
1227 and it is in big endian, we are really extracting a high part.
1228 Since we generally associate a low part of a value with the value itself,
1229 we must not do the same for the high part.
1230 Note we can still get low parts for the same mode combination through
1231 a two-step copy involving differently sized hard regs.
1232 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
1233 (set (reg:DI r0) (reg:DI fr0))
1234 (set (reg:SI fr2) (reg:SI r0))
1235 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
1236 (set (reg:SI fr2) (reg:SI fr0))
1237 loads the high part of (reg:DI fr0) into fr2.
1239 We can't properly represent the latter case in our tables, so don't
1240 record anything then. */
1241 else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
1242 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
1243 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
1244 return;
1246 /* If SRC had been assigned a mode narrower than the copy, we can't
1247 link DEST into the chain, because not all of the pieces of the
1248 copy came from oldest_regno. */
1249 else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
1250 return;
1252 /* Link DR at the end of the value chain used by SR. */
1254 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
1256 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
1257 continue;
1258 vd->e[i].next_regno = dr;
1260 #ifdef ENABLE_CHECKING
1261 validate_value_data (vd);
1262 #endif
1265 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
1267 static bool
1268 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
1269 unsigned int regno ATTRIBUTE_UNUSED)
1271 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
1272 return false;
1274 #ifdef CANNOT_CHANGE_MODE_CLASS
1275 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
1276 #endif
1278 return true;
1281 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
1282 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
1283 in NEW_MODE.
1284 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
1286 static rtx
1287 maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
1288 enum machine_mode new_mode, unsigned int regno,
1289 unsigned int copy_regno ATTRIBUTE_UNUSED)
1291 if (orig_mode == new_mode)
1292 return gen_rtx_raw_REG (new_mode, regno);
1293 else if (mode_change_ok (orig_mode, new_mode, regno))
1295 int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
1296 int use_nregs = hard_regno_nregs[copy_regno][new_mode];
1297 int copy_offset
1298 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
1299 int offset
1300 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
1301 int byteoffset = offset % UNITS_PER_WORD;
1302 int wordoffset = offset - byteoffset;
1304 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
1305 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
1306 return gen_rtx_raw_REG (new_mode,
1307 regno + subreg_regno_offset (regno, orig_mode,
1308 offset,
1309 new_mode));
1311 return NULL_RTX;
1314 /* Find the oldest copy of the value contained in REGNO that is in
1315 register class CLASS and has mode MODE. If found, return an rtx
1316 of that oldest register, otherwise return NULL. */
1318 static rtx
1319 find_oldest_value_reg (enum reg_class class, rtx reg, struct value_data *vd)
1321 unsigned int regno = REGNO (reg);
1322 enum machine_mode mode = GET_MODE (reg);
1323 unsigned int i;
1325 /* If we are accessing REG in some mode other that what we set it in,
1326 make sure that the replacement is valid. In particular, consider
1327 (set (reg:DI r11) (...))
1328 (set (reg:SI r9) (reg:SI r11))
1329 (set (reg:SI r10) (...))
1330 (set (...) (reg:DI r9))
1331 Replacing r9 with r11 is invalid. */
1332 if (mode != vd->e[regno].mode)
1334 if (hard_regno_nregs[regno][mode]
1335 > hard_regno_nregs[regno][vd->e[regno].mode])
1336 return NULL_RTX;
1339 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
1341 enum machine_mode oldmode = vd->e[i].mode;
1342 rtx new;
1343 unsigned int last;
1345 for (last = i; last < i + hard_regno_nregs[i][mode]; last++)
1346 if (!TEST_HARD_REG_BIT (reg_class_contents[class], last))
1347 return NULL_RTX;
1349 new = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
1350 if (new)
1352 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (reg);
1353 REG_ATTRS (new) = REG_ATTRS (reg);
1354 return new;
1358 return NULL_RTX;
1361 /* If possible, replace the register at *LOC with the oldest register
1362 in register class CLASS. Return true if successfully replaced. */
1364 static bool
1365 replace_oldest_value_reg (rtx *loc, enum reg_class class, rtx insn,
1366 struct value_data *vd)
1368 rtx new = find_oldest_value_reg (class, *loc, vd);
1369 if (new)
1371 if (dump_file)
1372 fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
1373 INSN_UID (insn), REGNO (*loc), REGNO (new));
1375 *loc = new;
1376 return true;
1378 return false;
1381 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1382 Adapted from find_reloads_address_1. CLASS is INDEX_REG_CLASS or
1383 BASE_REG_CLASS depending on how the register is being considered. */
1385 static bool
1386 replace_oldest_value_addr (rtx *loc, enum reg_class class,
1387 enum machine_mode mode, rtx insn,
1388 struct value_data *vd)
1390 rtx x = *loc;
1391 RTX_CODE code = GET_CODE (x);
1392 const char *fmt;
1393 int i, j;
1394 bool changed = false;
1396 switch (code)
1398 case PLUS:
1400 rtx orig_op0 = XEXP (x, 0);
1401 rtx orig_op1 = XEXP (x, 1);
1402 RTX_CODE code0 = GET_CODE (orig_op0);
1403 RTX_CODE code1 = GET_CODE (orig_op1);
1404 rtx op0 = orig_op0;
1405 rtx op1 = orig_op1;
1406 rtx *locI = NULL;
1407 rtx *locB = NULL;
1409 if (GET_CODE (op0) == SUBREG)
1411 op0 = SUBREG_REG (op0);
1412 code0 = GET_CODE (op0);
1415 if (GET_CODE (op1) == SUBREG)
1417 op1 = SUBREG_REG (op1);
1418 code1 = GET_CODE (op1);
1421 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1422 || code0 == ZERO_EXTEND || code1 == MEM)
1424 locI = &XEXP (x, 0);
1425 locB = &XEXP (x, 1);
1427 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1428 || code1 == ZERO_EXTEND || code0 == MEM)
1430 locI = &XEXP (x, 1);
1431 locB = &XEXP (x, 0);
1433 else if (code0 == CONST_INT || code0 == CONST
1434 || code0 == SYMBOL_REF || code0 == LABEL_REF)
1435 locB = &XEXP (x, 1);
1436 else if (code1 == CONST_INT || code1 == CONST
1437 || code1 == SYMBOL_REF || code1 == LABEL_REF)
1438 locB = &XEXP (x, 0);
1439 else if (code0 == REG && code1 == REG)
1441 int index_op;
1443 if (REG_OK_FOR_INDEX_P (op0)
1444 && REG_MODE_OK_FOR_BASE_P (op1, mode))
1445 index_op = 0;
1446 else if (REG_OK_FOR_INDEX_P (op1)
1447 && REG_MODE_OK_FOR_BASE_P (op0, mode))
1448 index_op = 1;
1449 else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
1450 index_op = 0;
1451 else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
1452 index_op = 1;
1453 else if (REG_OK_FOR_INDEX_P (op1))
1454 index_op = 1;
1455 else
1456 index_op = 0;
1458 locI = &XEXP (x, index_op);
1459 locB = &XEXP (x, !index_op);
1461 else if (code0 == REG)
1463 locI = &XEXP (x, 0);
1464 locB = &XEXP (x, 1);
1466 else if (code1 == REG)
1468 locI = &XEXP (x, 1);
1469 locB = &XEXP (x, 0);
1472 if (locI)
1473 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
1474 insn, vd);
1475 if (locB)
1476 changed |= replace_oldest_value_addr (locB,
1477 MODE_BASE_REG_CLASS (mode),
1478 mode, insn, vd);
1479 return changed;
1482 case POST_INC:
1483 case POST_DEC:
1484 case POST_MODIFY:
1485 case PRE_INC:
1486 case PRE_DEC:
1487 case PRE_MODIFY:
1488 return false;
1490 case MEM:
1491 return replace_oldest_value_mem (x, insn, vd);
1493 case REG:
1494 return replace_oldest_value_reg (loc, class, insn, vd);
1496 default:
1497 break;
1500 fmt = GET_RTX_FORMAT (code);
1501 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1503 if (fmt[i] == 'e')
1504 changed |= replace_oldest_value_addr (&XEXP (x, i), class, mode,
1505 insn, vd);
1506 else if (fmt[i] == 'E')
1507 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1508 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), class,
1509 mode, insn, vd);
1512 return changed;
1515 /* Similar to replace_oldest_value_reg, but X contains a memory. */
1517 static bool
1518 replace_oldest_value_mem (rtx x, rtx insn, struct value_data *vd)
1520 return replace_oldest_value_addr (&XEXP (x, 0),
1521 MODE_BASE_REG_CLASS (GET_MODE (x)),
1522 GET_MODE (x), insn, vd);
1525 /* Perform the forward copy propagation on basic block BB. */
1527 static bool
1528 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
1530 bool changed = false;
1531 rtx insn;
1533 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1535 int n_ops, i, alt, predicated;
1536 bool is_asm;
1537 rtx set;
1539 if (! INSN_P (insn))
1541 if (insn == BB_END (bb))
1542 break;
1543 else
1544 continue;
1547 set = single_set (insn);
1548 extract_insn (insn);
1549 if (! constrain_operands (1))
1550 fatal_insn_not_found (insn);
1551 preprocess_constraints ();
1552 alt = which_alternative;
1553 n_ops = recog_data.n_operands;
1554 is_asm = asm_noperands (PATTERN (insn)) >= 0;
1556 /* Simplify the code below by rewriting things to reflect
1557 matching constraints. Also promote OP_OUT to OP_INOUT
1558 in predicated instructions. */
1560 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1561 for (i = 0; i < n_ops; ++i)
1563 int matches = recog_op_alt[i][alt].matches;
1564 if (matches >= 0)
1565 recog_op_alt[i][alt].class = recog_op_alt[matches][alt].class;
1566 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1567 || (predicated && recog_data.operand_type[i] == OP_OUT))
1568 recog_data.operand_type[i] = OP_INOUT;
1571 /* For each earlyclobber operand, zap the value data. */
1572 for (i = 0; i < n_ops; i++)
1573 if (recog_op_alt[i][alt].earlyclobber)
1574 kill_value (recog_data.operand[i], vd);
1576 /* Within asms, a clobber cannot overlap inputs or outputs.
1577 I wouldn't think this were true for regular insns, but
1578 scan_rtx treats them like that... */
1579 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1581 /* Kill all auto-incremented values. */
1582 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
1583 for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
1585 /* Kill all early-clobbered operands. */
1586 for (i = 0; i < n_ops; i++)
1587 if (recog_op_alt[i][alt].earlyclobber)
1588 kill_value (recog_data.operand[i], vd);
1590 /* Special-case plain move instructions, since we may well
1591 be able to do the move from a different register class. */
1592 if (set && REG_P (SET_SRC (set)))
1594 rtx src = SET_SRC (set);
1595 unsigned int regno = REGNO (src);
1596 enum machine_mode mode = GET_MODE (src);
1597 unsigned int i;
1598 rtx new;
1600 /* If we are accessing SRC in some mode other that what we
1601 set it in, make sure that the replacement is valid. */
1602 if (mode != vd->e[regno].mode)
1604 if (hard_regno_nregs[regno][mode]
1605 > hard_regno_nregs[regno][vd->e[regno].mode])
1606 goto no_move_special_case;
1609 /* If the destination is also a register, try to find a source
1610 register in the same class. */
1611 if (REG_P (SET_DEST (set)))
1613 new = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
1614 if (new && validate_change (insn, &SET_SRC (set), new, 0))
1616 if (dump_file)
1617 fprintf (dump_file,
1618 "insn %u: replaced reg %u with %u\n",
1619 INSN_UID (insn), regno, REGNO (new));
1620 changed = true;
1621 goto did_replacement;
1625 /* Otherwise, try all valid registers and see if its valid. */
1626 for (i = vd->e[regno].oldest_regno; i != regno;
1627 i = vd->e[i].next_regno)
1629 new = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
1630 mode, i, regno);
1631 if (new != NULL_RTX)
1633 if (validate_change (insn, &SET_SRC (set), new, 0))
1635 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (src);
1636 REG_ATTRS (new) = REG_ATTRS (src);
1637 if (dump_file)
1638 fprintf (dump_file,
1639 "insn %u: replaced reg %u with %u\n",
1640 INSN_UID (insn), regno, REGNO (new));
1641 changed = true;
1642 goto did_replacement;
1647 no_move_special_case:
1649 /* For each input operand, replace a hard register with the
1650 eldest live copy that's in an appropriate register class. */
1651 for (i = 0; i < n_ops; i++)
1653 bool replaced = false;
1655 /* Don't scan match_operand here, since we've no reg class
1656 information to pass down. Any operands that we could
1657 substitute in will be represented elsewhere. */
1658 if (recog_data.constraints[i][0] == '\0')
1659 continue;
1661 /* Don't replace in asms intentionally referencing hard regs. */
1662 if (is_asm && REG_P (recog_data.operand[i])
1663 && (REGNO (recog_data.operand[i])
1664 == ORIGINAL_REGNO (recog_data.operand[i])))
1665 continue;
1667 if (recog_data.operand_type[i] == OP_IN)
1669 if (recog_op_alt[i][alt].is_address)
1670 replaced
1671 = replace_oldest_value_addr (recog_data.operand_loc[i],
1672 recog_op_alt[i][alt].class,
1673 VOIDmode, insn, vd);
1674 else if (REG_P (recog_data.operand[i]))
1675 replaced
1676 = replace_oldest_value_reg (recog_data.operand_loc[i],
1677 recog_op_alt[i][alt].class,
1678 insn, vd);
1679 else if (GET_CODE (recog_data.operand[i]) == MEM)
1680 replaced = replace_oldest_value_mem (recog_data.operand[i],
1681 insn, vd);
1683 else if (GET_CODE (recog_data.operand[i]) == MEM)
1684 replaced = replace_oldest_value_mem (recog_data.operand[i],
1685 insn, vd);
1687 /* If we performed any replacement, update match_dups. */
1688 if (replaced)
1690 int j;
1691 rtx new;
1693 changed = true;
1695 new = *recog_data.operand_loc[i];
1696 recog_data.operand[i] = new;
1697 for (j = 0; j < recog_data.n_dups; j++)
1698 if (recog_data.dup_num[j] == i)
1699 *recog_data.dup_loc[j] = new;
1703 did_replacement:
1704 /* Clobber call-clobbered registers. */
1705 if (GET_CODE (insn) == CALL_INSN)
1706 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1707 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1708 kill_value_regno (i, vd);
1710 /* Notice stores. */
1711 note_stores (PATTERN (insn), kill_set_value, vd);
1713 /* Notice copies. */
1714 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1715 copy_value (SET_DEST (set), SET_SRC (set), vd);
1717 if (insn == BB_END (bb))
1718 break;
1721 return changed;
1724 /* Main entry point for the forward copy propagation optimization. */
1726 void
1727 copyprop_hardreg_forward (void)
1729 struct value_data *all_vd;
1730 bool need_refresh;
1731 basic_block bb, bbp = 0;
1733 need_refresh = false;
1735 all_vd = xmalloc (sizeof (struct value_data) * last_basic_block);
1737 FOR_EACH_BB (bb)
1739 /* If a block has a single predecessor, that we've already
1740 processed, begin with the value data that was live at
1741 the end of the predecessor block. */
1742 /* ??? Ought to use more intelligent queuing of blocks. */
1743 if (bb->pred)
1744 for (bbp = bb; bbp && bbp != bb->pred->src; bbp = bbp->prev_bb);
1745 if (bb->pred
1746 && ! bb->pred->pred_next
1747 && ! (bb->pred->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1748 && bb->pred->src != ENTRY_BLOCK_PTR
1749 && bbp)
1750 all_vd[bb->index] = all_vd[bb->pred->src->index];
1751 else
1752 init_value_data (all_vd + bb->index);
1754 if (copyprop_hardreg_forward_1 (bb, all_vd + bb->index))
1755 need_refresh = true;
1758 if (need_refresh)
1760 if (dump_file)
1761 fputs ("\n\n", dump_file);
1763 /* ??? Irritatingly, delete_noop_moves does not take a set of blocks
1764 to scan, so we have to do a life update with no initial set of
1765 blocks Just In Case. */
1766 delete_noop_moves ();
1767 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
1768 PROP_DEATH_NOTES
1769 | PROP_SCAN_DEAD_CODE
1770 | PROP_KILL_DEAD_CODE);
1773 free (all_vd);
1776 /* Dump the value chain data to stderr. */
1778 void
1779 debug_value_data (struct value_data *vd)
1781 HARD_REG_SET set;
1782 unsigned int i, j;
1784 CLEAR_HARD_REG_SET (set);
1786 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1787 if (vd->e[i].oldest_regno == i)
1789 if (vd->e[i].mode == VOIDmode)
1791 if (vd->e[i].next_regno != INVALID_REGNUM)
1792 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1793 i, vd->e[i].next_regno);
1794 continue;
1797 SET_HARD_REG_BIT (set, i);
1798 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1800 for (j = vd->e[i].next_regno;
1801 j != INVALID_REGNUM;
1802 j = vd->e[j].next_regno)
1804 if (TEST_HARD_REG_BIT (set, j))
1806 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1807 return;
1810 if (vd->e[j].oldest_regno != i)
1812 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1813 j, vd->e[j].oldest_regno);
1814 return;
1816 SET_HARD_REG_BIT (set, j);
1817 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1819 fputc ('\n', stderr);
1822 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1823 if (! TEST_HARD_REG_BIT (set, i)
1824 && (vd->e[i].mode != VOIDmode
1825 || vd->e[i].oldest_regno != i
1826 || vd->e[i].next_regno != INVALID_REGNUM))
1827 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1828 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1829 vd->e[i].next_regno);
1832 #ifdef ENABLE_CHECKING
1833 static void
1834 validate_value_data (struct value_data *vd)
1836 HARD_REG_SET set;
1837 unsigned int i, j;
1839 CLEAR_HARD_REG_SET (set);
1841 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1842 if (vd->e[i].oldest_regno == i)
1844 if (vd->e[i].mode == VOIDmode)
1846 if (vd->e[i].next_regno != INVALID_REGNUM)
1847 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1848 i, vd->e[i].next_regno);
1849 continue;
1852 SET_HARD_REG_BIT (set, i);
1854 for (j = vd->e[i].next_regno;
1855 j != INVALID_REGNUM;
1856 j = vd->e[j].next_regno)
1858 if (TEST_HARD_REG_BIT (set, j))
1859 internal_error ("validate_value_data: Loop in regno chain (%u)",
1861 if (vd->e[j].oldest_regno != i)
1862 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1863 j, vd->e[j].oldest_regno);
1865 SET_HARD_REG_BIT (set, j);
1869 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1870 if (! TEST_HARD_REG_BIT (set, i)
1871 && (vd->e[i].mode != VOIDmode
1872 || vd->e[i].oldest_regno != i
1873 || vd->e[i].next_regno != INVALID_REGNUM))
1874 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1875 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1876 vd->e[i].next_regno);
1878 #endif