[AArch64] Replace temporary inline assembler for vset_lane
[official-gcc.git] / gcc / bt-load.c
blob2e21f86bc456914dbed141a2166fbf8579f349cf
2 /* Perform branch target register load optimizations.
3 Copyright (C) 2001-2014 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "hard-reg-set.h"
27 #include "regs.h"
28 #include "fibheap.h"
29 #include "target.h"
30 #include "expr.h"
31 #include "flags.h"
32 #include "insn-attr.h"
33 #include "function.h"
34 #include "except.h"
35 #include "tm_p.h"
36 #include "diagnostic-core.h"
37 #include "tree-pass.h"
38 #include "recog.h"
39 #include "df.h"
40 #include "cfgloop.h"
41 #include "rtl-iter.h"
43 /* Target register optimizations - these are performed after reload. */
45 typedef struct btr_def_group_s
47 struct btr_def_group_s *next;
48 rtx src;
49 struct btr_def_s *members;
50 } *btr_def_group;
52 typedef struct btr_user_s
54 struct btr_user_s *next;
55 basic_block bb;
56 int luid;
57 rtx_insn *insn;
58 /* If INSN has a single use of a single branch register, then
59 USE points to it within INSN. If there is more than
60 one branch register use, or the use is in some way ambiguous,
61 then USE is NULL. */
62 rtx use;
63 int n_reaching_defs;
64 int first_reaching_def;
65 char other_use_this_block;
66 } *btr_user;
68 /* btr_def structs appear on three lists:
69 1. A list of all btr_def structures (head is
70 ALL_BTR_DEFS, linked by the NEXT field).
71 2. A list of branch reg definitions per basic block (head is
72 BB_BTR_DEFS[i], linked by the NEXT_THIS_BB field).
73 3. A list of all branch reg definitions belonging to the same
74 group (head is in a BTR_DEF_GROUP struct, linked by
75 NEXT_THIS_GROUP field). */
77 typedef struct btr_def_s
79 struct btr_def_s *next_this_bb;
80 struct btr_def_s *next_this_group;
81 basic_block bb;
82 int luid;
83 rtx_insn *insn;
84 int btr;
85 int cost;
86 /* For a branch register setting insn that has a constant
87 source (i.e. a label), group links together all the
88 insns with the same source. For other branch register
89 setting insns, group is NULL. */
90 btr_def_group group;
91 btr_user uses;
92 /* If this def has a reaching use which is not a simple use
93 in a branch instruction, then has_ambiguous_use will be true,
94 and we will not attempt to migrate this definition. */
95 char has_ambiguous_use;
96 /* live_range is an approximation to the true live range for this
97 def/use web, because it records the set of blocks that contain
98 the live range. There could be other live ranges for the same
99 branch register in that set of blocks, either in the block
100 containing the def (before the def), or in a block containing
101 a use (after the use). If there are such other live ranges, then
102 other_btr_uses_before_def or other_btr_uses_after_use must be set true
103 as appropriate. */
104 char other_btr_uses_before_def;
105 char other_btr_uses_after_use;
106 /* We set own_end when we have moved a definition into a dominator.
107 Thus, when a later combination removes this definition again, we know
108 to clear out trs_live_at_end again. */
109 char own_end;
110 bitmap live_range;
111 } *btr_def;
113 static int issue_rate;
115 static int basic_block_freq (const_basic_block);
116 static int insn_sets_btr_p (const rtx_insn *, int, int *);
117 static void find_btr_def_group (btr_def_group *, btr_def);
118 static btr_def add_btr_def (fibheap_t, basic_block, int, rtx_insn *,
119 unsigned int, int, btr_def_group *);
120 static btr_user new_btr_user (basic_block, int, rtx_insn *);
121 static void dump_hard_reg_set (HARD_REG_SET);
122 static void dump_btrs_live (int);
123 static void note_other_use_this_block (unsigned int, btr_user);
124 static void compute_defs_uses_and_gen (fibheap_t, btr_def *,btr_user *,
125 sbitmap *, sbitmap *, HARD_REG_SET *);
126 static void compute_kill (sbitmap *, sbitmap *, HARD_REG_SET *);
127 static void compute_out (sbitmap *bb_out, sbitmap *, sbitmap *, int);
128 static void link_btr_uses (btr_def *, btr_user *, sbitmap *, sbitmap *, int);
129 static void build_btr_def_use_webs (fibheap_t);
130 static int block_at_edge_of_live_range_p (int, btr_def);
131 static void clear_btr_from_live_range (btr_def def);
132 static void add_btr_to_live_range (btr_def, int);
133 static void augment_live_range (bitmap, HARD_REG_SET *, basic_block,
134 basic_block, int);
135 static int choose_btr (HARD_REG_SET);
136 static void combine_btr_defs (btr_def, HARD_REG_SET *);
137 static void btr_def_live_range (btr_def, HARD_REG_SET *);
138 static void move_btr_def (basic_block, int, btr_def, bitmap, HARD_REG_SET *);
139 static int migrate_btr_def (btr_def, int);
140 static void migrate_btr_defs (enum reg_class, int);
141 static int can_move_up (const_basic_block, const rtx_insn *, int);
142 static void note_btr_set (rtx, const_rtx, void *);
144 /* The following code performs code motion of target load instructions
145 (instructions that set branch target registers), to move them
146 forward away from the branch instructions and out of loops (or,
147 more generally, from a more frequently executed place to a less
148 frequently executed place).
149 Moving target load instructions further in front of the branch
150 instruction that uses the target register value means that the hardware
151 has a better chance of preloading the instructions at the branch
152 target by the time the branch is reached. This avoids bubbles
153 when a taken branch needs to flush out the pipeline.
154 Moving target load instructions out of loops means they are executed
155 less frequently. */
157 /* An obstack to hold the def-use web data structures built up for
158 migrating branch target load instructions. */
159 static struct obstack migrate_btrl_obstack;
161 /* Array indexed by basic block number, giving the set of registers
162 live in that block. */
163 static HARD_REG_SET *btrs_live;
165 /* Array indexed by basic block number, giving the set of registers live at
166 the end of that block, including any uses by a final jump insn, if any. */
167 static HARD_REG_SET *btrs_live_at_end;
169 /* Set of all target registers that we are willing to allocate. */
170 static HARD_REG_SET all_btrs;
172 /* Provide lower and upper bounds for target register numbers, so that
173 we don't need to search through all the hard registers all the time. */
174 static int first_btr, last_btr;
178 /* Return an estimate of the frequency of execution of block bb. */
179 static int
180 basic_block_freq (const_basic_block bb)
182 return bb->frequency;
185 /* If X references (sets or reads) any branch target register, return one
186 such register. If EXCLUDEP is set, disregard any references within
187 that location. */
188 static rtx *
189 find_btr_use (rtx x, rtx *excludep = 0)
191 subrtx_ptr_iterator::array_type array;
192 FOR_EACH_SUBRTX_PTR (iter, array, &x, NONCONST)
194 rtx *loc = *iter;
195 if (loc == excludep)
196 iter.skip_subrtxes ();
197 else
199 const_rtx x = *loc;
200 if (REG_P (x)
201 && overlaps_hard_reg_set_p (all_btrs, GET_MODE (x), REGNO (x)))
202 return loc;
205 return 0;
208 /* Return true if insn is an instruction that sets a target register.
209 if CHECK_CONST is true, only return true if the source is constant.
210 If such a set is found and REGNO is nonzero, assign the register number
211 of the destination register to *REGNO. */
212 static int
213 insn_sets_btr_p (const rtx_insn *insn, int check_const, int *regno)
215 rtx set;
217 if (NONJUMP_INSN_P (insn)
218 && (set = single_set (insn)))
220 rtx dest = SET_DEST (set);
221 rtx src = SET_SRC (set);
223 if (GET_CODE (dest) == SUBREG)
224 dest = XEXP (dest, 0);
226 if (REG_P (dest)
227 && TEST_HARD_REG_BIT (all_btrs, REGNO (dest)))
229 gcc_assert (!find_btr_use (src));
231 if (!check_const || CONSTANT_P (src))
233 if (regno)
234 *regno = REGNO (dest);
235 return 1;
239 return 0;
242 /* Find the group that the target register definition DEF belongs
243 to in the list starting with *ALL_BTR_DEF_GROUPS. If no such
244 group exists, create one. Add def to the group. */
245 static void
246 find_btr_def_group (btr_def_group *all_btr_def_groups, btr_def def)
248 if (insn_sets_btr_p (def->insn, 1, NULL))
250 btr_def_group this_group;
251 rtx def_src = SET_SRC (single_set (def->insn));
253 /* ?? This linear search is an efficiency concern, particularly
254 as the search will almost always fail to find a match. */
255 for (this_group = *all_btr_def_groups;
256 this_group != NULL;
257 this_group = this_group->next)
258 if (rtx_equal_p (def_src, this_group->src))
259 break;
261 if (!this_group)
263 this_group = XOBNEW (&migrate_btrl_obstack, struct btr_def_group_s);
264 this_group->src = def_src;
265 this_group->members = NULL;
266 this_group->next = *all_btr_def_groups;
267 *all_btr_def_groups = this_group;
269 def->group = this_group;
270 def->next_this_group = this_group->members;
271 this_group->members = def;
273 else
274 def->group = NULL;
277 /* Create a new target register definition structure, for a definition in
278 block BB, instruction INSN, and insert it into ALL_BTR_DEFS. Return
279 the new definition. */
280 static btr_def
281 add_btr_def (fibheap_t all_btr_defs, basic_block bb, int insn_luid,
282 rtx_insn *insn,
283 unsigned int dest_reg, int other_btr_uses_before_def,
284 btr_def_group *all_btr_def_groups)
286 btr_def this_def = XOBNEW (&migrate_btrl_obstack, struct btr_def_s);
287 this_def->bb = bb;
288 this_def->luid = insn_luid;
289 this_def->insn = insn;
290 this_def->btr = dest_reg;
291 this_def->cost = basic_block_freq (bb);
292 this_def->has_ambiguous_use = 0;
293 this_def->other_btr_uses_before_def = other_btr_uses_before_def;
294 this_def->other_btr_uses_after_use = 0;
295 this_def->next_this_bb = NULL;
296 this_def->next_this_group = NULL;
297 this_def->uses = NULL;
298 this_def->live_range = NULL;
299 find_btr_def_group (all_btr_def_groups, this_def);
301 fibheap_insert (all_btr_defs, -this_def->cost, this_def);
303 if (dump_file)
304 fprintf (dump_file,
305 "Found target reg definition: sets %u { bb %d, insn %d }%s priority %d\n",
306 dest_reg, bb->index, INSN_UID (insn),
307 (this_def->group ? "" : ":not const"), this_def->cost);
309 return this_def;
312 /* Create a new target register user structure, for a use in block BB,
313 instruction INSN. Return the new user. */
314 static btr_user
315 new_btr_user (basic_block bb, int insn_luid, rtx_insn *insn)
317 /* This instruction reads target registers. We need
318 to decide whether we can replace all target register
319 uses easily.
321 rtx *usep = find_btr_use (PATTERN (insn));
322 rtx use;
323 btr_user user = NULL;
325 if (usep)
327 int unambiguous_single_use;
329 /* We want to ensure that USE is the only use of a target
330 register in INSN, so that we know that to rewrite INSN to use
331 a different target register, all we have to do is replace USE. */
332 unambiguous_single_use = !find_btr_use (PATTERN (insn), usep);
333 if (!unambiguous_single_use)
334 usep = NULL;
336 use = usep ? *usep : NULL_RTX;
337 user = XOBNEW (&migrate_btrl_obstack, struct btr_user_s);
338 user->bb = bb;
339 user->luid = insn_luid;
340 user->insn = insn;
341 user->use = use;
342 user->other_use_this_block = 0;
343 user->next = NULL;
344 user->n_reaching_defs = 0;
345 user->first_reaching_def = -1;
347 if (dump_file)
349 fprintf (dump_file, "Uses target reg: { bb %d, insn %d }",
350 bb->index, INSN_UID (insn));
352 if (user->use)
353 fprintf (dump_file, ": unambiguous use of reg %d\n",
354 REGNO (user->use));
357 return user;
360 /* Write the contents of S to the dump file. */
361 static void
362 dump_hard_reg_set (HARD_REG_SET s)
364 int reg;
365 for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
366 if (TEST_HARD_REG_BIT (s, reg))
367 fprintf (dump_file, " %d", reg);
370 /* Write the set of target regs live in block BB to the dump file. */
371 static void
372 dump_btrs_live (int bb)
374 fprintf (dump_file, "BB%d live:", bb);
375 dump_hard_reg_set (btrs_live[bb]);
376 fprintf (dump_file, "\n");
379 /* REGNO is the number of a branch target register that is being used or
380 set. USERS_THIS_BB is a list of preceding branch target register users;
381 If any of them use the same register, set their other_use_this_block
382 flag. */
383 static void
384 note_other_use_this_block (unsigned int regno, btr_user users_this_bb)
386 btr_user user;
388 for (user = users_this_bb; user != NULL; user = user->next)
389 if (user->use && REGNO (user->use) == regno)
390 user->other_use_this_block = 1;
393 typedef struct {
394 btr_user users_this_bb;
395 HARD_REG_SET btrs_written_in_block;
396 HARD_REG_SET btrs_live_in_block;
397 sbitmap bb_gen;
398 sbitmap *btr_defset;
399 } defs_uses_info;
401 /* Called via note_stores or directly to register stores into /
402 clobbers of a branch target register DEST that are not recognized as
403 straightforward definitions. DATA points to information about the
404 current basic block that needs updating. */
405 static void
406 note_btr_set (rtx dest, const_rtx set ATTRIBUTE_UNUSED, void *data)
408 defs_uses_info *info = (defs_uses_info *) data;
409 int regno, end_regno;
411 if (!REG_P (dest))
412 return;
413 regno = REGNO (dest);
414 end_regno = END_HARD_REGNO (dest);
415 for (; regno < end_regno; regno++)
416 if (TEST_HARD_REG_BIT (all_btrs, regno))
418 note_other_use_this_block (regno, info->users_this_bb);
419 SET_HARD_REG_BIT (info->btrs_written_in_block, regno);
420 SET_HARD_REG_BIT (info->btrs_live_in_block, regno);
421 bitmap_and_compl (info->bb_gen, info->bb_gen,
422 info->btr_defset[regno - first_btr]);
426 static void
427 compute_defs_uses_and_gen (fibheap_t all_btr_defs, btr_def *def_array,
428 btr_user *use_array, sbitmap *btr_defset,
429 sbitmap *bb_gen, HARD_REG_SET *btrs_written)
431 /* Scan the code building up the set of all defs and all uses.
432 For each target register, build the set of defs of that register.
433 For each block, calculate the set of target registers
434 written in that block.
435 Also calculate the set of btrs ever live in that block.
437 int i;
438 int insn_luid = 0;
439 btr_def_group all_btr_def_groups = NULL;
440 defs_uses_info info;
442 bitmap_vector_clear (bb_gen, last_basic_block_for_fn (cfun));
443 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
445 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
446 int reg;
447 btr_def defs_this_bb = NULL;
448 rtx_insn *insn;
449 rtx_insn *last;
450 int can_throw = 0;
452 info.users_this_bb = NULL;
453 info.bb_gen = bb_gen[i];
454 info.btr_defset = btr_defset;
456 CLEAR_HARD_REG_SET (info.btrs_live_in_block);
457 CLEAR_HARD_REG_SET (info.btrs_written_in_block);
458 for (reg = first_btr; reg <= last_btr; reg++)
459 if (TEST_HARD_REG_BIT (all_btrs, reg)
460 && REGNO_REG_SET_P (df_get_live_in (bb), reg))
461 SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
463 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
464 insn != last;
465 insn = NEXT_INSN (insn), insn_luid++)
467 if (INSN_P (insn))
469 int regno;
470 int insn_uid = INSN_UID (insn);
472 if (insn_sets_btr_p (insn, 0, &regno))
474 btr_def def = add_btr_def (
475 all_btr_defs, bb, insn_luid, insn, regno,
476 TEST_HARD_REG_BIT (info.btrs_live_in_block, regno),
477 &all_btr_def_groups);
479 def_array[insn_uid] = def;
480 SET_HARD_REG_BIT (info.btrs_written_in_block, regno);
481 SET_HARD_REG_BIT (info.btrs_live_in_block, regno);
482 bitmap_and_compl (bb_gen[i], bb_gen[i],
483 btr_defset[regno - first_btr]);
484 bitmap_set_bit (bb_gen[i], insn_uid);
485 def->next_this_bb = defs_this_bb;
486 defs_this_bb = def;
487 bitmap_set_bit (btr_defset[regno - first_btr], insn_uid);
488 note_other_use_this_block (regno, info.users_this_bb);
490 /* Check for the blockage emitted by expand_nl_goto_receiver. */
491 else if (cfun->has_nonlocal_label
492 && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE)
494 btr_user user;
496 /* Do the equivalent of calling note_other_use_this_block
497 for every target register. */
498 for (user = info.users_this_bb; user != NULL;
499 user = user->next)
500 if (user->use)
501 user->other_use_this_block = 1;
502 IOR_HARD_REG_SET (info.btrs_written_in_block, all_btrs);
503 IOR_HARD_REG_SET (info.btrs_live_in_block, all_btrs);
504 bitmap_clear (info.bb_gen);
506 else
508 if (find_btr_use (PATTERN (insn)))
510 btr_user user = new_btr_user (bb, insn_luid, insn);
512 use_array[insn_uid] = user;
513 if (user->use)
514 SET_HARD_REG_BIT (info.btrs_live_in_block,
515 REGNO (user->use));
516 else
518 int reg;
519 for (reg = first_btr; reg <= last_btr; reg++)
520 if (TEST_HARD_REG_BIT (all_btrs, reg)
521 && refers_to_regno_p (reg, reg + 1, user->insn,
522 NULL))
524 note_other_use_this_block (reg,
525 info.users_this_bb);
526 SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
528 note_stores (PATTERN (insn), note_btr_set, &info);
530 user->next = info.users_this_bb;
531 info.users_this_bb = user;
533 if (CALL_P (insn))
535 HARD_REG_SET *clobbered = &call_used_reg_set;
536 HARD_REG_SET call_saved;
537 rtx pat = PATTERN (insn);
538 int i;
540 /* Check for sibcall. */
541 if (GET_CODE (pat) == PARALLEL)
542 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
543 if (ANY_RETURN_P (XVECEXP (pat, 0, i)))
545 COMPL_HARD_REG_SET (call_saved,
546 call_used_reg_set);
547 clobbered = &call_saved;
550 for (regno = first_btr; regno <= last_btr; regno++)
551 if (TEST_HARD_REG_BIT (*clobbered, regno))
552 note_btr_set (regno_reg_rtx[regno], NULL_RTX, &info);
558 COPY_HARD_REG_SET (btrs_live[i], info.btrs_live_in_block);
559 COPY_HARD_REG_SET (btrs_written[i], info.btrs_written_in_block);
561 REG_SET_TO_HARD_REG_SET (btrs_live_at_end[i], df_get_live_out (bb));
562 /* If this block ends in a jump insn, add any uses or even clobbers
563 of branch target registers that it might have. */
564 for (insn = BB_END (bb); insn != BB_HEAD (bb) && ! INSN_P (insn); )
565 insn = PREV_INSN (insn);
566 /* ??? for the fall-through edge, it would make sense to insert the
567 btr set on the edge, but that would require to split the block
568 early on so that we can distinguish between dominance from the fall
569 through edge - which can use the call-clobbered registers - from
570 dominance by the throw edge. */
571 if (can_throw_internal (insn))
573 HARD_REG_SET tmp;
575 COPY_HARD_REG_SET (tmp, call_used_reg_set);
576 AND_HARD_REG_SET (tmp, all_btrs);
577 IOR_HARD_REG_SET (btrs_live_at_end[i], tmp);
578 can_throw = 1;
580 if (can_throw || JUMP_P (insn))
582 int regno;
584 for (regno = first_btr; regno <= last_btr; regno++)
585 if (refers_to_regno_p (regno, regno+1, insn, NULL))
586 SET_HARD_REG_BIT (btrs_live_at_end[i], regno);
589 if (dump_file)
590 dump_btrs_live (i);
594 static void
595 compute_kill (sbitmap *bb_kill, sbitmap *btr_defset,
596 HARD_REG_SET *btrs_written)
598 int i;
599 int regno;
601 /* For each basic block, form the set BB_KILL - the set
602 of definitions that the block kills. */
603 bitmap_vector_clear (bb_kill, last_basic_block_for_fn (cfun));
604 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
606 for (regno = first_btr; regno <= last_btr; regno++)
607 if (TEST_HARD_REG_BIT (all_btrs, regno)
608 && TEST_HARD_REG_BIT (btrs_written[i], regno))
609 bitmap_ior (bb_kill[i], bb_kill[i],
610 btr_defset[regno - first_btr]);
614 static void
615 compute_out (sbitmap *bb_out, sbitmap *bb_gen, sbitmap *bb_kill, int max_uid)
617 /* Perform iterative dataflow:
618 Initially, for all blocks, BB_OUT = BB_GEN.
619 For each block,
620 BB_IN = union over predecessors of BB_OUT(pred)
621 BB_OUT = (BB_IN - BB_KILL) + BB_GEN
622 Iterate until the bb_out sets stop growing. */
623 int i;
624 int changed;
625 sbitmap bb_in = sbitmap_alloc (max_uid);
627 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
628 bitmap_copy (bb_out[i], bb_gen[i]);
630 changed = 1;
631 while (changed)
633 changed = 0;
634 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
636 bitmap_union_of_preds (bb_in, bb_out, BASIC_BLOCK_FOR_FN (cfun, i));
637 changed |= bitmap_ior_and_compl (bb_out[i], bb_gen[i],
638 bb_in, bb_kill[i]);
641 sbitmap_free (bb_in);
644 static void
645 link_btr_uses (btr_def *def_array, btr_user *use_array, sbitmap *bb_out,
646 sbitmap *btr_defset, int max_uid)
648 int i;
649 sbitmap reaching_defs = sbitmap_alloc (max_uid);
651 /* Link uses to the uses lists of all of their reaching defs.
652 Count up the number of reaching defs of each use. */
653 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
655 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
656 rtx_insn *insn;
657 rtx_insn *last;
659 bitmap_union_of_preds (reaching_defs, bb_out, BASIC_BLOCK_FOR_FN (cfun, i));
660 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
661 insn != last;
662 insn = NEXT_INSN (insn))
664 if (INSN_P (insn))
666 int insn_uid = INSN_UID (insn);
668 btr_def def = def_array[insn_uid];
669 btr_user user = use_array[insn_uid];
670 if (def != NULL)
672 /* Remove all reaching defs of regno except
673 for this one. */
674 bitmap_and_compl (reaching_defs, reaching_defs,
675 btr_defset[def->btr - first_btr]);
676 bitmap_set_bit (reaching_defs, insn_uid);
679 if (user != NULL)
681 /* Find all the reaching defs for this use. */
682 sbitmap reaching_defs_of_reg = sbitmap_alloc (max_uid);
683 unsigned int uid = 0;
684 sbitmap_iterator sbi;
686 if (user->use)
687 bitmap_and (
688 reaching_defs_of_reg,
689 reaching_defs,
690 btr_defset[REGNO (user->use) - first_btr]);
691 else
693 int reg;
695 bitmap_clear (reaching_defs_of_reg);
696 for (reg = first_btr; reg <= last_btr; reg++)
697 if (TEST_HARD_REG_BIT (all_btrs, reg)
698 && refers_to_regno_p (reg, reg + 1, user->insn,
699 NULL))
700 bitmap_or_and (reaching_defs_of_reg,
701 reaching_defs_of_reg,
702 reaching_defs,
703 btr_defset[reg - first_btr]);
705 EXECUTE_IF_SET_IN_BITMAP (reaching_defs_of_reg, 0, uid, sbi)
707 btr_def def = def_array[uid];
709 /* We now know that def reaches user. */
711 if (dump_file)
712 fprintf (dump_file,
713 "Def in insn %d reaches use in insn %d\n",
714 uid, insn_uid);
716 user->n_reaching_defs++;
717 if (!user->use)
718 def->has_ambiguous_use = 1;
719 if (user->first_reaching_def != -1)
720 { /* There is more than one reaching def. This is
721 a rare case, so just give up on this def/use
722 web when it occurs. */
723 def->has_ambiguous_use = 1;
724 def_array[user->first_reaching_def]
725 ->has_ambiguous_use = 1;
726 if (dump_file)
727 fprintf (dump_file,
728 "(use %d has multiple reaching defs)\n",
729 insn_uid);
731 else
732 user->first_reaching_def = uid;
733 if (user->other_use_this_block)
734 def->other_btr_uses_after_use = 1;
735 user->next = def->uses;
736 def->uses = user;
738 sbitmap_free (reaching_defs_of_reg);
741 if (CALL_P (insn))
743 int regno;
745 for (regno = first_btr; regno <= last_btr; regno++)
746 if (TEST_HARD_REG_BIT (all_btrs, regno)
747 && TEST_HARD_REG_BIT (call_used_reg_set, regno))
748 bitmap_and_compl (reaching_defs, reaching_defs,
749 btr_defset[regno - first_btr]);
754 sbitmap_free (reaching_defs);
757 static void
758 build_btr_def_use_webs (fibheap_t all_btr_defs)
760 const int max_uid = get_max_uid ();
761 btr_def *def_array = XCNEWVEC (btr_def, max_uid);
762 btr_user *use_array = XCNEWVEC (btr_user, max_uid);
763 sbitmap *btr_defset = sbitmap_vector_alloc (
764 (last_btr - first_btr) + 1, max_uid);
765 sbitmap *bb_gen = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
766 max_uid);
767 HARD_REG_SET *btrs_written = XCNEWVEC (HARD_REG_SET,
768 last_basic_block_for_fn (cfun));
769 sbitmap *bb_kill;
770 sbitmap *bb_out;
772 bitmap_vector_clear (btr_defset, (last_btr - first_btr) + 1);
774 compute_defs_uses_and_gen (all_btr_defs, def_array, use_array, btr_defset,
775 bb_gen, btrs_written);
777 bb_kill = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), max_uid);
778 compute_kill (bb_kill, btr_defset, btrs_written);
779 free (btrs_written);
781 bb_out = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), max_uid);
782 compute_out (bb_out, bb_gen, bb_kill, max_uid);
784 sbitmap_vector_free (bb_gen);
785 sbitmap_vector_free (bb_kill);
787 link_btr_uses (def_array, use_array, bb_out, btr_defset, max_uid);
789 sbitmap_vector_free (bb_out);
790 sbitmap_vector_free (btr_defset);
791 free (use_array);
792 free (def_array);
795 /* Return true if basic block BB contains the start or end of the
796 live range of the definition DEF, AND there are other live
797 ranges of the same target register that include BB. */
798 static int
799 block_at_edge_of_live_range_p (int bb, btr_def def)
801 if (def->other_btr_uses_before_def
802 && BASIC_BLOCK_FOR_FN (cfun, bb) == def->bb)
803 return 1;
804 else if (def->other_btr_uses_after_use)
806 btr_user user;
807 for (user = def->uses; user != NULL; user = user->next)
808 if (BASIC_BLOCK_FOR_FN (cfun, bb) == user->bb)
809 return 1;
811 return 0;
814 /* We are removing the def/use web DEF. The target register
815 used in this web is therefore no longer live in the live range
816 of this web, so remove it from the live set of all basic blocks
817 in the live range of the web.
818 Blocks at the boundary of the live range may contain other live
819 ranges for the same target register, so we have to be careful
820 to remove the target register from the live set of these blocks
821 only if they do not contain other live ranges for the same register. */
822 static void
823 clear_btr_from_live_range (btr_def def)
825 unsigned bb;
826 bitmap_iterator bi;
828 EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
830 if ((!def->other_btr_uses_before_def
831 && !def->other_btr_uses_after_use)
832 || !block_at_edge_of_live_range_p (bb, def))
834 CLEAR_HARD_REG_BIT (btrs_live[bb], def->btr);
835 CLEAR_HARD_REG_BIT (btrs_live_at_end[bb], def->btr);
836 if (dump_file)
837 dump_btrs_live (bb);
840 if (def->own_end)
841 CLEAR_HARD_REG_BIT (btrs_live_at_end[def->bb->index], def->btr);
845 /* We are adding the def/use web DEF. Add the target register used
846 in this web to the live set of all of the basic blocks that contain
847 the live range of the web.
848 If OWN_END is set, also show that the register is live from our
849 definitions at the end of the basic block where it is defined. */
850 static void
851 add_btr_to_live_range (btr_def def, int own_end)
853 unsigned bb;
854 bitmap_iterator bi;
856 EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
858 SET_HARD_REG_BIT (btrs_live[bb], def->btr);
859 SET_HARD_REG_BIT (btrs_live_at_end[bb], def->btr);
860 if (dump_file)
861 dump_btrs_live (bb);
863 if (own_end)
865 SET_HARD_REG_BIT (btrs_live_at_end[def->bb->index], def->btr);
866 def->own_end = 1;
870 /* Update a live range to contain the basic block NEW_BLOCK, and all
871 blocks on paths between the existing live range and NEW_BLOCK.
872 HEAD is a block contained in the existing live range that dominates
873 all other blocks in the existing live range.
874 Also add to the set BTRS_LIVE_IN_RANGE all target registers that
875 are live in the blocks that we add to the live range.
876 If FULL_RANGE is set, include the full live range of NEW_BB;
877 otherwise, if NEW_BB dominates HEAD_BB, only add registers that
878 are life at the end of NEW_BB for NEW_BB itself.
879 It is a precondition that either NEW_BLOCK dominates HEAD,or
880 HEAD dom NEW_BLOCK. This is used to speed up the
881 implementation of this function. */
882 static void
883 augment_live_range (bitmap live_range, HARD_REG_SET *btrs_live_in_range,
884 basic_block head_bb, basic_block new_bb, int full_range)
886 basic_block *worklist, *tos;
888 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
890 if (dominated_by_p (CDI_DOMINATORS, new_bb, head_bb))
892 if (new_bb == head_bb)
894 if (full_range)
895 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[new_bb->index]);
896 free (tos);
897 return;
899 *tos++ = new_bb;
901 else
903 edge e;
904 edge_iterator ei;
905 int new_block = new_bb->index;
907 gcc_assert (dominated_by_p (CDI_DOMINATORS, head_bb, new_bb));
909 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[head_bb->index]);
910 bitmap_set_bit (live_range, new_block);
911 /* A previous btr migration could have caused a register to be
912 live just at the end of new_block which we need in full, so
913 use trs_live_at_end even if full_range is set. */
914 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live_at_end[new_block]);
915 if (full_range)
916 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[new_block]);
917 if (dump_file)
919 fprintf (dump_file,
920 "Adding end of block %d and rest of %d to live range\n",
921 new_block, head_bb->index);
922 fprintf (dump_file,"Now live btrs are ");
923 dump_hard_reg_set (*btrs_live_in_range);
924 fprintf (dump_file, "\n");
926 FOR_EACH_EDGE (e, ei, head_bb->preds)
927 *tos++ = e->src;
930 while (tos != worklist)
932 basic_block bb = *--tos;
933 if (!bitmap_bit_p (live_range, bb->index))
935 edge e;
936 edge_iterator ei;
938 bitmap_set_bit (live_range, bb->index);
939 IOR_HARD_REG_SET (*btrs_live_in_range,
940 btrs_live[bb->index]);
941 /* A previous btr migration could have caused a register to be
942 live just at the end of a block which we need in full. */
943 IOR_HARD_REG_SET (*btrs_live_in_range,
944 btrs_live_at_end[bb->index]);
945 if (dump_file)
947 fprintf (dump_file,
948 "Adding block %d to live range\n", bb->index);
949 fprintf (dump_file,"Now live btrs are ");
950 dump_hard_reg_set (*btrs_live_in_range);
951 fprintf (dump_file, "\n");
954 FOR_EACH_EDGE (e, ei, bb->preds)
956 basic_block pred = e->src;
957 if (!bitmap_bit_p (live_range, pred->index))
958 *tos++ = pred;
963 free (worklist);
966 /* Return the most desirable target register that is not in
967 the set USED_BTRS. */
968 static int
969 choose_btr (HARD_REG_SET used_btrs)
971 int i;
973 if (!hard_reg_set_subset_p (all_btrs, used_btrs))
974 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
976 #ifdef REG_ALLOC_ORDER
977 int regno = reg_alloc_order[i];
978 #else
979 int regno = i;
980 #endif
981 if (TEST_HARD_REG_BIT (all_btrs, regno)
982 && !TEST_HARD_REG_BIT (used_btrs, regno))
983 return regno;
985 return -1;
988 /* Calculate the set of basic blocks that contain the live range of
989 the def/use web DEF.
990 Also calculate the set of target registers that are live at time
991 in this live range, but ignore the live range represented by DEF
992 when calculating this set. */
993 static void
994 btr_def_live_range (btr_def def, HARD_REG_SET *btrs_live_in_range)
996 if (!def->live_range)
998 btr_user user;
1000 def->live_range = BITMAP_ALLOC (NULL);
1002 bitmap_set_bit (def->live_range, def->bb->index);
1003 COPY_HARD_REG_SET (*btrs_live_in_range,
1004 (flag_btr_bb_exclusive
1005 ? btrs_live : btrs_live_at_end)[def->bb->index]);
1007 for (user = def->uses; user != NULL; user = user->next)
1008 augment_live_range (def->live_range, btrs_live_in_range,
1009 def->bb, user->bb,
1010 (flag_btr_bb_exclusive
1011 || user->insn != BB_END (def->bb)
1012 || !JUMP_P (user->insn)));
1014 else
1016 /* def->live_range is accurate, but we need to recompute
1017 the set of target registers live over it, because migration
1018 of other PT instructions may have affected it.
1020 unsigned bb;
1021 unsigned def_bb = flag_btr_bb_exclusive ? -1 : def->bb->index;
1022 bitmap_iterator bi;
1024 CLEAR_HARD_REG_SET (*btrs_live_in_range);
1025 EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
1027 IOR_HARD_REG_SET (*btrs_live_in_range,
1028 (def_bb == bb
1029 ? btrs_live_at_end : btrs_live) [bb]);
1032 if (!def->other_btr_uses_before_def &&
1033 !def->other_btr_uses_after_use)
1034 CLEAR_HARD_REG_BIT (*btrs_live_in_range, def->btr);
1037 /* Merge into the def/use web DEF any other def/use webs in the same
1038 group that are dominated by DEF, provided that there is a target
1039 register available to allocate to the merged web. */
1040 static void
1041 combine_btr_defs (btr_def def, HARD_REG_SET *btrs_live_in_range)
1043 btr_def other_def;
1045 for (other_def = def->group->members;
1046 other_def != NULL;
1047 other_def = other_def->next_this_group)
1049 if (other_def != def
1050 && other_def->uses != NULL
1051 && ! other_def->has_ambiguous_use
1052 && dominated_by_p (CDI_DOMINATORS, other_def->bb, def->bb))
1054 /* def->bb dominates the other def, so def and other_def could
1055 be combined. */
1056 /* Merge their live ranges, and get the set of
1057 target registers live over the merged range. */
1058 int btr;
1059 HARD_REG_SET combined_btrs_live;
1060 bitmap combined_live_range = BITMAP_ALLOC (NULL);
1061 btr_user user;
1063 if (other_def->live_range == NULL)
1065 HARD_REG_SET dummy_btrs_live_in_range;
1066 btr_def_live_range (other_def, &dummy_btrs_live_in_range);
1068 COPY_HARD_REG_SET (combined_btrs_live, *btrs_live_in_range);
1069 bitmap_copy (combined_live_range, def->live_range);
1071 for (user = other_def->uses; user != NULL; user = user->next)
1072 augment_live_range (combined_live_range, &combined_btrs_live,
1073 def->bb, user->bb,
1074 (flag_btr_bb_exclusive
1075 || user->insn != BB_END (def->bb)
1076 || !JUMP_P (user->insn)));
1078 btr = choose_btr (combined_btrs_live);
1079 if (btr != -1)
1081 /* We can combine them. */
1082 if (dump_file)
1083 fprintf (dump_file,
1084 "Combining def in insn %d with def in insn %d\n",
1085 INSN_UID (other_def->insn), INSN_UID (def->insn));
1087 def->btr = btr;
1088 user = other_def->uses;
1089 while (user != NULL)
1091 btr_user next = user->next;
1093 user->next = def->uses;
1094 def->uses = user;
1095 user = next;
1097 /* Combining def/use webs can make target registers live
1098 after uses where they previously were not. This means
1099 some REG_DEAD notes may no longer be correct. We could
1100 be more precise about this if we looked at the combined
1101 live range, but here I just delete any REG_DEAD notes
1102 in case they are no longer correct. */
1103 for (user = def->uses; user != NULL; user = user->next)
1104 remove_note (user->insn,
1105 find_regno_note (user->insn, REG_DEAD,
1106 REGNO (user->use)));
1107 clear_btr_from_live_range (other_def);
1108 other_def->uses = NULL;
1109 bitmap_copy (def->live_range, combined_live_range);
1110 if (other_def->btr == btr && other_def->other_btr_uses_after_use)
1111 def->other_btr_uses_after_use = 1;
1112 COPY_HARD_REG_SET (*btrs_live_in_range, combined_btrs_live);
1114 /* Delete the old target register initialization. */
1115 delete_insn (other_def->insn);
1118 BITMAP_FREE (combined_live_range);
1123 /* Move the definition DEF from its current position to basic
1124 block NEW_DEF_BB, and modify it to use branch target register BTR.
1125 Delete the old defining insn, and insert a new one in NEW_DEF_BB.
1126 Update all reaching uses of DEF in the RTL to use BTR.
1127 If this new position means that other defs in the
1128 same group can be combined with DEF then combine them. */
1129 static void
1130 move_btr_def (basic_block new_def_bb, int btr, btr_def def, bitmap live_range,
1131 HARD_REG_SET *btrs_live_in_range)
1133 /* We can move the instruction.
1134 Set a target register in block NEW_DEF_BB to the value
1135 needed for this target register definition.
1136 Replace all uses of the old target register definition by
1137 uses of the new definition. Delete the old definition. */
1138 basic_block b = new_def_bb;
1139 rtx_insn *insp = BB_HEAD (b);
1140 rtx_insn *old_insn = def->insn;
1141 rtx src;
1142 rtx btr_rtx;
1143 rtx_insn *new_insn;
1144 enum machine_mode btr_mode;
1145 btr_user user;
1146 rtx set;
1148 if (dump_file)
1149 fprintf(dump_file, "migrating to basic block %d, using reg %d\n",
1150 new_def_bb->index, btr);
1152 clear_btr_from_live_range (def);
1153 def->btr = btr;
1154 def->bb = new_def_bb;
1155 def->luid = 0;
1156 def->cost = basic_block_freq (new_def_bb);
1157 bitmap_copy (def->live_range, live_range);
1158 combine_btr_defs (def, btrs_live_in_range);
1159 btr = def->btr;
1160 def->other_btr_uses_before_def
1161 = TEST_HARD_REG_BIT (btrs_live[b->index], btr) ? 1 : 0;
1162 add_btr_to_live_range (def, 1);
1163 if (LABEL_P (insp))
1164 insp = NEXT_INSN (insp);
1165 /* N.B.: insp is expected to be NOTE_INSN_BASIC_BLOCK now. Some
1166 optimizations can result in insp being both first and last insn of
1167 its basic block. */
1168 /* ?? some assertions to check that insp is sensible? */
1170 if (def->other_btr_uses_before_def)
1172 insp = BB_END (b);
1173 for (insp = BB_END (b); ! INSN_P (insp); insp = PREV_INSN (insp))
1174 gcc_assert (insp != BB_HEAD (b));
1176 if (JUMP_P (insp) || can_throw_internal (insp))
1177 insp = PREV_INSN (insp);
1180 set = single_set (old_insn);
1181 src = SET_SRC (set);
1182 btr_mode = GET_MODE (SET_DEST (set));
1183 btr_rtx = gen_rtx_REG (btr_mode, btr);
1185 new_insn = as_a <rtx_insn *> (gen_move_insn (btr_rtx, src));
1187 /* Insert target register initialization at head of basic block. */
1188 def->insn = emit_insn_after (new_insn, insp);
1190 df_set_regs_ever_live (btr, true);
1192 if (dump_file)
1193 fprintf (dump_file, "New pt is insn %d, inserted after insn %d\n",
1194 INSN_UID (def->insn), INSN_UID (insp));
1196 /* Delete the old target register initialization. */
1197 delete_insn (old_insn);
1199 /* Replace each use of the old target register by a use of the new target
1200 register. */
1201 for (user = def->uses; user != NULL; user = user->next)
1203 /* Some extra work here to ensure consistent modes, because
1204 it seems that a target register REG rtx can be given a different
1205 mode depending on the context (surely that should not be
1206 the case?). */
1207 rtx replacement_rtx;
1208 if (GET_MODE (user->use) == GET_MODE (btr_rtx)
1209 || GET_MODE (user->use) == VOIDmode)
1210 replacement_rtx = btr_rtx;
1211 else
1212 replacement_rtx = gen_rtx_REG (GET_MODE (user->use), btr);
1213 validate_replace_rtx (user->use, replacement_rtx, user->insn);
1214 user->use = replacement_rtx;
1218 /* We anticipate intra-block scheduling to be done. See if INSN could move
1219 up within BB by N_INSNS. */
1220 static int
1221 can_move_up (const_basic_block bb, const rtx_insn *insn, int n_insns)
1223 while (insn != BB_HEAD (bb) && n_insns > 0)
1225 insn = PREV_INSN (insn);
1226 /* ??? What if we have an anti-dependency that actually prevents the
1227 scheduler from doing the move? We'd like to re-allocate the register,
1228 but not necessarily put the load into another basic block. */
1229 if (INSN_P (insn))
1230 n_insns--;
1232 return n_insns <= 0;
1235 /* Attempt to migrate the target register definition DEF to an
1236 earlier point in the flowgraph.
1238 It is a precondition of this function that DEF is migratable:
1239 i.e. it has a constant source, and all uses are unambiguous.
1241 Only migrations that reduce the cost of DEF will be made.
1242 MIN_COST is the lower bound on the cost of the DEF after migration.
1243 If we migrate DEF so that its cost falls below MIN_COST,
1244 then we do not attempt to migrate further. The idea is that
1245 we migrate definitions in a priority order based on their cost,
1246 when the cost of this definition falls below MIN_COST, then
1247 there is another definition with cost == MIN_COST which now
1248 has a higher priority than this definition.
1250 Return nonzero if there may be benefit from attempting to
1251 migrate this DEF further (i.e. we have reduced the cost below
1252 MIN_COST, but we may be able to reduce it further).
1253 Return zero if no further migration is possible. */
1254 static int
1255 migrate_btr_def (btr_def def, int min_cost)
1257 bitmap live_range;
1258 HARD_REG_SET btrs_live_in_range;
1259 int btr_used_near_def = 0;
1260 int def_basic_block_freq;
1261 basic_block attempt;
1262 int give_up = 0;
1263 int def_moved = 0;
1264 btr_user user;
1265 int def_latency;
1267 if (dump_file)
1268 fprintf (dump_file,
1269 "Attempting to migrate pt from insn %d (cost = %d, min_cost = %d) ... ",
1270 INSN_UID (def->insn), def->cost, min_cost);
1272 if (!def->group || def->has_ambiguous_use)
1273 /* These defs are not migratable. */
1275 if (dump_file)
1276 fprintf (dump_file, "it's not migratable\n");
1277 return 0;
1280 if (!def->uses)
1281 /* We have combined this def with another in the same group, so
1282 no need to consider it further.
1285 if (dump_file)
1286 fprintf (dump_file, "it's already combined with another pt\n");
1287 return 0;
1290 btr_def_live_range (def, &btrs_live_in_range);
1291 live_range = BITMAP_ALLOC (NULL);
1292 bitmap_copy (live_range, def->live_range);
1294 #ifdef INSN_SCHEDULING
1295 def_latency = insn_default_latency (def->insn) * issue_rate;
1296 #else
1297 def_latency = issue_rate;
1298 #endif
1300 for (user = def->uses; user != NULL; user = user->next)
1302 if (user->bb == def->bb
1303 && user->luid > def->luid
1304 && (def->luid + def_latency) > user->luid
1305 && ! can_move_up (def->bb, def->insn,
1306 (def->luid + def_latency) - user->luid))
1308 btr_used_near_def = 1;
1309 break;
1313 def_basic_block_freq = basic_block_freq (def->bb);
1315 for (attempt = get_immediate_dominator (CDI_DOMINATORS, def->bb);
1316 !give_up && attempt && attempt != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1317 && def->cost >= min_cost;
1318 attempt = get_immediate_dominator (CDI_DOMINATORS, attempt))
1320 /* Try to move the instruction that sets the target register into
1321 basic block ATTEMPT. */
1322 int try_freq = basic_block_freq (attempt);
1323 edge_iterator ei;
1324 edge e;
1326 /* If ATTEMPT has abnormal edges, skip it. */
1327 FOR_EACH_EDGE (e, ei, attempt->succs)
1328 if (e->flags & EDGE_COMPLEX)
1329 break;
1330 if (e)
1331 continue;
1333 if (dump_file)
1334 fprintf (dump_file, "trying block %d ...", attempt->index);
1336 if (try_freq < def_basic_block_freq
1337 || (try_freq == def_basic_block_freq && btr_used_near_def))
1339 int btr;
1340 augment_live_range (live_range, &btrs_live_in_range, def->bb, attempt,
1341 flag_btr_bb_exclusive);
1342 if (dump_file)
1344 fprintf (dump_file, "Now btrs live in range are: ");
1345 dump_hard_reg_set (btrs_live_in_range);
1346 fprintf (dump_file, "\n");
1348 btr = choose_btr (btrs_live_in_range);
1349 if (btr != -1)
1351 move_btr_def (attempt, btr, def, live_range, &btrs_live_in_range);
1352 bitmap_copy (live_range, def->live_range);
1353 btr_used_near_def = 0;
1354 def_moved = 1;
1355 def_basic_block_freq = basic_block_freq (def->bb);
1357 else
1359 /* There are no free target registers available to move
1360 this far forward, so give up */
1361 give_up = 1;
1362 if (dump_file)
1363 fprintf (dump_file,
1364 "giving up because there are no free target registers\n");
1369 if (!def_moved)
1371 give_up = 1;
1372 if (dump_file)
1373 fprintf (dump_file, "failed to move\n");
1375 BITMAP_FREE (live_range);
1376 return !give_up;
1379 /* Attempt to move instructions that set target registers earlier
1380 in the flowgraph, away from their corresponding uses. */
1381 static void
1382 migrate_btr_defs (enum reg_class btr_class, int allow_callee_save)
1384 fibheap_t all_btr_defs = fibheap_new ();
1385 int reg;
1387 gcc_obstack_init (&migrate_btrl_obstack);
1388 if (dump_file)
1390 int i;
1392 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
1394 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1395 fprintf (dump_file,
1396 "Basic block %d: count = %" PRId64
1397 " loop-depth = %d idom = %d\n",
1398 i, (int64_t) bb->count, bb_loop_depth (bb),
1399 get_immediate_dominator (CDI_DOMINATORS, bb)->index);
1403 CLEAR_HARD_REG_SET (all_btrs);
1404 for (first_btr = -1, reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
1405 if (TEST_HARD_REG_BIT (reg_class_contents[(int) btr_class], reg)
1406 && (allow_callee_save || call_used_regs[reg]
1407 || df_regs_ever_live_p (reg)))
1409 SET_HARD_REG_BIT (all_btrs, reg);
1410 last_btr = reg;
1411 if (first_btr < 0)
1412 first_btr = reg;
1415 btrs_live = XCNEWVEC (HARD_REG_SET, last_basic_block_for_fn (cfun));
1416 btrs_live_at_end = XCNEWVEC (HARD_REG_SET, last_basic_block_for_fn (cfun));
1418 build_btr_def_use_webs (all_btr_defs);
1420 while (!fibheap_empty (all_btr_defs))
1422 btr_def def = (btr_def) fibheap_extract_min (all_btr_defs);
1423 int min_cost = -fibheap_min_key (all_btr_defs);
1424 if (migrate_btr_def (def, min_cost))
1426 fibheap_insert (all_btr_defs, -def->cost, (void *) def);
1427 if (dump_file)
1429 fprintf (dump_file,
1430 "Putting insn %d back on queue with priority %d\n",
1431 INSN_UID (def->insn), def->cost);
1434 else
1435 BITMAP_FREE (def->live_range);
1438 free (btrs_live);
1439 free (btrs_live_at_end);
1440 obstack_free (&migrate_btrl_obstack, NULL);
1441 fibheap_delete (all_btr_defs);
1444 static void
1445 branch_target_load_optimize (bool after_prologue_epilogue_gen)
1447 enum reg_class klass
1448 = (enum reg_class) targetm.branch_target_register_class ();
1449 if (klass != NO_REGS)
1451 /* Initialize issue_rate. */
1452 if (targetm.sched.issue_rate)
1453 issue_rate = targetm.sched.issue_rate ();
1454 else
1455 issue_rate = 1;
1457 if (!after_prologue_epilogue_gen)
1459 /* Build the CFG for migrate_btr_defs. */
1460 #if 1
1461 /* This may or may not be needed, depending on where we
1462 run this phase. */
1463 cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
1464 #endif
1466 df_analyze ();
1469 /* Dominator info is also needed for migrate_btr_def. */
1470 calculate_dominance_info (CDI_DOMINATORS);
1471 migrate_btr_defs (klass,
1472 (targetm.branch_target_register_callee_saved
1473 (after_prologue_epilogue_gen)));
1475 free_dominance_info (CDI_DOMINATORS);
1479 namespace {
1481 const pass_data pass_data_branch_target_load_optimize1 =
1483 RTL_PASS, /* type */
1484 "btl1", /* name */
1485 OPTGROUP_NONE, /* optinfo_flags */
1486 TV_NONE, /* tv_id */
1487 0, /* properties_required */
1488 0, /* properties_provided */
1489 0, /* properties_destroyed */
1490 0, /* todo_flags_start */
1491 0, /* todo_flags_finish */
1494 class pass_branch_target_load_optimize1 : public rtl_opt_pass
1496 public:
1497 pass_branch_target_load_optimize1 (gcc::context *ctxt)
1498 : rtl_opt_pass (pass_data_branch_target_load_optimize1, ctxt)
1501 /* opt_pass methods: */
1502 virtual bool gate (function *) { return flag_branch_target_load_optimize; }
1503 virtual unsigned int execute (function *)
1505 branch_target_load_optimize (epilogue_completed);
1506 return 0;
1509 }; // class pass_branch_target_load_optimize1
1511 } // anon namespace
1513 rtl_opt_pass *
1514 make_pass_branch_target_load_optimize1 (gcc::context *ctxt)
1516 return new pass_branch_target_load_optimize1 (ctxt);
1520 namespace {
1522 const pass_data pass_data_branch_target_load_optimize2 =
1524 RTL_PASS, /* type */
1525 "btl2", /* name */
1526 OPTGROUP_NONE, /* optinfo_flags */
1527 TV_NONE, /* tv_id */
1528 0, /* properties_required */
1529 0, /* properties_provided */
1530 0, /* properties_destroyed */
1531 0, /* todo_flags_start */
1532 0, /* todo_flags_finish */
1535 class pass_branch_target_load_optimize2 : public rtl_opt_pass
1537 public:
1538 pass_branch_target_load_optimize2 (gcc::context *ctxt)
1539 : rtl_opt_pass (pass_data_branch_target_load_optimize2, ctxt)
1542 /* opt_pass methods: */
1543 virtual bool gate (function *)
1545 return (optimize > 0 && flag_branch_target_load_optimize2);
1548 virtual unsigned int execute (function *);
1550 }; // class pass_branch_target_load_optimize2
1552 unsigned int
1553 pass_branch_target_load_optimize2::execute (function *)
1555 static int warned = 0;
1557 /* Leave this a warning for now so that it is possible to experiment
1558 with running this pass twice. In 3.6, we should either make this
1559 an error, or use separate dump files. */
1560 if (flag_branch_target_load_optimize
1561 && flag_branch_target_load_optimize2
1562 && !warned)
1564 warning (0, "branch target register load optimization is not intended "
1565 "to be run twice");
1567 warned = 1;
1570 branch_target_load_optimize (epilogue_completed);
1571 return 0;
1574 } // anon namespace
1576 rtl_opt_pass *
1577 make_pass_branch_target_load_optimize2 (gcc::context *ctxt)
1579 return new pass_branch_target_load_optimize2 (ctxt);