2016-09-25 François Dumont <fdumont@gcc.gnu.org>
[official-gcc.git] / gcc / bt-load.c
blob5b1bcecd5c3cfe232fc6c8f342fd2cf70a3e5402
1 /* Perform branch target register load optimizations.
2 Copyright (C) 2001-2016 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 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 "backend.h"
24 #include "target.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "df.h"
28 #include "insn-config.h"
29 #include "regs.h"
30 #include "emit-rtl.h"
31 #include "recog.h"
32 #include "diagnostic-core.h"
33 #include "expr.h"
34 #include "insn-attr.h"
35 #include "tree-pass.h"
36 #include "cfgrtl.h"
37 #include "cfganal.h"
38 #include "cfgcleanup.h"
39 #include "cfgloop.h"
40 #include "rtl-iter.h"
41 #include "fibonacci_heap.h"
43 struct btr_def;
45 /* Target register optimizations - these are performed after reload. */
47 struct btr_def_group
49 btr_def_group *next;
50 rtx src;
51 btr_def *members;
54 struct btr_user
56 btr_user *next;
57 basic_block bb;
58 int luid;
59 rtx_insn *insn;
60 /* If INSN has a single use of a single branch register, then
61 USE points to it within INSN. If there is more than
62 one branch register use, or the use is in some way ambiguous,
63 then USE is NULL. */
64 rtx use;
65 int n_reaching_defs;
66 int first_reaching_def;
67 char other_use_this_block;
70 /* btr_def structs appear on three lists:
71 1. A list of all btr_def structures (head is
72 ALL_BTR_DEFS, linked by the NEXT field).
73 2. A list of branch reg definitions per basic block (head is
74 BB_BTR_DEFS[i], linked by the NEXT_THIS_BB field).
75 3. A list of all branch reg definitions belonging to the same
76 group (head is in a BTR_DEF_GROUP struct, linked by
77 NEXT_THIS_GROUP field). */
79 struct btr_def
81 btr_def *next_this_bb;
82 btr_def *next_this_group;
83 basic_block bb;
84 int luid;
85 rtx_insn *insn;
86 int btr;
87 int cost;
88 /* For a branch register setting insn that has a constant
89 source (i.e. a label), group links together all the
90 insns with the same source. For other branch register
91 setting insns, group is NULL. */
92 btr_def_group *group;
93 btr_user *uses;
94 /* If this def has a reaching use which is not a simple use
95 in a branch instruction, then has_ambiguous_use will be true,
96 and we will not attempt to migrate this definition. */
97 char has_ambiguous_use;
98 /* live_range is an approximation to the true live range for this
99 def/use web, because it records the set of blocks that contain
100 the live range. There could be other live ranges for the same
101 branch register in that set of blocks, either in the block
102 containing the def (before the def), or in a block containing
103 a use (after the use). If there are such other live ranges, then
104 other_btr_uses_before_def or other_btr_uses_after_use must be set true
105 as appropriate. */
106 char other_btr_uses_before_def;
107 char other_btr_uses_after_use;
108 /* We set own_end when we have moved a definition into a dominator.
109 Thus, when a later combination removes this definition again, we know
110 to clear out trs_live_at_end again. */
111 char own_end;
112 bitmap live_range;
115 typedef fibonacci_heap <long, btr_def> btr_heap_t;
116 typedef fibonacci_node <long, btr_def> btr_heap_node_t;
118 static int issue_rate;
120 static int basic_block_freq (const_basic_block);
121 static int insn_sets_btr_p (const rtx_insn *, int, int *);
122 static void find_btr_def_group (btr_def_group **, btr_def *);
123 static btr_def *add_btr_def (btr_heap_t *, basic_block, int, rtx_insn *,
124 unsigned int, int, btr_def_group **);
125 static btr_user *new_btr_user (basic_block, int, rtx_insn *);
126 static void dump_hard_reg_set (HARD_REG_SET);
127 static void dump_btrs_live (int);
128 static void note_other_use_this_block (unsigned int, btr_user *);
129 static void compute_defs_uses_and_gen (btr_heap_t *, btr_def **, btr_user **,
130 sbitmap *, sbitmap *, HARD_REG_SET *);
131 static void compute_kill (sbitmap *, sbitmap *, HARD_REG_SET *);
132 static void compute_out (sbitmap *bb_out, sbitmap *, sbitmap *, int);
133 static void link_btr_uses (btr_def **, btr_user **, sbitmap *, sbitmap *, int);
134 static void build_btr_def_use_webs (btr_heap_t *);
135 static int block_at_edge_of_live_range_p (int, btr_def *);
136 static void clear_btr_from_live_range (btr_def *def);
137 static void add_btr_to_live_range (btr_def *, int);
138 static void augment_live_range (bitmap, HARD_REG_SET *, basic_block,
139 basic_block, int);
140 static int choose_btr (HARD_REG_SET);
141 static void combine_btr_defs (btr_def *, HARD_REG_SET *);
142 static void btr_def_live_range (btr_def *, HARD_REG_SET *);
143 static void move_btr_def (basic_block, int, btr_def *, bitmap, HARD_REG_SET *);
144 static int migrate_btr_def (btr_def *, int);
145 static void migrate_btr_defs (enum reg_class, int);
146 static int can_move_up (const_basic_block, const rtx_insn *, int);
147 static void note_btr_set (rtx, const_rtx, void *);
149 /* The following code performs code motion of target load instructions
150 (instructions that set branch target registers), to move them
151 forward away from the branch instructions and out of loops (or,
152 more generally, from a more frequently executed place to a less
153 frequently executed place).
154 Moving target load instructions further in front of the branch
155 instruction that uses the target register value means that the hardware
156 has a better chance of preloading the instructions at the branch
157 target by the time the branch is reached. This avoids bubbles
158 when a taken branch needs to flush out the pipeline.
159 Moving target load instructions out of loops means they are executed
160 less frequently. */
162 /* An obstack to hold the def-use web data structures built up for
163 migrating branch target load instructions. */
164 static struct obstack migrate_btrl_obstack;
166 /* Array indexed by basic block number, giving the set of registers
167 live in that block. */
168 static HARD_REG_SET *btrs_live;
170 /* Array indexed by basic block number, giving the set of registers live at
171 the end of that block, including any uses by a final jump insn, if any. */
172 static HARD_REG_SET *btrs_live_at_end;
174 /* Set of all target registers that we are willing to allocate. */
175 static HARD_REG_SET all_btrs;
177 /* Provide lower and upper bounds for target register numbers, so that
178 we don't need to search through all the hard registers all the time. */
179 static int first_btr, last_btr;
183 /* Return an estimate of the frequency of execution of block bb. */
184 static int
185 basic_block_freq (const_basic_block bb)
187 return bb->frequency;
190 /* If the rtx at *XP references (sets or reads) any branch target
191 register, return one such register. If EXCLUDEP is set, disregard
192 any references within that location. */
193 static rtx *
194 find_btr_use (rtx *xp, rtx *excludep = 0)
196 subrtx_ptr_iterator::array_type array;
197 FOR_EACH_SUBRTX_PTR (iter, array, xp, NONCONST)
199 rtx *loc = *iter;
200 if (loc == excludep)
201 iter.skip_subrtxes ();
202 else
204 const_rtx x = *loc;
205 if (REG_P (x)
206 && overlaps_hard_reg_set_p (all_btrs, GET_MODE (x), REGNO (x)))
207 return loc;
210 return 0;
213 /* Return true if insn is an instruction that sets a target register.
214 if CHECK_CONST is true, only return true if the source is constant.
215 If such a set is found and REGNO is nonzero, assign the register number
216 of the destination register to *REGNO. */
217 static int
218 insn_sets_btr_p (const rtx_insn *insn, int check_const, int *regno)
220 rtx set;
222 if (NONJUMP_INSN_P (insn)
223 && (set = single_set (insn)))
225 rtx dest = SET_DEST (set);
226 rtx src = SET_SRC (set);
228 if (GET_CODE (dest) == SUBREG)
229 dest = XEXP (dest, 0);
231 if (REG_P (dest)
232 && TEST_HARD_REG_BIT (all_btrs, REGNO (dest)))
234 gcc_assert (!find_btr_use (&src));
236 if (!check_const || CONSTANT_P (src))
238 if (regno)
239 *regno = REGNO (dest);
240 return 1;
244 return 0;
247 /* Find the group that the target register definition DEF belongs
248 to in the list starting with *ALL_BTR_DEF_GROUPS. If no such
249 group exists, create one. Add def to the group. */
250 static void
251 find_btr_def_group (btr_def_group **all_btr_def_groups, btr_def *def)
253 if (insn_sets_btr_p (def->insn, 1, NULL))
255 btr_def_group *this_group;
256 rtx def_src = SET_SRC (single_set (def->insn));
258 /* ?? This linear search is an efficiency concern, particularly
259 as the search will almost always fail to find a match. */
260 for (this_group = *all_btr_def_groups;
261 this_group != NULL;
262 this_group = this_group->next)
263 if (rtx_equal_p (def_src, this_group->src))
264 break;
266 if (!this_group)
268 this_group = XOBNEW (&migrate_btrl_obstack, btr_def_group);
269 this_group->src = def_src;
270 this_group->members = NULL;
271 this_group->next = *all_btr_def_groups;
272 *all_btr_def_groups = this_group;
274 def->group = this_group;
275 def->next_this_group = this_group->members;
276 this_group->members = def;
278 else
279 def->group = NULL;
282 /* Create a new target register definition structure, for a definition in
283 block BB, instruction INSN, and insert it into ALL_BTR_DEFS. Return
284 the new definition. */
285 static btr_def *
286 add_btr_def (btr_heap_t *all_btr_defs, basic_block bb, int insn_luid,
287 rtx_insn *insn,
288 unsigned int dest_reg, int other_btr_uses_before_def,
289 btr_def_group **all_btr_def_groups)
291 btr_def *this_def = XOBNEW (&migrate_btrl_obstack, btr_def);
292 this_def->bb = bb;
293 this_def->luid = insn_luid;
294 this_def->insn = insn;
295 this_def->btr = dest_reg;
296 this_def->cost = basic_block_freq (bb);
297 this_def->has_ambiguous_use = 0;
298 this_def->other_btr_uses_before_def = other_btr_uses_before_def;
299 this_def->other_btr_uses_after_use = 0;
300 this_def->next_this_bb = NULL;
301 this_def->next_this_group = NULL;
302 this_def->uses = NULL;
303 this_def->live_range = NULL;
304 find_btr_def_group (all_btr_def_groups, this_def);
306 all_btr_defs->insert (-this_def->cost, this_def);
308 if (dump_file)
309 fprintf (dump_file,
310 "Found target reg definition: sets %u { bb %d, insn %d }%s priority %d\n",
311 dest_reg, bb->index, INSN_UID (insn),
312 (this_def->group ? "" : ":not const"), this_def->cost);
314 return this_def;
317 /* Create a new target register user structure, for a use in block BB,
318 instruction INSN. Return the new user. */
319 static btr_user *
320 new_btr_user (basic_block bb, int insn_luid, rtx_insn *insn)
322 /* This instruction reads target registers. We need
323 to decide whether we can replace all target register
324 uses easily.
326 rtx *usep = find_btr_use (&PATTERN (insn));
327 rtx use;
328 btr_user *user = NULL;
330 if (usep)
332 int unambiguous_single_use;
334 /* We want to ensure that USE is the only use of a target
335 register in INSN, so that we know that to rewrite INSN to use
336 a different target register, all we have to do is replace USE. */
337 unambiguous_single_use = !find_btr_use (&PATTERN (insn), usep);
338 if (!unambiguous_single_use)
339 usep = NULL;
341 use = usep ? *usep : NULL_RTX;
342 user = XOBNEW (&migrate_btrl_obstack, btr_user);
343 user->bb = bb;
344 user->luid = insn_luid;
345 user->insn = insn;
346 user->use = use;
347 user->other_use_this_block = 0;
348 user->next = NULL;
349 user->n_reaching_defs = 0;
350 user->first_reaching_def = -1;
352 if (dump_file)
354 fprintf (dump_file, "Uses target reg: { bb %d, insn %d }",
355 bb->index, INSN_UID (insn));
357 if (user->use)
358 fprintf (dump_file, ": unambiguous use of reg %d\n",
359 REGNO (user->use));
362 return user;
365 /* Write the contents of S to the dump file. */
366 static void
367 dump_hard_reg_set (HARD_REG_SET s)
369 int reg;
370 for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
371 if (TEST_HARD_REG_BIT (s, reg))
372 fprintf (dump_file, " %d", reg);
375 /* Write the set of target regs live in block BB to the dump file. */
376 static void
377 dump_btrs_live (int bb)
379 fprintf (dump_file, "BB%d live:", bb);
380 dump_hard_reg_set (btrs_live[bb]);
381 fprintf (dump_file, "\n");
384 /* REGNO is the number of a branch target register that is being used or
385 set. USERS_THIS_BB is a list of preceding branch target register users;
386 If any of them use the same register, set their other_use_this_block
387 flag. */
388 static void
389 note_other_use_this_block (unsigned int regno, btr_user *users_this_bb)
391 btr_user *user;
393 for (user = users_this_bb; user != NULL; user = user->next)
394 if (user->use && REGNO (user->use) == regno)
395 user->other_use_this_block = 1;
398 struct defs_uses_info {
399 btr_user *users_this_bb;
400 HARD_REG_SET btrs_written_in_block;
401 HARD_REG_SET btrs_live_in_block;
402 sbitmap bb_gen;
403 sbitmap *btr_defset;
406 /* Called via note_stores or directly to register stores into /
407 clobbers of a branch target register DEST that are not recognized as
408 straightforward definitions. DATA points to information about the
409 current basic block that needs updating. */
410 static void
411 note_btr_set (rtx dest, const_rtx set ATTRIBUTE_UNUSED, void *data)
413 defs_uses_info *info = (defs_uses_info *) data;
414 int regno, end_regno;
416 if (!REG_P (dest))
417 return;
418 regno = REGNO (dest);
419 end_regno = END_REGNO (dest);
420 for (; regno < end_regno; regno++)
421 if (TEST_HARD_REG_BIT (all_btrs, regno))
423 note_other_use_this_block (regno, info->users_this_bb);
424 SET_HARD_REG_BIT (info->btrs_written_in_block, regno);
425 SET_HARD_REG_BIT (info->btrs_live_in_block, regno);
426 bitmap_and_compl (info->bb_gen, info->bb_gen,
427 info->btr_defset[regno - first_btr]);
431 static void
432 compute_defs_uses_and_gen (btr_heap_t *all_btr_defs, btr_def **def_array,
433 btr_user **use_array, sbitmap *btr_defset,
434 sbitmap *bb_gen, HARD_REG_SET *btrs_written)
436 /* Scan the code building up the set of all defs and all uses.
437 For each target register, build the set of defs of that register.
438 For each block, calculate the set of target registers
439 written in that block.
440 Also calculate the set of btrs ever live in that block.
442 int i;
443 int insn_luid = 0;
444 btr_def_group *all_btr_def_groups = NULL;
445 defs_uses_info info;
447 bitmap_vector_clear (bb_gen, last_basic_block_for_fn (cfun));
448 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
450 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
451 int reg;
452 btr_def *defs_this_bb = NULL;
453 rtx_insn *insn;
454 rtx_insn *last;
455 int can_throw = 0;
457 info.users_this_bb = NULL;
458 info.bb_gen = bb_gen[i];
459 info.btr_defset = btr_defset;
461 CLEAR_HARD_REG_SET (info.btrs_live_in_block);
462 CLEAR_HARD_REG_SET (info.btrs_written_in_block);
463 for (reg = first_btr; reg <= last_btr; reg++)
464 if (TEST_HARD_REG_BIT (all_btrs, reg)
465 && REGNO_REG_SET_P (df_get_live_in (bb), reg))
466 SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
468 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
469 insn != last;
470 insn = NEXT_INSN (insn), insn_luid++)
472 if (INSN_P (insn))
474 int regno;
475 int insn_uid = INSN_UID (insn);
477 if (insn_sets_btr_p (insn, 0, &regno))
479 btr_def *def = add_btr_def (
480 all_btr_defs, bb, insn_luid, insn, regno,
481 TEST_HARD_REG_BIT (info.btrs_live_in_block, regno),
482 &all_btr_def_groups);
484 def_array[insn_uid] = def;
485 SET_HARD_REG_BIT (info.btrs_written_in_block, regno);
486 SET_HARD_REG_BIT (info.btrs_live_in_block, regno);
487 bitmap_and_compl (bb_gen[i], bb_gen[i],
488 btr_defset[regno - first_btr]);
489 bitmap_set_bit (bb_gen[i], insn_uid);
490 def->next_this_bb = defs_this_bb;
491 defs_this_bb = def;
492 bitmap_set_bit (btr_defset[regno - first_btr], insn_uid);
493 note_other_use_this_block (regno, info.users_this_bb);
495 /* Check for the blockage emitted by expand_nl_goto_receiver. */
496 else if (cfun->has_nonlocal_label
497 && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE)
499 btr_user *user;
501 /* Do the equivalent of calling note_other_use_this_block
502 for every target register. */
503 for (user = info.users_this_bb; user != NULL;
504 user = user->next)
505 if (user->use)
506 user->other_use_this_block = 1;
507 IOR_HARD_REG_SET (info.btrs_written_in_block, all_btrs);
508 IOR_HARD_REG_SET (info.btrs_live_in_block, all_btrs);
509 bitmap_clear (info.bb_gen);
511 else
513 if (find_btr_use (&PATTERN (insn)))
515 btr_user *user = new_btr_user (bb, insn_luid, insn);
517 use_array[insn_uid] = user;
518 if (user->use)
519 SET_HARD_REG_BIT (info.btrs_live_in_block,
520 REGNO (user->use));
521 else
523 int reg;
524 for (reg = first_btr; reg <= last_btr; reg++)
525 if (TEST_HARD_REG_BIT (all_btrs, reg)
526 && refers_to_regno_p (reg, user->insn))
528 note_other_use_this_block (reg,
529 info.users_this_bb);
530 SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
532 note_stores (PATTERN (insn), note_btr_set, &info);
534 user->next = info.users_this_bb;
535 info.users_this_bb = user;
537 if (CALL_P (insn))
539 HARD_REG_SET *clobbered = &call_used_reg_set;
540 HARD_REG_SET call_saved;
541 rtx pat = PATTERN (insn);
542 int i;
544 /* Check for sibcall. */
545 if (GET_CODE (pat) == PARALLEL)
546 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
547 if (ANY_RETURN_P (XVECEXP (pat, 0, i)))
549 COMPL_HARD_REG_SET (call_saved,
550 call_used_reg_set);
551 clobbered = &call_saved;
554 for (regno = first_btr; regno <= last_btr; regno++)
555 if (TEST_HARD_REG_BIT (*clobbered, regno))
556 note_btr_set (regno_reg_rtx[regno], NULL_RTX, &info);
562 COPY_HARD_REG_SET (btrs_live[i], info.btrs_live_in_block);
563 COPY_HARD_REG_SET (btrs_written[i], info.btrs_written_in_block);
565 REG_SET_TO_HARD_REG_SET (btrs_live_at_end[i], df_get_live_out (bb));
566 /* If this block ends in a jump insn, add any uses or even clobbers
567 of branch target registers that it might have. */
568 for (insn = BB_END (bb); insn != BB_HEAD (bb) && ! INSN_P (insn); )
569 insn = PREV_INSN (insn);
570 /* ??? for the fall-through edge, it would make sense to insert the
571 btr set on the edge, but that would require to split the block
572 early on so that we can distinguish between dominance from the fall
573 through edge - which can use the call-clobbered registers - from
574 dominance by the throw edge. */
575 if (can_throw_internal (insn))
577 HARD_REG_SET tmp;
579 COPY_HARD_REG_SET (tmp, call_used_reg_set);
580 AND_HARD_REG_SET (tmp, all_btrs);
581 IOR_HARD_REG_SET (btrs_live_at_end[i], tmp);
582 can_throw = 1;
584 if (can_throw || JUMP_P (insn))
586 int regno;
588 for (regno = first_btr; regno <= last_btr; regno++)
589 if (refers_to_regno_p (regno, insn))
590 SET_HARD_REG_BIT (btrs_live_at_end[i], regno);
593 if (dump_file)
594 dump_btrs_live (i);
598 static void
599 compute_kill (sbitmap *bb_kill, sbitmap *btr_defset,
600 HARD_REG_SET *btrs_written)
602 int i;
603 int regno;
605 /* For each basic block, form the set BB_KILL - the set
606 of definitions that the block kills. */
607 bitmap_vector_clear (bb_kill, last_basic_block_for_fn (cfun));
608 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
610 for (regno = first_btr; regno <= last_btr; regno++)
611 if (TEST_HARD_REG_BIT (all_btrs, regno)
612 && TEST_HARD_REG_BIT (btrs_written[i], regno))
613 bitmap_ior (bb_kill[i], bb_kill[i],
614 btr_defset[regno - first_btr]);
618 static void
619 compute_out (sbitmap *bb_out, sbitmap *bb_gen, sbitmap *bb_kill, int max_uid)
621 /* Perform iterative dataflow:
622 Initially, for all blocks, BB_OUT = BB_GEN.
623 For each block,
624 BB_IN = union over predecessors of BB_OUT(pred)
625 BB_OUT = (BB_IN - BB_KILL) + BB_GEN
626 Iterate until the bb_out sets stop growing. */
627 int i;
628 int changed;
629 auto_sbitmap bb_in (max_uid);
631 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
632 bitmap_copy (bb_out[i], bb_gen[i]);
634 changed = 1;
635 while (changed)
637 changed = 0;
638 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
640 bitmap_union_of_preds (bb_in, bb_out, BASIC_BLOCK_FOR_FN (cfun, i));
641 changed |= bitmap_ior_and_compl (bb_out[i], bb_gen[i],
642 bb_in, bb_kill[i]);
647 static void
648 link_btr_uses (btr_def **def_array, btr_user **use_array, sbitmap *bb_out,
649 sbitmap *btr_defset, int max_uid)
651 int i;
652 auto_sbitmap reaching_defs (max_uid);
654 /* Link uses to the uses lists of all of their reaching defs.
655 Count up the number of reaching defs of each use. */
656 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
658 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
659 rtx_insn *insn;
660 rtx_insn *last;
662 bitmap_union_of_preds (reaching_defs, bb_out, BASIC_BLOCK_FOR_FN (cfun, i));
663 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
664 insn != last;
665 insn = NEXT_INSN (insn))
667 if (INSN_P (insn))
669 int insn_uid = INSN_UID (insn);
671 btr_def *def = def_array[insn_uid];
672 btr_user *user = use_array[insn_uid];
673 if (def != NULL)
675 /* Remove all reaching defs of regno except
676 for this one. */
677 bitmap_and_compl (reaching_defs, reaching_defs,
678 btr_defset[def->btr - first_btr]);
679 bitmap_set_bit (reaching_defs, insn_uid);
682 if (user != NULL)
684 /* Find all the reaching defs for this use. */
685 auto_sbitmap reaching_defs_of_reg (max_uid);
686 unsigned int uid = 0;
687 sbitmap_iterator sbi;
689 if (user->use)
690 bitmap_and (
691 reaching_defs_of_reg,
692 reaching_defs,
693 btr_defset[REGNO (user->use) - first_btr]);
694 else
696 int reg;
698 bitmap_clear (reaching_defs_of_reg);
699 for (reg = first_btr; reg <= last_btr; reg++)
700 if (TEST_HARD_REG_BIT (all_btrs, reg)
701 && refers_to_regno_p (reg, user->insn))
702 bitmap_or_and (reaching_defs_of_reg,
703 reaching_defs_of_reg,
704 reaching_defs,
705 btr_defset[reg - first_btr]);
707 EXECUTE_IF_SET_IN_BITMAP (reaching_defs_of_reg, 0, uid, sbi)
709 btr_def *def = def_array[uid];
711 /* We now know that def reaches user. */
713 if (dump_file)
714 fprintf (dump_file,
715 "Def in insn %d reaches use in insn %d\n",
716 uid, insn_uid);
718 user->n_reaching_defs++;
719 if (!user->use)
720 def->has_ambiguous_use = 1;
721 if (user->first_reaching_def != -1)
722 { /* There is more than one reaching def. This is
723 a rare case, so just give up on this def/use
724 web when it occurs. */
725 def->has_ambiguous_use = 1;
726 def_array[user->first_reaching_def]
727 ->has_ambiguous_use = 1;
728 if (dump_file)
729 fprintf (dump_file,
730 "(use %d has multiple reaching defs)\n",
731 insn_uid);
733 else
734 user->first_reaching_def = uid;
735 if (user->other_use_this_block)
736 def->other_btr_uses_after_use = 1;
737 user->next = def->uses;
738 def->uses = user;
742 if (CALL_P (insn))
744 int regno;
746 for (regno = first_btr; regno <= last_btr; regno++)
747 if (TEST_HARD_REG_BIT (all_btrs, regno)
748 && TEST_HARD_REG_BIT (call_used_reg_set, regno))
749 bitmap_and_compl (reaching_defs, reaching_defs,
750 btr_defset[regno - first_btr]);
757 static void
758 build_btr_def_use_webs (btr_heap_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 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 = 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 btr_heap_t all_btr_defs (LONG_MIN);
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 (!all_btr_defs.empty ())
1422 int min_cost = -all_btr_defs.min_key ();
1423 btr_def *def = all_btr_defs.extract_min ();
1424 if (migrate_btr_def (def, min_cost))
1426 all_btr_defs.insert (-def->cost, 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);
1443 static void
1444 branch_target_load_optimize (bool after_prologue_epilogue_gen)
1446 enum reg_class klass
1447 = (enum reg_class) targetm.branch_target_register_class ();
1448 if (klass != NO_REGS)
1450 /* Initialize issue_rate. */
1451 if (targetm.sched.issue_rate)
1452 issue_rate = targetm.sched.issue_rate ();
1453 else
1454 issue_rate = 1;
1456 if (!after_prologue_epilogue_gen)
1458 /* Build the CFG for migrate_btr_defs. */
1459 #if 1
1460 /* This may or may not be needed, depending on where we
1461 run this phase. */
1462 cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
1463 #endif
1465 df_analyze ();
1468 /* Dominator info is also needed for migrate_btr_def. */
1469 calculate_dominance_info (CDI_DOMINATORS);
1470 migrate_btr_defs (klass,
1471 (targetm.branch_target_register_callee_saved
1472 (after_prologue_epilogue_gen)));
1474 free_dominance_info (CDI_DOMINATORS);
1478 namespace {
1480 const pass_data pass_data_branch_target_load_optimize1 =
1482 RTL_PASS, /* type */
1483 "btl1", /* name */
1484 OPTGROUP_NONE, /* optinfo_flags */
1485 TV_NONE, /* tv_id */
1486 0, /* properties_required */
1487 0, /* properties_provided */
1488 0, /* properties_destroyed */
1489 0, /* todo_flags_start */
1490 0, /* todo_flags_finish */
1493 class pass_branch_target_load_optimize1 : public rtl_opt_pass
1495 public:
1496 pass_branch_target_load_optimize1 (gcc::context *ctxt)
1497 : rtl_opt_pass (pass_data_branch_target_load_optimize1, ctxt)
1500 /* opt_pass methods: */
1501 virtual bool gate (function *) { return flag_branch_target_load_optimize; }
1502 virtual unsigned int execute (function *)
1504 branch_target_load_optimize (epilogue_completed);
1505 return 0;
1508 }; // class pass_branch_target_load_optimize1
1510 } // anon namespace
1512 rtl_opt_pass *
1513 make_pass_branch_target_load_optimize1 (gcc::context *ctxt)
1515 return new pass_branch_target_load_optimize1 (ctxt);
1519 namespace {
1521 const pass_data pass_data_branch_target_load_optimize2 =
1523 RTL_PASS, /* type */
1524 "btl2", /* name */
1525 OPTGROUP_NONE, /* optinfo_flags */
1526 TV_NONE, /* tv_id */
1527 0, /* properties_required */
1528 0, /* properties_provided */
1529 0, /* properties_destroyed */
1530 0, /* todo_flags_start */
1531 0, /* todo_flags_finish */
1534 class pass_branch_target_load_optimize2 : public rtl_opt_pass
1536 public:
1537 pass_branch_target_load_optimize2 (gcc::context *ctxt)
1538 : rtl_opt_pass (pass_data_branch_target_load_optimize2, ctxt)
1541 /* opt_pass methods: */
1542 virtual bool gate (function *)
1544 return (optimize > 0 && flag_branch_target_load_optimize2);
1547 virtual unsigned int execute (function *);
1549 }; // class pass_branch_target_load_optimize2
1551 unsigned int
1552 pass_branch_target_load_optimize2::execute (function *)
1554 static int warned = 0;
1556 /* Leave this a warning for now so that it is possible to experiment
1557 with running this pass twice. In 3.6, we should either make this
1558 an error, or use separate dump files. */
1559 if (flag_branch_target_load_optimize
1560 && flag_branch_target_load_optimize2
1561 && !warned)
1563 warning (0, "branch target register load optimization is not intended "
1564 "to be run twice");
1566 warned = 1;
1569 branch_target_load_optimize (epilogue_completed);
1570 return 0;
1573 } // anon namespace
1575 rtl_opt_pass *
1576 make_pass_branch_target_load_optimize2 (gcc::context *ctxt)
1578 return new pass_branch_target_load_optimize2 (ctxt);