* regrename.c: Fix formatting.
[official-gcc.git] / gcc / regrename.c
blob4297da7f32756cf8f4ee5f394dc0926c8d263f80
1 /* Register renaming for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002 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 "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 #define obstack_chunk_alloc xmalloc
40 #define obstack_chunk_free free
42 #ifndef REGNO_MODE_OK_FOR_BASE_P
43 #define REGNO_MODE_OK_FOR_BASE_P(REGNO, MODE) REGNO_OK_FOR_BASE_P (REGNO)
44 #endif
46 #ifndef REG_MODE_OK_FOR_BASE_P
47 #define REG_MODE_OK_FOR_BASE_P(REGNO, MODE) REG_OK_FOR_BASE_P (REGNO)
48 #endif
50 static const char *const reg_class_names[] = REG_CLASS_NAMES;
52 struct du_chain
54 struct du_chain *next_chain;
55 struct du_chain *next_use;
57 rtx insn;
58 rtx *loc;
59 enum reg_class class;
60 unsigned int need_caller_save_reg:1;
61 unsigned int earlyclobber:1;
64 enum scan_actions
66 terminate_all_read,
67 terminate_overlapping_read,
68 terminate_write,
69 terminate_dead,
70 mark_read,
71 mark_write
74 static const char * const scan_actions_name[] =
76 "terminate_all_read",
77 "terminate_overlapping_read",
78 "terminate_write",
79 "terminate_dead",
80 "mark_read",
81 "mark_write"
84 static struct obstack rename_obstack;
86 static void do_replace PARAMS ((struct du_chain *, int));
87 static void scan_rtx_reg PARAMS ((rtx, rtx *, enum reg_class,
88 enum scan_actions, enum op_type, int));
89 static void scan_rtx_address PARAMS ((rtx, rtx *, enum reg_class,
90 enum scan_actions, enum machine_mode));
91 static void scan_rtx PARAMS ((rtx, rtx *, enum reg_class,
92 enum scan_actions, enum op_type, int));
93 static struct du_chain *build_def_use PARAMS ((basic_block));
94 static void dump_def_use_chain PARAMS ((struct du_chain *));
95 static void note_sets PARAMS ((rtx, rtx, void *));
96 static void clear_dead_regs PARAMS ((HARD_REG_SET *, enum machine_mode, rtx));
97 static void merge_overlapping_regs PARAMS ((basic_block, HARD_REG_SET *,
98 struct du_chain *));
100 /* Called through note_stores from update_life. Find sets of registers, and
101 record them in *DATA (which is actually a HARD_REG_SET *). */
103 static void
104 note_sets (x, set, data)
105 rtx x;
106 rtx set ATTRIBUTE_UNUSED;
107 void *data;
109 HARD_REG_SET *pset = (HARD_REG_SET *) data;
110 unsigned int regno;
111 int nregs;
112 if (GET_CODE (x) != REG)
113 return;
114 regno = REGNO (x);
115 nregs = HARD_REGNO_NREGS (regno, GET_MODE (x));
117 /* There must not be pseudos at this point. */
118 if (regno + nregs > FIRST_PSEUDO_REGISTER)
119 abort ();
121 while (nregs-- > 0)
122 SET_HARD_REG_BIT (*pset, regno + nregs);
125 /* Clear all registers from *PSET for which a note of kind KIND can be found
126 in the list NOTES. */
128 static void
129 clear_dead_regs (pset, kind, notes)
130 HARD_REG_SET *pset;
131 enum machine_mode kind;
132 rtx notes;
134 rtx note;
135 for (note = notes; note; note = XEXP (note, 1))
136 if (REG_NOTE_KIND (note) == kind && REG_P (XEXP (note, 0)))
138 rtx reg = XEXP (note, 0);
139 unsigned int regno = REGNO (reg);
140 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (reg));
142 /* There must not be pseudos at this point. */
143 if (regno + nregs > FIRST_PSEUDO_REGISTER)
144 abort ();
146 while (nregs-- > 0)
147 CLEAR_HARD_REG_BIT (*pset, regno + nregs);
151 /* For a def-use chain CHAIN in basic block B, find which registers overlap
152 its lifetime and set the corresponding bits in *PSET. */
154 static void
155 merge_overlapping_regs (b, pset, chain)
156 basic_block b;
157 HARD_REG_SET *pset;
158 struct du_chain *chain;
160 struct du_chain *t = chain;
161 rtx insn;
162 HARD_REG_SET live;
164 REG_SET_TO_HARD_REG_SET (live, b->global_live_at_start);
165 insn = b->head;
166 while (t)
168 /* Search forward until the next reference to the register to be
169 renamed. */
170 while (insn != t->insn)
172 if (INSN_P (insn))
174 clear_dead_regs (&live, REG_DEAD, REG_NOTES (insn));
175 note_stores (PATTERN (insn), note_sets, (void *) &live);
176 /* Only record currently live regs if we are inside the
177 reg's live range. */
178 if (t != chain)
179 IOR_HARD_REG_SET (*pset, live);
180 clear_dead_regs (&live, REG_UNUSED, REG_NOTES (insn));
182 insn = NEXT_INSN (insn);
185 IOR_HARD_REG_SET (*pset, live);
187 /* For the last reference, also merge in all registers set in the
188 same insn.
189 @@@ We only have take earlyclobbered sets into account. */
190 if (! t->next_use)
191 note_stores (PATTERN (insn), note_sets, (void *) pset);
193 t = t->next_use;
197 /* Perform register renaming on the current function. */
199 void
200 regrename_optimize ()
202 int tick[FIRST_PSEUDO_REGISTER];
203 int this_tick = 0;
204 int b;
205 char *first_obj;
207 memset (tick, 0, sizeof tick);
209 gcc_obstack_init (&rename_obstack);
210 first_obj = (char *) obstack_alloc (&rename_obstack, 0);
212 for (b = 0; b < n_basic_blocks; b++)
214 basic_block bb = BASIC_BLOCK (b);
215 struct du_chain *all_chains = 0;
216 HARD_REG_SET unavailable;
217 HARD_REG_SET regs_seen;
219 CLEAR_HARD_REG_SET (unavailable);
221 if (rtl_dump_file)
222 fprintf (rtl_dump_file, "\nBasic block %d:\n", b);
224 all_chains = build_def_use (bb);
226 if (rtl_dump_file)
227 dump_def_use_chain (all_chains);
229 CLEAR_HARD_REG_SET (unavailable);
230 /* Don't clobber traceback for noreturn functions. */
231 if (frame_pointer_needed)
233 int i;
235 for (i = HARD_REGNO_NREGS (FRAME_POINTER_REGNUM, Pmode); i--;)
236 SET_HARD_REG_BIT (unavailable, FRAME_POINTER_REGNUM + i);
238 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
239 for (i = HARD_REGNO_NREGS (HARD_FRAME_POINTER_REGNUM, Pmode); i--;)
240 SET_HARD_REG_BIT (unavailable, HARD_FRAME_POINTER_REGNUM + i);
241 #endif
244 CLEAR_HARD_REG_SET (regs_seen);
245 while (all_chains)
247 int new_reg, best_new_reg = -1;
248 int n_uses;
249 struct du_chain *this = all_chains;
250 struct du_chain *tmp, *last;
251 HARD_REG_SET this_unavailable;
252 int reg = REGNO (*this->loc);
253 int i;
255 all_chains = this->next_chain;
257 #if 0 /* This just disables optimization opportunities. */
258 /* Only rename once we've seen the reg more than once. */
259 if (! TEST_HARD_REG_BIT (regs_seen, reg))
261 SET_HARD_REG_BIT (regs_seen, reg);
262 continue;
264 #endif
266 if (fixed_regs[reg] || global_regs[reg]
267 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
268 || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
269 #else
270 || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
271 #endif
273 continue;
275 COPY_HARD_REG_SET (this_unavailable, unavailable);
277 /* Find last entry on chain (which has the need_caller_save bit),
278 count number of uses, and narrow the set of registers we can
279 use for renaming. */
280 n_uses = 0;
281 for (last = this; last->next_use; last = last->next_use)
283 n_uses++;
284 IOR_COMPL_HARD_REG_SET (this_unavailable,
285 reg_class_contents[last->class]);
287 if (n_uses < 1)
288 continue;
290 IOR_COMPL_HARD_REG_SET (this_unavailable,
291 reg_class_contents[last->class]);
293 if (this->need_caller_save_reg)
294 IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
296 merge_overlapping_regs (bb, &this_unavailable, this);
298 /* Now potential_regs is a reasonable approximation, let's
299 have a closer look at each register still in there. */
300 for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
302 int nregs = HARD_REGNO_NREGS (new_reg, GET_MODE (*this->loc));
304 for (i = nregs - 1; i >= 0; --i)
305 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
306 || fixed_regs[new_reg + i]
307 || global_regs[new_reg + i]
308 /* Can't use regs which aren't saved by the prologue. */
309 || (! regs_ever_live[new_reg + i]
310 && ! call_used_regs[new_reg + i])
311 #ifdef LEAF_REGISTERS
312 /* We can't use a non-leaf register if we're in a
313 leaf function. */
314 || (current_function_is_leaf
315 && !LEAF_REGISTERS[new_reg + i])
316 #endif
317 #ifdef HARD_REGNO_RENAME_OK
318 || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
319 #endif
321 break;
322 if (i >= 0)
323 continue;
325 /* See whether it accepts all modes that occur in
326 definition and uses. */
327 for (tmp = this; tmp; tmp = tmp->next_use)
328 if (! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
329 || (tmp->need_caller_save_reg
330 && ! (HARD_REGNO_CALL_PART_CLOBBERED
331 (reg, GET_MODE (*tmp->loc)))
332 && (HARD_REGNO_CALL_PART_CLOBBERED
333 (new_reg, GET_MODE (*tmp->loc)))))
334 break;
335 if (! tmp)
337 if (best_new_reg == -1
338 || tick[best_new_reg] > tick[new_reg])
339 best_new_reg = new_reg;
343 if (rtl_dump_file)
345 fprintf (rtl_dump_file, "Register %s in insn %d",
346 reg_names[reg], INSN_UID (last->insn));
347 if (last->need_caller_save_reg)
348 fprintf (rtl_dump_file, " crosses a call");
351 if (best_new_reg == -1)
353 if (rtl_dump_file)
354 fprintf (rtl_dump_file, "; no available registers\n");
355 continue;
358 do_replace (this, best_new_reg);
359 tick[best_new_reg] = this_tick++;
361 if (rtl_dump_file)
362 fprintf (rtl_dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
365 obstack_free (&rename_obstack, first_obj);
368 obstack_free (&rename_obstack, NULL);
370 if (rtl_dump_file)
371 fputc ('\n', rtl_dump_file);
373 count_or_remove_death_notes (NULL, 1);
374 update_life_info (NULL, UPDATE_LIFE_LOCAL,
375 PROP_REG_INFO | PROP_DEATH_NOTES);
378 static void
379 do_replace (chain, reg)
380 struct du_chain *chain;
381 int reg;
383 while (chain)
385 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
386 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
387 if (regno >= FIRST_PSEUDO_REGISTER)
388 ORIGINAL_REGNO (*chain->loc) = regno;
389 chain = chain->next_use;
394 static struct du_chain *open_chains;
395 static struct du_chain *closed_chains;
397 static void
398 scan_rtx_reg (insn, loc, class, action, type, earlyclobber)
399 rtx insn;
400 rtx *loc;
401 enum reg_class class;
402 enum scan_actions action;
403 enum op_type type;
404 int earlyclobber;
406 struct du_chain **p;
407 rtx x = *loc;
408 enum machine_mode mode = GET_MODE (x);
409 int this_regno = REGNO (x);
410 int this_nregs = HARD_REGNO_NREGS (this_regno, mode);
412 if (action == mark_write)
414 if (type == OP_OUT)
416 struct du_chain *this = (struct du_chain *)
417 obstack_alloc (&rename_obstack, sizeof (struct du_chain));
418 this->next_use = 0;
419 this->next_chain = open_chains;
420 this->loc = loc;
421 this->insn = insn;
422 this->class = class;
423 this->need_caller_save_reg = 0;
424 this->earlyclobber = earlyclobber;
425 open_chains = this;
427 return;
430 if ((type == OP_OUT && action != terminate_write)
431 || (type != OP_OUT && action == terminate_write))
432 return;
434 for (p = &open_chains; *p;)
436 struct du_chain *this = *p;
438 /* Check if the chain has been terminated if it has then skip to
439 the next chain.
441 This can happen when we've already appended the location to
442 the chain in Step 3, but are trying to hide in-out operands
443 from terminate_write in Step 5. */
445 if (*this->loc == cc0_rtx)
446 p = &this->next_chain;
447 else
449 int regno = REGNO (*this->loc);
450 int nregs = HARD_REGNO_NREGS (regno, GET_MODE (*this->loc));
451 int exact_match = (regno == this_regno && nregs == this_nregs);
453 if (regno + nregs <= this_regno
454 || this_regno + this_nregs <= regno)
456 p = &this->next_chain;
457 continue;
460 if (action == mark_read)
462 if (! exact_match)
463 abort ();
465 /* ??? Class NO_REGS can happen if the md file makes use of
466 EXTRA_CONSTRAINTS to match registers. Which is arguably
467 wrong, but there we are. Since we know not what this may
468 be replaced with, terminate the chain. */
469 if (class != NO_REGS)
471 this = (struct du_chain *)
472 obstack_alloc (&rename_obstack, sizeof (struct du_chain));
473 this->next_use = 0;
474 this->next_chain = (*p)->next_chain;
475 this->loc = loc;
476 this->insn = insn;
477 this->class = class;
478 this->need_caller_save_reg = 0;
479 while (*p)
480 p = &(*p)->next_use;
481 *p = this;
482 return;
486 if (action != terminate_overlapping_read || ! exact_match)
488 struct du_chain *next = this->next_chain;
490 /* Whether the terminated chain can be used for renaming
491 depends on the action and this being an exact match.
492 In either case, we remove this element from open_chains. */
494 if ((action == terminate_dead || action == terminate_write)
495 && exact_match)
497 this->next_chain = closed_chains;
498 closed_chains = this;
499 if (rtl_dump_file)
500 fprintf (rtl_dump_file,
501 "Closing chain %s at insn %d (%s)\n",
502 reg_names[REGNO (*this->loc)], INSN_UID (insn),
503 scan_actions_name[(int) action]);
505 else
507 if (rtl_dump_file)
508 fprintf (rtl_dump_file,
509 "Discarding chain %s at insn %d (%s)\n",
510 reg_names[REGNO (*this->loc)], INSN_UID (insn),
511 scan_actions_name[(int) action]);
513 *p = next;
515 else
516 p = &this->next_chain;
521 /* Adapted from find_reloads_address_1. CLASS is INDEX_REG_CLASS or
522 BASE_REG_CLASS depending on how the register is being considered. */
524 static void
525 scan_rtx_address (insn, loc, class, action, mode)
526 rtx insn;
527 rtx *loc;
528 enum reg_class class;
529 enum scan_actions action;
530 enum machine_mode mode;
532 rtx x = *loc;
533 RTX_CODE code = GET_CODE (x);
534 const char *fmt;
535 int i, j;
537 if (action == mark_write)
538 return;
540 switch (code)
542 case PLUS:
544 rtx orig_op0 = XEXP (x, 0);
545 rtx orig_op1 = XEXP (x, 1);
546 RTX_CODE code0 = GET_CODE (orig_op0);
547 RTX_CODE code1 = GET_CODE (orig_op1);
548 rtx op0 = orig_op0;
549 rtx op1 = orig_op1;
550 rtx *locI = NULL;
551 rtx *locB = NULL;
553 if (GET_CODE (op0) == SUBREG)
555 op0 = SUBREG_REG (op0);
556 code0 = GET_CODE (op0);
559 if (GET_CODE (op1) == SUBREG)
561 op1 = SUBREG_REG (op1);
562 code1 = GET_CODE (op1);
565 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
566 || code0 == ZERO_EXTEND || code1 == MEM)
568 locI = &XEXP (x, 0);
569 locB = &XEXP (x, 1);
571 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
572 || code1 == ZERO_EXTEND || code0 == MEM)
574 locI = &XEXP (x, 1);
575 locB = &XEXP (x, 0);
577 else if (code0 == CONST_INT || code0 == CONST
578 || code0 == SYMBOL_REF || code0 == LABEL_REF)
579 locB = &XEXP (x, 1);
580 else if (code1 == CONST_INT || code1 == CONST
581 || code1 == SYMBOL_REF || code1 == LABEL_REF)
582 locB = &XEXP (x, 0);
583 else if (code0 == REG && code1 == REG)
585 int index_op;
587 if (REG_OK_FOR_INDEX_P (op0)
588 && REG_MODE_OK_FOR_BASE_P (op1, mode))
589 index_op = 0;
590 else if (REG_OK_FOR_INDEX_P (op1)
591 && REG_MODE_OK_FOR_BASE_P (op0, mode))
592 index_op = 1;
593 else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
594 index_op = 0;
595 else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
596 index_op = 1;
597 else if (REG_OK_FOR_INDEX_P (op1))
598 index_op = 1;
599 else
600 index_op = 0;
602 locI = &XEXP (x, index_op);
603 locB = &XEXP (x, !index_op);
605 else if (code0 == REG)
607 locI = &XEXP (x, 0);
608 locB = &XEXP (x, 1);
610 else if (code1 == REG)
612 locI = &XEXP (x, 1);
613 locB = &XEXP (x, 0);
616 if (locI)
617 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
618 if (locB)
619 scan_rtx_address (insn, locB, MODE_BASE_REG_CLASS (mode), action, mode);
620 return;
623 case POST_INC:
624 case POST_DEC:
625 case POST_MODIFY:
626 case PRE_INC:
627 case PRE_DEC:
628 case PRE_MODIFY:
629 #ifndef AUTO_INC_DEC
630 /* If the target doesn't claim to handle autoinc, this must be
631 something special, like a stack push. Kill this chain. */
632 action = terminate_all_read;
633 #endif
634 break;
636 case MEM:
637 scan_rtx_address (insn, &XEXP (x, 0),
638 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
639 GET_MODE (x));
640 return;
642 case REG:
643 scan_rtx_reg (insn, loc, class, action, OP_IN, 0);
644 return;
646 default:
647 break;
650 fmt = GET_RTX_FORMAT (code);
651 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
653 if (fmt[i] == 'e')
654 scan_rtx_address (insn, &XEXP (x, i), class, action, mode);
655 else if (fmt[i] == 'E')
656 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
657 scan_rtx_address (insn, &XVECEXP (x, i, j), class, action, mode);
661 static void
662 scan_rtx (insn, loc, class, action, type, earlyclobber)
663 rtx insn;
664 rtx *loc;
665 enum reg_class class;
666 enum scan_actions action;
667 enum op_type type;
668 int earlyclobber;
670 const char *fmt;
671 rtx x = *loc;
672 enum rtx_code code = GET_CODE (x);
673 int i, j;
675 code = GET_CODE (x);
676 switch (code)
678 case CONST:
679 case CONST_INT:
680 case CONST_DOUBLE:
681 case CONST_VECTOR:
682 case SYMBOL_REF:
683 case LABEL_REF:
684 case CC0:
685 case PC:
686 return;
688 case REG:
689 scan_rtx_reg (insn, loc, class, action, type, earlyclobber);
690 return;
692 case MEM:
693 scan_rtx_address (insn, &XEXP (x, 0),
694 MODE_BASE_REG_CLASS (GET_MODE (x)), action,
695 GET_MODE (x));
696 return;
698 case SET:
699 scan_rtx (insn, &SET_SRC (x), class, action, OP_IN, 0);
700 scan_rtx (insn, &SET_DEST (x), class, action, OP_OUT, 0);
701 return;
703 case STRICT_LOW_PART:
704 scan_rtx (insn, &XEXP (x, 0), class, action, OP_INOUT, earlyclobber);
705 return;
707 case ZERO_EXTRACT:
708 case SIGN_EXTRACT:
709 scan_rtx (insn, &XEXP (x, 0), class, action,
710 type == OP_IN ? OP_IN : OP_INOUT, earlyclobber);
711 scan_rtx (insn, &XEXP (x, 1), class, action, OP_IN, 0);
712 scan_rtx (insn, &XEXP (x, 2), class, action, OP_IN, 0);
713 return;
715 case POST_INC:
716 case PRE_INC:
717 case POST_DEC:
718 case PRE_DEC:
719 case POST_MODIFY:
720 case PRE_MODIFY:
721 /* Should only happen inside MEM. */
722 abort ();
724 case CLOBBER:
725 scan_rtx (insn, &SET_DEST (x), class, action, OP_OUT, 1);
726 return;
728 case EXPR_LIST:
729 scan_rtx (insn, &XEXP (x, 0), class, action, type, 0);
730 if (XEXP (x, 1))
731 scan_rtx (insn, &XEXP (x, 1), class, action, type, 0);
732 return;
734 default:
735 break;
738 fmt = GET_RTX_FORMAT (code);
739 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
741 if (fmt[i] == 'e')
742 scan_rtx (insn, &XEXP (x, i), class, action, type, 0);
743 else if (fmt[i] == 'E')
744 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
745 scan_rtx (insn, &XVECEXP (x, i, j), class, action, type, 0);
749 /* Build def/use chain */
751 static struct du_chain *
752 build_def_use (bb)
753 basic_block bb;
755 rtx insn;
757 open_chains = closed_chains = NULL;
759 for (insn = bb->head; ; insn = NEXT_INSN (insn))
761 if (INSN_P (insn))
763 int n_ops;
764 rtx note;
765 rtx old_operands[MAX_RECOG_OPERANDS];
766 rtx old_dups[MAX_DUP_OPERANDS];
767 int i, icode;
768 int alt;
769 int predicated;
771 /* Process the insn, determining its effect on the def-use
772 chains. We perform the following steps with the register
773 references in the insn:
774 (1) Any read that overlaps an open chain, but doesn't exactly
775 match, causes that chain to be closed. We can't deal
776 with overlaps yet.
777 (2) Any read outside an operand causes any chain it overlaps
778 with to be closed, since we can't replace it.
779 (3) Any read inside an operand is added if there's already
780 an open chain for it.
781 (4) For any REG_DEAD note we find, close open chains that
782 overlap it.
783 (5) For any write we find, close open chains that overlap it.
784 (6) For any write we find in an operand, make a new chain.
785 (7) For any REG_UNUSED, close any chains we just opened. */
787 icode = recog_memoized (insn);
788 extract_insn (insn);
789 if (! constrain_operands (1))
790 fatal_insn_not_found (insn);
791 preprocess_constraints ();
792 alt = which_alternative;
793 n_ops = recog_data.n_operands;
795 /* Simplify the code below by rewriting things to reflect
796 matching constraints. Also promote OP_OUT to OP_INOUT
797 in predicated instructions. */
799 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
800 for (i = 0; i < n_ops; ++i)
802 int matches = recog_op_alt[i][alt].matches;
803 if (matches >= 0)
804 recog_op_alt[i][alt].class = recog_op_alt[matches][alt].class;
805 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
806 || (predicated && recog_data.operand_type[i] == OP_OUT))
807 recog_data.operand_type[i] = OP_INOUT;
810 /* Step 1: Close chains for which we have overlapping reads. */
811 for (i = 0; i < n_ops; i++)
812 scan_rtx (insn, recog_data.operand_loc[i],
813 NO_REGS, terminate_overlapping_read,
814 recog_data.operand_type[i], 0);
816 /* Step 2: Close chains for which we have reads outside operands.
817 We do this by munging all operands into CC0, and closing
818 everything remaining. */
820 for (i = 0; i < n_ops; i++)
822 old_operands[i] = recog_data.operand[i];
823 /* Don't squash match_operator or match_parallel here, since
824 we don't know that all of the contained registers are
825 reachable by proper operands. */
826 if (recog_data.constraints[i][0] == '\0')
827 continue;
828 *recog_data.operand_loc[i] = cc0_rtx;
830 for (i = 0; i < recog_data.n_dups; i++)
832 int dup_num = recog_data.dup_num[i];
834 old_dups[i] = *recog_data.dup_loc[i];
835 *recog_data.dup_loc[i] = cc0_rtx;
837 /* For match_dup of match_operator or match_parallel, share
838 them, so that we don't miss changes in the dup. */
839 if (icode >= 0
840 && insn_data[icode].operand[dup_num].eliminable == 0)
841 old_dups[i] = recog_data.operand[dup_num];
844 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_all_read,
845 OP_IN, 0);
847 for (i = 0; i < recog_data.n_dups; i++)
848 *recog_data.dup_loc[i] = old_dups[i];
849 for (i = 0; i < n_ops; i++)
850 *recog_data.operand_loc[i] = old_operands[i];
852 /* Step 2B: Can't rename function call argument registers. */
853 if (GET_CODE (insn) == CALL_INSN && CALL_INSN_FUNCTION_USAGE (insn))
854 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
855 NO_REGS, terminate_all_read, OP_IN, 0);
857 /* Step 2C: Can't rename asm operands that were originally
858 hard registers. */
859 if (asm_noperands (PATTERN (insn)) > 0)
860 for (i = 0; i < n_ops; i++)
862 rtx *loc = recog_data.operand_loc[i];
863 rtx op = *loc;
865 if (GET_CODE (op) == REG
866 && REGNO (op) == ORIGINAL_REGNO (op)
867 && (recog_data.operand_type[i] == OP_IN
868 || recog_data.operand_type[i] == OP_INOUT))
869 scan_rtx (insn, loc, NO_REGS, terminate_all_read, OP_IN, 0);
872 /* Step 3: Append to chains for reads inside operands. */
873 for (i = 0; i < n_ops + recog_data.n_dups; i++)
875 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
876 rtx *loc = (i < n_ops
877 ? recog_data.operand_loc[opn]
878 : recog_data.dup_loc[i - n_ops]);
879 enum reg_class class = recog_op_alt[opn][alt].class;
880 enum op_type type = recog_data.operand_type[opn];
882 /* Don't scan match_operand here, since we've no reg class
883 information to pass down. Any operands that we could
884 substitute in will be represented elsewhere. */
885 if (recog_data.constraints[opn][0] == '\0')
886 continue;
888 if (recog_op_alt[opn][alt].is_address)
889 scan_rtx_address (insn, loc, class, mark_read, VOIDmode);
890 else
891 scan_rtx (insn, loc, class, mark_read, type, 0);
894 /* Step 4: Close chains for registers that die here.
895 Also record updates for REG_INC notes. */
896 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
898 if (REG_NOTE_KIND (note) == REG_DEAD)
899 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
900 OP_IN, 0);
901 else if (REG_NOTE_KIND (note) == REG_INC)
902 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
903 OP_INOUT, 0);
906 /* Step 4B: If this is a call, any chain live at this point
907 requires a caller-saved reg. */
908 if (GET_CODE (insn) == CALL_INSN)
910 struct du_chain *p;
911 for (p = open_chains; p; p = p->next_chain)
912 p->need_caller_save_reg = 1;
915 /* Step 5: Close open chains that overlap writes. Similar to
916 step 2, we hide in-out operands, since we do not want to
917 close these chains. */
919 for (i = 0; i < n_ops; i++)
921 old_operands[i] = recog_data.operand[i];
922 if (recog_data.operand_type[i] == OP_INOUT)
923 *recog_data.operand_loc[i] = cc0_rtx;
925 for (i = 0; i < recog_data.n_dups; i++)
927 int opn = recog_data.dup_num[i];
928 old_dups[i] = *recog_data.dup_loc[i];
929 if (recog_data.operand_type[opn] == OP_INOUT)
930 *recog_data.dup_loc[i] = cc0_rtx;
933 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN, 0);
935 for (i = 0; i < recog_data.n_dups; i++)
936 *recog_data.dup_loc[i] = old_dups[i];
937 for (i = 0; i < n_ops; i++)
938 *recog_data.operand_loc[i] = old_operands[i];
940 /* Step 6: Begin new chains for writes inside operands. */
941 /* ??? Many targets have output constraints on the SET_DEST
942 of a call insn, which is stupid, since these are certainly
943 ABI defined hard registers. Don't change calls at all.
944 Similarly take special care for asm statement that originally
945 referenced hard registers. */
946 if (asm_noperands (PATTERN (insn)) > 0)
948 for (i = 0; i < n_ops; i++)
949 if (recog_data.operand_type[i] == OP_OUT)
951 rtx *loc = recog_data.operand_loc[i];
952 rtx op = *loc;
953 enum reg_class class = recog_op_alt[i][alt].class;
955 if (GET_CODE (op) == REG
956 && REGNO (op) == ORIGINAL_REGNO (op))
957 continue;
959 scan_rtx (insn, loc, class, mark_write, OP_OUT,
960 recog_op_alt[i][alt].earlyclobber);
963 else if (GET_CODE (insn) != CALL_INSN)
964 for (i = 0; i < n_ops + recog_data.n_dups; i++)
966 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
967 rtx *loc = (i < n_ops
968 ? recog_data.operand_loc[opn]
969 : recog_data.dup_loc[i - n_ops]);
970 enum reg_class class = recog_op_alt[opn][alt].class;
972 if (recog_data.operand_type[opn] == OP_OUT)
973 scan_rtx (insn, loc, class, mark_write, OP_OUT,
974 recog_op_alt[opn][alt].earlyclobber);
977 /* Step 7: Close chains for registers that were never
978 really used here. */
979 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
980 if (REG_NOTE_KIND (note) == REG_UNUSED)
981 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
982 OP_IN, 0);
984 if (insn == bb->end)
985 break;
988 /* Since we close every chain when we find a REG_DEAD note, anything that
989 is still open lives past the basic block, so it can't be renamed. */
990 return closed_chains;
993 /* Dump all def/use chains in CHAINS to RTL_DUMP_FILE. They are
994 printed in reverse order as that's how we build them. */
996 static void
997 dump_def_use_chain (chains)
998 struct du_chain *chains;
1000 while (chains)
1002 struct du_chain *this = chains;
1003 int r = REGNO (*this->loc);
1004 int nregs = HARD_REGNO_NREGS (r, GET_MODE (*this->loc));
1005 fprintf (rtl_dump_file, "Register %s (%d):", reg_names[r], nregs);
1006 while (this)
1008 fprintf (rtl_dump_file, " %d [%s]", INSN_UID (this->insn),
1009 reg_class_names[this->class]);
1010 this = this->next_use;
1012 fprintf (rtl_dump_file, "\n");
1013 chains = chains->next_chain;
1017 /* The following code does forward propagation of hard register copies.
1018 The object is to eliminate as many dependencies as possible, so that
1019 we have the most scheduling freedom. As a side effect, we also clean
1020 up some silly register allocation decisions made by reload. This
1021 code may be obsoleted by a new register allocator. */
1023 /* For each register, we have a list of registers that contain the same
1024 value. The OLDEST_REGNO field points to the head of the list, and
1025 the NEXT_REGNO field runs through the list. The MODE field indicates
1026 what mode the data is known to be in; this field is VOIDmode when the
1027 register is not known to contain valid data. */
1029 struct value_data_entry
1031 enum machine_mode mode;
1032 unsigned int oldest_regno;
1033 unsigned int next_regno;
1036 struct value_data
1038 struct value_data_entry e[FIRST_PSEUDO_REGISTER];
1039 unsigned int max_value_regs;
1042 static void kill_value_regno PARAMS ((unsigned, struct value_data *));
1043 static void kill_value PARAMS ((rtx, struct value_data *));
1044 static void set_value_regno PARAMS ((unsigned, enum machine_mode,
1045 struct value_data *));
1046 static void init_value_data PARAMS ((struct value_data *));
1047 static void kill_clobbered_value PARAMS ((rtx, rtx, void *));
1048 static void kill_set_value PARAMS ((rtx, rtx, void *));
1049 static int kill_autoinc_value PARAMS ((rtx *, void *));
1050 static void copy_value PARAMS ((rtx, rtx, struct value_data *));
1051 static bool mode_change_ok PARAMS ((enum machine_mode, enum machine_mode,
1052 unsigned int));
1053 static rtx find_oldest_value_reg PARAMS ((enum reg_class, rtx,
1054 struct value_data *));
1055 static bool replace_oldest_value_reg PARAMS ((rtx *, enum reg_class, rtx,
1056 struct value_data *));
1057 static bool replace_oldest_value_addr PARAMS ((rtx *, enum reg_class,
1058 enum machine_mode, rtx,
1059 struct value_data *));
1060 static bool replace_oldest_value_mem PARAMS ((rtx, rtx, struct value_data *));
1061 static bool copyprop_hardreg_forward_1 PARAMS ((basic_block,
1062 struct value_data *));
1063 extern void debug_value_data PARAMS ((struct value_data *));
1064 #ifdef ENABLE_CHECKING
1065 static void validate_value_data PARAMS ((struct value_data *));
1066 #endif
1068 /* Kill register REGNO. This involves removing it from any value lists,
1069 and resetting the value mode to VOIDmode. */
1071 static void
1072 kill_value_regno (regno, vd)
1073 unsigned int regno;
1074 struct value_data *vd;
1076 unsigned int i, next;
1078 if (vd->e[regno].oldest_regno != regno)
1080 for (i = vd->e[regno].oldest_regno;
1081 vd->e[i].next_regno != regno;
1082 i = vd->e[i].next_regno)
1083 continue;
1084 vd->e[i].next_regno = vd->e[regno].next_regno;
1086 else if ((next = vd->e[regno].next_regno) != INVALID_REGNUM)
1088 for (i = next; i != INVALID_REGNUM; i = vd->e[i].next_regno)
1089 vd->e[i].oldest_regno = next;
1092 vd->e[regno].mode = VOIDmode;
1093 vd->e[regno].oldest_regno = regno;
1094 vd->e[regno].next_regno = INVALID_REGNUM;
1096 #ifdef ENABLE_CHECKING
1097 validate_value_data (vd);
1098 #endif
1101 /* Kill X. This is a convenience function for kill_value_regno
1102 so that we mind the mode the register is in. */
1104 static void
1105 kill_value (x, vd)
1106 rtx x;
1107 struct value_data *vd;
1109 /* SUBREGS are supposed to have been eliminated by now. But some
1110 ports, e.g. i386 sse, use them to smuggle vector type information
1111 through to instruction selection. Each such SUBREG should simplify,
1112 so if we get a NULL we've done something wrong elsewhere. */
1114 if (GET_CODE (x) == SUBREG)
1115 x = simplify_subreg (GET_MODE (x), SUBREG_REG (x),
1116 GET_MODE (SUBREG_REG (x)), SUBREG_BYTE (x));
1117 if (REG_P (x))
1119 unsigned int regno = REGNO (x);
1120 unsigned int n = HARD_REGNO_NREGS (regno, GET_MODE (x));
1121 unsigned int i, j;
1123 /* Kill the value we're told to kill. */
1124 for (i = 0; i < n; ++i)
1125 kill_value_regno (regno + i, vd);
1127 /* Kill everything that overlapped what we're told to kill. */
1128 if (regno < vd->max_value_regs)
1129 j = 0;
1130 else
1131 j = regno - vd->max_value_regs;
1132 for (; j < regno; ++j)
1134 if (vd->e[j].mode == VOIDmode)
1135 continue;
1136 n = HARD_REGNO_NREGS (j, vd->e[j].mode);
1137 if (j + n > regno)
1138 for (i = 0; i < n; ++i)
1139 kill_value_regno (j + i, vd);
1144 /* Remember that REGNO is valid in MODE. */
1146 static void
1147 set_value_regno (regno, mode, vd)
1148 unsigned int regno;
1149 enum machine_mode mode;
1150 struct value_data *vd;
1152 unsigned int nregs;
1154 vd->e[regno].mode = mode;
1156 nregs = HARD_REGNO_NREGS (regno, mode);
1157 if (nregs > vd->max_value_regs)
1158 vd->max_value_regs = nregs;
1161 /* Initialize VD such that there are no known relationships between regs. */
1163 static void
1164 init_value_data (vd)
1165 struct value_data *vd;
1167 int i;
1168 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1170 vd->e[i].mode = VOIDmode;
1171 vd->e[i].oldest_regno = i;
1172 vd->e[i].next_regno = INVALID_REGNUM;
1174 vd->max_value_regs = 0;
1177 /* Called through note_stores. If X is clobbered, kill its value. */
1179 static void
1180 kill_clobbered_value (x, set, data)
1181 rtx x;
1182 rtx set;
1183 void *data;
1185 struct value_data *vd = data;
1186 if (GET_CODE (set) == CLOBBER)
1187 kill_value (x, vd);
1190 /* Called through note_stores. If X is set, not clobbered, kill its
1191 current value and install it as the root of its own value list. */
1193 static void
1194 kill_set_value (x, set, data)
1195 rtx x;
1196 rtx set;
1197 void *data;
1199 struct value_data *vd = data;
1200 if (GET_CODE (set) != CLOBBER)
1202 kill_value (x, vd);
1203 if (REG_P (x))
1204 set_value_regno (REGNO (x), GET_MODE (x), vd);
1208 /* Called through for_each_rtx. Kill any register used as the base of an
1209 auto-increment expression, and install that register as the root of its
1210 own value list. */
1212 static int
1213 kill_autoinc_value (px, data)
1214 rtx *px;
1215 void *data;
1217 rtx x = *px;
1218 struct value_data *vd = data;
1220 if (GET_RTX_CLASS (GET_CODE (x)) == 'a')
1222 x = XEXP (x, 0);
1223 kill_value (x, vd);
1224 set_value_regno (REGNO (x), Pmode, vd);
1225 return -1;
1228 return 0;
1231 /* Assert that SRC has been copied to DEST. Adjust the data structures
1232 to reflect that SRC contains an older copy of the shared value. */
1234 static void
1235 copy_value (dest, src, vd)
1236 rtx dest;
1237 rtx src;
1238 struct value_data *vd;
1240 unsigned int dr = REGNO (dest);
1241 unsigned int sr = REGNO (src);
1242 unsigned int dn, sn;
1243 unsigned int i;
1245 /* ??? At present, it's possible to see noop sets. It'd be nice if
1246 this were cleaned up beforehand... */
1247 if (sr == dr)
1248 return;
1250 /* Do not propagate copies to the stack pointer, as that can leave
1251 memory accesses with no scheduling dependancy on the stack update. */
1252 if (dr == STACK_POINTER_REGNUM)
1253 return;
1255 /* Likewise with the frame pointer, if we're using one. */
1256 if (frame_pointer_needed && dr == HARD_FRAME_POINTER_REGNUM)
1257 return;
1259 /* If SRC and DEST overlap, don't record anything. */
1260 dn = HARD_REGNO_NREGS (dr, GET_MODE (dest));
1261 sn = HARD_REGNO_NREGS (sr, GET_MODE (dest));
1262 if ((dr > sr && dr < sr + sn)
1263 || (sr > dr && sr < dr + dn))
1264 return;
1266 /* If SRC had no assigned mode (i.e. we didn't know it was live)
1267 assign it now and assume the value came from an input argument
1268 or somesuch. */
1269 if (vd->e[sr].mode == VOIDmode)
1270 set_value_regno (sr, vd->e[dr].mode, vd);
1272 /* If SRC had been assigned a mode narrower than the copy, we can't
1273 link DEST into the chain, because not all of the pieces of the
1274 copy came from oldest_regno. */
1275 else if (sn > (unsigned int) HARD_REGNO_NREGS (sr, vd->e[sr].mode))
1276 return;
1278 /* Link DR at the end of the value chain used by SR. */
1280 vd->e[dr].oldest_regno = vd->e[sr].oldest_regno;
1282 for (i = sr; vd->e[i].next_regno != INVALID_REGNUM; i = vd->e[i].next_regno)
1283 continue;
1284 vd->e[i].next_regno = dr;
1286 #ifdef ENABLE_CHECKING
1287 validate_value_data (vd);
1288 #endif
1291 /* Return true if a mode change from ORIG to NEW is allowed for REGNO. */
1293 static bool
1294 mode_change_ok (orig_mode, new_mode, regno)
1295 enum machine_mode orig_mode, new_mode;
1296 unsigned int regno ATTRIBUTE_UNUSED;
1298 if (GET_MODE_SIZE (orig_mode) < GET_MODE_SIZE (new_mode))
1299 return false;
1301 #ifdef CLASS_CANNOT_CHANGE_MODE
1302 if (TEST_HARD_REG_BIT (reg_class_contents[CLASS_CANNOT_CHANGE_MODE], regno)
1303 && CLASS_CANNOT_CHANGE_MODE_P (orig_mode, new_mode))
1304 return false;
1305 #endif
1307 return true;
1310 /* Find the oldest copy of the value contained in REGNO that is in
1311 register class CLASS and has mode MODE. If found, return an rtx
1312 of that oldest register, otherwise return NULL. */
1314 static rtx
1315 find_oldest_value_reg (class, reg, vd)
1316 enum reg_class class;
1317 rtx reg;
1318 struct value_data *vd;
1320 unsigned int regno = REGNO (reg);
1321 enum machine_mode mode = GET_MODE (reg);
1322 unsigned int i;
1324 /* If we are accessing REG in some mode other that what we set it in,
1325 make sure that the replacement is valid. In particular, consider
1326 (set (reg:DI r11) (...))
1327 (set (reg:SI r9) (reg:SI r11))
1328 (set (reg:SI r10) (...))
1329 (set (...) (reg:DI r9))
1330 Replacing r9 with r11 is invalid. */
1331 if (mode != vd->e[regno].mode)
1333 if (HARD_REGNO_NREGS (regno, mode)
1334 > HARD_REGNO_NREGS (regno, vd->e[regno].mode))
1335 return NULL_RTX;
1338 for (i = vd->e[regno].oldest_regno; i != regno; i = vd->e[i].next_regno)
1339 if (TEST_HARD_REG_BIT (reg_class_contents[class], i)
1340 && (vd->e[i].mode == mode
1341 || mode_change_ok (vd->e[i].mode, mode, i)))
1343 rtx new = gen_rtx_raw_REG (mode, i);
1344 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (reg);
1345 return new;
1348 return NULL_RTX;
1351 /* If possible, replace the register at *LOC with the oldest register
1352 in register class CLASS. Return true if successfully replaced. */
1354 static bool
1355 replace_oldest_value_reg (loc, class, insn, vd)
1356 rtx *loc;
1357 enum reg_class class;
1358 rtx insn;
1359 struct value_data *vd;
1361 rtx new = find_oldest_value_reg (class, *loc, vd);
1362 if (new)
1364 if (rtl_dump_file)
1365 fprintf (rtl_dump_file, "insn %u: replaced reg %u with %u\n",
1366 INSN_UID (insn), REGNO (*loc), REGNO (new));
1368 *loc = new;
1369 return true;
1371 return false;
1374 /* Similar to replace_oldest_value_reg, but *LOC contains an address.
1375 Adapted from find_reloads_address_1. CLASS is INDEX_REG_CLASS or
1376 BASE_REG_CLASS depending on how the register is being considered. */
1378 static bool
1379 replace_oldest_value_addr (loc, class, mode, insn, vd)
1380 rtx *loc;
1381 enum reg_class class;
1382 enum machine_mode mode;
1383 rtx insn;
1384 struct value_data *vd;
1386 rtx x = *loc;
1387 RTX_CODE code = GET_CODE (x);
1388 const char *fmt;
1389 int i, j;
1390 bool changed = false;
1392 switch (code)
1394 case PLUS:
1396 rtx orig_op0 = XEXP (x, 0);
1397 rtx orig_op1 = XEXP (x, 1);
1398 RTX_CODE code0 = GET_CODE (orig_op0);
1399 RTX_CODE code1 = GET_CODE (orig_op1);
1400 rtx op0 = orig_op0;
1401 rtx op1 = orig_op1;
1402 rtx *locI = NULL;
1403 rtx *locB = NULL;
1405 if (GET_CODE (op0) == SUBREG)
1407 op0 = SUBREG_REG (op0);
1408 code0 = GET_CODE (op0);
1411 if (GET_CODE (op1) == SUBREG)
1413 op1 = SUBREG_REG (op1);
1414 code1 = GET_CODE (op1);
1417 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1418 || code0 == ZERO_EXTEND || code1 == MEM)
1420 locI = &XEXP (x, 0);
1421 locB = &XEXP (x, 1);
1423 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1424 || code1 == ZERO_EXTEND || code0 == MEM)
1426 locI = &XEXP (x, 1);
1427 locB = &XEXP (x, 0);
1429 else if (code0 == CONST_INT || code0 == CONST
1430 || code0 == SYMBOL_REF || code0 == LABEL_REF)
1431 locB = &XEXP (x, 1);
1432 else if (code1 == CONST_INT || code1 == CONST
1433 || code1 == SYMBOL_REF || code1 == LABEL_REF)
1434 locB = &XEXP (x, 0);
1435 else if (code0 == REG && code1 == REG)
1437 int index_op;
1439 if (REG_OK_FOR_INDEX_P (op0)
1440 && REG_MODE_OK_FOR_BASE_P (op1, mode))
1441 index_op = 0;
1442 else if (REG_OK_FOR_INDEX_P (op1)
1443 && REG_MODE_OK_FOR_BASE_P (op0, mode))
1444 index_op = 1;
1445 else if (REG_MODE_OK_FOR_BASE_P (op1, mode))
1446 index_op = 0;
1447 else if (REG_MODE_OK_FOR_BASE_P (op0, mode))
1448 index_op = 1;
1449 else if (REG_OK_FOR_INDEX_P (op1))
1450 index_op = 1;
1451 else
1452 index_op = 0;
1454 locI = &XEXP (x, index_op);
1455 locB = &XEXP (x, !index_op);
1457 else if (code0 == REG)
1459 locI = &XEXP (x, 0);
1460 locB = &XEXP (x, 1);
1462 else if (code1 == REG)
1464 locI = &XEXP (x, 1);
1465 locB = &XEXP (x, 0);
1468 if (locI)
1469 changed |= replace_oldest_value_addr (locI, INDEX_REG_CLASS, mode,
1470 insn, vd);
1471 if (locB)
1472 changed |= replace_oldest_value_addr (locB,
1473 MODE_BASE_REG_CLASS (mode),
1474 mode, insn, vd);
1475 return changed;
1478 case POST_INC:
1479 case POST_DEC:
1480 case POST_MODIFY:
1481 case PRE_INC:
1482 case PRE_DEC:
1483 case PRE_MODIFY:
1484 return false;
1486 case MEM:
1487 return replace_oldest_value_mem (x, insn, vd);
1489 case REG:
1490 return replace_oldest_value_reg (loc, class, insn, vd);
1492 default:
1493 break;
1496 fmt = GET_RTX_FORMAT (code);
1497 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1499 if (fmt[i] == 'e')
1500 changed |= replace_oldest_value_addr (&XEXP (x, i), class, mode,
1501 insn, vd);
1502 else if (fmt[i] == 'E')
1503 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1504 changed |= replace_oldest_value_addr (&XVECEXP (x, i, j), class,
1505 mode, insn, vd);
1508 return changed;
1511 /* Similar to replace_oldest_value_reg, but X contains a memory. */
1513 static bool
1514 replace_oldest_value_mem (x, insn, vd)
1515 rtx x;
1516 rtx insn;
1517 struct value_data *vd;
1519 return replace_oldest_value_addr (&XEXP (x, 0),
1520 MODE_BASE_REG_CLASS (GET_MODE (x)),
1521 GET_MODE (x), insn, vd);
1524 /* Perform the forward copy propagation on basic block BB. */
1526 static bool
1527 copyprop_hardreg_forward_1 (bb, vd)
1528 basic_block bb;
1529 struct value_data *vd;
1531 bool changed = false;
1532 rtx insn;
1534 for (insn = bb->head; ; insn = NEXT_INSN (insn))
1536 int n_ops, i, alt, predicated;
1537 bool is_asm;
1538 rtx set;
1540 if (! INSN_P (insn))
1542 if (insn == bb->end)
1543 break;
1544 else
1545 continue;
1548 set = single_set (insn);
1549 extract_insn (insn);
1550 if (! constrain_operands (1))
1551 fatal_insn_not_found (insn);
1552 preprocess_constraints ();
1553 alt = which_alternative;
1554 n_ops = recog_data.n_operands;
1555 is_asm = asm_noperands (PATTERN (insn)) >= 0;
1557 /* Simplify the code below by rewriting things to reflect
1558 matching constraints. Also promote OP_OUT to OP_INOUT
1559 in predicated instructions. */
1561 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1562 for (i = 0; i < n_ops; ++i)
1564 int matches = recog_op_alt[i][alt].matches;
1565 if (matches >= 0)
1566 recog_op_alt[i][alt].class = recog_op_alt[matches][alt].class;
1567 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1568 || (predicated && recog_data.operand_type[i] == OP_OUT))
1569 recog_data.operand_type[i] = OP_INOUT;
1572 /* For each earlyclobber operand, zap the value data. */
1573 for (i = 0; i < n_ops; i++)
1574 if (recog_op_alt[i][alt].earlyclobber)
1575 kill_value (recog_data.operand[i], vd);
1577 /* Within asms, a clobber cannot overlap inputs or outputs.
1578 I wouldn't think this were true for regular insns, but
1579 scan_rtx treats them like that... */
1580 note_stores (PATTERN (insn), kill_clobbered_value, vd);
1582 /* Kill all auto-incremented values. */
1583 /* ??? REG_INC is useless, since stack pushes aren't done that way. */
1584 for_each_rtx (&PATTERN (insn), kill_autoinc_value, vd);
1586 /* Kill all early-clobbered operands. */
1587 for (i = 0; i < n_ops; i++)
1588 if (recog_op_alt[i][alt].earlyclobber)
1589 kill_value (recog_data.operand[i], vd);
1591 /* Special-case plain move instructions, since we may well
1592 be able to do the move from a different register class. */
1593 if (set && REG_P (SET_SRC (set)))
1595 rtx src = SET_SRC (set);
1596 unsigned int regno = REGNO (src);
1597 enum machine_mode mode = GET_MODE (src);
1598 unsigned int i;
1599 rtx new;
1601 /* If we are accessing SRC in some mode other that what we
1602 set it in, make sure that the replacement is valid. */
1603 if (mode != vd->e[regno].mode)
1605 if (HARD_REGNO_NREGS (regno, mode)
1606 > HARD_REGNO_NREGS (regno, vd->e[regno].mode))
1607 goto no_move_special_case;
1610 /* If the destination is also a register, try to find a source
1611 register in the same class. */
1612 if (REG_P (SET_DEST (set)))
1614 new = find_oldest_value_reg (REGNO_REG_CLASS (regno), src, vd);
1615 if (new && validate_change (insn, &SET_SRC (set), new, 0))
1617 if (rtl_dump_file)
1618 fprintf (rtl_dump_file,
1619 "insn %u: replaced reg %u with %u\n",
1620 INSN_UID (insn), regno, REGNO (new));
1621 changed = true;
1622 goto did_replacement;
1626 /* Otherwise, try all valid registers and see if its valid. */
1627 for (i = vd->e[regno].oldest_regno; i != regno;
1628 i = vd->e[i].next_regno)
1629 if (vd->e[i].mode == mode
1630 || mode_change_ok (vd->e[i].mode, mode, i))
1632 new = gen_rtx_raw_REG (mode, i);
1633 if (validate_change (insn, &SET_SRC (set), new, 0))
1635 ORIGINAL_REGNO (new) = ORIGINAL_REGNO (src);
1636 if (rtl_dump_file)
1637 fprintf (rtl_dump_file,
1638 "insn %u: replaced reg %u with %u\n",
1639 INSN_UID (insn), regno, REGNO (new));
1640 changed = true;
1641 goto did_replacement;
1645 no_move_special_case:
1647 /* For each input operand, replace a hard register with the
1648 eldest live copy that's in an appropriate register class. */
1649 for (i = 0; i < n_ops; i++)
1651 bool replaced = false;
1653 /* Don't scan match_operand here, since we've no reg class
1654 information to pass down. Any operands that we could
1655 substitute in will be represented elsewhere. */
1656 if (recog_data.constraints[i][0] == '\0')
1657 continue;
1659 /* Don't replace in asms intentionally referencing hard regs. */
1660 if (is_asm && GET_CODE (recog_data.operand[i]) == REG
1661 && (REGNO (recog_data.operand[i])
1662 == ORIGINAL_REGNO (recog_data.operand[i])))
1663 continue;
1665 if (recog_data.operand_type[i] == OP_IN)
1667 if (recog_op_alt[i][alt].is_address)
1668 replaced
1669 = replace_oldest_value_addr (recog_data.operand_loc[i],
1670 recog_op_alt[i][alt].class,
1671 VOIDmode, insn, vd);
1672 else if (REG_P (recog_data.operand[i]))
1673 replaced
1674 = replace_oldest_value_reg (recog_data.operand_loc[i],
1675 recog_op_alt[i][alt].class,
1676 insn, vd);
1677 else if (GET_CODE (recog_data.operand[i]) == MEM)
1678 replaced = replace_oldest_value_mem (recog_data.operand[i],
1679 insn, vd);
1681 else if (GET_CODE (recog_data.operand[i]) == MEM)
1682 replaced = replace_oldest_value_mem (recog_data.operand[i],
1683 insn, vd);
1685 /* If we performed any replacement, update match_dups. */
1686 if (replaced)
1688 int j;
1689 rtx new;
1691 changed = true;
1693 new = *recog_data.operand_loc[i];
1694 recog_data.operand[i] = new;
1695 for (j = 0; j < recog_data.n_dups; j++)
1696 if (recog_data.dup_num[j] == i)
1697 *recog_data.dup_loc[j] = new;
1701 did_replacement:
1702 /* Clobber call-clobbered registers. */
1703 if (GET_CODE (insn) == CALL_INSN)
1704 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1705 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, i))
1706 kill_value_regno (i, vd);
1708 /* Notice stores. */
1709 note_stores (PATTERN (insn), kill_set_value, vd);
1711 /* Notice copies. */
1712 if (set && REG_P (SET_DEST (set)) && REG_P (SET_SRC (set)))
1713 copy_value (SET_DEST (set), SET_SRC (set), vd);
1715 if (insn == bb->end)
1716 break;
1719 return changed;
1722 /* Main entry point for the forward copy propagation optimization. */
1724 void
1725 copyprop_hardreg_forward ()
1727 struct value_data *all_vd;
1728 bool need_refresh;
1729 int b;
1731 need_refresh = false;
1733 all_vd = xmalloc (sizeof (struct value_data) * n_basic_blocks);
1735 for (b = 0; b < n_basic_blocks; b++)
1737 basic_block bb = BASIC_BLOCK (b);
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 queueing of blocks. */
1743 if (bb->pred
1744 && ! bb->pred->pred_next
1745 && ! (bb->pred->flags & (EDGE_ABNORMAL_CALL | EDGE_EH))
1746 && bb->pred->src->index != ENTRY_BLOCK
1747 && bb->pred->src->index < b)
1748 all_vd[b] = all_vd[bb->pred->src->index];
1749 else
1750 init_value_data (all_vd + b);
1752 if (copyprop_hardreg_forward_1 (bb, all_vd + b))
1753 need_refresh = true;
1756 if (need_refresh)
1758 if (rtl_dump_file)
1759 fputs ("\n\n", rtl_dump_file);
1761 /* ??? Irritatingly, delete_noop_moves does not take a set of blocks
1762 to scan, so we have to do a life update with no initial set of
1763 blocks Just In Case. */
1764 delete_noop_moves (get_insns ());
1765 update_life_info (NULL, UPDATE_LIFE_GLOBAL_RM_NOTES,
1766 PROP_DEATH_NOTES
1767 | PROP_SCAN_DEAD_CODE
1768 | PROP_KILL_DEAD_CODE);
1771 free (all_vd);
1774 /* Dump the value chain data to stderr. */
1776 void
1777 debug_value_data (vd)
1778 struct value_data *vd;
1780 HARD_REG_SET set;
1781 unsigned int i, j;
1783 CLEAR_HARD_REG_SET (set);
1785 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1786 if (vd->e[i].oldest_regno == i)
1788 if (vd->e[i].mode == VOIDmode)
1790 if (vd->e[i].next_regno != INVALID_REGNUM)
1791 fprintf (stderr, "[%u] Bad next_regno for empty chain (%u)\n",
1792 i, vd->e[i].next_regno);
1793 continue;
1796 SET_HARD_REG_BIT (set, i);
1797 fprintf (stderr, "[%u %s] ", i, GET_MODE_NAME (vd->e[i].mode));
1799 for (j = vd->e[i].next_regno;
1800 j != INVALID_REGNUM;
1801 j = vd->e[j].next_regno)
1803 if (TEST_HARD_REG_BIT (set, j))
1805 fprintf (stderr, "[%u] Loop in regno chain\n", j);
1806 return;
1809 if (vd->e[j].oldest_regno != i)
1811 fprintf (stderr, "[%u] Bad oldest_regno (%u)\n",
1812 j, vd->e[j].oldest_regno);
1813 return;
1815 SET_HARD_REG_BIT (set, j);
1816 fprintf (stderr, "[%u %s] ", j, GET_MODE_NAME (vd->e[j].mode));
1818 fputc ('\n', stderr);
1821 for (i = 0; i < FIRST_PSEUDO_REGISTER; ++i)
1822 if (! TEST_HARD_REG_BIT (set, i)
1823 && (vd->e[i].mode != VOIDmode
1824 || vd->e[i].oldest_regno != i
1825 || vd->e[i].next_regno != INVALID_REGNUM))
1826 fprintf (stderr, "[%u] Non-empty reg in chain (%s %u %i)\n",
1827 i, GET_MODE_NAME (vd->e[i].mode), vd->e[i].oldest_regno,
1828 vd->e[i].next_regno);
1831 #ifdef ENABLE_CHECKING
1832 static void
1833 validate_value_data (vd)
1834 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