* config/avr/avr.c (avr_function_arg_advance): Undo r179037.
[official-gcc.git] / gcc / regrename.c
blobbfff1033018ccdf824e757e5df93fdc5ab5e9cfe
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-error.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 "obstack.h"
38 #include "timevar.h"
39 #include "tree-pass.h"
40 #include "df.h"
41 #include "target.h"
43 /* This file implements the RTL register renaming pass of the compiler. It is
44 a semi-local pass whose goal is to maximize the usage of the register file
45 of the processor by substituting registers for others in the solution given
46 by the register allocator. The algorithm is as follows:
48 1. Local def/use chains are built: within each basic block, chains are
49 opened and closed; if a chain isn't closed at the end of the block,
50 it is dropped. We pre-open chains if we have already examined a
51 predecessor block and found chains live at the end which match
52 live registers at the start of the new block.
54 2. We try to combine the local chains across basic block boundaries by
55 comparing chains that were open at the start or end of a block to
56 those in successor/predecessor blocks.
58 3. For each chain, the set of possible renaming registers is computed.
59 This takes into account the renaming of previously processed chains.
60 Optionally, a preferred class is computed for the renaming register.
62 4. The best renaming register is computed for the chain in the above set,
63 using a round-robin allocation. If a preferred class exists, then the
64 round-robin allocation is done within the class first, if possible.
65 The round-robin allocation of renaming registers itself is global.
67 5. If a renaming register has been found, it is substituted in the chain.
69 Targets can parameterize the pass by specifying a preferred class for the
70 renaming register for a given (super)class of registers to be renamed. */
72 #if HOST_BITS_PER_WIDE_INT <= MAX_RECOG_OPERANDS
73 #error "Use a different bitmap implementation for untracked_operands."
74 #endif
76 /* We keep linked lists of DU_HEAD structures, each of which describes
77 a chain of occurrences of a reg. */
78 struct du_head
80 /* The next chain. */
81 struct du_head *next_chain;
82 /* The first and last elements of this chain. */
83 struct du_chain *first, *last;
84 /* Describe the register being tracked, register number and count. */
85 unsigned regno;
86 int nregs;
88 /* A unique id to be used as an index into the conflicts bitmaps. */
89 unsigned id;
90 /* A bitmap to record conflicts with other chains. */
91 bitmap_head conflicts;
92 /* Conflicts with untracked hard registers. */
93 HARD_REG_SET hard_conflicts;
95 /* Nonzero if the chain crosses a call. */
96 unsigned int need_caller_save_reg:1;
97 /* Nonzero if the register is used in a way that prevents renaming,
98 such as the SET_DEST of a CALL_INSN or an asm operand that used
99 to be a hard register. */
100 unsigned int cannot_rename:1;
103 /* This struct describes a single occurrence of a register. */
104 struct du_chain
106 /* Links to the next occurrence of the register. */
107 struct du_chain *next_use;
109 /* The insn where the register appears. */
110 rtx insn;
111 /* The location inside the insn. */
112 rtx *loc;
113 /* The register class required by the insn at this location. */
114 ENUM_BITFIELD(reg_class) cl : 16;
117 enum scan_actions
119 terminate_write,
120 terminate_dead,
121 mark_all_read,
122 mark_read,
123 mark_write,
124 /* mark_access is for marking the destination regs in
125 REG_FRAME_RELATED_EXPR notes (as if they were read) so that the
126 note is updated properly. */
127 mark_access
130 static const char * const scan_actions_name[] =
132 "terminate_write",
133 "terminate_dead",
134 "mark_all_read",
135 "mark_read",
136 "mark_write",
137 "mark_access"
140 /* TICK and THIS_TICK are used to record the last time we saw each
141 register. */
142 static int tick[FIRST_PSEUDO_REGISTER];
143 static int this_tick = 0;
145 static struct obstack rename_obstack;
147 static void do_replace (struct du_head *, int);
148 static void scan_rtx (rtx, rtx *, enum reg_class, enum scan_actions,
149 enum op_type);
150 static bool build_def_use (basic_block);
152 typedef struct du_head *du_head_p;
153 DEF_VEC_P (du_head_p);
154 DEF_VEC_ALLOC_P (du_head_p, heap);
156 /* The id to be given to the next opened chain. */
157 static unsigned current_id;
159 /* A mapping of unique id numbers to chains. */
160 static VEC(du_head_p, heap) *id_to_chain;
162 /* List of currently open chains. */
163 static struct du_head *open_chains;
165 /* Bitmap of open chains. The bits set always match the list found in
166 open_chains. */
167 static bitmap_head open_chains_set;
169 /* Record the registers being tracked in open_chains. */
170 static HARD_REG_SET live_in_chains;
172 /* Record the registers that are live but not tracked. The intersection
173 between this and live_in_chains is empty. */
174 static HARD_REG_SET live_hard_regs;
176 /* Return the chain corresponding to id number ID. Take into account that
177 chains may have been merged. */
178 static du_head_p
179 chain_from_id (unsigned int id)
181 du_head_p first_chain = VEC_index (du_head_p, id_to_chain, id);
182 du_head_p chain = first_chain;
183 while (chain->id != id)
185 id = chain->id;
186 chain = VEC_index (du_head_p, id_to_chain, id);
188 first_chain->id = id;
189 return chain;
192 /* Dump all def/use chains, starting at id FROM. */
194 static void
195 dump_def_use_chain (int from)
197 du_head_p head;
198 int i;
199 FOR_EACH_VEC_ELT_FROM (du_head_p, id_to_chain, i, head, from)
201 struct du_chain *this_du = head->first;
203 fprintf (dump_file, "Register %s (%d):",
204 reg_names[head->regno], head->nregs);
205 while (this_du)
207 fprintf (dump_file, " %d [%s]", INSN_UID (this_du->insn),
208 reg_class_names[this_du->cl]);
209 this_du = this_du->next_use;
211 fprintf (dump_file, "\n");
212 head = head->next_chain;
216 static void
217 free_chain_data (void)
219 int i;
220 du_head_p ptr;
221 for (i = 0; VEC_iterate(du_head_p, id_to_chain, i, ptr); i++)
222 bitmap_clear (&ptr->conflicts);
224 VEC_free (du_head_p, heap, id_to_chain);
227 /* Walk all chains starting with CHAINS and record that they conflict with
228 another chain whose id is ID. */
230 static void
231 mark_conflict (struct du_head *chains, unsigned id)
233 while (chains)
235 bitmap_set_bit (&chains->conflicts, id);
236 chains = chains->next_chain;
240 /* Create a new chain for THIS_NREGS registers starting at THIS_REGNO,
241 and record its occurrence in *LOC, which is being written to in INSN.
242 This access requires a register of class CL. */
244 static du_head_p
245 create_new_chain (unsigned this_regno, unsigned this_nregs, rtx *loc,
246 rtx insn, enum reg_class cl)
248 struct du_head *head = XOBNEW (&rename_obstack, struct du_head);
249 struct du_chain *this_du;
250 int nregs;
252 head->next_chain = open_chains;
253 head->regno = this_regno;
254 head->nregs = this_nregs;
255 head->need_caller_save_reg = 0;
256 head->cannot_rename = 0;
258 VEC_safe_push (du_head_p, heap, id_to_chain, head);
259 head->id = current_id++;
261 bitmap_initialize (&head->conflicts, &bitmap_default_obstack);
262 bitmap_copy (&head->conflicts, &open_chains_set);
263 mark_conflict (open_chains, head->id);
265 /* Since we're tracking this as a chain now, remove it from the
266 list of conflicting live hard registers and track it in
267 live_in_chains instead. */
268 nregs = head->nregs;
269 while (nregs-- > 0)
271 SET_HARD_REG_BIT (live_in_chains, head->regno + nregs);
272 CLEAR_HARD_REG_BIT (live_hard_regs, head->regno + nregs);
275 COPY_HARD_REG_SET (head->hard_conflicts, live_hard_regs);
276 bitmap_set_bit (&open_chains_set, head->id);
278 open_chains = head;
280 if (dump_file)
282 fprintf (dump_file, "Creating chain %s (%d)",
283 reg_names[head->regno], head->id);
284 if (insn != NULL_RTX)
285 fprintf (dump_file, " at insn %d", INSN_UID (insn));
286 fprintf (dump_file, "\n");
289 if (insn == NULL_RTX)
291 head->first = head->last = NULL;
292 return head;
295 this_du = XOBNEW (&rename_obstack, struct du_chain);
296 head->first = head->last = this_du;
298 this_du->next_use = 0;
299 this_du->loc = loc;
300 this_du->insn = insn;
301 this_du->cl = cl;
302 return head;
305 /* For a def-use chain HEAD, find which registers overlap its lifetime and
306 set the corresponding bits in *PSET. */
308 static void
309 merge_overlapping_regs (HARD_REG_SET *pset, struct du_head *head)
311 bitmap_iterator bi;
312 unsigned i;
313 IOR_HARD_REG_SET (*pset, head->hard_conflicts);
314 EXECUTE_IF_SET_IN_BITMAP (&head->conflicts, 0, i, bi)
316 du_head_p other = chain_from_id (i);
317 unsigned j = other->nregs;
318 gcc_assert (other != head);
319 while (j-- > 0)
320 SET_HARD_REG_BIT (*pset, other->regno + j);
324 /* Check if NEW_REG can be the candidate register to rename for
325 REG in THIS_HEAD chain. THIS_UNAVAILABLE is a set of unavailable hard
326 registers. */
328 static bool
329 check_new_reg_p (int reg ATTRIBUTE_UNUSED, int new_reg,
330 struct du_head *this_head, HARD_REG_SET this_unavailable)
332 enum machine_mode mode = GET_MODE (*this_head->first->loc);
333 int nregs = hard_regno_nregs[new_reg][mode];
334 int i;
335 struct du_chain *tmp;
337 for (i = nregs - 1; i >= 0; --i)
338 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
339 || fixed_regs[new_reg + i]
340 || global_regs[new_reg + i]
341 /* Can't use regs which aren't saved by the prologue. */
342 || (! df_regs_ever_live_p (new_reg + i)
343 && ! call_used_regs[new_reg + i])
344 #ifdef LEAF_REGISTERS
345 /* We can't use a non-leaf register if we're in a
346 leaf function. */
347 || (current_function_is_leaf
348 && !LEAF_REGISTERS[new_reg + i])
349 #endif
350 #ifdef HARD_REGNO_RENAME_OK
351 || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
352 #endif
354 return false;
356 /* See whether it accepts all modes that occur in
357 definition and uses. */
358 for (tmp = this_head->first; tmp; tmp = tmp->next_use)
359 if ((! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
360 && ! DEBUG_INSN_P (tmp->insn))
361 || (this_head->need_caller_save_reg
362 && ! (HARD_REGNO_CALL_PART_CLOBBERED
363 (reg, GET_MODE (*tmp->loc)))
364 && (HARD_REGNO_CALL_PART_CLOBBERED
365 (new_reg, GET_MODE (*tmp->loc)))))
366 return false;
368 return true;
371 /* Perform register renaming on the current function. */
372 static void
373 rename_chains (void)
375 HARD_REG_SET unavailable;
376 du_head_p this_head;
377 int i;
379 memset (tick, 0, sizeof tick);
381 CLEAR_HARD_REG_SET (unavailable);
382 /* Don't clobber traceback for noreturn functions. */
383 if (frame_pointer_needed)
385 add_to_hard_reg_set (&unavailable, Pmode, FRAME_POINTER_REGNUM);
386 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
387 add_to_hard_reg_set (&unavailable, Pmode, HARD_FRAME_POINTER_REGNUM);
388 #endif
391 FOR_EACH_VEC_ELT (du_head_p, id_to_chain, i, this_head)
393 int new_reg, best_new_reg, best_nregs;
394 int n_uses;
395 struct du_chain *tmp;
396 HARD_REG_SET this_unavailable;
397 int reg = this_head->regno;
398 int pass;
399 enum reg_class super_class = NO_REGS;
400 enum reg_class preferred_class;
401 bool has_preferred_class;
403 if (this_head->cannot_rename)
404 continue;
406 best_new_reg = reg;
407 best_nregs = this_head->nregs;
409 if (fixed_regs[reg] || global_regs[reg]
410 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
411 || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
412 #else
413 || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
414 #endif
416 continue;
418 COPY_HARD_REG_SET (this_unavailable, unavailable);
420 /* Iterate over elements in the chain in order to:
421 1. Count number of uses, and narrow the set of registers we can
422 use for renaming.
423 2. Compute the superunion of register classes in this chain. */
424 n_uses = 0;
425 super_class = NO_REGS;
426 for (tmp = this_head->first; tmp; tmp = tmp->next_use)
428 if (DEBUG_INSN_P (tmp->insn))
429 continue;
430 n_uses++;
431 IOR_COMPL_HARD_REG_SET (this_unavailable,
432 reg_class_contents[tmp->cl]);
433 super_class
434 = reg_class_superunion[(int) super_class][(int) tmp->cl];
437 if (n_uses < 2)
438 continue;
440 /* Further narrow the set of registers we can use for renaming.
441 If the chain needs a call-saved register, mark the call-used
442 registers as unavailable. */
443 if (this_head->need_caller_save_reg)
444 IOR_HARD_REG_SET (this_unavailable, call_used_reg_set);
446 /* And mark registers that overlap its lifetime as unavailable. */
447 merge_overlapping_regs (&this_unavailable, this_head);
449 /* Compute preferred rename class of super union of all the classes
450 in the chain. */
451 preferred_class
452 = (enum reg_class) targetm.preferred_rename_class (super_class);
454 /* If PREFERRED_CLASS is not NO_REGS, we iterate in the first pass
455 over registers that belong to PREFERRED_CLASS and try to find the
456 best register within the class. If that failed, we iterate in
457 the second pass over registers that don't belong to the class.
458 If PREFERRED_CLASS is NO_REGS, we iterate over all registers in
459 ascending order without any preference. */
460 has_preferred_class = (preferred_class != NO_REGS);
461 for (pass = (has_preferred_class ? 0 : 1); pass < 2; pass++)
463 for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
465 if (has_preferred_class
466 && ((pass == 0) != TEST_HARD_REG_BIT
467 (reg_class_contents[preferred_class], new_reg)))
468 continue;
470 /* In the first pass, we force the renaming of registers that
471 don't belong to PREFERRED_CLASS to registers that do, even
472 though the latters were used not very long ago. */
473 if (check_new_reg_p (reg, new_reg, this_head,
474 this_unavailable)
475 && ((pass == 0
476 && (!TEST_HARD_REG_BIT
477 (reg_class_contents[preferred_class],
478 best_new_reg)))
479 || tick[best_new_reg] > tick[new_reg]))
481 enum machine_mode mode
482 = GET_MODE (*this_head->first->loc);
483 best_new_reg = new_reg;
484 best_nregs = hard_regno_nregs[new_reg][mode];
487 if (pass == 0 && best_new_reg != reg)
488 break;
491 if (dump_file)
493 fprintf (dump_file, "Register %s in insn %d",
494 reg_names[reg], INSN_UID (this_head->first->insn));
495 if (this_head->need_caller_save_reg)
496 fprintf (dump_file, " crosses a call");
499 if (best_new_reg == reg)
501 tick[reg] = ++this_tick;
502 if (dump_file)
503 fprintf (dump_file, "; no available better choice\n");
504 continue;
507 if (dump_file)
508 fprintf (dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
510 do_replace (this_head, best_new_reg);
511 this_head->regno = best_new_reg;
512 this_head->nregs = best_nregs;
513 tick[best_new_reg] = ++this_tick;
514 df_set_regs_ever_live (best_new_reg, true);
518 /* A structure to record information for each hard register at the start of
519 a basic block. */
520 struct incoming_reg_info {
521 /* Holds the number of registers used in the chain that gave us information
522 about this register. Zero means no information known yet, while a
523 negative value is used for something that is part of, but not the first
524 register in a multi-register value. */
525 int nregs;
526 /* Set to true if we have accesses that conflict in the number of registers
527 used. */
528 bool unusable;
531 /* A structure recording information about each basic block. It is saved
532 and restored around basic block boundaries.
533 A pointer to such a structure is stored in each basic block's aux field
534 during regrename_analyze, except for blocks we know can't be optimized
535 (such as entry and exit blocks). */
536 struct bb_rename_info
538 /* The basic block corresponding to this structure. */
539 basic_block bb;
540 /* Copies of the global information. */
541 bitmap_head open_chains_set;
542 bitmap_head incoming_open_chains_set;
543 struct incoming_reg_info incoming[FIRST_PSEUDO_REGISTER];
546 /* Initialize a rename_info structure P for basic block BB, which starts a new
547 scan. */
548 static void
549 init_rename_info (struct bb_rename_info *p, basic_block bb)
551 int i;
552 df_ref *def_rec;
553 HARD_REG_SET start_chains_set;
555 p->bb = bb;
556 bitmap_initialize (&p->open_chains_set, &bitmap_default_obstack);
557 bitmap_initialize (&p->incoming_open_chains_set, &bitmap_default_obstack);
559 open_chains = NULL;
560 bitmap_clear (&open_chains_set);
562 CLEAR_HARD_REG_SET (live_in_chains);
563 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_in (bb));
564 for (def_rec = df_get_artificial_defs (bb->index); *def_rec; def_rec++)
566 df_ref def = *def_rec;
567 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
568 SET_HARD_REG_BIT (live_hard_regs, DF_REF_REGNO (def));
571 /* Open chains based on information from (at least one) predecessor
572 block. This gives us a chance later on to combine chains across
573 basic block boundaries. Inconsistencies (in access sizes) will
574 be caught normally and dealt with conservatively by disabling the
575 chain for renaming, and there is no risk of losing optimization
576 opportunities by opening chains either: if we did not open the
577 chains, we'd have to track the live register as a hard reg, and
578 we'd be unable to rename it in any case. */
579 CLEAR_HARD_REG_SET (start_chains_set);
580 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
582 struct incoming_reg_info *iri = p->incoming + i;
583 if (iri->nregs > 0 && !iri->unusable
584 && range_in_hard_reg_set_p (live_hard_regs, i, iri->nregs))
586 SET_HARD_REG_BIT (start_chains_set, i);
587 remove_range_from_hard_reg_set (&live_hard_regs, i, iri->nregs);
590 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
592 struct incoming_reg_info *iri = p->incoming + i;
593 if (TEST_HARD_REG_BIT (start_chains_set, i))
595 du_head_p chain;
596 if (dump_file)
597 fprintf (dump_file, "opening incoming chain\n");
598 chain = create_new_chain (i, iri->nregs, NULL, NULL_RTX, NO_REGS);
599 bitmap_set_bit (&p->incoming_open_chains_set, chain->id);
604 /* Record in RI that the block corresponding to it has an incoming
605 live value, described by CHAIN. */
606 static void
607 set_incoming_from_chain (struct bb_rename_info *ri, du_head_p chain)
609 int i;
610 int incoming_nregs = ri->incoming[chain->regno].nregs;
611 int nregs;
613 /* If we've recorded the same information before, everything is fine. */
614 if (incoming_nregs == chain->nregs)
616 if (dump_file)
617 fprintf (dump_file, "reg %d/%d already recorded\n",
618 chain->regno, chain->nregs);
619 return;
622 /* If we have no information for any of the involved registers, update
623 the incoming array. */
624 nregs = chain->nregs;
625 while (nregs-- > 0)
626 if (ri->incoming[chain->regno + nregs].nregs != 0
627 || ri->incoming[chain->regno + nregs].unusable)
628 break;
629 if (nregs < 0)
631 nregs = chain->nregs;
632 ri->incoming[chain->regno].nregs = nregs;
633 while (nregs-- > 1)
634 ri->incoming[chain->regno + nregs].nregs = -nregs;
635 if (dump_file)
636 fprintf (dump_file, "recorded reg %d/%d\n",
637 chain->regno, chain->nregs);
638 return;
641 /* There must be some kind of conflict. Prevent both the old and
642 new ranges from being used. */
643 if (incoming_nregs < 0)
644 ri->incoming[chain->regno + incoming_nregs].unusable = true;
645 for (i = 0; i < chain->nregs; i++)
646 ri->incoming[chain->regno + i].unusable = true;
649 /* Merge the two chains C1 and C2 so that all conflict information is
650 recorded and C1, and the id of C2 is changed to that of C1. */
651 static void
652 merge_chains (du_head_p c1, du_head_p c2)
654 if (c1 == c2)
655 return;
657 if (c2->first != NULL)
659 if (c1->first == NULL)
660 c1->first = c2->first;
661 else
662 c1->last->next_use = c2->first;
663 c1->last = c2->last;
666 c2->first = c2->last = NULL;
667 c2->id = c1->id;
669 IOR_HARD_REG_SET (c1->hard_conflicts, c2->hard_conflicts);
670 bitmap_ior_into (&c1->conflicts, &c2->conflicts);
672 c1->need_caller_save_reg |= c2->need_caller_save_reg;
673 c1->cannot_rename |= c2->cannot_rename;
676 /* Analyze the current function and build chains for renaming. */
678 static void
679 regrename_analyze (void)
681 struct bb_rename_info *rename_info;
682 int i;
683 basic_block bb;
684 int n_bbs;
685 int *inverse_postorder;
687 inverse_postorder = XNEWVEC (int, last_basic_block);
688 n_bbs = pre_and_rev_post_order_compute (NULL, inverse_postorder, false);
690 /* Gather some information about the blocks in this function. */
691 rename_info = XCNEWVEC (struct bb_rename_info, n_basic_blocks);
692 i = 0;
693 FOR_EACH_BB (bb)
695 struct bb_rename_info *ri = rename_info + i;
696 ri->bb = bb;
697 bb->aux = ri;
698 i++;
701 current_id = 0;
702 id_to_chain = VEC_alloc (du_head_p, heap, 0);
703 bitmap_initialize (&open_chains_set, &bitmap_default_obstack);
705 /* The order in which we visit blocks ensures that whenever
706 possible, we only process a block after at least one of its
707 predecessors, which provides a "seeding" effect to make the logic
708 in set_incoming_from_chain and init_rename_info useful. */
710 for (i = 0; i < n_bbs; i++)
712 basic_block bb1 = BASIC_BLOCK (inverse_postorder[i]);
713 struct bb_rename_info *this_info;
714 bool success;
715 edge e;
716 edge_iterator ei;
717 int old_length = VEC_length (du_head_p, id_to_chain);
719 this_info = (struct bb_rename_info *) bb1->aux;
720 if (this_info == NULL)
721 continue;
723 if (dump_file)
724 fprintf (dump_file, "\nprocessing block %d:\n", bb1->index);
726 init_rename_info (this_info, bb1);
728 success = build_def_use (bb1);
729 if (!success)
731 if (dump_file)
732 fprintf (dump_file, "failed\n");
733 bb1->aux = NULL;
734 VEC_truncate (du_head_p, id_to_chain, old_length);
735 current_id = old_length;
736 bitmap_clear (&this_info->incoming_open_chains_set);
737 open_chains = NULL;
738 continue;
741 if (dump_file)
742 dump_def_use_chain (old_length);
743 bitmap_copy (&this_info->open_chains_set, &open_chains_set);
745 /* Add successor blocks to the worklist if necessary, and record
746 data about our own open chains at the end of this block, which
747 will be used to pre-open chains when processing the successors. */
748 FOR_EACH_EDGE (e, ei, bb1->succs)
750 struct bb_rename_info *dest_ri;
751 struct du_head *chain;
753 if (dump_file)
754 fprintf (dump_file, "successor block %d\n", e->dest->index);
756 if (e->flags & (EDGE_EH | EDGE_ABNORMAL))
757 continue;
758 dest_ri = (struct bb_rename_info *)e->dest->aux;
759 if (dest_ri == NULL)
760 continue;
761 for (chain = open_chains; chain; chain = chain->next_chain)
762 set_incoming_from_chain (dest_ri, chain);
766 free (inverse_postorder);
768 /* Now, combine the chains data we have gathered across basic block
769 boundaries.
771 For every basic block, there may be chains open at the start, or at the
772 end. Rather than exclude them from renaming, we look for open chains
773 with matching registers at the other side of the CFG edge.
775 For a given chain using register R, open at the start of block B, we
776 must find an open chain using R on the other side of every edge leading
777 to B, if the register is live across this edge. In the code below,
778 N_PREDS_USED counts the number of edges where the register is live, and
779 N_PREDS_JOINED counts those where we found an appropriate chain for
780 joining.
782 We perform the analysis for both incoming and outgoing edges, but we
783 only need to merge once (in the second part, after verifying outgoing
784 edges). */
785 FOR_EACH_BB (bb)
787 struct bb_rename_info *bb_ri = (struct bb_rename_info *) bb->aux;
788 unsigned j;
789 bitmap_iterator bi;
791 if (bb_ri == NULL)
792 continue;
794 if (dump_file)
795 fprintf (dump_file, "processing bb %d in edges\n", bb->index);
797 EXECUTE_IF_SET_IN_BITMAP (&bb_ri->incoming_open_chains_set, 0, j, bi)
799 edge e;
800 edge_iterator ei;
801 struct du_head *chain = chain_from_id (j);
802 int n_preds_used = 0, n_preds_joined = 0;
804 FOR_EACH_EDGE (e, ei, bb->preds)
806 struct bb_rename_info *src_ri;
807 unsigned k;
808 bitmap_iterator bi2;
809 HARD_REG_SET live;
810 bool success = false;
812 REG_SET_TO_HARD_REG_SET (live, df_get_live_out (e->src));
813 if (!range_overlaps_hard_reg_set_p (live, chain->regno,
814 chain->nregs))
815 continue;
816 n_preds_used++;
818 if (e->flags & (EDGE_EH | EDGE_ABNORMAL))
819 continue;
821 src_ri = (struct bb_rename_info *)e->src->aux;
822 if (src_ri == NULL)
823 continue;
825 EXECUTE_IF_SET_IN_BITMAP (&src_ri->open_chains_set,
826 0, k, bi2)
828 struct du_head *outgoing_chain = chain_from_id (k);
830 if (outgoing_chain->regno == chain->regno
831 && outgoing_chain->nregs == chain->nregs)
833 n_preds_joined++;
834 success = true;
835 break;
838 if (!success && dump_file)
839 fprintf (dump_file, "failure to match with pred block %d\n",
840 e->src->index);
842 if (n_preds_joined < n_preds_used)
844 if (dump_file)
845 fprintf (dump_file, "cannot rename chain %d\n", j);
846 chain->cannot_rename = 1;
850 FOR_EACH_BB (bb)
852 struct bb_rename_info *bb_ri = (struct bb_rename_info *) bb->aux;
853 unsigned j;
854 bitmap_iterator bi;
856 if (bb_ri == NULL)
857 continue;
859 if (dump_file)
860 fprintf (dump_file, "processing bb %d out edges\n", bb->index);
862 EXECUTE_IF_SET_IN_BITMAP (&bb_ri->open_chains_set, 0, j, bi)
864 edge e;
865 edge_iterator ei;
866 struct du_head *chain = chain_from_id (j);
867 int n_succs_used = 0, n_succs_joined = 0;
869 FOR_EACH_EDGE (e, ei, bb->succs)
871 bool printed = false;
872 struct bb_rename_info *dest_ri;
873 unsigned k;
874 bitmap_iterator bi2;
875 HARD_REG_SET live;
877 REG_SET_TO_HARD_REG_SET (live, df_get_live_in (e->dest));
878 if (!range_overlaps_hard_reg_set_p (live, chain->regno,
879 chain->nregs))
880 continue;
882 n_succs_used++;
884 dest_ri = (struct bb_rename_info *)e->dest->aux;
885 if (dest_ri == NULL)
886 continue;
888 EXECUTE_IF_SET_IN_BITMAP (&dest_ri->incoming_open_chains_set,
889 0, k, bi2)
891 struct du_head *incoming_chain = chain_from_id (k);
893 if (incoming_chain->regno == chain->regno
894 && incoming_chain->nregs == chain->nregs)
896 if (dump_file)
898 if (!printed)
899 fprintf (dump_file,
900 "merging blocks for edge %d -> %d\n",
901 e->src->index, e->dest->index);
902 printed = true;
903 fprintf (dump_file,
904 " merging chains %d (->%d) and %d (->%d) [%s]\n",
905 k, incoming_chain->id, j, chain->id,
906 reg_names[incoming_chain->regno]);
909 merge_chains (chain, incoming_chain);
910 n_succs_joined++;
911 break;
915 if (n_succs_joined < n_succs_used)
917 if (dump_file)
918 fprintf (dump_file, "cannot rename chain %d\n",
920 chain->cannot_rename = 1;
925 free (rename_info);
927 FOR_EACH_BB (bb)
928 bb->aux = NULL;
931 static void
932 do_replace (struct du_head *head, int reg)
934 struct du_chain *chain;
935 unsigned int base_regno = head->regno;
937 for (chain = head->first; chain; chain = chain->next_use)
939 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
940 struct reg_attrs *attr = REG_ATTRS (*chain->loc);
941 int reg_ptr = REG_POINTER (*chain->loc);
943 if (DEBUG_INSN_P (chain->insn) && REGNO (*chain->loc) != base_regno)
944 INSN_VAR_LOCATION_LOC (chain->insn) = gen_rtx_UNKNOWN_VAR_LOC ();
945 else
947 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
948 if (regno >= FIRST_PSEUDO_REGISTER)
949 ORIGINAL_REGNO (*chain->loc) = regno;
950 REG_ATTRS (*chain->loc) = attr;
951 REG_POINTER (*chain->loc) = reg_ptr;
954 df_insn_rescan (chain->insn);
959 /* True if we found a register with a size mismatch, which means that we
960 can't track its lifetime accurately. If so, we abort the current block
961 without renaming. */
962 static bool fail_current_block;
964 /* Return true if OP is a reg for which all bits are set in PSET, false
965 if all bits are clear.
966 In other cases, set fail_current_block and return false. */
968 static bool
969 verify_reg_in_set (rtx op, HARD_REG_SET *pset)
971 unsigned regno, nregs;
972 bool all_live, all_dead;
973 if (!REG_P (op))
974 return false;
976 regno = REGNO (op);
977 nregs = hard_regno_nregs[regno][GET_MODE (op)];
978 all_live = all_dead = true;
979 while (nregs-- > 0)
980 if (TEST_HARD_REG_BIT (*pset, regno + nregs))
981 all_dead = false;
982 else
983 all_live = false;
984 if (!all_dead && !all_live)
986 fail_current_block = true;
987 return false;
989 return all_live;
992 /* Return true if OP is a reg that is being tracked already in some form.
993 May set fail_current_block if it sees an unhandled case of overlap. */
995 static bool
996 verify_reg_tracked (rtx op)
998 return (verify_reg_in_set (op, &live_hard_regs)
999 || verify_reg_in_set (op, &live_in_chains));
1002 /* Called through note_stores. DATA points to a rtx_code, either SET or
1003 CLOBBER, which tells us which kind of rtx to look at. If we have a
1004 match, record the set register in live_hard_regs and in the hard_conflicts
1005 bitmap of open chains. */
1007 static void
1008 note_sets_clobbers (rtx x, const_rtx set, void *data)
1010 enum rtx_code code = *(enum rtx_code *)data;
1011 struct du_head *chain;
1013 if (GET_CODE (x) == SUBREG)
1014 x = SUBREG_REG (x);
1015 if (!REG_P (x) || GET_CODE (set) != code)
1016 return;
1017 /* There must not be pseudos at this point. */
1018 gcc_assert (HARD_REGISTER_P (x));
1019 add_to_hard_reg_set (&live_hard_regs, GET_MODE (x), REGNO (x));
1020 for (chain = open_chains; chain; chain = chain->next_chain)
1021 add_to_hard_reg_set (&chain->hard_conflicts, GET_MODE (x), REGNO (x));
1024 static void
1025 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class cl, enum scan_actions action,
1026 enum op_type type)
1028 struct du_head **p;
1029 rtx x = *loc;
1030 enum machine_mode mode = GET_MODE (x);
1031 unsigned this_regno = REGNO (x);
1032 int this_nregs = hard_regno_nregs[this_regno][mode];
1034 if (action == mark_write)
1036 if (type == OP_OUT)
1037 create_new_chain (this_regno, this_nregs, loc, insn, cl);
1038 return;
1041 if ((type == OP_OUT) != (action == terminate_write || action == mark_access))
1042 return;
1044 for (p = &open_chains; *p;)
1046 struct du_head *head = *p;
1047 struct du_head *next = head->next_chain;
1048 int exact_match = (head->regno == this_regno
1049 && head->nregs == this_nregs);
1050 int superset = (this_regno <= head->regno
1051 && this_regno + this_nregs >= head->regno + head->nregs);
1052 int subset = (this_regno >= head->regno
1053 && this_regno + this_nregs <= head->regno + head->nregs);
1055 if (!bitmap_bit_p (&open_chains_set, head->id)
1056 || head->regno + head->nregs <= this_regno
1057 || this_regno + this_nregs <= head->regno)
1059 p = &head->next_chain;
1060 continue;
1063 if (action == mark_read || action == mark_access)
1065 /* ??? Class NO_REGS can happen if the md file makes use of
1066 EXTRA_CONSTRAINTS to match registers. Which is arguably
1067 wrong, but there we are. */
1069 if (cl == NO_REGS || (!exact_match && !DEBUG_INSN_P (insn)))
1071 if (dump_file)
1072 fprintf (dump_file,
1073 "Cannot rename chain %s (%d) at insn %d (%s)\n",
1074 reg_names[head->regno], head->id, INSN_UID (insn),
1075 scan_actions_name[(int) action]);
1076 head->cannot_rename = 1;
1077 if (superset)
1079 unsigned nregs = this_nregs;
1080 head->regno = this_regno;
1081 head->nregs = this_nregs;
1082 while (nregs-- > 0)
1083 SET_HARD_REG_BIT (live_in_chains, head->regno + nregs);
1084 if (dump_file)
1085 fprintf (dump_file,
1086 "Widening register in chain %s (%d) at insn %d\n",
1087 reg_names[head->regno], head->id, INSN_UID (insn));
1089 else if (!subset)
1091 fail_current_block = true;
1092 if (dump_file)
1093 fprintf (dump_file,
1094 "Failing basic block due to unhandled overlap\n");
1097 else
1099 struct du_chain *this_du;
1100 this_du = XOBNEW (&rename_obstack, struct du_chain);
1101 this_du->next_use = 0;
1102 this_du->loc = loc;
1103 this_du->insn = insn;
1104 this_du->cl = cl;
1105 if (head->first == NULL)
1106 head->first = this_du;
1107 else
1108 head->last->next_use = this_du;
1109 head->last = this_du;
1111 /* Avoid adding the same location in a DEBUG_INSN multiple times,
1112 which could happen with non-exact overlap. */
1113 if (DEBUG_INSN_P (insn))
1114 return;
1115 /* Otherwise, find any other chains that do not match exactly;
1116 ensure they all get marked unrenamable. */
1117 p = &head->next_chain;
1118 continue;
1121 /* Whether the terminated chain can be used for renaming
1122 depends on the action and this being an exact match.
1123 In either case, we remove this element from open_chains. */
1125 if ((action == terminate_dead || action == terminate_write)
1126 && (superset || subset))
1128 unsigned nregs;
1130 if (subset && !superset)
1131 head->cannot_rename = 1;
1132 bitmap_clear_bit (&open_chains_set, head->id);
1134 nregs = head->nregs;
1135 while (nregs-- > 0)
1137 CLEAR_HARD_REG_BIT (live_in_chains, head->regno + nregs);
1138 if (subset && !superset
1139 && (head->regno + nregs < this_regno
1140 || head->regno + nregs >= this_regno + this_nregs))
1141 SET_HARD_REG_BIT (live_hard_regs, head->regno + nregs);
1144 *p = next;
1145 if (dump_file)
1146 fprintf (dump_file,
1147 "Closing chain %s (%d) at insn %d (%s%s)\n",
1148 reg_names[head->regno], head->id, INSN_UID (insn),
1149 scan_actions_name[(int) action],
1150 superset ? ", superset" : subset ? ", subset" : "");
1152 else if (action == terminate_dead || action == terminate_write)
1154 /* In this case, tracking liveness gets too hard. Fail the
1155 entire basic block. */
1156 if (dump_file)
1157 fprintf (dump_file,
1158 "Failing basic block due to unhandled overlap\n");
1159 fail_current_block = true;
1160 return;
1162 else
1164 head->cannot_rename = 1;
1165 if (dump_file)
1166 fprintf (dump_file,
1167 "Cannot rename chain %s (%d) at insn %d (%s)\n",
1168 reg_names[head->regno], head->id, INSN_UID (insn),
1169 scan_actions_name[(int) action]);
1170 p = &head->next_chain;
1175 /* Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
1176 BASE_REG_CLASS depending on how the register is being considered. */
1178 static void
1179 scan_rtx_address (rtx insn, rtx *loc, enum reg_class cl,
1180 enum scan_actions action, enum machine_mode mode)
1182 rtx x = *loc;
1183 RTX_CODE code = GET_CODE (x);
1184 const char *fmt;
1185 int i, j;
1187 if (action == mark_write || action == mark_access)
1188 return;
1190 switch (code)
1192 case PLUS:
1194 rtx orig_op0 = XEXP (x, 0);
1195 rtx orig_op1 = XEXP (x, 1);
1196 RTX_CODE code0 = GET_CODE (orig_op0);
1197 RTX_CODE code1 = GET_CODE (orig_op1);
1198 rtx op0 = orig_op0;
1199 rtx op1 = orig_op1;
1200 rtx *locI = NULL;
1201 rtx *locB = NULL;
1202 enum rtx_code index_code = SCRATCH;
1204 if (GET_CODE (op0) == SUBREG)
1206 op0 = SUBREG_REG (op0);
1207 code0 = GET_CODE (op0);
1210 if (GET_CODE (op1) == SUBREG)
1212 op1 = SUBREG_REG (op1);
1213 code1 = GET_CODE (op1);
1216 if (code0 == MULT || code0 == SIGN_EXTEND || code0 == TRUNCATE
1217 || code0 == ZERO_EXTEND || code1 == MEM)
1219 locI = &XEXP (x, 0);
1220 locB = &XEXP (x, 1);
1221 index_code = GET_CODE (*locI);
1223 else if (code1 == MULT || code1 == SIGN_EXTEND || code1 == TRUNCATE
1224 || code1 == ZERO_EXTEND || code0 == MEM)
1226 locI = &XEXP (x, 1);
1227 locB = &XEXP (x, 0);
1228 index_code = GET_CODE (*locI);
1230 else if (code0 == CONST_INT || code0 == CONST
1231 || code0 == SYMBOL_REF || code0 == LABEL_REF)
1233 locB = &XEXP (x, 1);
1234 index_code = GET_CODE (XEXP (x, 0));
1236 else if (code1 == CONST_INT || code1 == CONST
1237 || code1 == SYMBOL_REF || code1 == LABEL_REF)
1239 locB = &XEXP (x, 0);
1240 index_code = GET_CODE (XEXP (x, 1));
1242 else if (code0 == REG && code1 == REG)
1244 int index_op;
1245 unsigned regno0 = REGNO (op0), regno1 = REGNO (op1);
1247 if (REGNO_OK_FOR_INDEX_P (regno1)
1248 && regno_ok_for_base_p (regno0, mode, PLUS, REG))
1249 index_op = 1;
1250 else if (REGNO_OK_FOR_INDEX_P (regno0)
1251 && regno_ok_for_base_p (regno1, mode, PLUS, REG))
1252 index_op = 0;
1253 else if (regno_ok_for_base_p (regno0, mode, PLUS, REG)
1254 || REGNO_OK_FOR_INDEX_P (regno1))
1255 index_op = 1;
1256 else if (regno_ok_for_base_p (regno1, mode, PLUS, REG))
1257 index_op = 0;
1258 else
1259 index_op = 1;
1261 locI = &XEXP (x, index_op);
1262 locB = &XEXP (x, !index_op);
1263 index_code = GET_CODE (*locI);
1265 else if (code0 == REG)
1267 locI = &XEXP (x, 0);
1268 locB = &XEXP (x, 1);
1269 index_code = GET_CODE (*locI);
1271 else if (code1 == REG)
1273 locI = &XEXP (x, 1);
1274 locB = &XEXP (x, 0);
1275 index_code = GET_CODE (*locI);
1278 if (locI)
1279 scan_rtx_address (insn, locI, INDEX_REG_CLASS, action, mode);
1280 if (locB)
1281 scan_rtx_address (insn, locB, base_reg_class (mode, PLUS, index_code),
1282 action, mode);
1284 return;
1287 case POST_INC:
1288 case POST_DEC:
1289 case POST_MODIFY:
1290 case PRE_INC:
1291 case PRE_DEC:
1292 case PRE_MODIFY:
1293 #ifndef AUTO_INC_DEC
1294 /* If the target doesn't claim to handle autoinc, this must be
1295 something special, like a stack push. Kill this chain. */
1296 action = mark_all_read;
1297 #endif
1298 break;
1300 case MEM:
1301 scan_rtx_address (insn, &XEXP (x, 0),
1302 base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
1303 GET_MODE (x));
1304 return;
1306 case REG:
1307 scan_rtx_reg (insn, loc, cl, action, OP_IN);
1308 return;
1310 default:
1311 break;
1314 fmt = GET_RTX_FORMAT (code);
1315 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1317 if (fmt[i] == 'e')
1318 scan_rtx_address (insn, &XEXP (x, i), cl, action, mode);
1319 else if (fmt[i] == 'E')
1320 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1321 scan_rtx_address (insn, &XVECEXP (x, i, j), cl, action, mode);
1325 static void
1326 scan_rtx (rtx insn, rtx *loc, enum reg_class cl, enum scan_actions action,
1327 enum op_type type)
1329 const char *fmt;
1330 rtx x = *loc;
1331 enum rtx_code code = GET_CODE (x);
1332 int i, j;
1334 code = GET_CODE (x);
1335 switch (code)
1337 case CONST:
1338 case CONST_INT:
1339 case CONST_DOUBLE:
1340 case CONST_FIXED:
1341 case CONST_VECTOR:
1342 case SYMBOL_REF:
1343 case LABEL_REF:
1344 case CC0:
1345 case PC:
1346 return;
1348 case REG:
1349 scan_rtx_reg (insn, loc, cl, action, type);
1350 return;
1352 case MEM:
1353 scan_rtx_address (insn, &XEXP (x, 0),
1354 base_reg_class (GET_MODE (x), MEM, SCRATCH), action,
1355 GET_MODE (x));
1356 return;
1358 case SET:
1359 scan_rtx (insn, &SET_SRC (x), cl, action, OP_IN);
1360 scan_rtx (insn, &SET_DEST (x), cl, action,
1361 (GET_CODE (PATTERN (insn)) == COND_EXEC
1362 && verify_reg_tracked (SET_DEST (x))) ? OP_INOUT : OP_OUT);
1363 return;
1365 case STRICT_LOW_PART:
1366 scan_rtx (insn, &XEXP (x, 0), cl, action,
1367 verify_reg_tracked (XEXP (x, 0)) ? OP_INOUT : OP_OUT);
1368 return;
1370 case ZERO_EXTRACT:
1371 case SIGN_EXTRACT:
1372 scan_rtx (insn, &XEXP (x, 0), cl, action,
1373 (type == OP_IN ? OP_IN :
1374 verify_reg_tracked (XEXP (x, 0)) ? OP_INOUT : OP_OUT));
1375 scan_rtx (insn, &XEXP (x, 1), cl, action, OP_IN);
1376 scan_rtx (insn, &XEXP (x, 2), cl, action, OP_IN);
1377 return;
1379 case POST_INC:
1380 case PRE_INC:
1381 case POST_DEC:
1382 case PRE_DEC:
1383 case POST_MODIFY:
1384 case PRE_MODIFY:
1385 /* Should only happen inside MEM. */
1386 gcc_unreachable ();
1388 case CLOBBER:
1389 scan_rtx (insn, &SET_DEST (x), cl, action,
1390 (GET_CODE (PATTERN (insn)) == COND_EXEC
1391 && verify_reg_tracked (SET_DEST (x))) ? OP_INOUT : OP_OUT);
1392 return;
1394 case EXPR_LIST:
1395 scan_rtx (insn, &XEXP (x, 0), cl, action, type);
1396 if (XEXP (x, 1))
1397 scan_rtx (insn, &XEXP (x, 1), cl, action, type);
1398 return;
1400 default:
1401 break;
1404 fmt = GET_RTX_FORMAT (code);
1405 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1407 if (fmt[i] == 'e')
1408 scan_rtx (insn, &XEXP (x, i), cl, action, type);
1409 else if (fmt[i] == 'E')
1410 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1411 scan_rtx (insn, &XVECEXP (x, i, j), cl, action, type);
1415 /* Hide operands of the current insn (of which there are N_OPS) by
1416 substituting cc0 for them.
1417 Previous values are stored in the OLD_OPERANDS and OLD_DUPS.
1418 For every bit set in DO_NOT_HIDE, we leave the operand alone.
1419 If INOUT_AND_EC_ONLY is set, we only do this for OP_INOUT type operands
1420 and earlyclobbers. */
1422 static void
1423 hide_operands (int n_ops, rtx *old_operands, rtx *old_dups,
1424 unsigned HOST_WIDE_INT do_not_hide, bool inout_and_ec_only)
1426 int i;
1427 int alt = which_alternative;
1428 for (i = 0; i < n_ops; i++)
1430 old_operands[i] = recog_data.operand[i];
1431 /* Don't squash match_operator or match_parallel here, since
1432 we don't know that all of the contained registers are
1433 reachable by proper operands. */
1434 if (recog_data.constraints[i][0] == '\0')
1435 continue;
1436 if (do_not_hide & (1 << i))
1437 continue;
1438 if (!inout_and_ec_only || recog_data.operand_type[i] == OP_INOUT
1439 || recog_op_alt[i][alt].earlyclobber)
1440 *recog_data.operand_loc[i] = cc0_rtx;
1442 for (i = 0; i < recog_data.n_dups; i++)
1444 int opn = recog_data.dup_num[i];
1445 old_dups[i] = *recog_data.dup_loc[i];
1446 if (do_not_hide & (1 << opn))
1447 continue;
1448 if (!inout_and_ec_only || recog_data.operand_type[opn] == OP_INOUT
1449 || recog_op_alt[opn][alt].earlyclobber)
1450 *recog_data.dup_loc[i] = cc0_rtx;
1454 /* Undo the substitution performed by hide_operands. INSN is the insn we
1455 are processing; the arguments are the same as in hide_operands. */
1457 static void
1458 restore_operands (rtx insn, int n_ops, rtx *old_operands, rtx *old_dups)
1460 int i;
1461 for (i = 0; i < recog_data.n_dups; i++)
1462 *recog_data.dup_loc[i] = old_dups[i];
1463 for (i = 0; i < n_ops; i++)
1464 *recog_data.operand_loc[i] = old_operands[i];
1465 if (recog_data.n_dups)
1466 df_insn_rescan (insn);
1469 /* For each output operand of INSN, call scan_rtx to create a new
1470 open chain. Do this only for normal or earlyclobber outputs,
1471 depending on EARLYCLOBBER. */
1473 static void
1474 record_out_operands (rtx insn, bool earlyclobber)
1476 int n_ops = recog_data.n_operands;
1477 int alt = which_alternative;
1479 int i;
1481 for (i = 0; i < n_ops + recog_data.n_dups; i++)
1483 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
1484 rtx *loc = (i < n_ops
1485 ? recog_data.operand_loc[opn]
1486 : recog_data.dup_loc[i - n_ops]);
1487 rtx op = *loc;
1488 enum reg_class cl = recog_op_alt[opn][alt].cl;
1490 struct du_head *prev_open;
1492 if (recog_data.operand_type[opn] != OP_OUT
1493 || recog_op_alt[opn][alt].earlyclobber != earlyclobber)
1494 continue;
1496 prev_open = open_chains;
1497 scan_rtx (insn, loc, cl, mark_write, OP_OUT);
1499 /* ??? Many targets have output constraints on the SET_DEST
1500 of a call insn, which is stupid, since these are certainly
1501 ABI defined hard registers. For these, and for asm operands
1502 that originally referenced hard registers, we must record that
1503 the chain cannot be renamed. */
1504 if (CALL_P (insn)
1505 || (asm_noperands (PATTERN (insn)) > 0
1506 && REG_P (op)
1507 && REGNO (op) == ORIGINAL_REGNO (op)))
1509 if (prev_open != open_chains)
1510 open_chains->cannot_rename = 1;
1515 /* Build def/use chain. */
1517 static bool
1518 build_def_use (basic_block bb)
1520 rtx insn;
1521 unsigned HOST_WIDE_INT untracked_operands;
1523 fail_current_block = false;
1525 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1527 if (NONDEBUG_INSN_P (insn))
1529 int n_ops;
1530 rtx note;
1531 rtx old_operands[MAX_RECOG_OPERANDS];
1532 rtx old_dups[MAX_DUP_OPERANDS];
1533 int i;
1534 int alt;
1535 int predicated;
1536 enum rtx_code set_code = SET;
1537 enum rtx_code clobber_code = CLOBBER;
1539 /* Process the insn, determining its effect on the def-use
1540 chains and live hard registers. We perform the following
1541 steps with the register references in the insn, simulating
1542 its effect:
1543 (1) Deal with earlyclobber operands and CLOBBERs of non-operands
1544 by creating chains and marking hard regs live.
1545 (2) Any read outside an operand causes any chain it overlaps
1546 with to be marked unrenamable.
1547 (3) Any read inside an operand is added if there's already
1548 an open chain for it.
1549 (4) For any REG_DEAD note we find, close open chains that
1550 overlap it.
1551 (5) For any non-earlyclobber write we find, close open chains
1552 that overlap it.
1553 (6) For any non-earlyclobber write we find in an operand, make
1554 a new chain or mark the hard register as live.
1555 (7) For any REG_UNUSED, close any chains we just opened.
1557 We cannot deal with situations where we track a reg in one mode
1558 and see a reference in another mode; these will cause the chain
1559 to be marked unrenamable or even cause us to abort the entire
1560 basic block. */
1562 extract_insn (insn);
1563 if (! constrain_operands (1))
1564 fatal_insn_not_found (insn);
1565 preprocess_constraints ();
1566 alt = which_alternative;
1567 n_ops = recog_data.n_operands;
1568 untracked_operands = 0;
1570 /* Simplify the code below by rewriting things to reflect
1571 matching constraints. Also promote OP_OUT to OP_INOUT in
1572 predicated instructions, but only for register operands
1573 that are already tracked, so that we can create a chain
1574 when the first SET makes a register live. */
1576 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1577 for (i = 0; i < n_ops; ++i)
1579 rtx op = recog_data.operand[i];
1580 int matches = recog_op_alt[i][alt].matches;
1581 if (matches >= 0)
1582 recog_op_alt[i][alt].cl = recog_op_alt[matches][alt].cl;
1583 if (matches >= 0 || recog_op_alt[i][alt].matched >= 0
1584 || (predicated && recog_data.operand_type[i] == OP_OUT))
1586 recog_data.operand_type[i] = OP_INOUT;
1587 /* A special case to deal with instruction patterns that
1588 have matching operands with different modes. If we're
1589 not already tracking such a reg, we won't start here,
1590 and we must instead make sure to make the operand visible
1591 to the machinery that tracks hard registers. */
1592 if (matches >= 0
1593 && (GET_MODE_SIZE (recog_data.operand_mode[i])
1594 != GET_MODE_SIZE (recog_data.operand_mode[matches]))
1595 && !verify_reg_in_set (op, &live_in_chains))
1597 untracked_operands |= 1 << i;
1598 untracked_operands |= 1 << matches;
1601 /* If there's an in-out operand with a register that is not
1602 being tracked at all yet, open a chain. */
1603 if (recog_data.operand_type[i] == OP_INOUT
1604 && !(untracked_operands & (1 << i))
1605 && REG_P (op)
1606 && !verify_reg_tracked (op))
1608 enum machine_mode mode = GET_MODE (op);
1609 unsigned this_regno = REGNO (op);
1610 unsigned this_nregs = hard_regno_nregs[this_regno][mode];
1611 create_new_chain (this_regno, this_nregs, NULL, NULL_RTX,
1612 NO_REGS);
1616 if (fail_current_block)
1617 break;
1619 /* Step 1a: Mark hard registers that are clobbered in this insn,
1620 outside an operand, as live. */
1621 hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1622 false);
1623 note_stores (PATTERN (insn), note_sets_clobbers, &clobber_code);
1624 restore_operands (insn, n_ops, old_operands, old_dups);
1626 /* Step 1b: Begin new chains for earlyclobbered writes inside
1627 operands. */
1628 record_out_operands (insn, true);
1630 /* Step 2: Mark chains for which we have reads outside operands
1631 as unrenamable.
1632 We do this by munging all operands into CC0, and closing
1633 everything remaining. */
1635 hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1636 false);
1637 scan_rtx (insn, &PATTERN (insn), NO_REGS, mark_all_read, OP_IN);
1638 restore_operands (insn, n_ops, old_operands, old_dups);
1640 /* Step 2B: Can't rename function call argument registers. */
1641 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
1642 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
1643 NO_REGS, mark_all_read, OP_IN);
1645 /* Step 2C: Can't rename asm operands that were originally
1646 hard registers. */
1647 if (asm_noperands (PATTERN (insn)) > 0)
1648 for (i = 0; i < n_ops; i++)
1650 rtx *loc = recog_data.operand_loc[i];
1651 rtx op = *loc;
1653 if (REG_P (op)
1654 && REGNO (op) == ORIGINAL_REGNO (op)
1655 && (recog_data.operand_type[i] == OP_IN
1656 || recog_data.operand_type[i] == OP_INOUT))
1657 scan_rtx (insn, loc, NO_REGS, mark_all_read, OP_IN);
1660 /* Step 3: Append to chains for reads inside operands. */
1661 for (i = 0; i < n_ops + recog_data.n_dups; i++)
1663 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
1664 rtx *loc = (i < n_ops
1665 ? recog_data.operand_loc[opn]
1666 : recog_data.dup_loc[i - n_ops]);
1667 enum reg_class cl = recog_op_alt[opn][alt].cl;
1668 enum op_type type = recog_data.operand_type[opn];
1670 /* Don't scan match_operand here, since we've no reg class
1671 information to pass down. Any operands that we could
1672 substitute in will be represented elsewhere. */
1673 if (recog_data.constraints[opn][0] == '\0'
1674 || untracked_operands & (1 << opn))
1675 continue;
1677 if (recog_op_alt[opn][alt].is_address)
1678 scan_rtx_address (insn, loc, cl, mark_read, VOIDmode);
1679 else
1680 scan_rtx (insn, loc, cl, mark_read, type);
1683 /* Step 3B: Record updates for regs in REG_INC notes, and
1684 source regs in REG_FRAME_RELATED_EXPR notes. */
1685 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1686 if (REG_NOTE_KIND (note) == REG_INC
1687 || REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
1688 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
1689 OP_INOUT);
1691 /* Step 4: Close chains for registers that die here. */
1692 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1693 if (REG_NOTE_KIND (note) == REG_DEAD)
1695 remove_from_hard_reg_set (&live_hard_regs,
1696 GET_MODE (XEXP (note, 0)),
1697 REGNO (XEXP (note, 0)));
1698 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
1699 OP_IN);
1702 /* Step 4B: If this is a call, any chain live at this point
1703 requires a caller-saved reg. */
1704 if (CALL_P (insn))
1706 struct du_head *p;
1707 for (p = open_chains; p; p = p->next_chain)
1708 p->need_caller_save_reg = 1;
1711 /* Step 5: Close open chains that overlap writes. Similar to
1712 step 2, we hide in-out operands, since we do not want to
1713 close these chains. We also hide earlyclobber operands,
1714 since we've opened chains for them in step 1, and earlier
1715 chains they would overlap with must have been closed at
1716 the previous insn at the latest, as such operands cannot
1717 possibly overlap with any input operands. */
1719 hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1720 true);
1721 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN);
1722 restore_operands (insn, n_ops, old_operands, old_dups);
1724 /* Step 6a: Mark hard registers that are set in this insn,
1725 outside an operand, as live. */
1726 hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1727 false);
1728 note_stores (PATTERN (insn), note_sets_clobbers, &set_code);
1729 restore_operands (insn, n_ops, old_operands, old_dups);
1731 /* Step 6b: Begin new chains for writes inside operands. */
1732 record_out_operands (insn, false);
1734 /* Step 6c: Record destination regs in REG_FRAME_RELATED_EXPR
1735 notes for update. */
1736 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1737 if (REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
1738 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_access,
1739 OP_INOUT);
1741 /* Step 7: Close chains for registers that were never
1742 really used here. */
1743 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1744 if (REG_NOTE_KIND (note) == REG_UNUSED)
1746 remove_from_hard_reg_set (&live_hard_regs,
1747 GET_MODE (XEXP (note, 0)),
1748 REGNO (XEXP (note, 0)));
1749 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
1750 OP_IN);
1753 else if (DEBUG_INSN_P (insn)
1754 && !VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn)))
1756 scan_rtx (insn, &INSN_VAR_LOCATION_LOC (insn),
1757 ALL_REGS, mark_read, OP_IN);
1759 if (insn == BB_END (bb))
1760 break;
1763 if (fail_current_block)
1764 return false;
1766 return true;
1769 /* Perform register renaming on the current function. */
1771 static unsigned int
1772 regrename_optimize (void)
1774 df_set_flags (DF_LR_RUN_DCE);
1775 df_note_add_problem ();
1776 df_analyze ();
1777 df_set_flags (DF_DEFER_INSN_RESCAN);
1779 gcc_obstack_init (&rename_obstack);
1781 regrename_analyze ();
1783 rename_chains ();
1785 free_chain_data ();
1786 obstack_free (&rename_obstack, NULL);
1788 return 0;
1791 static bool
1792 gate_handle_regrename (void)
1794 return (optimize > 0 && (flag_rename_registers));
1797 struct rtl_opt_pass pass_regrename =
1800 RTL_PASS,
1801 "rnreg", /* name */
1802 gate_handle_regrename, /* gate */
1803 regrename_optimize, /* execute */
1804 NULL, /* sub */
1805 NULL, /* next */
1806 0, /* static_pass_number */
1807 TV_RENAME_REGISTERS, /* tv_id */
1808 0, /* properties_required */
1809 0, /* properties_provided */
1810 0, /* properties_destroyed */
1811 0, /* todo_flags_start */
1812 TODO_df_finish | TODO_verify_rtl_sharing |
1813 0 /* todo_flags_finish */