2010-04-19 Richard Guenther <rguenther@suse.de>
[official-gcc.git] / gcc / regrename.c
bloba5df6ee4f57570fa00020a1d517c972769b68def
1 /* Register renaming for the GNU compiler.
2 Copyright (C) 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
3 2010 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
15 License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
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 "addresses.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
32 #include "reload.h"
33 #include "output.h"
34 #include "function.h"
35 #include "recog.h"
36 #include "flags.h"
37 #include "toplev.h"
38 #include "obstack.h"
39 #include "timevar.h"
40 #include "tree-pass.h"
41 #include "df.h"
43 #if HOST_BITS_PER_WIDE_INT <= MAX_RECOG_OPERANDS
44 #error "Use a different bitmap implementation for untracked_operands."
45 #endif
47 /* We keep linked lists of DU_HEAD structures, each of which describes
48 a chain of occurrences of a reg. */
49 struct du_head
51 /* The next chain. */
52 struct du_head *next_chain;
53 /* The first and last elements of this chain. */
54 struct du_chain *first, *last;
55 /* Describes the register being tracked. */
56 unsigned regno, nregs;
58 /* A unique id to be used as an index into the conflicts bitmaps. */
59 unsigned id;
60 /* A bitmap to record conflicts with other chains. */
61 bitmap_head conflicts;
62 /* Conflicts with untracked hard registers. */
63 HARD_REG_SET hard_conflicts;
65 /* Nonzero if the chain is finished; zero if it is still open. */
66 unsigned int terminated:1;
67 /* Nonzero if the chain crosses a call. */
68 unsigned int need_caller_save_reg:1;
69 /* Nonzero if the register is used in a way that prevents renaming,
70 such as the SET_DEST of a CALL_INSN or an asm operand that used
71 to be a hard register. */
72 unsigned int cannot_rename:1;
75 /* This struct describes a single occurrence of a register. */
76 struct du_chain
78 /* Links to the next occurrence of the register. */
79 struct du_chain *next_use;
81 /* The insn where the register appears. */
82 rtx insn;
83 /* The location inside the insn. */
84 rtx *loc;
85 /* The register class required by the insn at this location. */
86 ENUM_BITFIELD(reg_class) cl : 16;
89 enum scan_actions
91 terminate_write,
92 terminate_dead,
93 mark_all_read,
94 mark_read,
95 mark_write,
96 /* mark_access is for marking the destination regs in
97 REG_FRAME_RELATED_EXPR notes (as if they were read) so that the
98 note is updated properly. */
99 mark_access
102 static const char * const scan_actions_name[] =
104 "terminate_write",
105 "terminate_dead",
106 "mark_all_read",
107 "mark_read",
108 "mark_write",
109 "mark_access"
112 static struct obstack rename_obstack;
114 static void do_replace (struct du_head *, int);
115 static void scan_rtx_reg (rtx, rtx *, enum reg_class,
116 enum scan_actions, enum op_type);
117 static void scan_rtx_address (rtx, rtx *, enum reg_class,
118 enum scan_actions, enum machine_mode);
119 static void scan_rtx (rtx, rtx *, enum reg_class, enum scan_actions,
120 enum op_type);
121 static struct du_head *build_def_use (basic_block);
122 static void dump_def_use_chain (struct du_head *);
124 typedef struct du_head *du_head_p;
125 DEF_VEC_P (du_head_p);
126 DEF_VEC_ALLOC_P (du_head_p, heap);
127 static VEC(du_head_p, heap) *id_to_chain;
129 static void
130 free_chain_data (void)
132 int i;
133 du_head_p ptr;
134 for (i = 0; VEC_iterate(du_head_p, id_to_chain, i, ptr); i++)
135 bitmap_clear (&ptr->conflicts);
137 VEC_free (du_head_p, heap, id_to_chain);
140 /* For a def-use chain HEAD, find which registers overlap its lifetime and
141 set the corresponding bits in *PSET. */
143 static void
144 merge_overlapping_regs (HARD_REG_SET *pset, struct du_head *head)
146 bitmap_iterator bi;
147 unsigned i;
148 IOR_HARD_REG_SET (*pset, head->hard_conflicts);
149 EXECUTE_IF_SET_IN_BITMAP (&head->conflicts, 0, i, bi)
151 du_head_p other = VEC_index (du_head_p, id_to_chain, i);
152 unsigned j = other->nregs;
153 while (j-- > 0)
154 SET_HARD_REG_BIT (*pset, other->regno + j);
158 /* Perform register renaming on the current function. */
160 static unsigned int
161 regrename_optimize (void)
163 int tick[FIRST_PSEUDO_REGISTER];
164 int this_tick = 0;
165 basic_block bb;
166 char *first_obj;
168 df_set_flags (DF_LR_RUN_DCE);
169 df_note_add_problem ();
170 df_analyze ();
171 df_set_flags (DF_DEFER_INSN_RESCAN);
173 memset (tick, 0, sizeof tick);
175 gcc_obstack_init (&rename_obstack);
176 first_obj = XOBNEWVAR (&rename_obstack, char, 0);
178 FOR_EACH_BB (bb)
180 struct du_head *all_chains = 0;
181 HARD_REG_SET unavailable;
182 #if 0
183 HARD_REG_SET regs_seen;
184 CLEAR_HARD_REG_SET (regs_seen);
185 #endif
187 id_to_chain = VEC_alloc (du_head_p, heap, 0);
189 CLEAR_HARD_REG_SET (unavailable);
191 if (dump_file)
192 fprintf (dump_file, "\nBasic block %d:\n", bb->index);
194 all_chains = build_def_use (bb);
196 if (dump_file)
197 dump_def_use_chain (all_chains);
199 CLEAR_HARD_REG_SET (unavailable);
200 /* Don't clobber traceback for noreturn functions. */
201 if (frame_pointer_needed)
203 add_to_hard_reg_set (&unavailable, Pmode, FRAME_POINTER_REGNUM);
204 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
205 add_to_hard_reg_set (&unavailable, Pmode, HARD_FRAME_POINTER_REGNUM);
206 #endif
209 while (all_chains)
211 int new_reg, best_new_reg, best_nregs;
212 int n_uses;
213 struct du_head *this_head = all_chains;
214 struct du_chain *tmp;
215 HARD_REG_SET this_unavailable;
216 int reg = this_head->regno;
217 int i;
219 all_chains = this_head->next_chain;
221 if (this_head->cannot_rename)
222 continue;
224 best_new_reg = reg;
225 best_nregs = this_head->nregs;
227 #if 0 /* This just disables optimization opportunities. */
228 /* Only rename once we've seen the reg more than once. */
229 if (! TEST_HARD_REG_BIT (regs_seen, reg))
231 SET_HARD_REG_BIT (regs_seen, reg);
232 continue;
234 #endif
236 if (fixed_regs[reg] || global_regs[reg]
237 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
238 || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
239 #else
240 || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
241 #endif
243 continue;
245 COPY_HARD_REG_SET (this_unavailable, unavailable);
247 /* Count number of uses, and narrow the set of registers we can
248 use for renaming. */
249 n_uses = 0;
250 for (tmp = this_head->first; tmp; tmp = tmp->next_use)
252 if (DEBUG_INSN_P (tmp->insn))
253 continue;
254 n_uses++;
255 IOR_COMPL_HARD_REG_SET (this_unavailable,
256 reg_class_contents[tmp->cl]);
259 if (n_uses < 2)
260 continue;
262 if (this_head->need_caller_save_reg)
263 IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
265 merge_overlapping_regs (&this_unavailable, this_head);
267 /* Now potential_regs is a reasonable approximation, let's
268 have a closer look at each register still in there. */
269 for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
271 enum machine_mode mode = GET_MODE (*this_head->first->loc);
272 int nregs = hard_regno_nregs[new_reg][mode];
274 for (i = nregs - 1; i >= 0; --i)
275 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
276 || fixed_regs[new_reg + i]
277 || global_regs[new_reg + i]
278 /* Can't use regs which aren't saved by the prologue. */
279 || (! df_regs_ever_live_p (new_reg + i)
280 && ! call_used_regs[new_reg + i])
281 #ifdef LEAF_REGISTERS
282 /* We can't use a non-leaf register if we're in a
283 leaf function. */
284 || (current_function_is_leaf
285 && !LEAF_REGISTERS[new_reg + i])
286 #endif
287 #ifdef HARD_REGNO_RENAME_OK
288 || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
289 #endif
291 break;
292 if (i >= 0)
293 continue;
295 /* See whether it accepts all modes that occur in
296 definition and uses. */
297 for (tmp = this_head->first; tmp; tmp = tmp->next_use)
298 if ((! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
299 && ! DEBUG_INSN_P (tmp->insn))
300 || (this_head->need_caller_save_reg
301 && ! (HARD_REGNO_CALL_PART_CLOBBERED
302 (reg, GET_MODE (*tmp->loc)))
303 && (HARD_REGNO_CALL_PART_CLOBBERED
304 (new_reg, GET_MODE (*tmp->loc)))))
305 break;
306 if (! tmp)
308 if (tick[best_new_reg] > tick[new_reg])
310 best_new_reg = new_reg;
311 best_nregs = nregs;
316 if (dump_file)
318 fprintf (dump_file, "Register %s in insn %d",
319 reg_names[reg], INSN_UID (this_head->first->insn));
320 if (this_head->need_caller_save_reg)
321 fprintf (dump_file, " crosses a call");
324 if (best_new_reg == reg)
326 tick[reg] = ++this_tick;
327 if (dump_file)
328 fprintf (dump_file, "; no available better choice\n");
329 continue;
332 if (dump_file)
333 fprintf (dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
335 do_replace (this_head, best_new_reg);
336 this_head->regno = best_new_reg;
337 this_head->nregs = best_nregs;
338 tick[best_new_reg] = ++this_tick;
339 df_set_regs_ever_live (best_new_reg, true);
342 free_chain_data ();
343 obstack_free (&rename_obstack, first_obj);
346 obstack_free (&rename_obstack, NULL);
348 if (dump_file)
349 fputc ('\n', dump_file);
351 return 0;
354 static void
355 do_replace (struct du_head *head, int reg)
357 struct du_chain *chain;
358 unsigned int base_regno = head->regno;
359 bool found_note = false;
361 gcc_assert (! DEBUG_INSN_P (head->first->insn));
363 for (chain = head->first; chain; chain = chain->next_use)
365 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
366 struct reg_attrs *attr = REG_ATTRS (*chain->loc);
367 int reg_ptr = REG_POINTER (*chain->loc);
369 if (DEBUG_INSN_P (chain->insn) && REGNO (*chain->loc) != base_regno)
370 INSN_VAR_LOCATION_LOC (chain->insn) = gen_rtx_UNKNOWN_VAR_LOC ();
371 else
373 rtx note;
375 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
376 if (regno >= FIRST_PSEUDO_REGISTER)
377 ORIGINAL_REGNO (*chain->loc) = regno;
378 REG_ATTRS (*chain->loc) = attr;
379 REG_POINTER (*chain->loc) = reg_ptr;
381 for (note = REG_NOTES (chain->insn); note; note = XEXP (note, 1))
383 enum reg_note kind = REG_NOTE_KIND (note);
384 if (kind == REG_DEAD || kind == REG_UNUSED)
386 rtx reg = XEXP (note, 0);
387 gcc_assert (HARD_REGISTER_P (reg));
389 if (REGNO (reg) == base_regno)
391 found_note = true;
392 if (kind == REG_DEAD
393 && reg_set_p (*chain->loc, chain->insn))
394 remove_note (chain->insn, note);
395 else
396 XEXP (note, 0) = *chain->loc;
397 break;
403 df_insn_rescan (chain->insn);
405 if (!found_note)
407 /* If the chain's first insn is the same as the last, we should have
408 found a REG_UNUSED note. */
409 gcc_assert (head->first->insn != head->last->insn);
410 if (!reg_set_p (*head->last->loc, head->last->insn))
411 add_reg_note (head->last->insn, REG_DEAD, *head->last->loc);
416 /* Walk all chains starting with CHAINS and record that they conflict with
417 another chain whose id is ID. */
419 static void
420 mark_conflict (struct du_head *chains, unsigned id)
422 while (chains)
424 bitmap_set_bit (&chains->conflicts, id);
425 chains = chains->next_chain;
429 /* True if we found a register with a size mismatch, which means that we
430 can't track its lifetime accurately. If so, we abort the current block
431 without renaming. */
432 static bool fail_current_block;
434 /* The id to be given to the next opened chain. */
435 static unsigned current_id;
437 /* List of currently open chains, and closed chains that can be renamed. */
438 static struct du_head *open_chains;
439 static struct du_head *closed_chains;
441 /* Bitmap of open chains. The bits set always match the list found in
442 open_chains. */
443 static bitmap_head open_chains_set;
445 /* Record the registers being tracked in open_chains. */
446 static HARD_REG_SET live_in_chains;
448 /* Record the registers that are live but not tracked. The intersection
449 between this and live_in_chains is empty. */
450 static HARD_REG_SET live_hard_regs;
452 /* Return true if OP is a reg for which all bits are set in PSET, false
453 if all bits are clear.
454 In other cases, set fail_current_block and return false. */
456 static bool
457 verify_reg_in_set (rtx op, HARD_REG_SET *pset)
459 unsigned regno, nregs;
460 bool all_live, all_dead;
461 if (!REG_P (op))
462 return false;
464 regno = REGNO (op);
465 nregs = hard_regno_nregs[regno][GET_MODE (op)];
466 all_live = all_dead = true;
467 while (nregs-- > 0)
468 if (TEST_HARD_REG_BIT (*pset, regno + nregs))
469 all_dead = false;
470 else
471 all_live = false;
472 if (!all_dead && !all_live)
474 fail_current_block = true;
475 return false;
477 return all_live;
480 /* Return true if OP is a reg that is being tracked already in some form.
481 May set fail_current_block if it sees an unhandled case of overlap. */
483 static bool
484 verify_reg_tracked (rtx op)
486 return (verify_reg_in_set (op, &live_hard_regs)
487 || verify_reg_in_set (op, &live_in_chains));
490 /* Called through note_stores. DATA points to a rtx_code, either SET or
491 CLOBBER, which tells us which kind of rtx to look at. If we have a
492 match, record the set register in live_hard_regs and in the hard_conflicts
493 bitmap of open chains. */
495 static void
496 note_sets_clobbers (rtx x, const_rtx set, void *data)
498 enum rtx_code code = *(enum rtx_code *)data;
499 struct du_head *chain;
501 if (GET_CODE (x) == SUBREG)
502 x = SUBREG_REG (x);
503 if (!REG_P (x) || GET_CODE (set) != code)
504 return;
505 /* There must not be pseudos at this point. */
506 gcc_assert (HARD_REGISTER_P (x));
507 add_to_hard_reg_set (&live_hard_regs, GET_MODE (x), REGNO (x));
508 for (chain = open_chains; chain; chain = chain->next_chain)
509 add_to_hard_reg_set (&chain->hard_conflicts, GET_MODE (x), REGNO (x));
512 /* Create a new chain for THIS_NREGS registers starting at THIS_REGNO,
513 and record its occurrence in *LOC, which is being written to in INSN.
514 This access requires a register of class CL. */
516 static void
517 create_new_chain (unsigned this_regno, unsigned this_nregs, rtx *loc,
518 rtx insn, enum reg_class cl)
520 struct du_head *head = XOBNEW (&rename_obstack, struct du_head);
521 struct du_chain *this_du;
522 int nregs;
524 head->next_chain = open_chains;
525 open_chains = head;
526 head->regno = this_regno;
527 head->nregs = this_nregs;
528 head->need_caller_save_reg = 0;
529 head->cannot_rename = 0;
530 head->terminated = 0;
532 VEC_safe_push (du_head_p, heap, id_to_chain, head);
533 head->id = current_id++;
535 bitmap_initialize (&head->conflicts, &bitmap_default_obstack);
536 bitmap_copy (&head->conflicts, &open_chains_set);
537 mark_conflict (open_chains, head->id);
539 /* Since we're tracking this as a chain now, remove it from the
540 list of conflicting live hard registers and track it in
541 live_in_chains instead. */
542 nregs = head->nregs;
543 while (nregs-- > 0)
545 SET_HARD_REG_BIT (live_in_chains, head->regno + nregs);
546 CLEAR_HARD_REG_BIT (live_hard_regs, head->regno + nregs);
549 COPY_HARD_REG_SET (head->hard_conflicts, live_hard_regs);
550 bitmap_set_bit (&open_chains_set, head->id);
552 open_chains = head;
554 if (dump_file)
556 fprintf (dump_file, "Creating chain %s (%d)",
557 reg_names[head->regno], head->id);
558 if (insn != NULL_RTX)
559 fprintf (dump_file, " at insn %d", INSN_UID (insn));
560 fprintf (dump_file, "\n");
563 if (insn == NULL_RTX)
565 head->first = head->last = NULL;
566 return;
569 this_du = XOBNEW (&rename_obstack, struct du_chain);
570 head->first = head->last = this_du;
572 this_du->next_use = 0;
573 this_du->loc = loc;
574 this_du->insn = insn;
575 this_du->cl = cl;
578 static void
579 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class cl, enum scan_actions action,
580 enum op_type type)
582 struct du_head **p;
583 rtx x = *loc;
584 enum machine_mode mode = GET_MODE (x);
585 unsigned this_regno = REGNO (x);
586 unsigned this_nregs = hard_regno_nregs[this_regno][mode];
588 if (action == mark_write)
590 if (type == OP_OUT)
591 create_new_chain (this_regno, this_nregs, loc, insn, cl);
592 return;
595 if ((type == OP_OUT) != (action == terminate_write || action == mark_access))
596 return;
598 for (p = &open_chains; *p;)
600 struct du_head *head = *p;
601 struct du_head *next = head->next_chain;
602 int exact_match = (head->regno == this_regno
603 && head->nregs == this_nregs);
604 int superset = (this_regno <= head->regno
605 && this_regno + this_nregs >= head->regno + head->nregs);
606 int subset = (this_regno >= head->regno
607 && this_regno + this_nregs <= head->regno + head->nregs);
609 if (head->terminated
610 || head->regno + head->nregs <= this_regno
611 || this_regno + this_nregs <= head->regno)
613 p = &head->next_chain;
614 continue;
617 if (action == mark_read || action == mark_access)
619 /* ??? Class NO_REGS can happen if the md file makes use of
620 EXTRA_CONSTRAINTS to match registers. Which is arguably
621 wrong, but there we are. */
623 if (cl == NO_REGS || (!exact_match && !DEBUG_INSN_P (insn)))
625 if (dump_file)
626 fprintf (dump_file,
627 "Cannot rename chain %s (%d) at insn %d (%s)\n",
628 reg_names[head->regno], head->id, INSN_UID (insn),
629 scan_actions_name[(int) action]);
630 head->cannot_rename = 1;
631 if (superset)
633 unsigned nregs = this_nregs;
634 head->regno = this_regno;
635 head->nregs = this_nregs;
636 while (nregs-- > 0)
637 SET_HARD_REG_BIT (live_in_chains, head->regno + nregs);
638 if (dump_file)
639 fprintf (dump_file,
640 "Widening register in chain %s (%d) at insn %d\n",
641 reg_names[head->regno], head->id, INSN_UID (insn));
643 else if (!subset)
645 fail_current_block = true;
646 if (dump_file)
647 fprintf (dump_file,
648 "Failing basic block due to unhandled overlap\n");
651 else
653 struct du_chain *this_du;
654 this_du = XOBNEW (&rename_obstack, struct du_chain);
655 this_du->next_use = 0;
656 this_du->loc = loc;
657 this_du->insn = insn;
658 this_du->cl = cl;
659 if (head->first == NULL)
660 head->first = this_du;
661 else
662 head->last->next_use = this_du;
663 head->last = this_du;
666 /* Avoid adding the same location in a DEBUG_INSN multiple times,
667 which could happen with non-exact overlap. */
668 if (DEBUG_INSN_P (insn))
669 return;
670 /* Otherwise, find any other chains that do not match exactly;
671 ensure they all get marked unrenamable. */
672 p = &head->next_chain;
673 continue;
676 /* Whether the terminated chain can be used for renaming
677 depends on the action and this being an exact match.
678 In either case, we remove this element from open_chains. */
680 if ((action == terminate_dead || action == terminate_write)
681 && superset)
683 unsigned nregs;
685 head->terminated = 1;
686 head->next_chain = closed_chains;
687 closed_chains = head;
688 bitmap_clear_bit (&open_chains_set, head->id);
690 nregs = head->nregs;
691 while (nregs-- > 0)
692 CLEAR_HARD_REG_BIT (live_in_chains, head->regno + nregs);
694 *p = next;
695 if (dump_file)
696 fprintf (dump_file,
697 "Closing chain %s (%d) at insn %d (%s)\n",
698 reg_names[head->regno], head->id, INSN_UID (insn),
699 scan_actions_name[(int) action]);
701 else if (action == terminate_dead || action == terminate_write)
703 /* In this case, tracking liveness gets too hard. Fail the
704 entire basic block. */
705 if (dump_file)
706 fprintf (dump_file,
707 "Failing basic block due to unhandled overlap\n");
708 fail_current_block = true;
709 return;
711 else
713 head->cannot_rename = 1;
714 if (dump_file)
715 fprintf (dump_file,
716 "Cannot rename chain %s (%d) at insn %d (%s)\n",
717 reg_names[head->regno], head->id, INSN_UID (insn),
718 scan_actions_name[(int) action]);
719 p = &head->next_chain;
724 /* Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
725 BASE_REG_CLASS depending on how the register is being considered. */
727 static void
728 scan_rtx_address (rtx insn, rtx *loc, enum reg_class cl,
729 enum scan_actions action, enum machine_mode mode)
731 rtx x = *loc;
732 RTX_CODE code = GET_CODE (x);
733 const char *fmt;
734 int i, j;
736 if (action == mark_write || action == mark_access)
737 return;
739 switch (code)
741 case PLUS:
743 rtx orig_op0 = XEXP (x, 0);
744 rtx orig_op1 = XEXP (x, 1);
745 RTX_CODE code0 = GET_CODE (orig_op0);
746 RTX_CODE code1 = GET_CODE (orig_op1);
747 rtx op0 = orig_op0;
748 rtx op1 = orig_op1;
749 rtx *locI = NULL;
750 rtx *locB = NULL;
751 enum rtx_code index_code = SCRATCH;
753 if (GET_CODE (op0) == SUBREG)
755 op0 = SUBREG_REG (op0);
756 code0 = GET_CODE (op0);
759 if (GET_CODE (op1) == SUBREG)
761 op1 = SUBREG_REG (op1);
762 code1 = GET_CODE (op1);
765 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
766 || code0 == ZERO_EXTEND || code1 == MEM)
768 locI = &XEXP (x, 0);
769 locB = &XEXP (x, 1);
770 index_code = GET_CODE (*locI);
772 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
773 || code1 == ZERO_EXTEND || code0 == MEM)
775 locI = &XEXP (x, 1);
776 locB = &XEXP (x, 0);
777 index_code = GET_CODE (*locI);
779 else if (code0 == CONST_INT || code0 == CONST
780 || code0 == SYMBOL_REF || code0 == LABEL_REF)
782 locB = &XEXP (x, 1);
783 index_code = GET_CODE (XEXP (x, 0));
785 else if (code1 == CONST_INT || code1 == CONST
786 || code1 == SYMBOL_REF || code1 == LABEL_REF)
788 locB = &XEXP (x, 0);
789 index_code = GET_CODE (XEXP (x, 1));
791 else if (code0 == REG && code1 == REG)
793 int index_op;
794 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
796 if (REGNO_OK_FOR_INDEX_P (regno1)
797 && regno_ok_for_base_p (regno0, mode, PLUS, REG))
798 index_op = 1;
799 else if (REGNO_OK_FOR_INDEX_P (regno0)
800 && regno_ok_for_base_p (regno1, mode, PLUS, REG))
801 index_op = 0;
802 else if (regno_ok_for_base_p (regno0, mode, PLUS, REG)
803 || REGNO_OK_FOR_INDEX_P (regno1))
804 index_op = 1;
805 else if (regno_ok_for_base_p (regno1, mode, PLUS, REG))
806 index_op = 0;
807 else
808 index_op = 1;
810 locI = &XEXP (x, index_op);
811 locB = &XEXP (x, !index_op);
812 index_code = GET_CODE (*locI);
814 else if (code0 == REG)
816 locI = &XEXP (x, 0);
817 locB = &XEXP (x, 1);
818 index_code = GET_CODE (*locI);
820 else if (code1 == REG)
822 locI = &XEXP (x, 1);
823 locB = &XEXP (x, 0);
824 index_code = GET_CODE (*locI);
827 if (locI)
828 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
829 if (locB)
830 scan_rtx_address (insn, locB, base_reg_class (mode, PLUS, index_code),
831 action, mode);
833 return;
836 case POST_INC:
837 case POST_DEC:
838 case POST_MODIFY:
839 case PRE_INC:
840 case PRE_DEC:
841 case PRE_MODIFY:
842 #ifndef AUTO_INC_DEC
843 /* If the target doesn't claim to handle autoinc, this must be
844 something special, like a stack push. Kill this chain. */
845 action = mark_all_read;
846 #endif
847 break;
849 case MEM:
850 scan_rtx_address (insn, &XEXP (x, 0),
851 base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
852 GET_MODE (x));
853 return;
855 case REG:
856 scan_rtx_reg (insn, loc, cl, action, OP_IN);
857 return;
859 default:
860 break;
863 fmt = GET_RTX_FORMAT (code);
864 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
866 if (fmt[i] == 'e')
867 scan_rtx_address (insn, &XEXP (x, i), cl, action, mode);
868 else if (fmt[i] == 'E')
869 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
870 scan_rtx_address (insn, &XVECEXP (x, i, j), cl, action, mode);
874 static void
875 scan_rtx (rtx insn, rtx *loc, enum reg_class cl, enum scan_actions action,
876 enum op_type type)
878 const char *fmt;
879 rtx x = *loc;
880 enum rtx_code code = GET_CODE (x);
881 int i, j;
883 code = GET_CODE (x);
884 switch (code)
886 case CONST:
887 case CONST_INT:
888 case CONST_DOUBLE:
889 case CONST_FIXED:
890 case CONST_VECTOR:
891 case SYMBOL_REF:
892 case LABEL_REF:
893 case CC0:
894 case PC:
895 return;
897 case REG:
898 scan_rtx_reg (insn, loc, cl, action, type);
899 return;
901 case MEM:
902 scan_rtx_address (insn, &XEXP (x, 0),
903 base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
904 GET_MODE (x));
905 return;
907 case SET:
908 scan_rtx (insn, &SET_SRC (x), cl, action, OP_IN);
909 scan_rtx (insn, &SET_DEST (x), cl, action,
910 (GET_CODE (PATTERN (insn)) == COND_EXEC
911 && verify_reg_tracked (SET_DEST (x))) ? OP_INOUT : OP_OUT);
912 return;
914 case STRICT_LOW_PART:
915 scan_rtx (insn, &XEXP (x, 0), cl, action,
916 verify_reg_tracked (XEXP (x, 0)) ? OP_INOUT : OP_OUT);
917 return;
919 case ZERO_EXTRACT:
920 case SIGN_EXTRACT:
921 scan_rtx (insn, &XEXP (x, 0), cl, action,
922 (type == OP_IN ? OP_IN :
923 verify_reg_tracked (XEXP (x, 0)) ? OP_INOUT : OP_OUT));
924 scan_rtx (insn, &XEXP (x, 1), cl, action, OP_IN);
925 scan_rtx (insn, &XEXP (x, 2), cl, action, OP_IN);
926 return;
928 case POST_INC:
929 case PRE_INC:
930 case POST_DEC:
931 case PRE_DEC:
932 case POST_MODIFY:
933 case PRE_MODIFY:
934 /* Should only happen inside MEM. */
935 gcc_unreachable ();
937 case CLOBBER:
938 scan_rtx (insn, &SET_DEST (x), cl, action,
939 (GET_CODE (PATTERN (insn)) == COND_EXEC
940 && verify_reg_tracked (SET_DEST (x))) ? OP_INOUT : OP_OUT);
941 return;
943 case EXPR_LIST:
944 scan_rtx (insn, &XEXP (x, 0), cl, action, type);
945 if (XEXP (x, 1))
946 scan_rtx (insn, &XEXP (x, 1), cl, action, type);
947 return;
949 default:
950 break;
953 fmt = GET_RTX_FORMAT (code);
954 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
956 if (fmt[i] == 'e')
957 scan_rtx (insn, &XEXP (x, i), cl, action, type);
958 else if (fmt[i] == 'E')
959 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
960 scan_rtx (insn, &XVECEXP (x, i, j), cl, action, type);
964 /* Hide operands of the current insn (of which there are N_OPS) by
965 substituting cc0 for them.
966 Previous values are stored in the OLD_OPERANDS and OLD_DUPS.
967 For every bit set in DO_NOT_HIDE, we leave the operand alone.
968 If INOUT_AND_EC_ONLY is set, we only do this for OP_INOUT type operands
969 and earlyclobbers. */
971 static void
972 hide_operands (int n_ops, rtx *old_operands, rtx *old_dups,
973 unsigned HOST_WIDE_INT do_not_hide, bool inout_and_ec_only)
975 int i;
976 int alt = which_alternative;
977 for (i = 0; i < n_ops; i++)
979 old_operands[i] = recog_data.operand[i];
980 /* Don't squash match_operator or match_parallel here, since
981 we don't know that all of the contained registers are
982 reachable by proper operands. */
983 if (recog_data.constraints[i][0] == '\0')
984 continue;
985 if (do_not_hide & (1 << i))
986 continue;
987 if (!inout_and_ec_only || recog_data.operand_type[i] == OP_INOUT
988 || recog_op_alt[i][alt].earlyclobber)
989 *recog_data.operand_loc[i] = cc0_rtx;
991 for (i = 0; i < recog_data.n_dups; i++)
993 int opn = recog_data.dup_num[i];
994 old_dups[i] = *recog_data.dup_loc[i];
995 if (do_not_hide & (1 << opn))
996 continue;
997 if (!inout_and_ec_only || recog_data.operand_type[opn] == OP_INOUT
998 || recog_op_alt[opn][alt].earlyclobber)
999 *recog_data.dup_loc[i] = cc0_rtx;
1003 /* Undo the substitution performed by hide_operands. INSN is the insn we
1004 are processing; the arguments are the same as in hide_operands. */
1006 static void
1007 restore_operands (rtx insn, int n_ops, rtx *old_operands, rtx *old_dups)
1009 int i;
1010 for (i = 0; i < recog_data.n_dups; i++)
1011 *recog_data.dup_loc[i] = old_dups[i];
1012 for (i = 0; i < n_ops; i++)
1013 *recog_data.operand_loc[i] = old_operands[i];
1014 if (recog_data.n_dups)
1015 df_insn_rescan (insn);
1018 /* For each output operand of INSN, call scan_rtx to create a new
1019 open chain. Do this only for normal or earlyclobber outputs,
1020 depending on EARLYCLOBBER. */
1022 static void
1023 record_out_operands (rtx insn, bool earlyclobber)
1025 int n_ops = recog_data.n_operands;
1026 int alt = which_alternative;
1028 int i;
1030 for (i = 0; i < n_ops + recog_data.n_dups; i++)
1032 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
1033 rtx *loc = (i < n_ops
1034 ? recog_data.operand_loc[opn]
1035 : recog_data.dup_loc[i - n_ops]);
1036 rtx op = *loc;
1037 enum reg_class cl = recog_op_alt[opn][alt].cl;
1039 struct du_head *prev_open;
1041 if (recog_data.operand_type[opn] != OP_OUT
1042 || recog_op_alt[opn][alt].earlyclobber != earlyclobber)
1043 continue;
1045 prev_open = open_chains;
1046 scan_rtx (insn, loc, cl, mark_write, OP_OUT);
1048 /* ??? Many targets have output constraints on the SET_DEST
1049 of a call insn, which is stupid, since these are certainly
1050 ABI defined hard registers. For these, and for asm operands
1051 that originally referenced hard registers, we must record that
1052 the chain cannot be renamed. */
1053 if (CALL_P (insn)
1054 || (asm_noperands (PATTERN (insn)) > 0
1055 && REG_P (op)
1056 && REGNO (op) == ORIGINAL_REGNO (op)))
1058 if (prev_open != open_chains)
1059 open_chains->cannot_rename = 1;
1064 /* Build def/use chain. */
1066 static struct du_head *
1067 build_def_use (basic_block bb)
1069 rtx insn;
1070 df_ref *def_rec;
1071 unsigned HOST_WIDE_INT untracked_operands;
1073 open_chains = closed_chains = NULL;
1075 fail_current_block = false;
1077 current_id = 0;
1078 bitmap_initialize (&open_chains_set, &bitmap_default_obstack);
1079 CLEAR_HARD_REG_SET (live_in_chains);
1080 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_in (bb));
1081 for (def_rec = df_get_artificial_defs (bb->index); *def_rec; def_rec++)
1083 df_ref def = *def_rec;
1084 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
1085 SET_HARD_REG_BIT (live_hard_regs, DF_REF_REGNO (def));
1088 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1090 if (NONDEBUG_INSN_P (insn))
1092 int n_ops;
1093 rtx note;
1094 rtx old_operands[MAX_RECOG_OPERANDS];
1095 rtx old_dups[MAX_DUP_OPERANDS];
1096 int i;
1097 int alt;
1098 int predicated;
1099 enum rtx_code set_code = SET;
1100 enum rtx_code clobber_code = CLOBBER;
1102 /* Process the insn, determining its effect on the def-use
1103 chains and live hard registers. We perform the following
1104 steps with the register references in the insn, simulating
1105 its effect:
1106 (1) Deal with earlyclobber operands and CLOBBERs of non-operands
1107 by creating chains and marking hard regs live.
1108 (2) Any read outside an operand causes any chain it overlaps
1109 with to be marked unrenamable.
1110 (3) Any read inside an operand is added if there's already
1111 an open chain for it.
1112 (4) For any REG_DEAD note we find, close open chains that
1113 overlap it.
1114 (5) For any non-earlyclobber write we find, close open chains
1115 that overlap it.
1116 (6) For any non-earlyclobber write we find in an operand, make
1117 a new chain or mark the hard register as live.
1118 (7) For any REG_UNUSED, close any chains we just opened.
1120 We cannot deal with situations where we track a reg in one mode
1121 and see a reference in another mode; these will cause the chain
1122 to be marked unrenamable or even cause us to abort the entire
1123 basic block. */
1125 extract_insn (insn);
1126 if (! constrain_operands (1))
1127 fatal_insn_not_found (insn);
1128 preprocess_constraints ();
1129 alt = which_alternative;
1130 n_ops = recog_data.n_operands;
1131 untracked_operands = 0;
1133 /* Simplify the code below by rewriting things to reflect
1134 matching constraints. Also promote OP_OUT to OP_INOUT in
1135 predicated instructions, but only for register operands
1136 that are already tracked, so that we can create a chain
1137 when the first SET makes a register live. */
1139 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1140 for (i = 0; i < n_ops; ++i)
1142 rtx op = recog_data.operand[i];
1143 int matches = recog_op_alt[i][alt].matches;
1144 if (matches >= 0)
1145 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
1146 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1147 || (predicated && recog_data.operand_type[i] == OP_OUT))
1149 recog_data.operand_type[i] = OP_INOUT;
1150 /* A special case to deal with instruction patterns that
1151 have matching operands with different modes. If we're
1152 not already tracking such a reg, we won't start here,
1153 and we must instead make sure to make the operand visible
1154 to the machinery that tracks hard registers. */
1155 if (matches >= 0
1156 && (GET_MODE_SIZE (recog_data.operand_mode[i])
1157 != GET_MODE_SIZE (recog_data.operand_mode[matches]))
1158 && !verify_reg_in_set (op, &live_in_chains))
1160 untracked_operands |= 1 << i;
1161 untracked_operands |= 1 << matches;
1164 /* If there's an in-out operand with a register that is not
1165 being tracked at all yet, open a chain. */
1166 if (recog_data.operand_type[i] == OP_INOUT
1167 && !(untracked_operands & (1 << i))
1168 && REG_P (op)
1169 && !verify_reg_tracked (op))
1171 enum machine_mode mode = GET_MODE (op);
1172 unsigned this_regno = REGNO (op);
1173 unsigned this_nregs = hard_regno_nregs[this_regno][mode];
1174 create_new_chain (this_regno, this_nregs, NULL, NULL_RTX,
1175 NO_REGS);
1179 if (fail_current_block)
1180 break;
1182 /* Step 1a: Mark hard registers that are clobbered in this insn,
1183 outside an operand, as live. */
1184 hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1185 false);
1186 note_stores (PATTERN (insn), note_sets_clobbers, &clobber_code);
1187 restore_operands (insn, n_ops, old_operands, old_dups);
1189 /* Step 1b: Begin new chains for earlyclobbered writes inside
1190 operands. */
1191 record_out_operands (insn, true);
1193 /* Step 2: Mark chains for which we have reads outside operands
1194 as unrenamable.
1195 We do this by munging all operands into CC0, and closing
1196 everything remaining. */
1198 hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1199 false);
1200 scan_rtx (insn, &PATTERN (insn), NO_REGS, mark_all_read, OP_IN);
1201 restore_operands (insn, n_ops, old_operands, old_dups);
1203 /* Step 2B: Can't rename function call argument registers. */
1204 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
1205 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
1206 NO_REGS, mark_all_read, OP_IN);
1208 /* Step 2C: Can't rename asm operands that were originally
1209 hard registers. */
1210 if (asm_noperands (PATTERN (insn)) > 0)
1211 for (i = 0; i < n_ops; i++)
1213 rtx *loc = recog_data.operand_loc[i];
1214 rtx op = *loc;
1216 if (REG_P (op)
1217 && REGNO (op) == ORIGINAL_REGNO (op)
1218 && (recog_data.operand_type[i] == OP_IN
1219 || recog_data.operand_type[i] == OP_INOUT))
1220 scan_rtx (insn, loc, NO_REGS, mark_all_read, OP_IN);
1223 /* Step 3: Append to chains for reads inside operands. */
1224 for (i = 0; i < n_ops + recog_data.n_dups; i++)
1226 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
1227 rtx *loc = (i < n_ops
1228 ? recog_data.operand_loc[opn]
1229 : recog_data.dup_loc[i - n_ops]);
1230 enum reg_class cl = recog_op_alt[opn][alt].cl;
1231 enum op_type type = recog_data.operand_type[opn];
1233 /* Don't scan match_operand here, since we've no reg class
1234 information to pass down. Any operands that we could
1235 substitute in will be represented elsewhere. */
1236 if (recog_data.constraints[opn][0] == '\0'
1237 || untracked_operands & (1 << opn))
1238 continue;
1240 if (recog_op_alt[opn][alt].is_address)
1241 scan_rtx_address (insn, loc, cl, mark_read, VOIDmode);
1242 else
1243 scan_rtx (insn, loc, cl, mark_read, type);
1246 /* Step 3B: Record updates for regs in REG_INC notes, and
1247 source regs in REG_FRAME_RELATED_EXPR notes. */
1248 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1249 if (REG_NOTE_KIND (note) == REG_INC
1250 || REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
1251 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
1252 OP_INOUT);
1254 /* Step 4: Close chains for registers that die here. */
1255 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1256 if (REG_NOTE_KIND (note) == REG_DEAD)
1258 remove_from_hard_reg_set (&live_hard_regs,
1259 GET_MODE (XEXP (note, 0)),
1260 REGNO (XEXP (note, 0)));
1261 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
1262 OP_IN);
1265 /* Step 4B: If this is a call, any chain live at this point
1266 requires a caller-saved reg. */
1267 if (CALL_P (insn))
1269 struct du_head *p;
1270 for (p = open_chains; p; p = p->next_chain)
1271 p->need_caller_save_reg = 1;
1274 /* Step 5: Close open chains that overlap writes. Similar to
1275 step 2, we hide in-out operands, since we do not want to
1276 close these chains. We also hide earlyclobber operands,
1277 since we've opened chains for them in step 1, and earlier
1278 chains they would overlap with must have been closed at
1279 the previous insn at the latest, as such operands cannot
1280 possibly overlap with any input operands. */
1282 hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1283 true);
1284 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN);
1285 restore_operands (insn, n_ops, old_operands, old_dups);
1287 /* Step 6a: Mark hard registers that are set in this insn,
1288 outside an operand, as live. */
1289 hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1290 false);
1291 note_stores (PATTERN (insn), note_sets_clobbers, &set_code);
1292 restore_operands (insn, n_ops, old_operands, old_dups);
1294 /* Step 6b: Begin new chains for writes inside operands. */
1295 record_out_operands (insn, false);
1297 /* Step 6c: Record destination regs in REG_FRAME_RELATED_EXPR
1298 notes for update. */
1299 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1300 if (REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
1301 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_access,
1302 OP_INOUT);
1304 /* Step 7: Close chains for registers that were never
1305 really used here. */
1306 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1307 if (REG_NOTE_KIND (note) == REG_UNUSED)
1309 remove_from_hard_reg_set (&live_hard_regs,
1310 GET_MODE (XEXP (note, 0)),
1311 REGNO (XEXP (note, 0)));
1312 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
1313 OP_IN);
1316 else if (DEBUG_INSN_P (insn)
1317 && !VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn)))
1319 scan_rtx (insn, &INSN_VAR_LOCATION_LOC (insn),
1320 ALL_REGS, mark_read, OP_IN);
1322 if (insn == BB_END (bb))
1323 break;
1326 bitmap_clear (&open_chains_set);
1328 if (fail_current_block)
1329 return NULL;
1331 /* Since we close every chain when we find a REG_DEAD note, anything that
1332 is still open lives past the basic block, so it can't be renamed. */
1333 return closed_chains;
1336 /* Dump all def/use chains in CHAINS to DUMP_FILE. They are
1337 printed in reverse order as that's how we build them. */
1339 static void
1340 dump_def_use_chain (struct du_head *head)
1342 while (head)
1344 struct du_chain *this_du = head->first;
1345 fprintf (dump_file, "Register %s (%d):",
1346 reg_names[head->regno], head->nregs);
1347 while (this_du)
1349 fprintf (dump_file, " %d [%s]", INSN_UID (this_du->insn),
1350 reg_class_names[this_du->cl]);
1351 this_du = this_du->next_use;
1353 fprintf (dump_file, "\n");
1354 head = head->next_chain;
1359 static bool
1360 gate_handle_regrename (void)
1362 return (optimize > 0 && (flag_rename_registers));
1365 struct rtl_opt_pass pass_regrename =
1368 RTL_PASS,
1369 "rnreg", /* name */
1370 gate_handle_regrename, /* gate */
1371 regrename_optimize, /* execute */
1372 NULL, /* sub */
1373 NULL, /* next */
1374 0, /* static_pass_number */
1375 TV_RENAME_REGISTERS, /* tv_id */
1376 0, /* properties_required */
1377 0, /* properties_provided */
1378 0, /* properties_destroyed */
1379 0, /* todo_flags_start */
1380 TODO_df_finish | TODO_verify_rtl_sharing |
1381 TODO_dump_func /* todo_flags_finish */