2014-07-29 Ed Smith-Rowland <3dw4rd@verizon.net>
[official-gcc.git] / gcc / regrename.c
blob9eeb6e241dc5989333588e316d33073d21be1035
1 /* Register renaming for the GNU compiler.
2 Copyright (C) 2000-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "rtl-error.h"
25 #include "tm_p.h"
26 #include "insn-config.h"
27 #include "regs.h"
28 #include "addresses.h"
29 #include "hard-reg-set.h"
30 #include "basic-block.h"
31 #include "reload.h"
32 #include "output.h"
33 #include "function.h"
34 #include "recog.h"
35 #include "flags.h"
36 #include "obstack.h"
37 #include "tree-pass.h"
38 #include "df.h"
39 #include "target.h"
40 #include "emit-rtl.h"
41 #include "regrename.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 enum scan_actions
78 terminate_write,
79 terminate_dead,
80 mark_all_read,
81 mark_read,
82 mark_write,
83 /* mark_access is for marking the destination regs in
84 REG_FRAME_RELATED_EXPR notes (as if they were read) so that the
85 note is updated properly. */
86 mark_access
89 static const char * const scan_actions_name[] =
91 "terminate_write",
92 "terminate_dead",
93 "mark_all_read",
94 "mark_read",
95 "mark_write",
96 "mark_access"
99 /* TICK and THIS_TICK are used to record the last time we saw each
100 register. */
101 static int tick[FIRST_PSEUDO_REGISTER];
102 static int this_tick = 0;
104 static struct obstack rename_obstack;
106 /* If nonnull, the code calling into the register renamer requested
107 information about insn operands, and we store it here. */
108 vec<insn_rr_info> insn_rr;
110 static void scan_rtx (rtx, rtx *, enum reg_class, enum scan_actions,
111 enum op_type);
112 static bool build_def_use (basic_block);
114 /* The id to be given to the next opened chain. */
115 static unsigned current_id;
117 /* A mapping of unique id numbers to chains. */
118 static vec<du_head_p> id_to_chain;
120 /* List of currently open chains. */
121 static struct du_head *open_chains;
123 /* Bitmap of open chains. The bits set always match the list found in
124 open_chains. */
125 static bitmap_head open_chains_set;
127 /* Record the registers being tracked in open_chains. */
128 static HARD_REG_SET live_in_chains;
130 /* Record the registers that are live but not tracked. The intersection
131 between this and live_in_chains is empty. */
132 static HARD_REG_SET live_hard_regs;
134 /* Set while scanning RTL if INSN_RR is nonnull, i.e. if the current analysis
135 is for a caller that requires operand data. Used in
136 record_operand_use. */
137 static operand_rr_info *cur_operand;
139 /* Return the chain corresponding to id number ID. Take into account that
140 chains may have been merged. */
141 du_head_p
142 regrename_chain_from_id (unsigned int id)
144 du_head_p first_chain = id_to_chain[id];
145 du_head_p chain = first_chain;
146 while (chain->id != id)
148 id = chain->id;
149 chain = id_to_chain[id];
151 first_chain->id = id;
152 return chain;
155 /* Dump all def/use chains, starting at id FROM. */
157 static void
158 dump_def_use_chain (int from)
160 du_head_p head;
161 int i;
162 FOR_EACH_VEC_ELT_FROM (id_to_chain, i, head, from)
164 struct du_chain *this_du = head->first;
166 fprintf (dump_file, "Register %s (%d):",
167 reg_names[head->regno], head->nregs);
168 while (this_du)
170 fprintf (dump_file, " %d [%s]", INSN_UID (this_du->insn),
171 reg_class_names[this_du->cl]);
172 this_du = this_du->next_use;
174 fprintf (dump_file, "\n");
175 head = head->next_chain;
179 static void
180 free_chain_data (void)
182 int i;
183 du_head_p ptr;
184 for (i = 0; id_to_chain.iterate (i, &ptr); i++)
185 bitmap_clear (&ptr->conflicts);
187 id_to_chain.release ();
190 /* Walk all chains starting with CHAINS and record that they conflict with
191 another chain whose id is ID. */
193 static void
194 mark_conflict (struct du_head *chains, unsigned id)
196 while (chains)
198 bitmap_set_bit (&chains->conflicts, id);
199 chains = chains->next_chain;
203 /* Examine cur_operand, and if it is nonnull, record information about the
204 use THIS_DU which is part of the chain HEAD. */
206 static void
207 record_operand_use (struct du_head *head, struct du_chain *this_du)
209 if (cur_operand == NULL)
210 return;
211 gcc_assert (cur_operand->n_chains < MAX_REGS_PER_ADDRESS);
212 cur_operand->heads[cur_operand->n_chains] = head;
213 cur_operand->chains[cur_operand->n_chains++] = this_du;
216 /* Create a new chain for THIS_NREGS registers starting at THIS_REGNO,
217 and record its occurrence in *LOC, which is being written to in INSN.
218 This access requires a register of class CL. */
220 static du_head_p
221 create_new_chain (unsigned this_regno, unsigned this_nregs, rtx *loc,
222 rtx insn, enum reg_class cl)
224 struct du_head *head = XOBNEW (&rename_obstack, struct du_head);
225 struct du_chain *this_du;
226 int nregs;
228 head->next_chain = open_chains;
229 head->regno = this_regno;
230 head->nregs = this_nregs;
231 head->need_caller_save_reg = 0;
232 head->cannot_rename = 0;
234 id_to_chain.safe_push (head);
235 head->id = current_id++;
237 bitmap_initialize (&head->conflicts, &bitmap_default_obstack);
238 bitmap_copy (&head->conflicts, &open_chains_set);
239 mark_conflict (open_chains, head->id);
241 /* Since we're tracking this as a chain now, remove it from the
242 list of conflicting live hard registers and track it in
243 live_in_chains instead. */
244 nregs = head->nregs;
245 while (nregs-- > 0)
247 SET_HARD_REG_BIT (live_in_chains, head->regno + nregs);
248 CLEAR_HARD_REG_BIT (live_hard_regs, head->regno + nregs);
251 COPY_HARD_REG_SET (head->hard_conflicts, live_hard_regs);
252 bitmap_set_bit (&open_chains_set, head->id);
254 open_chains = head;
256 if (dump_file)
258 fprintf (dump_file, "Creating chain %s (%d)",
259 reg_names[head->regno], head->id);
260 if (insn != NULL_RTX)
261 fprintf (dump_file, " at insn %d", INSN_UID (insn));
262 fprintf (dump_file, "\n");
265 if (insn == NULL_RTX)
267 head->first = head->last = NULL;
268 return head;
271 this_du = XOBNEW (&rename_obstack, struct du_chain);
272 head->first = head->last = this_du;
274 this_du->next_use = 0;
275 this_du->loc = loc;
276 this_du->insn = insn;
277 this_du->cl = cl;
278 record_operand_use (head, this_du);
279 return head;
282 /* For a def-use chain HEAD, find which registers overlap its lifetime and
283 set the corresponding bits in *PSET. */
285 static void
286 merge_overlapping_regs (HARD_REG_SET *pset, struct du_head *head)
288 bitmap_iterator bi;
289 unsigned i;
290 IOR_HARD_REG_SET (*pset, head->hard_conflicts);
291 EXECUTE_IF_SET_IN_BITMAP (&head->conflicts, 0, i, bi)
293 du_head_p other = regrename_chain_from_id (i);
294 unsigned j = other->nregs;
295 gcc_assert (other != head);
296 while (j-- > 0)
297 SET_HARD_REG_BIT (*pset, other->regno + j);
301 /* Check if NEW_REG can be the candidate register to rename for
302 REG in THIS_HEAD chain. THIS_UNAVAILABLE is a set of unavailable hard
303 registers. */
305 static bool
306 check_new_reg_p (int reg ATTRIBUTE_UNUSED, int new_reg,
307 struct du_head *this_head, HARD_REG_SET this_unavailable)
309 enum machine_mode mode = GET_MODE (*this_head->first->loc);
310 int nregs = hard_regno_nregs[new_reg][mode];
311 int i;
312 struct du_chain *tmp;
314 for (i = nregs - 1; i >= 0; --i)
315 if (TEST_HARD_REG_BIT (this_unavailable, new_reg + i)
316 || fixed_regs[new_reg + i]
317 || global_regs[new_reg + i]
318 /* Can't use regs which aren't saved by the prologue. */
319 || (! df_regs_ever_live_p (new_reg + i)
320 && ! call_used_regs[new_reg + i])
321 #ifdef LEAF_REGISTERS
322 /* We can't use a non-leaf register if we're in a
323 leaf function. */
324 || (crtl->is_leaf
325 && !LEAF_REGISTERS[new_reg + i])
326 #endif
327 #ifdef HARD_REGNO_RENAME_OK
328 || ! HARD_REGNO_RENAME_OK (reg + i, new_reg + i)
329 #endif
331 return false;
333 /* See whether it accepts all modes that occur in
334 definition and uses. */
335 for (tmp = this_head->first; tmp; tmp = tmp->next_use)
336 if ((! HARD_REGNO_MODE_OK (new_reg, GET_MODE (*tmp->loc))
337 && ! DEBUG_INSN_P (tmp->insn))
338 || (this_head->need_caller_save_reg
339 && ! (HARD_REGNO_CALL_PART_CLOBBERED
340 (reg, GET_MODE (*tmp->loc)))
341 && (HARD_REGNO_CALL_PART_CLOBBERED
342 (new_reg, GET_MODE (*tmp->loc)))))
343 return false;
345 return true;
348 /* For the chain THIS_HEAD, compute and return the best register to
349 rename to. SUPER_CLASS is the superunion of register classes in
350 the chain. UNAVAILABLE is a set of registers that cannot be used.
351 OLD_REG is the register currently used for the chain. */
354 find_best_rename_reg (du_head_p this_head, enum reg_class super_class,
355 HARD_REG_SET *unavailable, int old_reg)
357 bool has_preferred_class;
358 enum reg_class preferred_class;
359 int pass;
360 int best_new_reg = old_reg;
362 /* Further narrow the set of registers we can use for renaming.
363 If the chain needs a call-saved register, mark the call-used
364 registers as unavailable. */
365 if (this_head->need_caller_save_reg)
366 IOR_HARD_REG_SET (*unavailable, call_used_reg_set);
368 /* Mark registers that overlap this chain's lifetime as unavailable. */
369 merge_overlapping_regs (unavailable, this_head);
371 /* Compute preferred rename class of super union of all the classes
372 in the chain. */
373 preferred_class
374 = (enum reg_class) targetm.preferred_rename_class (super_class);
376 /* If PREFERRED_CLASS is not NO_REGS, we iterate in the first pass
377 over registers that belong to PREFERRED_CLASS and try to find the
378 best register within the class. If that failed, we iterate in
379 the second pass over registers that don't belong to the class.
380 If PREFERRED_CLASS is NO_REGS, we iterate over all registers in
381 ascending order without any preference. */
382 has_preferred_class = (preferred_class != NO_REGS);
383 for (pass = (has_preferred_class ? 0 : 1); pass < 2; pass++)
385 int new_reg;
386 for (new_reg = 0; new_reg < FIRST_PSEUDO_REGISTER; new_reg++)
388 if (has_preferred_class
389 && (pass == 0)
390 != TEST_HARD_REG_BIT (reg_class_contents[preferred_class],
391 new_reg))
392 continue;
394 /* In the first pass, we force the renaming of registers that
395 don't belong to PREFERRED_CLASS to registers that do, even
396 though the latters were used not very long ago. */
397 if (check_new_reg_p (old_reg, new_reg, this_head,
398 *unavailable)
399 && ((pass == 0
400 && !TEST_HARD_REG_BIT (reg_class_contents[preferred_class],
401 best_new_reg))
402 || tick[best_new_reg] > tick[new_reg]))
403 best_new_reg = new_reg;
405 if (pass == 0 && best_new_reg != old_reg)
406 break;
408 return best_new_reg;
411 /* Perform register renaming on the current function. */
412 static void
413 rename_chains (void)
415 HARD_REG_SET unavailable;
416 du_head_p this_head;
417 int i;
419 memset (tick, 0, sizeof tick);
421 CLEAR_HARD_REG_SET (unavailable);
422 /* Don't clobber traceback for noreturn functions. */
423 if (frame_pointer_needed)
425 add_to_hard_reg_set (&unavailable, Pmode, FRAME_POINTER_REGNUM);
426 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
427 add_to_hard_reg_set (&unavailable, Pmode, HARD_FRAME_POINTER_REGNUM);
428 #endif
431 FOR_EACH_VEC_ELT (id_to_chain, i, this_head)
433 int best_new_reg;
434 int n_uses;
435 struct du_chain *tmp;
436 HARD_REG_SET this_unavailable;
437 int reg = this_head->regno;
438 enum reg_class super_class = NO_REGS;
440 if (this_head->cannot_rename)
441 continue;
443 if (fixed_regs[reg] || global_regs[reg]
444 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
445 || (frame_pointer_needed && reg == HARD_FRAME_POINTER_REGNUM)
446 #else
447 || (frame_pointer_needed && reg == FRAME_POINTER_REGNUM)
448 #endif
450 continue;
452 COPY_HARD_REG_SET (this_unavailable, unavailable);
454 /* Iterate over elements in the chain in order to:
455 1. Count number of uses, and narrow the set of registers we can
456 use for renaming.
457 2. Compute the superunion of register classes in this chain. */
458 n_uses = 0;
459 super_class = NO_REGS;
460 for (tmp = this_head->first; tmp; tmp = tmp->next_use)
462 if (DEBUG_INSN_P (tmp->insn))
463 continue;
464 n_uses++;
465 IOR_COMPL_HARD_REG_SET (this_unavailable,
466 reg_class_contents[tmp->cl]);
467 super_class
468 = reg_class_superunion[(int) super_class][(int) tmp->cl];
471 if (n_uses < 2)
472 continue;
474 best_new_reg = find_best_rename_reg (this_head, super_class,
475 &this_unavailable, reg);
477 if (dump_file)
479 fprintf (dump_file, "Register %s in insn %d",
480 reg_names[reg], INSN_UID (this_head->first->insn));
481 if (this_head->need_caller_save_reg)
482 fprintf (dump_file, " crosses a call");
485 if (best_new_reg == reg)
487 tick[reg] = ++this_tick;
488 if (dump_file)
489 fprintf (dump_file, "; no available better choice\n");
490 continue;
493 if (dump_file)
494 fprintf (dump_file, ", renamed as %s\n", reg_names[best_new_reg]);
496 regrename_do_replace (this_head, best_new_reg);
497 tick[best_new_reg] = ++this_tick;
498 df_set_regs_ever_live (best_new_reg, true);
502 /* A structure to record information for each hard register at the start of
503 a basic block. */
504 struct incoming_reg_info {
505 /* Holds the number of registers used in the chain that gave us information
506 about this register. Zero means no information known yet, while a
507 negative value is used for something that is part of, but not the first
508 register in a multi-register value. */
509 int nregs;
510 /* Set to true if we have accesses that conflict in the number of registers
511 used. */
512 bool unusable;
515 /* A structure recording information about each basic block. It is saved
516 and restored around basic block boundaries.
517 A pointer to such a structure is stored in each basic block's aux field
518 during regrename_analyze, except for blocks we know can't be optimized
519 (such as entry and exit blocks). */
520 struct bb_rename_info
522 /* The basic block corresponding to this structure. */
523 basic_block bb;
524 /* Copies of the global information. */
525 bitmap_head open_chains_set;
526 bitmap_head incoming_open_chains_set;
527 struct incoming_reg_info incoming[FIRST_PSEUDO_REGISTER];
530 /* Initialize a rename_info structure P for basic block BB, which starts a new
531 scan. */
532 static void
533 init_rename_info (struct bb_rename_info *p, basic_block bb)
535 int i;
536 df_ref def;
537 HARD_REG_SET start_chains_set;
539 p->bb = bb;
540 bitmap_initialize (&p->open_chains_set, &bitmap_default_obstack);
541 bitmap_initialize (&p->incoming_open_chains_set, &bitmap_default_obstack);
543 open_chains = NULL;
544 bitmap_clear (&open_chains_set);
546 CLEAR_HARD_REG_SET (live_in_chains);
547 REG_SET_TO_HARD_REG_SET (live_hard_regs, df_get_live_in (bb));
548 FOR_EACH_ARTIFICIAL_DEF (def, bb->index)
549 if (DF_REF_FLAGS (def) & DF_REF_AT_TOP)
550 SET_HARD_REG_BIT (live_hard_regs, DF_REF_REGNO (def));
552 /* Open chains based on information from (at least one) predecessor
553 block. This gives us a chance later on to combine chains across
554 basic block boundaries. Inconsistencies (in access sizes) will
555 be caught normally and dealt with conservatively by disabling the
556 chain for renaming, and there is no risk of losing optimization
557 opportunities by opening chains either: if we did not open the
558 chains, we'd have to track the live register as a hard reg, and
559 we'd be unable to rename it in any case. */
560 CLEAR_HARD_REG_SET (start_chains_set);
561 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
563 struct incoming_reg_info *iri = p->incoming + i;
564 if (iri->nregs > 0 && !iri->unusable
565 && range_in_hard_reg_set_p (live_hard_regs, i, iri->nregs))
567 SET_HARD_REG_BIT (start_chains_set, i);
568 remove_range_from_hard_reg_set (&live_hard_regs, i, iri->nregs);
571 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
573 struct incoming_reg_info *iri = p->incoming + i;
574 if (TEST_HARD_REG_BIT (start_chains_set, i))
576 du_head_p chain;
577 if (dump_file)
578 fprintf (dump_file, "opening incoming chain\n");
579 chain = create_new_chain (i, iri->nregs, NULL, NULL_RTX, NO_REGS);
580 bitmap_set_bit (&p->incoming_open_chains_set, chain->id);
585 /* Record in RI that the block corresponding to it has an incoming
586 live value, described by CHAIN. */
587 static void
588 set_incoming_from_chain (struct bb_rename_info *ri, du_head_p chain)
590 int i;
591 int incoming_nregs = ri->incoming[chain->regno].nregs;
592 int nregs;
594 /* If we've recorded the same information before, everything is fine. */
595 if (incoming_nregs == chain->nregs)
597 if (dump_file)
598 fprintf (dump_file, "reg %d/%d already recorded\n",
599 chain->regno, chain->nregs);
600 return;
603 /* If we have no information for any of the involved registers, update
604 the incoming array. */
605 nregs = chain->nregs;
606 while (nregs-- > 0)
607 if (ri->incoming[chain->regno + nregs].nregs != 0
608 || ri->incoming[chain->regno + nregs].unusable)
609 break;
610 if (nregs < 0)
612 nregs = chain->nregs;
613 ri->incoming[chain->regno].nregs = nregs;
614 while (nregs-- > 1)
615 ri->incoming[chain->regno + nregs].nregs = -nregs;
616 if (dump_file)
617 fprintf (dump_file, "recorded reg %d/%d\n",
618 chain->regno, chain->nregs);
619 return;
622 /* There must be some kind of conflict. Prevent both the old and
623 new ranges from being used. */
624 if (incoming_nregs < 0)
625 ri->incoming[chain->regno + incoming_nregs].unusable = true;
626 for (i = 0; i < chain->nregs; i++)
627 ri->incoming[chain->regno + i].unusable = true;
630 /* Merge the two chains C1 and C2 so that all conflict information is
631 recorded and C1, and the id of C2 is changed to that of C1. */
632 static void
633 merge_chains (du_head_p c1, du_head_p c2)
635 if (c1 == c2)
636 return;
638 if (c2->first != NULL)
640 if (c1->first == NULL)
641 c1->first = c2->first;
642 else
643 c1->last->next_use = c2->first;
644 c1->last = c2->last;
647 c2->first = c2->last = NULL;
648 c2->id = c1->id;
650 IOR_HARD_REG_SET (c1->hard_conflicts, c2->hard_conflicts);
651 bitmap_ior_into (&c1->conflicts, &c2->conflicts);
653 c1->need_caller_save_reg |= c2->need_caller_save_reg;
654 c1->cannot_rename |= c2->cannot_rename;
657 /* Analyze the current function and build chains for renaming. */
659 void
660 regrename_analyze (bitmap bb_mask)
662 struct bb_rename_info *rename_info;
663 int i;
664 basic_block bb;
665 int n_bbs;
666 int *inverse_postorder;
668 inverse_postorder = XNEWVEC (int, last_basic_block_for_fn (cfun));
669 n_bbs = pre_and_rev_post_order_compute (NULL, inverse_postorder, false);
671 /* Gather some information about the blocks in this function. */
672 rename_info = XCNEWVEC (struct bb_rename_info, n_basic_blocks_for_fn (cfun));
673 i = 0;
674 FOR_EACH_BB_FN (bb, cfun)
676 struct bb_rename_info *ri = rename_info + i;
677 ri->bb = bb;
678 if (bb_mask != NULL && !bitmap_bit_p (bb_mask, bb->index))
679 bb->aux = NULL;
680 else
681 bb->aux = ri;
682 i++;
685 current_id = 0;
686 id_to_chain.create (0);
687 bitmap_initialize (&open_chains_set, &bitmap_default_obstack);
689 /* The order in which we visit blocks ensures that whenever
690 possible, we only process a block after at least one of its
691 predecessors, which provides a "seeding" effect to make the logic
692 in set_incoming_from_chain and init_rename_info useful. */
694 for (i = 0; i < n_bbs; i++)
696 basic_block bb1 = BASIC_BLOCK_FOR_FN (cfun, inverse_postorder[i]);
697 struct bb_rename_info *this_info;
698 bool success;
699 edge e;
700 edge_iterator ei;
701 int old_length = id_to_chain.length ();
703 this_info = (struct bb_rename_info *) bb1->aux;
704 if (this_info == NULL)
705 continue;
707 if (dump_file)
708 fprintf (dump_file, "\nprocessing block %d:\n", bb1->index);
710 init_rename_info (this_info, bb1);
712 success = build_def_use (bb1);
713 if (!success)
715 if (dump_file)
716 fprintf (dump_file, "failed\n");
717 bb1->aux = NULL;
718 id_to_chain.truncate (old_length);
719 current_id = old_length;
720 bitmap_clear (&this_info->incoming_open_chains_set);
721 open_chains = NULL;
722 if (insn_rr.exists ())
724 rtx insn;
725 FOR_BB_INSNS (bb1, insn)
727 insn_rr_info *p = &insn_rr[INSN_UID (insn)];
728 p->op_info = NULL;
731 continue;
734 if (dump_file)
735 dump_def_use_chain (old_length);
736 bitmap_copy (&this_info->open_chains_set, &open_chains_set);
738 /* Add successor blocks to the worklist if necessary, and record
739 data about our own open chains at the end of this block, which
740 will be used to pre-open chains when processing the successors. */
741 FOR_EACH_EDGE (e, ei, bb1->succs)
743 struct bb_rename_info *dest_ri;
744 struct du_head *chain;
746 if (dump_file)
747 fprintf (dump_file, "successor block %d\n", e->dest->index);
749 if (e->flags & (EDGE_EH | EDGE_ABNORMAL))
750 continue;
751 dest_ri = (struct bb_rename_info *)e->dest->aux;
752 if (dest_ri == NULL)
753 continue;
754 for (chain = open_chains; chain; chain = chain->next_chain)
755 set_incoming_from_chain (dest_ri, chain);
759 free (inverse_postorder);
761 /* Now, combine the chains data we have gathered across basic block
762 boundaries.
764 For every basic block, there may be chains open at the start, or at the
765 end. Rather than exclude them from renaming, we look for open chains
766 with matching registers at the other side of the CFG edge.
768 For a given chain using register R, open at the start of block B, we
769 must find an open chain using R on the other side of every edge leading
770 to B, if the register is live across this edge. In the code below,
771 N_PREDS_USED counts the number of edges where the register is live, and
772 N_PREDS_JOINED counts those where we found an appropriate chain for
773 joining.
775 We perform the analysis for both incoming and outgoing edges, but we
776 only need to merge once (in the second part, after verifying outgoing
777 edges). */
778 FOR_EACH_BB_FN (bb, cfun)
780 struct bb_rename_info *bb_ri = (struct bb_rename_info *) bb->aux;
781 unsigned j;
782 bitmap_iterator bi;
784 if (bb_ri == NULL)
785 continue;
787 if (dump_file)
788 fprintf (dump_file, "processing bb %d in edges\n", bb->index);
790 EXECUTE_IF_SET_IN_BITMAP (&bb_ri->incoming_open_chains_set, 0, j, bi)
792 edge e;
793 edge_iterator ei;
794 struct du_head *chain = regrename_chain_from_id (j);
795 int n_preds_used = 0, n_preds_joined = 0;
797 FOR_EACH_EDGE (e, ei, bb->preds)
799 struct bb_rename_info *src_ri;
800 unsigned k;
801 bitmap_iterator bi2;
802 HARD_REG_SET live;
803 bool success = false;
805 REG_SET_TO_HARD_REG_SET (live, df_get_live_out (e->src));
806 if (!range_overlaps_hard_reg_set_p (live, chain->regno,
807 chain->nregs))
808 continue;
809 n_preds_used++;
811 if (e->flags & (EDGE_EH | EDGE_ABNORMAL))
812 continue;
814 src_ri = (struct bb_rename_info *)e->src->aux;
815 if (src_ri == NULL)
816 continue;
818 EXECUTE_IF_SET_IN_BITMAP (&src_ri->open_chains_set,
819 0, k, bi2)
821 struct du_head *outgoing_chain = regrename_chain_from_id (k);
823 if (outgoing_chain->regno == chain->regno
824 && outgoing_chain->nregs == chain->nregs)
826 n_preds_joined++;
827 success = true;
828 break;
831 if (!success && dump_file)
832 fprintf (dump_file, "failure to match with pred block %d\n",
833 e->src->index);
835 if (n_preds_joined < n_preds_used)
837 if (dump_file)
838 fprintf (dump_file, "cannot rename chain %d\n", j);
839 chain->cannot_rename = 1;
843 FOR_EACH_BB_FN (bb, cfun)
845 struct bb_rename_info *bb_ri = (struct bb_rename_info *) bb->aux;
846 unsigned j;
847 bitmap_iterator bi;
849 if (bb_ri == NULL)
850 continue;
852 if (dump_file)
853 fprintf (dump_file, "processing bb %d out edges\n", bb->index);
855 EXECUTE_IF_SET_IN_BITMAP (&bb_ri->open_chains_set, 0, j, bi)
857 edge e;
858 edge_iterator ei;
859 struct du_head *chain = regrename_chain_from_id (j);
860 int n_succs_used = 0, n_succs_joined = 0;
862 FOR_EACH_EDGE (e, ei, bb->succs)
864 bool printed = false;
865 struct bb_rename_info *dest_ri;
866 unsigned k;
867 bitmap_iterator bi2;
868 HARD_REG_SET live;
870 REG_SET_TO_HARD_REG_SET (live, df_get_live_in (e->dest));
871 if (!range_overlaps_hard_reg_set_p (live, chain->regno,
872 chain->nregs))
873 continue;
875 n_succs_used++;
877 dest_ri = (struct bb_rename_info *)e->dest->aux;
878 if (dest_ri == NULL)
879 continue;
881 EXECUTE_IF_SET_IN_BITMAP (&dest_ri->incoming_open_chains_set,
882 0, k, bi2)
884 struct du_head *incoming_chain = regrename_chain_from_id (k);
886 if (incoming_chain->regno == chain->regno
887 && incoming_chain->nregs == chain->nregs)
889 if (dump_file)
891 if (!printed)
892 fprintf (dump_file,
893 "merging blocks for edge %d -> %d\n",
894 e->src->index, e->dest->index);
895 printed = true;
896 fprintf (dump_file,
897 " merging chains %d (->%d) and %d (->%d) [%s]\n",
898 k, incoming_chain->id, j, chain->id,
899 reg_names[incoming_chain->regno]);
902 merge_chains (chain, incoming_chain);
903 n_succs_joined++;
904 break;
908 if (n_succs_joined < n_succs_used)
910 if (dump_file)
911 fprintf (dump_file, "cannot rename chain %d\n",
913 chain->cannot_rename = 1;
918 free (rename_info);
920 FOR_EACH_BB_FN (bb, cfun)
921 bb->aux = NULL;
924 void
925 regrename_do_replace (struct du_head *head, int reg)
927 struct du_chain *chain;
928 unsigned int base_regno = head->regno;
929 enum machine_mode mode;
931 for (chain = head->first; chain; chain = chain->next_use)
933 unsigned int regno = ORIGINAL_REGNO (*chain->loc);
934 struct reg_attrs *attr = REG_ATTRS (*chain->loc);
935 int reg_ptr = REG_POINTER (*chain->loc);
937 if (DEBUG_INSN_P (chain->insn) && REGNO (*chain->loc) != base_regno)
938 INSN_VAR_LOCATION_LOC (chain->insn) = gen_rtx_UNKNOWN_VAR_LOC ();
939 else
941 *chain->loc = gen_raw_REG (GET_MODE (*chain->loc), reg);
942 if (regno >= FIRST_PSEUDO_REGISTER)
943 ORIGINAL_REGNO (*chain->loc) = regno;
944 REG_ATTRS (*chain->loc) = attr;
945 REG_POINTER (*chain->loc) = reg_ptr;
948 df_insn_rescan (chain->insn);
951 mode = GET_MODE (*head->first->loc);
952 head->regno = reg;
953 head->nregs = hard_regno_nregs[reg][mode];
957 /* True if we found a register with a size mismatch, which means that we
958 can't track its lifetime accurately. If so, we abort the current block
959 without renaming. */
960 static bool fail_current_block;
962 /* Return true if OP is a reg for which all bits are set in PSET, false
963 if all bits are clear.
964 In other cases, set fail_current_block and return false. */
966 static bool
967 verify_reg_in_set (rtx op, HARD_REG_SET *pset)
969 unsigned regno, nregs;
970 bool all_live, all_dead;
971 if (!REG_P (op))
972 return false;
974 regno = REGNO (op);
975 nregs = hard_regno_nregs[regno][GET_MODE (op)];
976 all_live = all_dead = true;
977 while (nregs-- > 0)
978 if (TEST_HARD_REG_BIT (*pset, regno + nregs))
979 all_dead = false;
980 else
981 all_live = false;
982 if (!all_dead && !all_live)
984 fail_current_block = true;
985 return false;
987 return all_live;
990 /* Return true if OP is a reg that is being tracked already in some form.
991 May set fail_current_block if it sees an unhandled case of overlap. */
993 static bool
994 verify_reg_tracked (rtx op)
996 return (verify_reg_in_set (op, &live_hard_regs)
997 || verify_reg_in_set (op, &live_in_chains));
1000 /* Called through note_stores. DATA points to a rtx_code, either SET or
1001 CLOBBER, which tells us which kind of rtx to look at. If we have a
1002 match, record the set register in live_hard_regs and in the hard_conflicts
1003 bitmap of open chains. */
1005 static void
1006 note_sets_clobbers (rtx x, const_rtx set, void *data)
1008 enum rtx_code code = *(enum rtx_code *)data;
1009 struct du_head *chain;
1011 if (GET_CODE (x) == SUBREG)
1012 x = SUBREG_REG (x);
1013 if (!REG_P (x) || GET_CODE (set) != code)
1014 return;
1015 /* There must not be pseudos at this point. */
1016 gcc_assert (HARD_REGISTER_P (x));
1017 add_to_hard_reg_set (&live_hard_regs, GET_MODE (x), REGNO (x));
1018 for (chain = open_chains; chain; chain = chain->next_chain)
1019 add_to_hard_reg_set (&chain->hard_conflicts, GET_MODE (x), REGNO (x));
1022 static void
1023 scan_rtx_reg (rtx insn, rtx *loc, enum reg_class cl, enum scan_actions action,
1024 enum op_type type)
1026 struct du_head **p;
1027 rtx x = *loc;
1028 enum machine_mode mode = GET_MODE (x);
1029 unsigned this_regno = REGNO (x);
1030 int this_nregs = hard_regno_nregs[this_regno][mode];
1032 if (action == mark_write)
1034 if (type == OP_OUT)
1035 create_new_chain (this_regno, this_nregs, loc, insn, cl);
1036 return;
1039 if ((type == OP_OUT) != (action == terminate_write || action == mark_access))
1040 return;
1042 for (p = &open_chains; *p;)
1044 struct du_head *head = *p;
1045 struct du_head *next = head->next_chain;
1046 int exact_match = (head->regno == this_regno
1047 && head->nregs == this_nregs);
1048 int superset = (this_regno <= head->regno
1049 && this_regno + this_nregs >= head->regno + head->nregs);
1050 int subset = (this_regno >= head->regno
1051 && this_regno + this_nregs <= head->regno + head->nregs);
1053 if (!bitmap_bit_p (&open_chains_set, head->id)
1054 || head->regno + head->nregs <= this_regno
1055 || this_regno + this_nregs <= head->regno)
1057 p = &head->next_chain;
1058 continue;
1061 if (action == mark_read || action == mark_access)
1063 /* ??? Class NO_REGS can happen if the md file makes use of
1064 EXTRA_CONSTRAINTS to match registers. Which is arguably
1065 wrong, but there we are. */
1067 if (cl == NO_REGS || (!exact_match && !DEBUG_INSN_P (insn)))
1069 if (dump_file)
1070 fprintf (dump_file,
1071 "Cannot rename chain %s (%d) at insn %d (%s)\n",
1072 reg_names[head->regno], head->id, INSN_UID (insn),
1073 scan_actions_name[(int) action]);
1074 head->cannot_rename = 1;
1075 if (superset)
1077 unsigned nregs = this_nregs;
1078 head->regno = this_regno;
1079 head->nregs = this_nregs;
1080 while (nregs-- > 0)
1081 SET_HARD_REG_BIT (live_in_chains, head->regno + nregs);
1082 if (dump_file)
1083 fprintf (dump_file,
1084 "Widening register in chain %s (%d) at insn %d\n",
1085 reg_names[head->regno], head->id, INSN_UID (insn));
1087 else if (!subset)
1089 fail_current_block = true;
1090 if (dump_file)
1091 fprintf (dump_file,
1092 "Failing basic block due to unhandled overlap\n");
1095 else
1097 struct du_chain *this_du;
1098 this_du = XOBNEW (&rename_obstack, struct du_chain);
1099 this_du->next_use = 0;
1100 this_du->loc = loc;
1101 this_du->insn = insn;
1102 this_du->cl = cl;
1103 if (head->first == NULL)
1104 head->first = this_du;
1105 else
1106 head->last->next_use = this_du;
1107 record_operand_use (head, this_du);
1108 head->last = this_du;
1110 /* Avoid adding the same location in a DEBUG_INSN multiple times,
1111 which could happen with non-exact overlap. */
1112 if (DEBUG_INSN_P (insn))
1113 return;
1114 /* Otherwise, find any other chains that do not match exactly;
1115 ensure they all get marked unrenamable. */
1116 p = &head->next_chain;
1117 continue;
1120 /* Whether the terminated chain can be used for renaming
1121 depends on the action and this being an exact match.
1122 In either case, we remove this element from open_chains. */
1124 if ((action == terminate_dead || action == terminate_write)
1125 && (superset || subset))
1127 unsigned nregs;
1129 if (subset && !superset)
1130 head->cannot_rename = 1;
1131 bitmap_clear_bit (&open_chains_set, head->id);
1133 nregs = head->nregs;
1134 while (nregs-- > 0)
1136 CLEAR_HARD_REG_BIT (live_in_chains, head->regno + nregs);
1137 if (subset && !superset
1138 && (head->regno + nregs < this_regno
1139 || head->regno + nregs >= this_regno + this_nregs))
1140 SET_HARD_REG_BIT (live_hard_regs, head->regno + nregs);
1143 *p = next;
1144 if (dump_file)
1145 fprintf (dump_file,
1146 "Closing chain %s (%d) at insn %d (%s%s)\n",
1147 reg_names[head->regno], head->id, INSN_UID (insn),
1148 scan_actions_name[(int) action],
1149 superset ? ", superset" : subset ? ", subset" : "");
1151 else if (action == terminate_dead || action == terminate_write)
1153 /* In this case, tracking liveness gets too hard. Fail the
1154 entire basic block. */
1155 if (dump_file)
1156 fprintf (dump_file,
1157 "Failing basic block due to unhandled overlap\n");
1158 fail_current_block = true;
1159 return;
1161 else
1163 head->cannot_rename = 1;
1164 if (dump_file)
1165 fprintf (dump_file,
1166 "Cannot rename chain %s (%d) at insn %d (%s)\n",
1167 reg_names[head->regno], head->id, INSN_UID (insn),
1168 scan_actions_name[(int) action]);
1169 p = &head->next_chain;
1174 /* Adapted from find_reloads_address_1. CL is INDEX_REG_CLASS or
1175 BASE_REG_CLASS depending on how the register is being considered. */
1177 static void
1178 scan_rtx_address (rtx insn, rtx *loc, enum reg_class cl,
1179 enum scan_actions action, enum machine_mode mode,
1180 addr_space_t as)
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, as, PLUS, REG))
1249 index_op = 1;
1250 else if (REGNO_OK_FOR_INDEX_P (regno0)
1251 && regno_ok_for_base_p (regno1, mode, as, PLUS, REG))
1252 index_op = 0;
1253 else if (regno_ok_for_base_p (regno0, mode, as, PLUS, REG)
1254 || REGNO_OK_FOR_INDEX_P (regno1))
1255 index_op = 1;
1256 else if (regno_ok_for_base_p (regno1, mode, as, 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, as);
1280 if (locB)
1281 scan_rtx_address (insn, locB,
1282 base_reg_class (mode, as, PLUS, index_code),
1283 action, mode, as);
1285 return;
1288 case POST_INC:
1289 case POST_DEC:
1290 case POST_MODIFY:
1291 case PRE_INC:
1292 case PRE_DEC:
1293 case PRE_MODIFY:
1294 #ifndef AUTO_INC_DEC
1295 /* If the target doesn't claim to handle autoinc, this must be
1296 something special, like a stack push. Kill this chain. */
1297 action = mark_all_read;
1298 #endif
1299 break;
1301 case MEM:
1302 scan_rtx_address (insn, &XEXP (x, 0),
1303 base_reg_class (GET_MODE (x), MEM_ADDR_SPACE (x),
1304 MEM, SCRATCH),
1305 action, GET_MODE (x), MEM_ADDR_SPACE (x));
1306 return;
1308 case REG:
1309 scan_rtx_reg (insn, loc, cl, action, OP_IN);
1310 return;
1312 default:
1313 break;
1316 fmt = GET_RTX_FORMAT (code);
1317 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1319 if (fmt[i] == 'e')
1320 scan_rtx_address (insn, &XEXP (x, i), cl, action, mode, as);
1321 else if (fmt[i] == 'E')
1322 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
1323 scan_rtx_address (insn, &XVECEXP (x, i, j), cl, action, mode, as);
1327 static void
1328 scan_rtx (rtx insn, rtx *loc, enum reg_class cl, enum scan_actions action,
1329 enum op_type type)
1331 const char *fmt;
1332 rtx x = *loc;
1333 enum rtx_code code = GET_CODE (x);
1334 int i, j;
1336 code = GET_CODE (x);
1337 switch (code)
1339 case CONST:
1340 CASE_CONST_ANY:
1341 case SYMBOL_REF:
1342 case LABEL_REF:
1343 case CC0:
1344 case PC:
1345 return;
1347 case REG:
1348 scan_rtx_reg (insn, loc, cl, action, type);
1349 return;
1351 case MEM:
1352 scan_rtx_address (insn, &XEXP (x, 0),
1353 base_reg_class (GET_MODE (x), MEM_ADDR_SPACE (x),
1354 MEM, SCRATCH),
1355 action, GET_MODE (x), MEM_ADDR_SPACE (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 const operand_alternative *op_alt = which_op_alt ();
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 || op_alt[i].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 || op_alt[opn].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. If INSN_INFO is nonnull, use it to
1472 record information about the operands in the insn. */
1474 static void
1475 record_out_operands (rtx insn, bool earlyclobber, insn_rr_info *insn_info)
1477 int n_ops = recog_data.n_operands;
1478 const operand_alternative *op_alt = which_op_alt ();
1480 int i;
1482 for (i = 0; i < n_ops + recog_data.n_dups; i++)
1484 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
1485 rtx *loc = (i < n_ops
1486 ? recog_data.operand_loc[opn]
1487 : recog_data.dup_loc[i - n_ops]);
1488 rtx op = *loc;
1489 enum reg_class cl = alternative_class (op_alt, opn);
1491 struct du_head *prev_open;
1493 if (recog_data.operand_type[opn] != OP_OUT
1494 || op_alt[opn].earlyclobber != earlyclobber)
1495 continue;
1497 if (insn_info)
1498 cur_operand = insn_info->op_info + i;
1500 prev_open = open_chains;
1501 scan_rtx (insn, loc, cl, mark_write, OP_OUT);
1503 /* ??? Many targets have output constraints on the SET_DEST
1504 of a call insn, which is stupid, since these are certainly
1505 ABI defined hard registers. For these, and for asm operands
1506 that originally referenced hard registers, we must record that
1507 the chain cannot be renamed. */
1508 if (CALL_P (insn)
1509 || (asm_noperands (PATTERN (insn)) > 0
1510 && REG_P (op)
1511 && REGNO (op) == ORIGINAL_REGNO (op)))
1513 if (prev_open != open_chains)
1514 open_chains->cannot_rename = 1;
1517 cur_operand = NULL;
1520 /* Build def/use chain. */
1522 static bool
1523 build_def_use (basic_block bb)
1525 rtx insn;
1526 unsigned HOST_WIDE_INT untracked_operands;
1528 fail_current_block = false;
1530 for (insn = BB_HEAD (bb); ; insn = NEXT_INSN (insn))
1532 if (NONDEBUG_INSN_P (insn))
1534 int n_ops;
1535 rtx note;
1536 rtx old_operands[MAX_RECOG_OPERANDS];
1537 rtx old_dups[MAX_DUP_OPERANDS];
1538 int i;
1539 int predicated;
1540 enum rtx_code set_code = SET;
1541 enum rtx_code clobber_code = CLOBBER;
1542 insn_rr_info *insn_info = NULL;
1544 /* Process the insn, determining its effect on the def-use
1545 chains and live hard registers. We perform the following
1546 steps with the register references in the insn, simulating
1547 its effect:
1548 (1) Deal with earlyclobber operands and CLOBBERs of non-operands
1549 by creating chains and marking hard regs live.
1550 (2) Any read outside an operand causes any chain it overlaps
1551 with to be marked unrenamable.
1552 (3) Any read inside an operand is added if there's already
1553 an open chain for it.
1554 (4) For any REG_DEAD note we find, close open chains that
1555 overlap it.
1556 (5) For any non-earlyclobber write we find, close open chains
1557 that overlap it.
1558 (6) For any non-earlyclobber write we find in an operand, make
1559 a new chain or mark the hard register as live.
1560 (7) For any REG_UNUSED, close any chains we just opened.
1562 We cannot deal with situations where we track a reg in one mode
1563 and see a reference in another mode; these will cause the chain
1564 to be marked unrenamable or even cause us to abort the entire
1565 basic block. */
1567 extract_insn (insn);
1568 if (! constrain_operands (1))
1569 fatal_insn_not_found (insn);
1570 preprocess_constraints (insn);
1571 const operand_alternative *op_alt = which_op_alt ();
1572 n_ops = recog_data.n_operands;
1573 untracked_operands = 0;
1575 if (insn_rr.exists ())
1577 insn_info = &insn_rr[INSN_UID (insn)];
1578 insn_info->op_info = XOBNEWVEC (&rename_obstack, operand_rr_info,
1579 recog_data.n_operands);
1580 memset (insn_info->op_info, 0,
1581 sizeof (operand_rr_info) * recog_data.n_operands);
1584 /* Simplify the code below by promoting OP_OUT to OP_INOUT in
1585 predicated instructions, but only for register operands
1586 that are already tracked, so that we can create a chain
1587 when the first SET makes a register live. */
1589 predicated = GET_CODE (PATTERN (insn)) == COND_EXEC;
1590 for (i = 0; i < n_ops; ++i)
1592 rtx op = recog_data.operand[i];
1593 int matches = op_alt[i].matches;
1594 if (matches >= 0 || op_alt[i].matched >= 0
1595 || (predicated && recog_data.operand_type[i] == OP_OUT))
1597 recog_data.operand_type[i] = OP_INOUT;
1598 /* A special case to deal with instruction patterns that
1599 have matching operands with different modes. If we're
1600 not already tracking such a reg, we won't start here,
1601 and we must instead make sure to make the operand visible
1602 to the machinery that tracks hard registers. */
1603 if (matches >= 0
1604 && (GET_MODE_SIZE (recog_data.operand_mode[i])
1605 != GET_MODE_SIZE (recog_data.operand_mode[matches]))
1606 && !verify_reg_in_set (op, &live_in_chains))
1608 untracked_operands |= 1 << i;
1609 untracked_operands |= 1 << matches;
1612 /* If there's an in-out operand with a register that is not
1613 being tracked at all yet, open a chain. */
1614 if (recog_data.operand_type[i] == OP_INOUT
1615 && !(untracked_operands & (1 << i))
1616 && REG_P (op)
1617 && !verify_reg_tracked (op))
1619 enum machine_mode mode = GET_MODE (op);
1620 unsigned this_regno = REGNO (op);
1621 unsigned this_nregs = hard_regno_nregs[this_regno][mode];
1622 create_new_chain (this_regno, this_nregs, NULL, NULL_RTX,
1623 NO_REGS);
1627 if (fail_current_block)
1628 break;
1630 /* Step 1a: Mark hard registers that are clobbered in this insn,
1631 outside an operand, as live. */
1632 hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1633 false);
1634 note_stores (PATTERN (insn), note_sets_clobbers, &clobber_code);
1635 restore_operands (insn, n_ops, old_operands, old_dups);
1637 /* Step 1b: Begin new chains for earlyclobbered writes inside
1638 operands. */
1639 record_out_operands (insn, true, insn_info);
1641 /* Step 2: Mark chains for which we have reads outside operands
1642 as unrenamable.
1643 We do this by munging all operands into CC0, and closing
1644 everything remaining. */
1646 hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1647 false);
1648 scan_rtx (insn, &PATTERN (insn), NO_REGS, mark_all_read, OP_IN);
1649 restore_operands (insn, n_ops, old_operands, old_dups);
1651 /* Step 2B: Can't rename function call argument registers. */
1652 if (CALL_P (insn) && CALL_INSN_FUNCTION_USAGE (insn))
1653 scan_rtx (insn, &CALL_INSN_FUNCTION_USAGE (insn),
1654 NO_REGS, mark_all_read, OP_IN);
1656 /* Step 2C: Can't rename asm operands that were originally
1657 hard registers. */
1658 if (asm_noperands (PATTERN (insn)) > 0)
1659 for (i = 0; i < n_ops; i++)
1661 rtx *loc = recog_data.operand_loc[i];
1662 rtx op = *loc;
1664 if (REG_P (op)
1665 && REGNO (op) == ORIGINAL_REGNO (op)
1666 && (recog_data.operand_type[i] == OP_IN
1667 || recog_data.operand_type[i] == OP_INOUT))
1668 scan_rtx (insn, loc, NO_REGS, mark_all_read, OP_IN);
1671 /* Step 3: Append to chains for reads inside operands. */
1672 for (i = 0; i < n_ops + recog_data.n_dups; i++)
1674 int opn = i < n_ops ? i : recog_data.dup_num[i - n_ops];
1675 rtx *loc = (i < n_ops
1676 ? recog_data.operand_loc[opn]
1677 : recog_data.dup_loc[i - n_ops]);
1678 enum reg_class cl = alternative_class (op_alt, opn);
1679 enum op_type type = recog_data.operand_type[opn];
1681 /* Don't scan match_operand here, since we've no reg class
1682 information to pass down. Any operands that we could
1683 substitute in will be represented elsewhere. */
1684 if (recog_data.constraints[opn][0] == '\0'
1685 || untracked_operands & (1 << opn))
1686 continue;
1688 if (insn_info)
1689 cur_operand = i == opn ? insn_info->op_info + i : NULL;
1690 if (op_alt[opn].is_address)
1691 scan_rtx_address (insn, loc, cl, mark_read,
1692 VOIDmode, ADDR_SPACE_GENERIC);
1693 else
1694 scan_rtx (insn, loc, cl, mark_read, type);
1696 cur_operand = NULL;
1698 /* Step 3B: Record updates for regs in REG_INC notes, and
1699 source regs in REG_FRAME_RELATED_EXPR notes. */
1700 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1701 if (REG_NOTE_KIND (note) == REG_INC
1702 || REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
1703 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_read,
1704 OP_INOUT);
1706 /* Step 4: Close chains for registers that die here, unless
1707 the register is mentioned in a REG_UNUSED note. In that
1708 case we keep the chain open until step #7 below to ensure
1709 it conflicts with other output operands of this insn.
1710 See PR 52573. Arguably the insn should not have both
1711 notes; it has proven difficult to fix that without
1712 other undesirable side effects. */
1713 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1714 if (REG_NOTE_KIND (note) == REG_DEAD
1715 && !find_regno_note (insn, REG_UNUSED, REGNO (XEXP (note, 0))))
1717 remove_from_hard_reg_set (&live_hard_regs,
1718 GET_MODE (XEXP (note, 0)),
1719 REGNO (XEXP (note, 0)));
1720 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
1721 OP_IN);
1724 /* Step 4B: If this is a call, any chain live at this point
1725 requires a caller-saved reg. */
1726 if (CALL_P (insn))
1728 struct du_head *p;
1729 for (p = open_chains; p; p = p->next_chain)
1730 p->need_caller_save_reg = 1;
1733 /* Step 5: Close open chains that overlap writes. Similar to
1734 step 2, we hide in-out operands, since we do not want to
1735 close these chains. We also hide earlyclobber operands,
1736 since we've opened chains for them in step 1, and earlier
1737 chains they would overlap with must have been closed at
1738 the previous insn at the latest, as such operands cannot
1739 possibly overlap with any input operands. */
1741 hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1742 true);
1743 scan_rtx (insn, &PATTERN (insn), NO_REGS, terminate_write, OP_IN);
1744 restore_operands (insn, n_ops, old_operands, old_dups);
1746 /* Step 6a: Mark hard registers that are set in this insn,
1747 outside an operand, as live. */
1748 hide_operands (n_ops, old_operands, old_dups, untracked_operands,
1749 false);
1750 note_stores (PATTERN (insn), note_sets_clobbers, &set_code);
1751 restore_operands (insn, n_ops, old_operands, old_dups);
1753 /* Step 6b: Begin new chains for writes inside operands. */
1754 record_out_operands (insn, false, insn_info);
1756 /* Step 6c: Record destination regs in REG_FRAME_RELATED_EXPR
1757 notes for update. */
1758 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1759 if (REG_NOTE_KIND (note) == REG_FRAME_RELATED_EXPR)
1760 scan_rtx (insn, &XEXP (note, 0), ALL_REGS, mark_access,
1761 OP_INOUT);
1763 /* Step 7: Close chains for registers that were never
1764 really used here. */
1765 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1766 if (REG_NOTE_KIND (note) == REG_UNUSED)
1768 remove_from_hard_reg_set (&live_hard_regs,
1769 GET_MODE (XEXP (note, 0)),
1770 REGNO (XEXP (note, 0)));
1771 scan_rtx (insn, &XEXP (note, 0), NO_REGS, terminate_dead,
1772 OP_IN);
1775 else if (DEBUG_INSN_P (insn)
1776 && !VAR_LOC_UNKNOWN_P (INSN_VAR_LOCATION_LOC (insn)))
1778 scan_rtx (insn, &INSN_VAR_LOCATION_LOC (insn),
1779 ALL_REGS, mark_read, OP_IN);
1781 if (insn == BB_END (bb))
1782 break;
1785 if (fail_current_block)
1786 return false;
1788 return true;
1791 /* Initialize the register renamer. If INSN_INFO is true, ensure that
1792 insn_rr is nonnull. */
1793 void
1794 regrename_init (bool insn_info)
1796 gcc_obstack_init (&rename_obstack);
1797 insn_rr.create (0);
1798 if (insn_info)
1799 insn_rr.safe_grow_cleared (get_max_uid ());
1802 /* Free all global data used by the register renamer. */
1803 void
1804 regrename_finish (void)
1806 insn_rr.release ();
1807 free_chain_data ();
1808 obstack_free (&rename_obstack, NULL);
1811 /* Perform register renaming on the current function. */
1813 static unsigned int
1814 regrename_optimize (void)
1816 df_set_flags (DF_LR_RUN_DCE);
1817 df_note_add_problem ();
1818 df_analyze ();
1819 df_set_flags (DF_DEFER_INSN_RESCAN);
1821 regrename_init (false);
1823 regrename_analyze (NULL);
1825 rename_chains ();
1827 regrename_finish ();
1829 return 0;
1832 namespace {
1834 const pass_data pass_data_regrename =
1836 RTL_PASS, /* type */
1837 "rnreg", /* name */
1838 OPTGROUP_NONE, /* optinfo_flags */
1839 TV_RENAME_REGISTERS, /* tv_id */
1840 0, /* properties_required */
1841 0, /* properties_provided */
1842 0, /* properties_destroyed */
1843 0, /* todo_flags_start */
1844 TODO_df_finish, /* todo_flags_finish */
1847 class pass_regrename : public rtl_opt_pass
1849 public:
1850 pass_regrename (gcc::context *ctxt)
1851 : rtl_opt_pass (pass_data_regrename, ctxt)
1854 /* opt_pass methods: */
1855 virtual bool gate (function *)
1857 return (optimize > 0 && (flag_rename_registers));
1860 virtual unsigned int execute (function *) { return regrename_optimize (); }
1862 }; // class pass_regrename
1864 } // anon namespace
1866 rtl_opt_pass *
1867 make_pass_regrename (gcc::context *ctxt)
1869 return new pass_regrename (ctxt);