* arm.h (REVERSE_CONDITION): Define.
[official-gcc.git] / gcc / regrename.c
blobb8f1955db910613c39ab3e00f083d707c4f4c9b6
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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "insn-config.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "reload.h"
32 #include "output.h"
33 #include "function.h"
34 #include "recog.h"
35 #include "flags.h"
36 #include "toplev.h"
37 #include "obstack.h"
39 #ifndef REG_MODE_OK_FOR_BASE_P
40 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
41 #endif
43 static const char *const reg_class_names[] = REG_CLASS_NAMES;
45 struct du_chain
47 struct du_chain *next_chain;
48 struct du_chain *next_use;
50 rtx insn;
51 rtx *loc;
52 ENUM_BITFIELD(reg_class) cl : 16;
53 unsigned int need_caller_save_reg:1;
54 unsigned int earlyclobber:1;
57 enum scan_actions
59 terminate_all_read,
60 terminate_overlapping_read,
61 terminate_write,
62 terminate_dead,
63 mark_read,
64 mark_write
67 static const char * const scan_actions_name[] =
69 "terminate_all_read",
70 "terminate_overlapping_read",
71 "terminate_write",
72 "terminate_dead",
73 "mark_read",
74 "mark_write"
77 static struct obstack rename_obstack;
79 static void do_replace (struct du_chain *, int);
80 static void scan_rtx_reg (rtx, rtx *, enum reg_class,
81 enum scan_actions, enum op_type, int);
82 static void scan_rtx_address (rtx, rtx *, enum reg_class,
83 enum scan_actions, enum machine_mode);
84 static void scan_rtx (rtx, rtx *, enum reg_class, enum scan_actions,
85 enum op_type, int);
86 static struct du_chain *build_def_use (basic_block);
87 static void dump_def_use_chain (struct du_chain *);
88 static void note_sets (rtx, rtx, void *);
89 static void clear_dead_regs (HARD_REG_SET *, enum machine_mode, rtx);
90 static void merge_overlapping_regs (basic_block, HARD_REG_SET *,
91 struct du_chain *);
93 /* Called through note_stores from update_life. Find sets of registers, and
94 record them in *DATA (which is actually a HARD_REG_SET *). */
96 static void
97 note_sets (rtx x, rtx set ATTRIBUTE_UNUSED, void *data)
99 HARD_REG_SET *pset = (HARD_REG_SET *) data;
100 unsigned int regno;
101 int nregs;
102 if (!REG_P (x))
103 return;
104 regno = REGNO (x);
105 nregs = hard_regno_nregs[regno][GET_MODE (x)];
107 /* There must not be pseudos at this point. */
108 if (regno + nregs > FIRST_PSEUDO_REGISTER)
109 abort ();
111 while (nregs-- > 0)
112 SET_HARD_REG_BIT (*pset, regno + nregs);
115 /* Clear all registers from *PSET for which a note of kind KIND can be found
116 in the list NOTES. */
118 static void
119 clear_dead_regs (HARD_REG_SET *pset, enum machine_mode kind, rtx notes)
121 rtx note;
122 for (note = notes; note; note = XEXP (note, 1))
123 if (REG_NOTE_KIND (note) == kind && REG_P (XEXP (note, 0)))
125 rtx reg = XEXP (note, 0);
126 unsigned int regno = REGNO (reg);
127 int nregs = hard_regno_nregs[regno][GET_MODE (reg)];
129 /* There must not be pseudos at this point. */
130 if (regno + nregs > FIRST_PSEUDO_REGISTER)
131 abort ();
133 while (nregs-- > 0)
134 CLEAR_HARD_REG_BIT (*pset, regno + nregs);
138 /* For a def-use chain CHAIN in basic block B, find which registers overlap
139 its lifetime and set the corresponding bits in *PSET. */
141 static void
142 merge_overlapping_regs (basic_block b, HARD_REG_SET *pset,
143 struct du_chain *chain)
145 struct du_chain *t = chain;
146 rtx insn;
147 HARD_REG_SET live;
149 REG_SET_TO_HARD_REG_SET (live, b->global_live_at_start);
150 insn = BB_HEAD (b);
151 while (t)
153 /* Search forward until the next reference to the register to be
154 renamed. */
155 while (insn != t->insn)
157 if (INSN_P (insn))
159 clear_dead_regs (&live, REG_DEAD, REG_NOTES (insn));
160 note_stores (PATTERN (insn), note_sets, (void *) &live);
161 /* Only record currently live regs if we are inside the
162 reg's live range. */
163 if (t != chain)
164 IOR_HARD_REG_SET (*pset, live);
165 clear_dead_regs (&live, REG_UNUSED, REG_NOTES (insn));
167 insn = NEXT_INSN (insn);
170 IOR_HARD_REG_SET (*pset, live);
172 /* For the last reference, also merge in all registers set in the
173 same insn.
174 @@@ We only have take earlyclobbered sets into account. */
175 if (! t->next_use)
176 note_stores (PATTERN (insn), note_sets, (void *) pset);
178 t = t->next_use;
182 /* Perform register renaming on the current function. */
184 void
185 regrename_optimize (void)
187 int tick[FIRST_PSEUDO_REGISTER];
188 int this_tick = 0;
189 basic_block bb;
190 char *first_obj;
192 memset (tick, 0, sizeof tick);
194 gcc_obstack_init (&rename_obstack);
195 first_obj = obstack_alloc (&rename_obstack, 0);
197 FOR_EACH_BB (bb)
199 struct du_chain *all_chains = 0;
200 HARD_REG_SET unavailable;
201 HARD_REG_SET regs_seen;
203 CLEAR_HARD_REG_SET (unavailable);
205 if (dump_file)
206 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
208 all_chains = build_def_use (bb);
210 if (dump_file)
211 dump_def_use_chain (all_chains);
213 CLEAR_HARD_REG_SET (unavailable);
214 /* Don't clobber traceback for noreturn functions. */
215 if (frame_pointer_needed)
217 int i;
219 for (i = hard_regno_nregs[FRAME_POINTER_REGNUM][Pmode]; i--;)
220 SET_HARD_REG_BIT (unavailable, FRAME_POINTER_REGNUM + i);
222 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
223 for (i = hard_regno_nregs[HARD_FRAME_POINTER_REGNUM][Pmode]; i--;)
224 SET_HARD_REG_BIT (unavailable, HARD_FRAME_POINTER_REGNUM + i);
225 #endif
228 CLEAR_HARD_REG_SET (regs_seen);
229 while (all_chains)
231 int new_reg, best_new_reg;
232 int n_uses;
233 struct du_chain *this = all_chains;
234 struct du_chain *tmp, *last;
235 HARD_REG_SET this_unavailable;
236 int reg = REGNO (*this->loc);
237 int i;
239 all_chains = this->next_chain;
241 best_new_reg = reg;
243 #if 0 /* This just disables optimization opportunities. */
244 /* Only rename once we've seen the reg more than once. */
245 if (! TEST_HARD_REG_BIT (regs_seen, reg))
247 SET_HARD_REG_BIT (regs_seen, reg);
248 continue;
250 #endif
252 if (fixed_regs[reg] || global_regs[reg]
253 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
254 || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
255 #else
256 || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
257 #endif
259 continue;
261 COPY_HARD_REG_SET (this_unavailable, unavailable);
263 /* Find last entry on chain (which has the need_caller_save bit),
264 count number of uses, and narrow the set of registers we can
265 use for renaming. */
266 n_uses = 0;
267 for (last = this; last->next_use; last = last->next_use)
269 n_uses++;
270 IOR_COMPL_HARD_REG_SET (this_unavailable,
271 reg_class_contents[last->cl]);
273 if (n_uses < 1)
274 continue;
276 IOR_COMPL_HARD_REG_SET (this_unavailable,
277 reg_class_contents[last->cl]);
279 if (this->need_caller_save_reg)
280 IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
282 merge_overlapping_regs (bb, &this_unavailable, this);
284 /* Now potential_regs is a reasonable approximation, let's
285 have a closer look at each register still in there. */
286 for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
288 int nregs = hard_regno_nregs[new_reg][GET_MODE (*this->loc)];
290 for (i = nregs - 1; i >= 0; --i)
291 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
292 || fixed_regs[new_reg + i]
293 || global_regs[new_reg + i]
294 /* Can't use regs which aren't saved by the prologue. */
295 || (! regs_ever_live[new_reg + i]
296 && ! call_used_regs[new_reg + i])
297 #ifdef LEAF_REGISTERS
298 /* We can't use a non-leaf register if we're in a
299 leaf function. */
300 || (current_function_is_leaf
301 && !LEAF_REGISTERS[new_reg + i])
302 #endif
303 #ifdef HARD_REGNO_RENAME_OK
304 || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
305 #endif
307 break;
308 if (i >= 0)
309 continue;
311 /* See whether it accepts all modes that occur in
312 definition and uses. */
313 for (tmp = this; tmp; tmp = tmp->next_use)
314 if (! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
315 || (tmp->need_caller_save_reg
316 && ! (HARD_REGNO_CALL_PART_CLOBBERED
317 (reg, GET_MODE (*tmp->loc)))
318 && (HARD_REGNO_CALL_PART_CLOBBERED
319 (new_reg, GET_MODE (*tmp->loc)))))
320 break;
321 if (! tmp)
323 if (tick[best_new_reg] > tick[new_reg])
324 best_new_reg = new_reg;
328 if (dump_file)
330 fprintf (dump_file, "Register %s in insn %d",
331 reg_names[reg], INSN_UID (last->insn));
332 if (last->need_caller_save_reg)
333 fprintf (dump_file, " crosses a call");
336 if (best_new_reg == reg)
338 tick[reg] = ++this_tick;
339 if (dump_file)
340 fprintf (dump_file, "; no available better choice\n");
341 continue;
344 do_replace (this, best_new_reg);
345 tick[best_new_reg] = ++this_tick;
346 regs_ever_live[best_new_reg] = 1;
348 if (dump_file)
349 fprintf (dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
352 obstack_free (&rename_obstack, first_obj);
355 obstack_free (&rename_obstack, NULL);
357 if (dump_file)
358 fputc ('\n', dump_file);
360 count_or_remove_death_notes (NULL, 1);
361 update_life_info (NULL, UPDATE_LIFE_LOCAL,
362 PROP_DEATH_NOTES);
365 static void
366 do_replace (struct du_chain *chain, int reg)
368 while (chain)
370 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
371 struct reg_attrs * attr = REG_ATTRS (*chain->loc);
373 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
374 if (regno >= FIRST_PSEUDO_REGISTER)
375 ORIGINAL_REGNO (*chain->loc) = regno;
376 REG_ATTRS (*chain->loc) = attr;
377 chain = chain->next_use;
382 static struct du_chain *open_chains;
383 static struct du_chain *closed_chains;
385 static void
386 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class cl,
387 enum scan_actions action, enum op_type type, int earlyclobber)
389 struct du_chain **p;
390 rtx x = *loc;
391 enum machine_mode mode = GET_MODE (x);
392 int this_regno = REGNO (x);
393 int this_nregs = hard_regno_nregs[this_regno][mode];
395 if (action == mark_write)
397 if (type == OP_OUT)
399 struct du_chain *this
400 = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
401 this->next_use = 0;
402 this->next_chain = open_chains;
403 this->loc = loc;
404 this->insn = insn;
405 this->cl = cl;
406 this->need_caller_save_reg = 0;
407 this->earlyclobber = earlyclobber;
408 open_chains = this;
410 return;
413 if ((type == OP_OUT && action != terminate_write)
414 || (type != OP_OUT && action == terminate_write))
415 return;
417 for (p = &open_chains; *p;)
419 struct du_chain *this = *p;
421 /* Check if the chain has been terminated if it has then skip to
422 the next chain.
424 This can happen when we've already appended the location to
425 the chain in Step 3, but are trying to hide in-out operands
426 from terminate_write in Step 5. */
428 if (*this->loc == cc0_rtx)
429 p = &this->next_chain;
430 else
432 int regno = REGNO (*this->loc);
433 int nregs = hard_regno_nregs[regno][GET_MODE (*this->loc)];
434 int exact_match = (regno == this_regno && nregs == this_nregs);
436 if (regno + nregs <= this_regno
437 || this_regno + this_nregs <= regno)
439 p = &this->next_chain;
440 continue;
443 if (action == mark_read)
445 if (! exact_match)
446 abort ();
448 /* ??? Class NO_REGS can happen if the md file makes use of
449 EXTRA_CONSTRAINTS to match registers. Which is arguably
450 wrong, but there we are. Since we know not what this may
451 be replaced with, terminate the chain. */
452 if (cl != NO_REGS)
454 this = obstack_alloc (&rename_obstack, sizeof (struct du_chain));
455 this->next_use = 0;
456 this->next_chain = (*p)->next_chain;
457 this->loc = loc;
458 this->insn = insn;
459 this->cl = cl;
460 this->need_caller_save_reg = 0;
461 while (*p)
462 p = &(*p)->next_use;
463 *p = this;
464 return;
468 if (action != terminate_overlapping_read || ! exact_match)
470 struct du_chain *next = this->next_chain;
472 /* Whether the terminated chain can be used for renaming
473 depends on the action and this being an exact match.
474 In either case, we remove this element from open_chains. */
476 if ((action == terminate_dead || action == terminate_write)
477 && exact_match)
479 this->next_chain = closed_chains;
480 closed_chains = this;
481 if (dump_file)
482 fprintf (dump_file,
483 "Closing chain %s at insn %d (%s)\n",
484 reg_names[REGNO (*this->loc)], INSN_UID (insn),
485 scan_actions_name[(int) action]);
487 else
489 if (dump_file)
490 fprintf (dump_file,
491 "Discarding chain %s at insn %d (%s)\n",
492 reg_names[REGNO (*this->loc)], INSN_UID (insn),
493 scan_actions_name[(int) action]);
495 *p = next;
497 else
498 p = &this->next_chain;
503 /* Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
504 BASE_REG_CLASS depending on how the register is being considered. */
506 static void
507 scan_rtx_address (rtx insn, rtx *loc, enum reg_class cl,
508 enum scan_actions action, enum machine_mode mode)
510 rtx x = *loc;
511 RTX_CODE code = GET_CODE (x);
512 const char *fmt;
513 int i, j;
515 if (action == mark_write)
516 return;
518 switch (code)
520 case PLUS:
522 rtx orig_op0 = XEXP (x, 0);
523 rtx orig_op1 = XEXP (x, 1);
524 RTX_CODE code0 = GET_CODE (orig_op0);
525 RTX_CODE code1 = GET_CODE (orig_op1);
526 rtx op0 = orig_op0;
527 rtx op1 = orig_op1;
528 rtx *locI = NULL;
529 rtx *locB = NULL;
531 if (GET_CODE (op0) == SUBREG)
533 op0 = SUBREG_REG (op0);
534 code0 = GET_CODE (op0);
537 if (GET_CODE (op1) == SUBREG)
539 op1 = SUBREG_REG (op1);
540 code1 = GET_CODE (op1);
543 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
544 || code0 == ZERO_EXTEND || code1 == MEM)
546 locI = &XEXP (x, 0);
547 locB = &XEXP (x, 1);
549 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
550 || code1 == ZERO_EXTEND || code0 == MEM)
552 locI = &XEXP (x, 1);
553 locB = &XEXP (x, 0);
555 else if (code0 == CONST_INT || code0 == CONST
556 || code0 == SYMBOL_REF || code0 == LABEL_REF)
557 locB = &XEXP (x, 1);
558 else if (code1 == CONST_INT || code1 == CONST
559 || code1 == SYMBOL_REF || code1 == LABEL_REF)
560 locB = &XEXP (x, 0);
561 else if (code0 == REG && code1 == REG)
563 int index_op;
565 if (REG_OK_FOR_INDEX_P (op0)
566 && REG_MODE_OK_FOR_BASE_P (op1, mode))
567 index_op = 0;
568 else if (REG_OK_FOR_INDEX_P (op1)
569 && REG_MODE_OK_FOR_BASE_P (op0, mode))
570 index_op = 1;
571 else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
572 index_op = 0;
573 else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
574 index_op = 1;
575 else if (REG_OK_FOR_INDEX_P (op1))
576 index_op = 1;
577 else
578 index_op = 0;
580 locI = &XEXP (x, index_op);
581 locB = &XEXP (x, !index_op);
583 else if (code0 == REG)
585 locI = &XEXP (x, 0);
586 locB = &XEXP (x, 1);
588 else if (code1 == REG)
590 locI = &XEXP (x, 1);
591 locB = &XEXP (x, 0);
594 if (locI)
595 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
596 if (locB)
597 scan_rtx_address (insn, locB, MODE_BASE_REG_CLASS (mode), action, mode);
598 return;
601 case POST_INC:
602 case POST_DEC:
603 case POST_MODIFY:
604 case PRE_INC:
605 case PRE_DEC:
606 case PRE_MODIFY:
607 #ifndef AUTO_INC_DEC
608 /* If the target doesn't claim to handle autoinc, this must be
609 something special, like a stack push. Kill this chain. */
610 action = terminate_all_read;
611 #endif
612 break;
614 case MEM:
615 scan_rtx_address (insn, &XEXP (x, 0),
616 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
617 GET_MODE (x));
618 return;
620 case REG:
621 scan_rtx_reg (insn, loc, cl, action, OP_IN, 0);
622 return;
624 default:
625 break;
628 fmt = GET_RTX_FORMAT (code);
629 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
631 if (fmt[i] == 'e')
632 scan_rtx_address (insn, &XEXP (x, i), cl, action, mode);
633 else if (fmt[i] == 'E')
634 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
635 scan_rtx_address (insn, &XVECEXP (x, i, j), cl, action, mode);
639 static void
640 scan_rtx (rtx insn, rtx *loc, enum reg_class cl,
641 enum scan_actions action, enum op_type type, int earlyclobber)
643 const char *fmt;
644 rtx x = *loc;
645 enum rtx_code code = GET_CODE (x);
646 int i, j;
648 code = GET_CODE (x);
649 switch (code)
651 case CONST:
652 case CONST_INT:
653 case CONST_DOUBLE:
654 case CONST_VECTOR:
655 case SYMBOL_REF:
656 case LABEL_REF:
657 case CC0:
658 case PC:
659 return;
661 case REG:
662 scan_rtx_reg (insn, loc, cl, action, type, earlyclobber);
663 return;
665 case MEM:
666 scan_rtx_address (insn, &XEXP (x, 0),
667 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
668 GET_MODE (x));
669 return;
671 case SET:
672 scan_rtx (insn, &SET_SRC (x), cl, action, OP_IN, 0);
673 scan_rtx (insn, &SET_DEST (x), cl, action, 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 abort ();
697 case CLOBBER:
698 scan_rtx (insn, &SET_DEST (x), cl, action, OP_OUT, 1);
699 return;
701 case EXPR_LIST:
702 scan_rtx (insn, &XEXP (x, 0), cl, action, type, 0);
703 if (XEXP (x, 1))
704 scan_rtx (insn, &XEXP (x, 1), cl, action, type, 0);
705 return;
707 default:
708 break;
711 fmt = GET_RTX_FORMAT (code);
712 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
714 if (fmt[i] == 'e')
715 scan_rtx (insn, &XEXP (x, i), cl, action, type, 0);
716 else if (fmt[i] == 'E')
717 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
718 scan_rtx (insn, &XVECEXP (x, i, j), cl, action, type, 0);
722 /* Build def/use chain. */
724 static struct du_chain *
725 build_def_use (basic_block bb)
727 rtx insn;
729 open_chains = closed_chains = NULL;
731 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
733 if (INSN_P (insn))
735 int n_ops;
736 rtx note;
737 rtx old_operands[MAX_RECOG_OPERANDS];
738 rtx old_dups[MAX_DUP_OPERANDS];
739 int i, icode;
740 int alt;
741 int predicated;
743 /* Process the insn, determining its effect on the def-use
744 chains. We perform the following steps with the register
745 references in the insn:
746 (1) Any read that overlaps an open chain, but doesn't exactly
747 match, causes that chain to be closed. We can't deal
748 with overlaps yet.
749 (2) Any read outside an operand causes any chain it overlaps
750 with to be closed, since we can't replace it.
751 (3) Any read inside an operand is added if there's already
752 an open chain for it.
753 (4) For any REG_DEAD note we find, close open chains that
754 overlap it.
755 (5) For any write we find, close open chains that overlap it.
756 (6) For any write we find in an operand, make a new chain.
757 (7) For any REG_UNUSED, close any chains we just opened. */
759 icode = recog_memoized (insn);
760 extract_insn (insn);
761 if (! constrain_operands (1))
762 fatal_insn_not_found (insn);
763 preprocess_constraints ();
764 alt = which_alternative;
765 n_ops = recog_data.n_operands;
767 /* Simplify the code below by rewriting things to reflect
768 matching constraints. Also promote OP_OUT to OP_INOUT
769 in predicated instructions. */
771 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
772 for (i = 0; i < n_ops; ++i)
774 int matches = recog_op_alt[i][alt].matches;
775 if (matches >= 0)
776 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
777 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
778 || (predicated && recog_data.operand_type[i] == OP_OUT))
779 recog_data.operand_type[i] = OP_INOUT;
782 /* Step 1: Close chains for which we have overlapping reads. */
783 for (i = 0; i < n_ops; i++)
784 scan_rtx (insn, recog_data.operand_loc[i],
785 NO_REGS, terminate_overlapping_read,
786 recog_data.operand_type[i], 0);
788 /* Step 2: Close chains for which we have reads outside operands.
789 We do this by munging all operands into CC0, and closing
790 everything remaining. */
792 for (i = 0; i < n_ops; i++)
794 old_operands[i] = recog_data.operand[i];
795 /* Don't squash match_operator or match_parallel here, since
796 we don't know that all of the contained registers are
797 reachable by proper operands. */
798 if (recog_data.constraints[i][0] == '\0')
799 continue;
800 *recog_data.operand_loc[i] = cc0_rtx;
802 for (i = 0; i < recog_data.n_dups; i++)
804 int dup_num = recog_data.dup_num[i];
806 old_dups[i] = *recog_data.dup_loc[i];
807 *recog_data.dup_loc[i] = cc0_rtx;
809 /* For match_dup of match_operator or match_parallel, share
810 them, so that we don't miss changes in the dup. */
811 if (icode >= 0
812 && insn_data[icode].operand[dup_num].eliminable == 0)
813 old_dups[i] = recog_data.operand[dup_num];
816 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
817 OP_IN, 0);
819 for (i = 0; i < recog_data.n_dups; i++)
820 *recog_data.dup_loc[i] = old_dups[i];
821 for (i = 0; i < n_ops; i++)
822 *recog_data.operand_loc[i] = old_operands[i];
824 /* Step 2B: Can't rename function call argument registers. */
825 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
826 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
827 NO_REGS, terminate_all_read, OP_IN, 0);
829 /* Step 2C: Can't rename asm operands that were originally
830 hard registers. */
831 if (asm_noperands (PATTERN (insn)) > 0)
832 for (i = 0; i < n_ops; i++)
834 rtx *loc = recog_data.operand_loc[i];
835 rtx op = *loc;
837 if (REG_P (op)
838 && REGNO (op) == ORIGINAL_REGNO (op)
839 && (recog_data.operand_type[i] == OP_IN
840 || recog_data.operand_type[i] == OP_INOUT))
841 scan_rtx (insn, loc, NO_REGS, terminate_all_read, OP_IN, 0);
844 /* Step 3: Append to chains for reads inside operands. */
845 for (i = 0; i < n_ops + recog_data.n_dups; i++)
847 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
848 rtx *loc = (i < n_ops
849 ? recog_data.operand_loc[opn]
850 : recog_data.dup_loc[i - n_ops]);
851 enum reg_class cl = recog_op_alt[opn][alt].cl;
852 enum op_type type = recog_data.operand_type[opn];
854 /* Don't scan match_operand here, since we've no reg class
855 information to pass down. Any operands that we could
856 substitute in will be represented elsewhere. */
857 if (recog_data.constraints[opn][0] == '\0')
858 continue;
860 if (recog_op_alt[opn][alt].is_address)
861 scan_rtx_address (insn, loc, cl, mark_read, VOIDmode);
862 else
863 scan_rtx (insn, loc, cl, mark_read, type, 0);
866 /* Step 4: Close chains for registers that die here.
867 Also record updates for REG_INC notes. */
868 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
870 if (REG_NOTE_KIND (note) == REG_DEAD)
871 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
872 OP_IN, 0);
873 else if (REG_NOTE_KIND (note) == REG_INC)
874 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
875 OP_INOUT, 0);
878 /* Step 4B: If this is a call, any chain live at this point
879 requires a caller-saved reg. */
880 if (CALL_P (insn))
882 struct du_chain *p;
883 for (p = open_chains; p; p = p->next_chain)
884 p->need_caller_save_reg = 1;
887 /* Step 5: Close open chains that overlap writes. Similar to
888 step 2, we hide in-out operands, since we do not want to
889 close these chains. */
891 for (i = 0; i < n_ops; i++)
893 old_operands[i] = recog_data.operand[i];
894 if (recog_data.operand_type[i] == OP_INOUT)
895 *recog_data.operand_loc[i] = cc0_rtx;
897 for (i = 0; i < recog_data.n_dups; i++)
899 int opn = recog_data.dup_num[i];
900 old_dups[i] = *recog_data.dup_loc[i];
901 if (recog_data.operand_type[opn] == OP_INOUT)
902 *recog_data.dup_loc[i] = cc0_rtx;
905 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
907 for (i = 0; i < recog_data.n_dups; i++)
908 *recog_data.dup_loc[i] = old_dups[i];
909 for (i = 0; i < n_ops; i++)
910 *recog_data.operand_loc[i] = old_operands[i];
912 /* Step 6: Begin new chains for writes inside operands. */
913 /* ??? Many targets have output constraints on the SET_DEST
914 of a call insn, which is stupid, since these are certainly
915 ABI defined hard registers. Don't change calls at all.
916 Similarly take special care for asm statement that originally
917 referenced hard registers. */
918 if (asm_noperands (PATTERN (insn)) > 0)
920 for (i = 0; i < n_ops; i++)
921 if (recog_data.operand_type[i] == OP_OUT)
923 rtx *loc = recog_data.operand_loc[i];
924 rtx op = *loc;
925 enum reg_class cl = recog_op_alt[i][alt].cl;
927 if (REG_P (op)
928 && REGNO (op) == ORIGINAL_REGNO (op))
929 continue;
931 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
932 recog_op_alt[i][alt].earlyclobber);
935 else if (!CALL_P (insn))
936 for (i = 0; i < n_ops + recog_data.n_dups; i++)
938 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
939 rtx *loc = (i < n_ops
940 ? recog_data.operand_loc[opn]
941 : recog_data.dup_loc[i - n_ops]);
942 enum reg_class cl = recog_op_alt[opn][alt].cl;
944 if (recog_data.operand_type[opn] == OP_OUT)
945 scan_rtx (insn, loc, cl, mark_write, OP_OUT,
946 recog_op_alt[opn][alt].earlyclobber);
949 /* Step 7: Close chains for registers that were never
950 really used here. */
951 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
952 if (REG_NOTE_KIND (note) == REG_UNUSED)
953 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
954 OP_IN, 0);
956 if (insn == BB_END (bb))
957 break;
960 /* Since we close every chain when we find a REG_DEAD note, anything that
961 is still open lives past the basic block, so it can't be renamed. */
962 return closed_chains;
965 /* Dump all def/use chains in CHAINS to DUMP_FILE. They are
966 printed in reverse order as that's how we build them. */
968 static void
969 dump_def_use_chain (struct du_chain *chains)
971 while (chains)
973 struct du_chain *this = chains;
974 int r = REGNO (*this->loc);
975 int nregs = hard_regno_nregs[r][GET_MODE (*this->loc)];
976 fprintf (dump_file, "Register %s (%d):", reg_names[r], nregs);
977 while (this)
979 fprintf (dump_file, " %d [%s]", INSN_UID (this->insn),
980 reg_class_names[this->cl]);
981 this = this->next_use;
983 fprintf (dump_file, "\n");
984 chains = chains->next_chain;
988 /* The following code does forward propagation of hard register copies.
989 The object is to eliminate as many dependencies as possible, so that
990 we have the most scheduling freedom. As a side effect, we also clean
991 up some silly register allocation decisions made by reload. This
992 code may be obsoleted by a new register allocator. */
994 /* For each register, we have a list of registers that contain the same
995 value. The OLDEST_REGNO field points to the head of the list, and
996 the NEXT_REGNO field runs through the list. The MODE field indicates
997 what mode the data is known to be in; this field is VOIDmode when the
998 register is not known to contain valid data. */
1000 struct value_data_entry
1002 enum machine_mode mode;
1003 unsigned int oldest_regno;
1004 unsigned int next_regno;
1007 struct value_data
1009 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
1010 unsigned int max_value_regs;
1013 static void kill_value_one_regno (unsigned, struct value_data *);
1014 static void kill_value_regno (unsigned, unsigned, struct value_data *);
1015 static void kill_value (rtx, struct value_data *);
1016 static void set_value_regno (unsigned, enum machine_mode, struct value_data *);
1017 static void init_value_data (struct value_data *);
1018 static void kill_clobbered_value (rtx, rtx, void *);
1019 static void kill_set_value (rtx, rtx, void *);
1020 static int kill_autoinc_value (rtx *, void *);
1021 static void copy_value (rtx, rtx, struct value_data *);
1022 static bool mode_change_ok (enum machine_mode, enum machine_mode,
1023 unsigned int);
1024 static rtx maybe_mode_change (enum machine_mode, enum machine_mode,
1025 enum machine_mode, unsigned int, unsigned int);
1026 static rtx find_oldest_value_reg (enum reg_class, rtx, struct value_data *);
1027 static bool replace_oldest_value_reg (rtx *, enum reg_class, rtx,
1028 struct value_data *);
1029 static bool replace_oldest_value_addr (rtx *, enum reg_class,
1030 enum machine_mode, rtx,
1031 struct value_data *);
1032 static bool replace_oldest_value_mem (rtx, rtx, struct value_data *);
1033 static bool copyprop_hardreg_forward_1 (basic_block, struct value_data *);
1034 extern void debug_value_data (struct value_data *);
1035 #ifdef ENABLE_CHECKING
1036 static void validate_value_data (struct value_data *);
1037 #endif
1039 /* Kill register REGNO. This involves removing it from any value
1040 lists, and resetting the value mode to VOIDmode. This is only a
1041 helper function; it does not handle any hard registers overlapping
1042 with REGNO. */
1044 static void
1045 kill_value_one_regno (unsigned int regno, struct value_data *vd)
1047 unsigned int i, next;
1049 if (vd->e[regno].oldest_regno != regno)
1051 for (i = vd->e[regno].oldest_regno;
1052 vd->e[i].next_regno != regno;
1053 i = vd->e[i].next_regno)
1054 continue;
1055 vd->e[i].next_regno = vd->e[regno].next_regno;
1057 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
1059 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
1060 vd->e[i].oldest_regno = next;
1063 vd->e[regno].mode = VOIDmode;
1064 vd->e[regno].oldest_regno = regno;
1065 vd->e[regno].next_regno = INVALID_REGNUM;
1067 #ifdef ENABLE_CHECKING
1068 validate_value_data (vd);
1069 #endif
1072 /* Kill the value in register REGNO for NREGS, and any other registers
1073 whose values overlap. */
1075 static void
1076 kill_value_regno (unsigned int regno, unsigned int nregs,
1077 struct value_data *vd)
1079 unsigned int j;
1081 /* Kill the value we're told to kill. */
1082 for (j = 0; j < nregs; ++j)
1083 kill_value_one_regno (regno + j, vd);
1085 /* Kill everything that overlapped what we're told to kill. */
1086 if (regno < vd->max_value_regs)
1087 j = 0;
1088 else
1089 j = regno - vd->max_value_regs;
1090 for (; j < regno; ++j)
1092 unsigned int i, n;
1093 if (vd->e[j].mode == VOIDmode)
1094 continue;
1095 n = hard_regno_nregs[j][vd->e[j].mode];
1096 if (j + n > regno)
1097 for (i = 0; i < n; ++i)
1098 kill_value_one_regno (j + i, vd);
1102 /* Kill X. This is a convenience function wrapping kill_value_regno
1103 so that we mind the mode the register is in. */
1105 static void
1106 kill_value (rtx x, struct value_data *vd)
1108 /* SUBREGS are supposed to have been eliminated by now. But some
1109 ports, e.g. i386 sse, use them to smuggle vector type information
1110 through to instruction selection. Each such SUBREG should simplify,
1111 so if we get a NULL we've done something wrong elsewhere. */
1113 if (GET_CODE (x) == SUBREG)
1114 x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
1115 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
1116 if (REG_P (x))
1118 unsigned int regno = REGNO (x);
1119 unsigned int n = hard_regno_nregs[regno][GET_MODE (x)];
1121 kill_value_regno (regno, n, vd);
1125 /* Remember that REGNO is valid in MODE. */
1127 static void
1128 set_value_regno (unsigned int regno, enum machine_mode mode,
1129 struct value_data *vd)
1131 unsigned int nregs;
1133 vd->e[regno].mode = mode;
1135 nregs = hard_regno_nregs[regno][mode];
1136 if (nregs > vd->max_value_regs)
1137 vd->max_value_regs = nregs;
1140 /* Initialize VD such that there are no known relationships between regs. */
1142 static void
1143 init_value_data (struct value_data *vd)
1145 int i;
1146 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1148 vd->e[i].mode = VOIDmode;
1149 vd->e[i].oldest_regno = i;
1150 vd->e[i].next_regno = INVALID_REGNUM;
1152 vd->max_value_regs = 0;
1155 /* Called through note_stores. If X is clobbered, kill its value. */
1157 static void
1158 kill_clobbered_value (rtx x, rtx set, void *data)
1160 struct value_data *vd = data;
1161 if (GET_CODE (set) == CLOBBER)
1162 kill_value (x, vd);
1165 /* Called through note_stores. If X is set, not clobbered, kill its
1166 current value and install it as the root of its own value list. */
1168 static void
1169 kill_set_value (rtx x, rtx set, void *data)
1171 struct value_data *vd = data;
1172 if (GET_CODE (set) != CLOBBER)
1174 kill_value (x, vd);
1175 if (REG_P (x))
1176 set_value_regno (REGNO (x), GET_MODE (x), vd);
1180 /* Called through for_each_rtx. Kill any register used as the base of an
1181 auto-increment expression, and install that register as the root of its
1182 own value list. */
1184 static int
1185 kill_autoinc_value (rtx *px, void *data)
1187 rtx x = *px;
1188 struct value_data *vd = data;
1190 if (GET_RTX_CLASS (GET_CODE (x)) == RTX_AUTOINC)
1192 x = XEXP (x, 0);
1193 kill_value (x, vd);
1194 set_value_regno (REGNO (x), Pmode, vd);
1195 return -1;
1198 return 0;
1201 /* Assert that SRC has been copied to DEST. Adjust the data structures
1202 to reflect that SRC contains an older copy of the shared value. */
1204 static void
1205 copy_value (rtx dest, rtx src, struct value_data *vd)
1207 unsigned int dr = REGNO (dest);
1208 unsigned int sr = REGNO (src);
1209 unsigned int dn, sn;
1210 unsigned int i;
1212 /* ??? At present, it's possible to see noop sets. It'd be nice if
1213 this were cleaned up beforehand... */
1214 if (sr == dr)
1215 return;
1217 /* Do not propagate copies to the stack pointer, as that can leave
1218 memory accesses with no scheduling dependency on the stack update. */
1219 if (dr == STACK_POINTER_REGNUM)
1220 return;
1222 /* Likewise with the frame pointer, if we're using one. */
1223 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
1224 return;
1226 /* If SRC and DEST overlap, don't record anything. */
1227 dn = hard_regno_nregs[dr][GET_MODE (dest)];
1228 sn = hard_regno_nregs[sr][GET_MODE (dest)];
1229 if ((dr > sr && dr < sr + sn)
1230 || (sr > dr && sr < dr + dn))
1231 return;
1233 /* If SRC had no assigned mode (i.e. we didn't know it was live)
1234 assign it now and assume the value came from an input argument
1235 or somesuch. */
1236 if (vd->e[sr].mode == VOIDmode)
1237 set_value_regno (sr, vd->e[dr].mode, vd);
1239 /* If we are narrowing the input to a smaller number of hard regs,
1240 and it is in big endian, we are really extracting a high part.
1241 Since we generally associate a low part of a value with the value itself,
1242 we must not do the same for the high part.
1243 Note we can still get low parts for the same mode combination through
1244 a two-step copy involving differently sized hard regs.
1245 Assume hard regs fr* are 32 bits bits each, while r* are 64 bits each:
1246 (set (reg:DI r0) (reg:DI fr0))
1247 (set (reg:SI fr2) (reg:SI r0))
1248 loads the low part of (reg:DI fr0) - i.e. fr1 - into fr2, while:
1249 (set (reg:SI fr2) (reg:SI fr0))
1250 loads the high part of (reg:DI fr0) into fr2.
1252 We can't properly represent the latter case in our tables, so don't
1253 record anything then. */
1254 else if (sn < (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode]
1255 && (GET_MODE_SIZE (vd->e[sr].mode) > UNITS_PER_WORD
1256 ? WORDS_BIG_ENDIAN : BYTES_BIG_ENDIAN))
1257 return;
1259 /* If SRC had been assigned a mode narrower than the copy, we can't
1260 link DEST into the chain, because not all of the pieces of the
1261 copy came from oldest_regno. */
1262 else if (sn > (unsigned int) hard_regno_nregs[sr][vd->e[sr].mode])
1263 return;
1265 /* Link DR at the end of the value chain used by SR. */
1267 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
1269 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
1270 continue;
1271 vd->e[i].next_regno = dr;
1273 #ifdef ENABLE_CHECKING
1274 validate_value_data (vd);
1275 #endif
1278 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
1280 static bool
1281 mode_change_ok (enum machine_mode orig_mode, enum machine_mode new_mode,
1282 unsigned int regno ATTRIBUTE_UNUSED)
1284 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
1285 return false;
1287 #ifdef CANNOT_CHANGE_MODE_CLASS
1288 return !REG_CANNOT_CHANGE_MODE_P (regno, orig_mode, new_mode);
1289 #endif
1291 return true;
1294 /* Register REGNO was originally set in ORIG_MODE. It - or a copy of it -
1295 was copied in COPY_MODE to COPY_REGNO, and then COPY_REGNO was accessed
1296 in NEW_MODE.
1297 Return a NEW_MODE rtx for REGNO if that's OK, otherwise return NULL_RTX. */
1299 static rtx
1300 maybe_mode_change (enum machine_mode orig_mode, enum machine_mode copy_mode,
1301 enum machine_mode new_mode, unsigned int regno,
1302 unsigned int copy_regno ATTRIBUTE_UNUSED)
1304 if (orig_mode == new_mode)
1305 return gen_rtx_raw_REG (new_mode, regno);
1306 else if (mode_change_ok (orig_mode, new_mode, regno))
1308 int copy_nregs = hard_regno_nregs[copy_regno][copy_mode];
1309 int use_nregs = hard_regno_nregs[copy_regno][new_mode];
1310 int copy_offset
1311 = GET_MODE_SIZE (copy_mode) / copy_nregs * (copy_nregs - use_nregs);
1312 int offset
1313 = GET_MODE_SIZE (orig_mode) - GET_MODE_SIZE (new_mode) - copy_offset;
1314 int byteoffset = offset % UNITS_PER_WORD;
1315 int wordoffset = offset - byteoffset;
1317 offset = ((WORDS_BIG_ENDIAN ? wordoffset : 0)
1318 + (BYTES_BIG_ENDIAN ? byteoffset : 0));
1319 return gen_rtx_raw_REG (new_mode,
1320 regno + subreg_regno_offset (regno, orig_mode,
1321 offset,
1322 new_mode));
1324 return NULL_RTX;
1327 /* Find the oldest copy of the value contained in REGNO that is in
1328 register class CL and has mode MODE. If found, return an rtx
1329 of that oldest register, otherwise return NULL. */
1331 static rtx
1332 find_oldest_value_reg (enum reg_class cl, rtx reg, struct value_data *vd)
1334 unsigned int regno = REGNO (reg);
1335 enum machine_mode mode = GET_MODE (reg);
1336 unsigned int i;
1338 /* If we are accessing REG in some mode other that what we set it in,
1339 make sure that the replacement is valid. In particular, consider
1340 (set (reg:DI r11) (...))
1341 (set (reg:SI r9) (reg:SI r11))
1342 (set (reg:SI r10) (...))
1343 (set (...) (reg:DI r9))
1344 Replacing r9 with r11 is invalid. */
1345 if (mode != vd->e[regno].mode)
1347 if (hard_regno_nregs[regno][mode]
1348 > hard_regno_nregs[regno][vd->e[regno].mode])
1349 return NULL_RTX;
1352 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
1354 enum machine_mode oldmode = vd->e[i].mode;
1355 rtx new;
1356 unsigned int last;
1358 for (last = i; last < i + hard_regno_nregs[i][mode]; last++)
1359 if (!TEST_HARD_REG_BIT (reg_class_contents[cl], last))
1360 return NULL_RTX;
1362 new = maybe_mode_change (oldmode, vd->e[regno].mode, mode, i, regno);
1363 if (new)
1365 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (reg);
1366 REG_ATTRS (new) = REG_ATTRS (reg);
1367 return new;
1371 return NULL_RTX;
1374 /* If possible, replace the register at *LOC with the oldest register
1375 in register class CL. Return true if successfully replaced. */
1377 static bool
1378 replace_oldest_value_reg (rtx *loc, enum reg_class cl, rtx insn,
1379 struct value_data *vd)
1381 rtx new = find_oldest_value_reg (cl, *loc, vd);
1382 if (new)
1384 if (dump_file)
1385 fprintf (dump_file, "insn %u: replaced reg %u with %u\n",
1386 INSN_UID (insn), REGNO (*loc), REGNO (new));
1388 *loc = new;
1389 return true;
1391 return false;
1394 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1395 Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
1396 BASE_REG_CLASS depending on how the register is being considered. */
1398 static bool
1399 replace_oldest_value_addr (rtx *loc, enum reg_class cl,
1400 enum machine_mode mode, rtx insn,
1401 struct value_data *vd)
1403 rtx x = *loc;
1404 RTX_CODE code = GET_CODE (x);
1405 const char *fmt;
1406 int i, j;
1407 bool changed = false;
1409 switch (code)
1411 case PLUS:
1413 rtx orig_op0 = XEXP (x, 0);
1414 rtx orig_op1 = XEXP (x, 1);
1415 RTX_CODE code0 = GET_CODE (orig_op0);
1416 RTX_CODE code1 = GET_CODE (orig_op1);
1417 rtx op0 = orig_op0;
1418 rtx op1 = orig_op1;
1419 rtx *locI = NULL;
1420 rtx *locB = NULL;
1422 if (GET_CODE (op0) == SUBREG)
1424 op0 = SUBREG_REG (op0);
1425 code0 = GET_CODE (op0);
1428 if (GET_CODE (op1) == SUBREG)
1430 op1 = SUBREG_REG (op1);
1431 code1 = GET_CODE (op1);
1434 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1435 || code0 == ZERO_EXTEND || code1 == MEM)
1437 locI = &XEXP (x, 0);
1438 locB = &XEXP (x, 1);
1440 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1441 || code1 == ZERO_EXTEND || code0 == MEM)
1443 locI = &XEXP (x, 1);
1444 locB = &XEXP (x, 0);
1446 else if (code0 == CONST_INT || code0 == CONST
1447 || code0 == SYMBOL_REF || code0 == LABEL_REF)
1448 locB = &XEXP (x, 1);
1449 else if (code1 == CONST_INT || code1 == CONST
1450 || code1 == SYMBOL_REF || code1 == LABEL_REF)
1451 locB = &XEXP (x, 0);
1452 else if (code0 == REG && code1 == REG)
1454 int index_op;
1456 if (REG_OK_FOR_INDEX_P (op0)
1457 && REG_MODE_OK_FOR_BASE_P (op1, mode))
1458 index_op = 0;
1459 else if (REG_OK_FOR_INDEX_P (op1)
1460 && REG_MODE_OK_FOR_BASE_P (op0, mode))
1461 index_op = 1;
1462 else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
1463 index_op = 0;
1464 else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
1465 index_op = 1;
1466 else if (REG_OK_FOR_INDEX_P (op1))
1467 index_op = 1;
1468 else
1469 index_op = 0;
1471 locI = &XEXP (x, index_op);
1472 locB = &XEXP (x, !index_op);
1474 else if (code0 == REG)
1476 locI = &XEXP (x, 0);
1477 locB = &XEXP (x, 1);
1479 else if (code1 == REG)
1481 locI = &XEXP (x, 1);
1482 locB = &XEXP (x, 0);
1485 if (locI)
1486 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
1487 insn, vd);
1488 if (locB)
1489 changed |= replace_oldest_value_addr (locB,
1490 MODE_BASE_REG_CLASS (mode),
1491 mode, insn, vd);
1492 return changed;
1495 case POST_INC:
1496 case POST_DEC:
1497 case POST_MODIFY:
1498 case PRE_INC:
1499 case PRE_DEC:
1500 case PRE_MODIFY:
1501 return false;
1503 case MEM:
1504 return replace_oldest_value_mem (x, insn, vd);
1506 case REG:
1507 return replace_oldest_value_reg (loc, cl, insn, vd);
1509 default:
1510 break;
1513 fmt = GET_RTX_FORMAT (code);
1514 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1516 if (fmt[i] == 'e')
1517 changed |= replace_oldest_value_addr (&XEXP (x, i), cl, mode,
1518 insn, vd);
1519 else if (fmt[i] == 'E')
1520 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1521 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), cl,
1522 mode, insn, vd);
1525 return changed;
1528 /* Similar to replace_oldest_value_reg, but X contains a memory. */
1530 static bool
1531 replace_oldest_value_mem (rtx x, rtx insn, struct value_data *vd)
1533 return replace_oldest_value_addr (&XEXP (x, 0),
1534 MODE_BASE_REG_CLASS (GET_MODE (x)),
1535 GET_MODE (x), insn, vd);
1538 /* Perform the forward copy propagation on basic block BB. */
1540 static bool
1541 copyprop_hardreg_forward_1 (basic_block bb, struct value_data *vd)
1543 bool changed = false;
1544 rtx insn;
1546 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1548 int n_ops, i, alt, predicated;
1549 bool is_asm;
1550 rtx set;
1552 if (! INSN_P (insn))
1554 if (insn == BB_END (bb))
1555 break;
1556 else
1557 continue;
1560 set = single_set (insn);
1561 extract_insn (insn);
1562 if (! constrain_operands (1))
1563 fatal_insn_not_found (insn);
1564 preprocess_constraints ();
1565 alt = which_alternative;
1566 n_ops = recog_data.n_operands;
1567 is_asm = asm_noperands (PATTERN (insn)) >= 0;
1569 /* Simplify the code below by rewriting things to reflect
1570 matching constraints. Also promote OP_OUT to OP_INOUT
1571 in predicated instructions. */
1573 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1574 for (i = 0; i < n_ops; ++i)
1576 int matches = recog_op_alt[i][alt].matches;
1577 if (matches >= 0)
1578 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
1579 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1580 || (predicated && recog_data.operand_type[i] == OP_OUT))
1581 recog_data.operand_type[i] = OP_INOUT;
1584 /* For each earlyclobber operand, zap the value data. */
1585 for (i = 0; i < n_ops; i++)
1586 if (recog_op_alt[i][alt].earlyclobber)
1587 kill_value (recog_data.operand[i], vd);
1589 /* Within asms, a clobber cannot overlap inputs or outputs.
1590 I wouldn't think this were true for regular insns, but
1591 scan_rtx treats them like that... */
1592 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1594 /* Kill all auto-incremented values. */
1595 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
1596 for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
1598 /* Kill all early-clobbered operands. */
1599 for (i = 0; i < n_ops; i++)
1600 if (recog_op_alt[i][alt].earlyclobber)
1601 kill_value (recog_data.operand[i], vd);
1603 /* Special-case plain move instructions, since we may well
1604 be able to do the move from a different register class. */
1605 if (set && REG_P (SET_SRC (set)))
1607 rtx src = SET_SRC (set);
1608 unsigned int regno = REGNO (src);
1609 enum machine_mode mode = GET_MODE (src);
1610 unsigned int i;
1611 rtx new;
1613 /* If we are accessing SRC in some mode other that what we
1614 set it in, make sure that the replacement is valid. */
1615 if (mode != vd->e[regno].mode)
1617 if (hard_regno_nregs[regno][mode]
1618 > hard_regno_nregs[regno][vd->e[regno].mode])
1619 goto no_move_special_case;
1622 /* If the destination is also a register, try to find a source
1623 register in the same class. */
1624 if (REG_P (SET_DEST (set)))
1626 new = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
1627 if (new && validate_change (insn, &SET_SRC (set), new, 0))
1629 if (dump_file)
1630 fprintf (dump_file,
1631 "insn %u: replaced reg %u with %u\n",
1632 INSN_UID (insn), regno, REGNO (new));
1633 changed = true;
1634 goto did_replacement;
1638 /* Otherwise, try all valid registers and see if its valid. */
1639 for (i = vd->e[regno].oldest_regno; i != regno;
1640 i = vd->e[i].next_regno)
1642 new = maybe_mode_change (vd->e[i].mode, vd->e[regno].mode,
1643 mode, i, regno);
1644 if (new != NULL_RTX)
1646 if (validate_change (insn, &SET_SRC (set), new, 0))
1648 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (src);
1649 REG_ATTRS (new) = REG_ATTRS (src);
1650 if (dump_file)
1651 fprintf (dump_file,
1652 "insn %u: replaced reg %u with %u\n",
1653 INSN_UID (insn), regno, REGNO (new));
1654 changed = true;
1655 goto did_replacement;
1660 no_move_special_case:
1662 /* For each input operand, replace a hard register with the
1663 eldest live copy that's in an appropriate register class. */
1664 for (i = 0; i < n_ops; i++)
1666 bool replaced = false;
1668 /* Don't scan match_operand here, since we've no reg class
1669 information to pass down. Any operands that we could
1670 substitute in will be represented elsewhere. */
1671 if (recog_data.constraints[i][0] == '\0')
1672 continue;
1674 /* Don't replace in asms intentionally referencing hard regs. */
1675 if (is_asm && REG_P (recog_data.operand[i])
1676 && (REGNO (recog_data.operand[i])
1677 == ORIGINAL_REGNO (recog_data.operand[i])))
1678 continue;
1680 if (recog_data.operand_type[i] == OP_IN)
1682 if (recog_op_alt[i][alt].is_address)
1683 replaced
1684 = replace_oldest_value_addr (recog_data.operand_loc[i],
1685 recog_op_alt[i][alt].cl,
1686 VOIDmode, insn, vd);
1687 else if (REG_P (recog_data.operand[i]))
1688 replaced
1689 = replace_oldest_value_reg (recog_data.operand_loc[i],
1690 recog_op_alt[i][alt].cl,
1691 insn, vd);
1692 else if (MEM_P (recog_data.operand[i]))
1693 replaced = replace_oldest_value_mem (recog_data.operand[i],
1694 insn, vd);
1696 else if (MEM_P (recog_data.operand[i]))
1697 replaced = replace_oldest_value_mem (recog_data.operand[i],
1698 insn, vd);
1700 /* If we performed any replacement, update match_dups. */
1701 if (replaced)
1703 int j;
1704 rtx new;
1706 changed = true;
1708 new = *recog_data.operand_loc[i];
1709 recog_data.operand[i] = new;
1710 for (j = 0; j < recog_data.n_dups; j++)
1711 if (recog_data.dup_num[j] == i)
1712 *recog_data.dup_loc[j] = new;
1716 did_replacement:
1717 /* Clobber call-clobbered registers. */
1718 if (CALL_P (insn))
1719 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1720 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1721 kill_value_regno (i, 1, vd);
1723 /* Notice stores. */
1724 note_stores (PATTERN (insn), kill_set_value, vd);
1726 /* Notice copies. */
1727 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1728 copy_value (SET_DEST (set), SET_SRC (set), vd);
1730 if (insn == BB_END (bb))
1731 break;
1734 return changed;
1737 /* Main entry point for the forward copy propagation optimization. */
1739 void
1740 copyprop_hardreg_forward (void)
1742 struct value_data *all_vd;
1743 bool need_refresh;
1744 basic_block bb, bbp = 0;
1746 need_refresh = false;
1748 all_vd = xmalloc (sizeof (struct value_data) * last_basic_block);
1750 FOR_EACH_BB (bb)
1752 /* If a block has a single predecessor, that we've already
1753 processed, begin with the value data that was live at
1754 the end of the predecessor block. */
1755 /* ??? Ought to use more intelligent queuing of blocks. */
1756 if (bb->pred)
1757 for (bbp = bb; bbp && bbp != bb->pred->src; bbp = bbp->prev_bb);
1758 if (bb->pred
1759 && ! bb->pred->pred_next
1760 && ! (bb->pred->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1761 && bb->pred->src != ENTRY_BLOCK_PTR
1762 && bbp)
1763 all_vd[bb->index] = all_vd[bb->pred->src->index];
1764 else
1765 init_value_data (all_vd + bb->index);
1767 if (copyprop_hardreg_forward_1 (bb, all_vd + bb->index))
1768 need_refresh = true;
1771 if (need_refresh)
1773 if (dump_file)
1774 fputs ("\n\n", dump_file);
1776 /* ??? Irritatingly, delete_noop_moves does not take a set of blocks
1777 to scan, so we have to do a life update with no initial set of
1778 blocks Just In Case. */
1779 delete_noop_moves ();
1780 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
1781 PROP_DEATH_NOTES
1782 | PROP_SCAN_DEAD_CODE
1783 | PROP_KILL_DEAD_CODE);
1786 free (all_vd);
1789 /* Dump the value chain data to stderr. */
1791 void
1792 debug_value_data (struct value_data *vd)
1794 HARD_REG_SET set;
1795 unsigned int i, j;
1797 CLEAR_HARD_REG_SET (set);
1799 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1800 if (vd->e[i].oldest_regno == i)
1802 if (vd->e[i].mode == VOIDmode)
1804 if (vd->e[i].next_regno != INVALID_REGNUM)
1805 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1806 i, vd->e[i].next_regno);
1807 continue;
1810 SET_HARD_REG_BIT (set, i);
1811 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1813 for (j = vd->e[i].next_regno;
1814 j != INVALID_REGNUM;
1815 j = vd->e[j].next_regno)
1817 if (TEST_HARD_REG_BIT (set, j))
1819 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1820 return;
1823 if (vd->e[j].oldest_regno != i)
1825 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1826 j, vd->e[j].oldest_regno);
1827 return;
1829 SET_HARD_REG_BIT (set, j);
1830 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1832 fputc ('\n', stderr);
1835 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1836 if (! TEST_HARD_REG_BIT (set, i)
1837 && (vd->e[i].mode != VOIDmode
1838 || vd->e[i].oldest_regno != i
1839 || vd->e[i].next_regno != INVALID_REGNUM))
1840 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1841 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1842 vd->e[i].next_regno);
1845 #ifdef ENABLE_CHECKING
1846 static void
1847 validate_value_data (struct value_data *vd)
1849 HARD_REG_SET set;
1850 unsigned int i, j;
1852 CLEAR_HARD_REG_SET (set);
1854 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1855 if (vd->e[i].oldest_regno == i)
1857 if (vd->e[i].mode == VOIDmode)
1859 if (vd->e[i].next_regno != INVALID_REGNUM)
1860 internal_error ("validate_value_data: [%u] Bad next_regno for empty chain (%u)",
1861 i, vd->e[i].next_regno);
1862 continue;
1865 SET_HARD_REG_BIT (set, i);
1867 for (j = vd->e[i].next_regno;
1868 j != INVALID_REGNUM;
1869 j = vd->e[j].next_regno)
1871 if (TEST_HARD_REG_BIT (set, j))
1872 internal_error ("validate_value_data: Loop in regno chain (%u)",
1874 if (vd->e[j].oldest_regno != i)
1875 internal_error ("validate_value_data: [%u] Bad oldest_regno (%u)",
1876 j, vd->e[j].oldest_regno);
1878 SET_HARD_REG_BIT (set, j);
1882 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1883 if (! TEST_HARD_REG_BIT (set, i)
1884 && (vd->e[i].mode != VOIDmode
1885 || vd->e[i].oldest_regno != i
1886 || vd->e[i].next_regno != INVALID_REGNUM))
1887 internal_error ("validate_value_data: [%u] Non-empty reg in chain (%s %u %i)",
1888 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1889 vd->e[i].next_regno);
1891 #endif