* c-ubsan.c (ubsan_instrument_shift): Use type0.
[official-gcc.git] / gcc / bt-load.c
blobae0db9d778a38a9542a8b8bd7ad1cf04e7baaa2a
2 /* Perform branch target register load optimizations.
3 Copyright (C) 2001-2015 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 "target.h"
29 #include "symtab.h"
30 #include "hashtab.h"
31 #include "hash-set.h"
32 #include "vec.h"
33 #include "machmode.h"
34 #include "input.h"
35 #include "function.h"
36 #include "flags.h"
37 #include "statistics.h"
38 #include "double-int.h"
39 #include "real.h"
40 #include "fixed-value.h"
41 #include "alias.h"
42 #include "wide-int.h"
43 #include "inchash.h"
44 #include "tree.h"
45 #include "insn-config.h"
46 #include "expmed.h"
47 #include "dojump.h"
48 #include "explow.h"
49 #include "calls.h"
50 #include "emit-rtl.h"
51 #include "varasm.h"
52 #include "stmt.h"
53 #include "expr.h"
54 #include "insn-attr.h"
55 #include "except.h"
56 #include "tm_p.h"
57 #include "diagnostic-core.h"
58 #include "tree-pass.h"
59 #include "recog.h"
60 #include "dominance.h"
61 #include "cfg.h"
62 #include "cfgrtl.h"
63 #include "cfganal.h"
64 #include "cfgcleanup.h"
65 #include "predict.h"
66 #include "basic-block.h"
67 #include "df.h"
68 #include "cfgloop.h"
69 #include "rtl-iter.h"
70 #include "fibonacci_heap.h"
72 /* Target register optimizations - these are performed after reload. */
74 typedef struct btr_def_group_s
76 struct btr_def_group_s *next;
77 rtx src;
78 struct btr_def_s *members;
79 } *btr_def_group;
81 typedef struct btr_user_s
83 struct btr_user_s *next;
84 basic_block bb;
85 int luid;
86 rtx_insn *insn;
87 /* If INSN has a single use of a single branch register, then
88 USE points to it within INSN. If there is more than
89 one branch register use, or the use is in some way ambiguous,
90 then USE is NULL. */
91 rtx use;
92 int n_reaching_defs;
93 int first_reaching_def;
94 char other_use_this_block;
95 } *btr_user;
97 /* btr_def structs appear on three lists:
98 1. A list of all btr_def structures (head is
99 ALL_BTR_DEFS, linked by the NEXT field).
100 2. A list of branch reg definitions per basic block (head is
101 BB_BTR_DEFS[i], linked by the NEXT_THIS_BB field).
102 3. A list of all branch reg definitions belonging to the same
103 group (head is in a BTR_DEF_GROUP struct, linked by
104 NEXT_THIS_GROUP field). */
106 typedef struct btr_def_s
108 struct btr_def_s *next_this_bb;
109 struct btr_def_s *next_this_group;
110 basic_block bb;
111 int luid;
112 rtx_insn *insn;
113 int btr;
114 int cost;
115 /* For a branch register setting insn that has a constant
116 source (i.e. a label), group links together all the
117 insns with the same source. For other branch register
118 setting insns, group is NULL. */
119 btr_def_group group;
120 btr_user uses;
121 /* If this def has a reaching use which is not a simple use
122 in a branch instruction, then has_ambiguous_use will be true,
123 and we will not attempt to migrate this definition. */
124 char has_ambiguous_use;
125 /* live_range is an approximation to the true live range for this
126 def/use web, because it records the set of blocks that contain
127 the live range. There could be other live ranges for the same
128 branch register in that set of blocks, either in the block
129 containing the def (before the def), or in a block containing
130 a use (after the use). If there are such other live ranges, then
131 other_btr_uses_before_def or other_btr_uses_after_use must be set true
132 as appropriate. */
133 char other_btr_uses_before_def;
134 char other_btr_uses_after_use;
135 /* We set own_end when we have moved a definition into a dominator.
136 Thus, when a later combination removes this definition again, we know
137 to clear out trs_live_at_end again. */
138 char own_end;
139 bitmap live_range;
140 } *btr_def;
142 typedef fibonacci_heap <long, btr_def_s> btr_heap_t;
143 typedef fibonacci_node <long, btr_def_s> btr_heap_node_t;
145 static int issue_rate;
147 static int basic_block_freq (const_basic_block);
148 static int insn_sets_btr_p (const rtx_insn *, int, int *);
149 static void find_btr_def_group (btr_def_group *, btr_def);
150 static btr_def add_btr_def (btr_heap_t *, basic_block, int, rtx_insn *,
151 unsigned int, int, btr_def_group *);
152 static btr_user new_btr_user (basic_block, int, rtx_insn *);
153 static void dump_hard_reg_set (HARD_REG_SET);
154 static void dump_btrs_live (int);
155 static void note_other_use_this_block (unsigned int, btr_user);
156 static void compute_defs_uses_and_gen (btr_heap_t *, btr_def *,btr_user *,
157 sbitmap *, sbitmap *, HARD_REG_SET *);
158 static void compute_kill (sbitmap *, sbitmap *, HARD_REG_SET *);
159 static void compute_out (sbitmap *bb_out, sbitmap *, sbitmap *, int);
160 static void link_btr_uses (btr_def *, btr_user *, sbitmap *, sbitmap *, int);
161 static void build_btr_def_use_webs (btr_heap_t *);
162 static int block_at_edge_of_live_range_p (int, btr_def);
163 static void clear_btr_from_live_range (btr_def def);
164 static void add_btr_to_live_range (btr_def, int);
165 static void augment_live_range (bitmap, HARD_REG_SET *, basic_block,
166 basic_block, int);
167 static int choose_btr (HARD_REG_SET);
168 static void combine_btr_defs (btr_def, HARD_REG_SET *);
169 static void btr_def_live_range (btr_def, HARD_REG_SET *);
170 static void move_btr_def (basic_block, int, btr_def, bitmap, HARD_REG_SET *);
171 static int migrate_btr_def (btr_def, int);
172 static void migrate_btr_defs (enum reg_class, int);
173 static int can_move_up (const_basic_block, const rtx_insn *, int);
174 static void note_btr_set (rtx, const_rtx, void *);
176 /* The following code performs code motion of target load instructions
177 (instructions that set branch target registers), to move them
178 forward away from the branch instructions and out of loops (or,
179 more generally, from a more frequently executed place to a less
180 frequently executed place).
181 Moving target load instructions further in front of the branch
182 instruction that uses the target register value means that the hardware
183 has a better chance of preloading the instructions at the branch
184 target by the time the branch is reached. This avoids bubbles
185 when a taken branch needs to flush out the pipeline.
186 Moving target load instructions out of loops means they are executed
187 less frequently. */
189 /* An obstack to hold the def-use web data structures built up for
190 migrating branch target load instructions. */
191 static struct obstack migrate_btrl_obstack;
193 /* Array indexed by basic block number, giving the set of registers
194 live in that block. */
195 static HARD_REG_SET *btrs_live;
197 /* Array indexed by basic block number, giving the set of registers live at
198 the end of that block, including any uses by a final jump insn, if any. */
199 static HARD_REG_SET *btrs_live_at_end;
201 /* Set of all target registers that we are willing to allocate. */
202 static HARD_REG_SET all_btrs;
204 /* Provide lower and upper bounds for target register numbers, so that
205 we don't need to search through all the hard registers all the time. */
206 static int first_btr, last_btr;
210 /* Return an estimate of the frequency of execution of block bb. */
211 static int
212 basic_block_freq (const_basic_block bb)
214 return bb->frequency;
217 /* If X references (sets or reads) any branch target register, return one
218 such register. If EXCLUDEP is set, disregard any references within
219 that location. */
220 static rtx *
221 find_btr_use (rtx x, rtx *excludep = 0)
223 subrtx_ptr_iterator::array_type array;
224 FOR_EACH_SUBRTX_PTR (iter, array, &x, NONCONST)
226 rtx *loc = *iter;
227 if (loc == excludep)
228 iter.skip_subrtxes ();
229 else
231 const_rtx x = *loc;
232 if (REG_P (x)
233 && overlaps_hard_reg_set_p (all_btrs, GET_MODE (x), REGNO (x)))
234 return loc;
237 return 0;
240 /* Return true if insn is an instruction that sets a target register.
241 if CHECK_CONST is true, only return true if the source is constant.
242 If such a set is found and REGNO is nonzero, assign the register number
243 of the destination register to *REGNO. */
244 static int
245 insn_sets_btr_p (const rtx_insn *insn, int check_const, int *regno)
247 rtx set;
249 if (NONJUMP_INSN_P (insn)
250 && (set = single_set (insn)))
252 rtx dest = SET_DEST (set);
253 rtx src = SET_SRC (set);
255 if (GET_CODE (dest) == SUBREG)
256 dest = XEXP (dest, 0);
258 if (REG_P (dest)
259 && TEST_HARD_REG_BIT (all_btrs, REGNO (dest)))
261 gcc_assert (!find_btr_use (src));
263 if (!check_const || CONSTANT_P (src))
265 if (regno)
266 *regno = REGNO (dest);
267 return 1;
271 return 0;
274 /* Find the group that the target register definition DEF belongs
275 to in the list starting with *ALL_BTR_DEF_GROUPS. If no such
276 group exists, create one. Add def to the group. */
277 static void
278 find_btr_def_group (btr_def_group *all_btr_def_groups, btr_def def)
280 if (insn_sets_btr_p (def->insn, 1, NULL))
282 btr_def_group this_group;
283 rtx def_src = SET_SRC (single_set (def->insn));
285 /* ?? This linear search is an efficiency concern, particularly
286 as the search will almost always fail to find a match. */
287 for (this_group = *all_btr_def_groups;
288 this_group != NULL;
289 this_group = this_group->next)
290 if (rtx_equal_p (def_src, this_group->src))
291 break;
293 if (!this_group)
295 this_group = XOBNEW (&migrate_btrl_obstack, struct btr_def_group_s);
296 this_group->src = def_src;
297 this_group->members = NULL;
298 this_group->next = *all_btr_def_groups;
299 *all_btr_def_groups = this_group;
301 def->group = this_group;
302 def->next_this_group = this_group->members;
303 this_group->members = def;
305 else
306 def->group = NULL;
309 /* Create a new target register definition structure, for a definition in
310 block BB, instruction INSN, and insert it into ALL_BTR_DEFS. Return
311 the new definition. */
312 static btr_def
313 add_btr_def (btr_heap_t *all_btr_defs, basic_block bb, int insn_luid,
314 rtx_insn *insn,
315 unsigned int dest_reg, int other_btr_uses_before_def,
316 btr_def_group *all_btr_def_groups)
318 btr_def this_def = XOBNEW (&migrate_btrl_obstack, struct btr_def_s);
319 this_def->bb = bb;
320 this_def->luid = insn_luid;
321 this_def->insn = insn;
322 this_def->btr = dest_reg;
323 this_def->cost = basic_block_freq (bb);
324 this_def->has_ambiguous_use = 0;
325 this_def->other_btr_uses_before_def = other_btr_uses_before_def;
326 this_def->other_btr_uses_after_use = 0;
327 this_def->next_this_bb = NULL;
328 this_def->next_this_group = NULL;
329 this_def->uses = NULL;
330 this_def->live_range = NULL;
331 find_btr_def_group (all_btr_def_groups, this_def);
333 all_btr_defs->insert (-this_def->cost, this_def);
335 if (dump_file)
336 fprintf (dump_file,
337 "Found target reg definition: sets %u { bb %d, insn %d }%s priority %d\n",
338 dest_reg, bb->index, INSN_UID (insn),
339 (this_def->group ? "" : ":not const"), this_def->cost);
341 return this_def;
344 /* Create a new target register user structure, for a use in block BB,
345 instruction INSN. Return the new user. */
346 static btr_user
347 new_btr_user (basic_block bb, int insn_luid, rtx_insn *insn)
349 /* This instruction reads target registers. We need
350 to decide whether we can replace all target register
351 uses easily.
353 rtx *usep = find_btr_use (PATTERN (insn));
354 rtx use;
355 btr_user user = NULL;
357 if (usep)
359 int unambiguous_single_use;
361 /* We want to ensure that USE is the only use of a target
362 register in INSN, so that we know that to rewrite INSN to use
363 a different target register, all we have to do is replace USE. */
364 unambiguous_single_use = !find_btr_use (PATTERN (insn), usep);
365 if (!unambiguous_single_use)
366 usep = NULL;
368 use = usep ? *usep : NULL_RTX;
369 user = XOBNEW (&migrate_btrl_obstack, struct btr_user_s);
370 user->bb = bb;
371 user->luid = insn_luid;
372 user->insn = insn;
373 user->use = use;
374 user->other_use_this_block = 0;
375 user->next = NULL;
376 user->n_reaching_defs = 0;
377 user->first_reaching_def = -1;
379 if (dump_file)
381 fprintf (dump_file, "Uses target reg: { bb %d, insn %d }",
382 bb->index, INSN_UID (insn));
384 if (user->use)
385 fprintf (dump_file, ": unambiguous use of reg %d\n",
386 REGNO (user->use));
389 return user;
392 /* Write the contents of S to the dump file. */
393 static void
394 dump_hard_reg_set (HARD_REG_SET s)
396 int reg;
397 for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
398 if (TEST_HARD_REG_BIT (s, reg))
399 fprintf (dump_file, " %d", reg);
402 /* Write the set of target regs live in block BB to the dump file. */
403 static void
404 dump_btrs_live (int bb)
406 fprintf (dump_file, "BB%d live:", bb);
407 dump_hard_reg_set (btrs_live[bb]);
408 fprintf (dump_file, "\n");
411 /* REGNO is the number of a branch target register that is being used or
412 set. USERS_THIS_BB is a list of preceding branch target register users;
413 If any of them use the same register, set their other_use_this_block
414 flag. */
415 static void
416 note_other_use_this_block (unsigned int regno, btr_user users_this_bb)
418 btr_user user;
420 for (user = users_this_bb; user != NULL; user = user->next)
421 if (user->use && REGNO (user->use) == regno)
422 user->other_use_this_block = 1;
425 typedef struct {
426 btr_user users_this_bb;
427 HARD_REG_SET btrs_written_in_block;
428 HARD_REG_SET btrs_live_in_block;
429 sbitmap bb_gen;
430 sbitmap *btr_defset;
431 } defs_uses_info;
433 /* Called via note_stores or directly to register stores into /
434 clobbers of a branch target register DEST that are not recognized as
435 straightforward definitions. DATA points to information about the
436 current basic block that needs updating. */
437 static void
438 note_btr_set (rtx dest, const_rtx set ATTRIBUTE_UNUSED, void *data)
440 defs_uses_info *info = (defs_uses_info *) data;
441 int regno, end_regno;
443 if (!REG_P (dest))
444 return;
445 regno = REGNO (dest);
446 end_regno = END_REGNO (dest);
447 for (; regno < end_regno; regno++)
448 if (TEST_HARD_REG_BIT (all_btrs, regno))
450 note_other_use_this_block (regno, info->users_this_bb);
451 SET_HARD_REG_BIT (info->btrs_written_in_block, regno);
452 SET_HARD_REG_BIT (info->btrs_live_in_block, regno);
453 bitmap_and_compl (info->bb_gen, info->bb_gen,
454 info->btr_defset[regno - first_btr]);
458 static void
459 compute_defs_uses_and_gen (btr_heap_t *all_btr_defs, btr_def *def_array,
460 btr_user *use_array, sbitmap *btr_defset,
461 sbitmap *bb_gen, HARD_REG_SET *btrs_written)
463 /* Scan the code building up the set of all defs and all uses.
464 For each target register, build the set of defs of that register.
465 For each block, calculate the set of target registers
466 written in that block.
467 Also calculate the set of btrs ever live in that block.
469 int i;
470 int insn_luid = 0;
471 btr_def_group all_btr_def_groups = NULL;
472 defs_uses_info info;
474 bitmap_vector_clear (bb_gen, last_basic_block_for_fn (cfun));
475 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
477 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
478 int reg;
479 btr_def defs_this_bb = NULL;
480 rtx_insn *insn;
481 rtx_insn *last;
482 int can_throw = 0;
484 info.users_this_bb = NULL;
485 info.bb_gen = bb_gen[i];
486 info.btr_defset = btr_defset;
488 CLEAR_HARD_REG_SET (info.btrs_live_in_block);
489 CLEAR_HARD_REG_SET (info.btrs_written_in_block);
490 for (reg = first_btr; reg <= last_btr; reg++)
491 if (TEST_HARD_REG_BIT (all_btrs, reg)
492 && REGNO_REG_SET_P (df_get_live_in (bb), reg))
493 SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
495 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
496 insn != last;
497 insn = NEXT_INSN (insn), insn_luid++)
499 if (INSN_P (insn))
501 int regno;
502 int insn_uid = INSN_UID (insn);
504 if (insn_sets_btr_p (insn, 0, &regno))
506 btr_def def = add_btr_def (
507 all_btr_defs, bb, insn_luid, insn, regno,
508 TEST_HARD_REG_BIT (info.btrs_live_in_block, regno),
509 &all_btr_def_groups);
511 def_array[insn_uid] = def;
512 SET_HARD_REG_BIT (info.btrs_written_in_block, regno);
513 SET_HARD_REG_BIT (info.btrs_live_in_block, regno);
514 bitmap_and_compl (bb_gen[i], bb_gen[i],
515 btr_defset[regno - first_btr]);
516 bitmap_set_bit (bb_gen[i], insn_uid);
517 def->next_this_bb = defs_this_bb;
518 defs_this_bb = def;
519 bitmap_set_bit (btr_defset[regno - first_btr], insn_uid);
520 note_other_use_this_block (regno, info.users_this_bb);
522 /* Check for the blockage emitted by expand_nl_goto_receiver. */
523 else if (cfun->has_nonlocal_label
524 && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE)
526 btr_user user;
528 /* Do the equivalent of calling note_other_use_this_block
529 for every target register. */
530 for (user = info.users_this_bb; user != NULL;
531 user = user->next)
532 if (user->use)
533 user->other_use_this_block = 1;
534 IOR_HARD_REG_SET (info.btrs_written_in_block, all_btrs);
535 IOR_HARD_REG_SET (info.btrs_live_in_block, all_btrs);
536 bitmap_clear (info.bb_gen);
538 else
540 if (find_btr_use (PATTERN (insn)))
542 btr_user user = new_btr_user (bb, insn_luid, insn);
544 use_array[insn_uid] = user;
545 if (user->use)
546 SET_HARD_REG_BIT (info.btrs_live_in_block,
547 REGNO (user->use));
548 else
550 int reg;
551 for (reg = first_btr; reg <= last_btr; reg++)
552 if (TEST_HARD_REG_BIT (all_btrs, reg)
553 && refers_to_regno_p (reg, user->insn))
555 note_other_use_this_block (reg,
556 info.users_this_bb);
557 SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
559 note_stores (PATTERN (insn), note_btr_set, &info);
561 user->next = info.users_this_bb;
562 info.users_this_bb = user;
564 if (CALL_P (insn))
566 HARD_REG_SET *clobbered = &call_used_reg_set;
567 HARD_REG_SET call_saved;
568 rtx pat = PATTERN (insn);
569 int i;
571 /* Check for sibcall. */
572 if (GET_CODE (pat) == PARALLEL)
573 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
574 if (ANY_RETURN_P (XVECEXP (pat, 0, i)))
576 COMPL_HARD_REG_SET (call_saved,
577 call_used_reg_set);
578 clobbered = &call_saved;
581 for (regno = first_btr; regno <= last_btr; regno++)
582 if (TEST_HARD_REG_BIT (*clobbered, regno))
583 note_btr_set (regno_reg_rtx[regno], NULL_RTX, &info);
589 COPY_HARD_REG_SET (btrs_live[i], info.btrs_live_in_block);
590 COPY_HARD_REG_SET (btrs_written[i], info.btrs_written_in_block);
592 REG_SET_TO_HARD_REG_SET (btrs_live_at_end[i], df_get_live_out (bb));
593 /* If this block ends in a jump insn, add any uses or even clobbers
594 of branch target registers that it might have. */
595 for (insn = BB_END (bb); insn != BB_HEAD (bb) && ! INSN_P (insn); )
596 insn = PREV_INSN (insn);
597 /* ??? for the fall-through edge, it would make sense to insert the
598 btr set on the edge, but that would require to split the block
599 early on so that we can distinguish between dominance from the fall
600 through edge - which can use the call-clobbered registers - from
601 dominance by the throw edge. */
602 if (can_throw_internal (insn))
604 HARD_REG_SET tmp;
606 COPY_HARD_REG_SET (tmp, call_used_reg_set);
607 AND_HARD_REG_SET (tmp, all_btrs);
608 IOR_HARD_REG_SET (btrs_live_at_end[i], tmp);
609 can_throw = 1;
611 if (can_throw || JUMP_P (insn))
613 int regno;
615 for (regno = first_btr; regno <= last_btr; regno++)
616 if (refers_to_regno_p (regno, insn))
617 SET_HARD_REG_BIT (btrs_live_at_end[i], regno);
620 if (dump_file)
621 dump_btrs_live (i);
625 static void
626 compute_kill (sbitmap *bb_kill, sbitmap *btr_defset,
627 HARD_REG_SET *btrs_written)
629 int i;
630 int regno;
632 /* For each basic block, form the set BB_KILL - the set
633 of definitions that the block kills. */
634 bitmap_vector_clear (bb_kill, last_basic_block_for_fn (cfun));
635 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
637 for (regno = first_btr; regno <= last_btr; regno++)
638 if (TEST_HARD_REG_BIT (all_btrs, regno)
639 && TEST_HARD_REG_BIT (btrs_written[i], regno))
640 bitmap_ior (bb_kill[i], bb_kill[i],
641 btr_defset[regno - first_btr]);
645 static void
646 compute_out (sbitmap *bb_out, sbitmap *bb_gen, sbitmap *bb_kill, int max_uid)
648 /* Perform iterative dataflow:
649 Initially, for all blocks, BB_OUT = BB_GEN.
650 For each block,
651 BB_IN = union over predecessors of BB_OUT(pred)
652 BB_OUT = (BB_IN - BB_KILL) + BB_GEN
653 Iterate until the bb_out sets stop growing. */
654 int i;
655 int changed;
656 sbitmap bb_in = sbitmap_alloc (max_uid);
658 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
659 bitmap_copy (bb_out[i], bb_gen[i]);
661 changed = 1;
662 while (changed)
664 changed = 0;
665 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
667 bitmap_union_of_preds (bb_in, bb_out, BASIC_BLOCK_FOR_FN (cfun, i));
668 changed |= bitmap_ior_and_compl (bb_out[i], bb_gen[i],
669 bb_in, bb_kill[i]);
672 sbitmap_free (bb_in);
675 static void
676 link_btr_uses (btr_def *def_array, btr_user *use_array, sbitmap *bb_out,
677 sbitmap *btr_defset, int max_uid)
679 int i;
680 sbitmap reaching_defs = sbitmap_alloc (max_uid);
682 /* Link uses to the uses lists of all of their reaching defs.
683 Count up the number of reaching defs of each use. */
684 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
686 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
687 rtx_insn *insn;
688 rtx_insn *last;
690 bitmap_union_of_preds (reaching_defs, bb_out, BASIC_BLOCK_FOR_FN (cfun, i));
691 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
692 insn != last;
693 insn = NEXT_INSN (insn))
695 if (INSN_P (insn))
697 int insn_uid = INSN_UID (insn);
699 btr_def def = def_array[insn_uid];
700 btr_user user = use_array[insn_uid];
701 if (def != NULL)
703 /* Remove all reaching defs of regno except
704 for this one. */
705 bitmap_and_compl (reaching_defs, reaching_defs,
706 btr_defset[def->btr - first_btr]);
707 bitmap_set_bit (reaching_defs, insn_uid);
710 if (user != NULL)
712 /* Find all the reaching defs for this use. */
713 sbitmap reaching_defs_of_reg = sbitmap_alloc (max_uid);
714 unsigned int uid = 0;
715 sbitmap_iterator sbi;
717 if (user->use)
718 bitmap_and (
719 reaching_defs_of_reg,
720 reaching_defs,
721 btr_defset[REGNO (user->use) - first_btr]);
722 else
724 int reg;
726 bitmap_clear (reaching_defs_of_reg);
727 for (reg = first_btr; reg <= last_btr; reg++)
728 if (TEST_HARD_REG_BIT (all_btrs, reg)
729 && refers_to_regno_p (reg, user->insn))
730 bitmap_or_and (reaching_defs_of_reg,
731 reaching_defs_of_reg,
732 reaching_defs,
733 btr_defset[reg - first_btr]);
735 EXECUTE_IF_SET_IN_BITMAP (reaching_defs_of_reg, 0, uid, sbi)
737 btr_def def = def_array[uid];
739 /* We now know that def reaches user. */
741 if (dump_file)
742 fprintf (dump_file,
743 "Def in insn %d reaches use in insn %d\n",
744 uid, insn_uid);
746 user->n_reaching_defs++;
747 if (!user->use)
748 def->has_ambiguous_use = 1;
749 if (user->first_reaching_def != -1)
750 { /* There is more than one reaching def. This is
751 a rare case, so just give up on this def/use
752 web when it occurs. */
753 def->has_ambiguous_use = 1;
754 def_array[user->first_reaching_def]
755 ->has_ambiguous_use = 1;
756 if (dump_file)
757 fprintf (dump_file,
758 "(use %d has multiple reaching defs)\n",
759 insn_uid);
761 else
762 user->first_reaching_def = uid;
763 if (user->other_use_this_block)
764 def->other_btr_uses_after_use = 1;
765 user->next = def->uses;
766 def->uses = user;
768 sbitmap_free (reaching_defs_of_reg);
771 if (CALL_P (insn))
773 int regno;
775 for (regno = first_btr; regno <= last_btr; regno++)
776 if (TEST_HARD_REG_BIT (all_btrs, regno)
777 && TEST_HARD_REG_BIT (call_used_reg_set, regno))
778 bitmap_and_compl (reaching_defs, reaching_defs,
779 btr_defset[regno - first_btr]);
784 sbitmap_free (reaching_defs);
787 static void
788 build_btr_def_use_webs (btr_heap_t *all_btr_defs)
790 const int max_uid = get_max_uid ();
791 btr_def *def_array = XCNEWVEC (btr_def, max_uid);
792 btr_user *use_array = XCNEWVEC (btr_user, max_uid);
793 sbitmap *btr_defset = sbitmap_vector_alloc (
794 (last_btr - first_btr) + 1, max_uid);
795 sbitmap *bb_gen = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
796 max_uid);
797 HARD_REG_SET *btrs_written = XCNEWVEC (HARD_REG_SET,
798 last_basic_block_for_fn (cfun));
799 sbitmap *bb_kill;
800 sbitmap *bb_out;
802 bitmap_vector_clear (btr_defset, (last_btr - first_btr) + 1);
804 compute_defs_uses_and_gen (all_btr_defs, def_array, use_array, btr_defset,
805 bb_gen, btrs_written);
807 bb_kill = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), max_uid);
808 compute_kill (bb_kill, btr_defset, btrs_written);
809 free (btrs_written);
811 bb_out = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), max_uid);
812 compute_out (bb_out, bb_gen, bb_kill, max_uid);
814 sbitmap_vector_free (bb_gen);
815 sbitmap_vector_free (bb_kill);
817 link_btr_uses (def_array, use_array, bb_out, btr_defset, max_uid);
819 sbitmap_vector_free (bb_out);
820 sbitmap_vector_free (btr_defset);
821 free (use_array);
822 free (def_array);
825 /* Return true if basic block BB contains the start or end of the
826 live range of the definition DEF, AND there are other live
827 ranges of the same target register that include BB. */
828 static int
829 block_at_edge_of_live_range_p (int bb, btr_def def)
831 if (def->other_btr_uses_before_def
832 && BASIC_BLOCK_FOR_FN (cfun, bb) == def->bb)
833 return 1;
834 else if (def->other_btr_uses_after_use)
836 btr_user user;
837 for (user = def->uses; user != NULL; user = user->next)
838 if (BASIC_BLOCK_FOR_FN (cfun, bb) == user->bb)
839 return 1;
841 return 0;
844 /* We are removing the def/use web DEF. The target register
845 used in this web is therefore no longer live in the live range
846 of this web, so remove it from the live set of all basic blocks
847 in the live range of the web.
848 Blocks at the boundary of the live range may contain other live
849 ranges for the same target register, so we have to be careful
850 to remove the target register from the live set of these blocks
851 only if they do not contain other live ranges for the same register. */
852 static void
853 clear_btr_from_live_range (btr_def def)
855 unsigned bb;
856 bitmap_iterator bi;
858 EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
860 if ((!def->other_btr_uses_before_def
861 && !def->other_btr_uses_after_use)
862 || !block_at_edge_of_live_range_p (bb, def))
864 CLEAR_HARD_REG_BIT (btrs_live[bb], def->btr);
865 CLEAR_HARD_REG_BIT (btrs_live_at_end[bb], def->btr);
866 if (dump_file)
867 dump_btrs_live (bb);
870 if (def->own_end)
871 CLEAR_HARD_REG_BIT (btrs_live_at_end[def->bb->index], def->btr);
875 /* We are adding the def/use web DEF. Add the target register used
876 in this web to the live set of all of the basic blocks that contain
877 the live range of the web.
878 If OWN_END is set, also show that the register is live from our
879 definitions at the end of the basic block where it is defined. */
880 static void
881 add_btr_to_live_range (btr_def def, int own_end)
883 unsigned bb;
884 bitmap_iterator bi;
886 EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
888 SET_HARD_REG_BIT (btrs_live[bb], def->btr);
889 SET_HARD_REG_BIT (btrs_live_at_end[bb], def->btr);
890 if (dump_file)
891 dump_btrs_live (bb);
893 if (own_end)
895 SET_HARD_REG_BIT (btrs_live_at_end[def->bb->index], def->btr);
896 def->own_end = 1;
900 /* Update a live range to contain the basic block NEW_BLOCK, and all
901 blocks on paths between the existing live range and NEW_BLOCK.
902 HEAD is a block contained in the existing live range that dominates
903 all other blocks in the existing live range.
904 Also add to the set BTRS_LIVE_IN_RANGE all target registers that
905 are live in the blocks that we add to the live range.
906 If FULL_RANGE is set, include the full live range of NEW_BB;
907 otherwise, if NEW_BB dominates HEAD_BB, only add registers that
908 are life at the end of NEW_BB for NEW_BB itself.
909 It is a precondition that either NEW_BLOCK dominates HEAD,or
910 HEAD dom NEW_BLOCK. This is used to speed up the
911 implementation of this function. */
912 static void
913 augment_live_range (bitmap live_range, HARD_REG_SET *btrs_live_in_range,
914 basic_block head_bb, basic_block new_bb, int full_range)
916 basic_block *worklist, *tos;
918 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
920 if (dominated_by_p (CDI_DOMINATORS, new_bb, head_bb))
922 if (new_bb == head_bb)
924 if (full_range)
925 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[new_bb->index]);
926 free (tos);
927 return;
929 *tos++ = new_bb;
931 else
933 edge e;
934 edge_iterator ei;
935 int new_block = new_bb->index;
937 gcc_assert (dominated_by_p (CDI_DOMINATORS, head_bb, new_bb));
939 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[head_bb->index]);
940 bitmap_set_bit (live_range, new_block);
941 /* A previous btr migration could have caused a register to be
942 live just at the end of new_block which we need in full, so
943 use trs_live_at_end even if full_range is set. */
944 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live_at_end[new_block]);
945 if (full_range)
946 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[new_block]);
947 if (dump_file)
949 fprintf (dump_file,
950 "Adding end of block %d and rest of %d to live range\n",
951 new_block, head_bb->index);
952 fprintf (dump_file,"Now live btrs are ");
953 dump_hard_reg_set (*btrs_live_in_range);
954 fprintf (dump_file, "\n");
956 FOR_EACH_EDGE (e, ei, head_bb->preds)
957 *tos++ = e->src;
960 while (tos != worklist)
962 basic_block bb = *--tos;
963 if (!bitmap_bit_p (live_range, bb->index))
965 edge e;
966 edge_iterator ei;
968 bitmap_set_bit (live_range, bb->index);
969 IOR_HARD_REG_SET (*btrs_live_in_range,
970 btrs_live[bb->index]);
971 /* A previous btr migration could have caused a register to be
972 live just at the end of a block which we need in full. */
973 IOR_HARD_REG_SET (*btrs_live_in_range,
974 btrs_live_at_end[bb->index]);
975 if (dump_file)
977 fprintf (dump_file,
978 "Adding block %d to live range\n", bb->index);
979 fprintf (dump_file,"Now live btrs are ");
980 dump_hard_reg_set (*btrs_live_in_range);
981 fprintf (dump_file, "\n");
984 FOR_EACH_EDGE (e, ei, bb->preds)
986 basic_block pred = e->src;
987 if (!bitmap_bit_p (live_range, pred->index))
988 *tos++ = pred;
993 free (worklist);
996 /* Return the most desirable target register that is not in
997 the set USED_BTRS. */
998 static int
999 choose_btr (HARD_REG_SET used_btrs)
1001 int i;
1003 if (!hard_reg_set_subset_p (all_btrs, used_btrs))
1004 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1006 #ifdef REG_ALLOC_ORDER
1007 int regno = reg_alloc_order[i];
1008 #else
1009 int regno = i;
1010 #endif
1011 if (TEST_HARD_REG_BIT (all_btrs, regno)
1012 && !TEST_HARD_REG_BIT (used_btrs, regno))
1013 return regno;
1015 return -1;
1018 /* Calculate the set of basic blocks that contain the live range of
1019 the def/use web DEF.
1020 Also calculate the set of target registers that are live at time
1021 in this live range, but ignore the live range represented by DEF
1022 when calculating this set. */
1023 static void
1024 btr_def_live_range (btr_def def, HARD_REG_SET *btrs_live_in_range)
1026 if (!def->live_range)
1028 btr_user user;
1030 def->live_range = BITMAP_ALLOC (NULL);
1032 bitmap_set_bit (def->live_range, def->bb->index);
1033 COPY_HARD_REG_SET (*btrs_live_in_range,
1034 (flag_btr_bb_exclusive
1035 ? btrs_live : btrs_live_at_end)[def->bb->index]);
1037 for (user = def->uses; user != NULL; user = user->next)
1038 augment_live_range (def->live_range, btrs_live_in_range,
1039 def->bb, user->bb,
1040 (flag_btr_bb_exclusive
1041 || user->insn != BB_END (def->bb)
1042 || !JUMP_P (user->insn)));
1044 else
1046 /* def->live_range is accurate, but we need to recompute
1047 the set of target registers live over it, because migration
1048 of other PT instructions may have affected it.
1050 unsigned bb;
1051 unsigned def_bb = flag_btr_bb_exclusive ? -1 : def->bb->index;
1052 bitmap_iterator bi;
1054 CLEAR_HARD_REG_SET (*btrs_live_in_range);
1055 EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
1057 IOR_HARD_REG_SET (*btrs_live_in_range,
1058 (def_bb == bb
1059 ? btrs_live_at_end : btrs_live) [bb]);
1062 if (!def->other_btr_uses_before_def &&
1063 !def->other_btr_uses_after_use)
1064 CLEAR_HARD_REG_BIT (*btrs_live_in_range, def->btr);
1067 /* Merge into the def/use web DEF any other def/use webs in the same
1068 group that are dominated by DEF, provided that there is a target
1069 register available to allocate to the merged web. */
1070 static void
1071 combine_btr_defs (btr_def def, HARD_REG_SET *btrs_live_in_range)
1073 btr_def other_def;
1075 for (other_def = def->group->members;
1076 other_def != NULL;
1077 other_def = other_def->next_this_group)
1079 if (other_def != def
1080 && other_def->uses != NULL
1081 && ! other_def->has_ambiguous_use
1082 && dominated_by_p (CDI_DOMINATORS, other_def->bb, def->bb))
1084 /* def->bb dominates the other def, so def and other_def could
1085 be combined. */
1086 /* Merge their live ranges, and get the set of
1087 target registers live over the merged range. */
1088 int btr;
1089 HARD_REG_SET combined_btrs_live;
1090 bitmap combined_live_range = BITMAP_ALLOC (NULL);
1091 btr_user user;
1093 if (other_def->live_range == NULL)
1095 HARD_REG_SET dummy_btrs_live_in_range;
1096 btr_def_live_range (other_def, &dummy_btrs_live_in_range);
1098 COPY_HARD_REG_SET (combined_btrs_live, *btrs_live_in_range);
1099 bitmap_copy (combined_live_range, def->live_range);
1101 for (user = other_def->uses; user != NULL; user = user->next)
1102 augment_live_range (combined_live_range, &combined_btrs_live,
1103 def->bb, user->bb,
1104 (flag_btr_bb_exclusive
1105 || user->insn != BB_END (def->bb)
1106 || !JUMP_P (user->insn)));
1108 btr = choose_btr (combined_btrs_live);
1109 if (btr != -1)
1111 /* We can combine them. */
1112 if (dump_file)
1113 fprintf (dump_file,
1114 "Combining def in insn %d with def in insn %d\n",
1115 INSN_UID (other_def->insn), INSN_UID (def->insn));
1117 def->btr = btr;
1118 user = other_def->uses;
1119 while (user != NULL)
1121 btr_user next = user->next;
1123 user->next = def->uses;
1124 def->uses = user;
1125 user = next;
1127 /* Combining def/use webs can make target registers live
1128 after uses where they previously were not. This means
1129 some REG_DEAD notes may no longer be correct. We could
1130 be more precise about this if we looked at the combined
1131 live range, but here I just delete any REG_DEAD notes
1132 in case they are no longer correct. */
1133 for (user = def->uses; user != NULL; user = user->next)
1134 remove_note (user->insn,
1135 find_regno_note (user->insn, REG_DEAD,
1136 REGNO (user->use)));
1137 clear_btr_from_live_range (other_def);
1138 other_def->uses = NULL;
1139 bitmap_copy (def->live_range, combined_live_range);
1140 if (other_def->btr == btr && other_def->other_btr_uses_after_use)
1141 def->other_btr_uses_after_use = 1;
1142 COPY_HARD_REG_SET (*btrs_live_in_range, combined_btrs_live);
1144 /* Delete the old target register initialization. */
1145 delete_insn (other_def->insn);
1148 BITMAP_FREE (combined_live_range);
1153 /* Move the definition DEF from its current position to basic
1154 block NEW_DEF_BB, and modify it to use branch target register BTR.
1155 Delete the old defining insn, and insert a new one in NEW_DEF_BB.
1156 Update all reaching uses of DEF in the RTL to use BTR.
1157 If this new position means that other defs in the
1158 same group can be combined with DEF then combine them. */
1159 static void
1160 move_btr_def (basic_block new_def_bb, int btr, btr_def def, bitmap live_range,
1161 HARD_REG_SET *btrs_live_in_range)
1163 /* We can move the instruction.
1164 Set a target register in block NEW_DEF_BB to the value
1165 needed for this target register definition.
1166 Replace all uses of the old target register definition by
1167 uses of the new definition. Delete the old definition. */
1168 basic_block b = new_def_bb;
1169 rtx_insn *insp = BB_HEAD (b);
1170 rtx_insn *old_insn = def->insn;
1171 rtx src;
1172 rtx btr_rtx;
1173 rtx_insn *new_insn;
1174 machine_mode btr_mode;
1175 btr_user user;
1176 rtx set;
1178 if (dump_file)
1179 fprintf(dump_file, "migrating to basic block %d, using reg %d\n",
1180 new_def_bb->index, btr);
1182 clear_btr_from_live_range (def);
1183 def->btr = btr;
1184 def->bb = new_def_bb;
1185 def->luid = 0;
1186 def->cost = basic_block_freq (new_def_bb);
1187 bitmap_copy (def->live_range, live_range);
1188 combine_btr_defs (def, btrs_live_in_range);
1189 btr = def->btr;
1190 def->other_btr_uses_before_def
1191 = TEST_HARD_REG_BIT (btrs_live[b->index], btr) ? 1 : 0;
1192 add_btr_to_live_range (def, 1);
1193 if (LABEL_P (insp))
1194 insp = NEXT_INSN (insp);
1195 /* N.B.: insp is expected to be NOTE_INSN_BASIC_BLOCK now. Some
1196 optimizations can result in insp being both first and last insn of
1197 its basic block. */
1198 /* ?? some assertions to check that insp is sensible? */
1200 if (def->other_btr_uses_before_def)
1202 insp = BB_END (b);
1203 for (insp = BB_END (b); ! INSN_P (insp); insp = PREV_INSN (insp))
1204 gcc_assert (insp != BB_HEAD (b));
1206 if (JUMP_P (insp) || can_throw_internal (insp))
1207 insp = PREV_INSN (insp);
1210 set = single_set (old_insn);
1211 src = SET_SRC (set);
1212 btr_mode = GET_MODE (SET_DEST (set));
1213 btr_rtx = gen_rtx_REG (btr_mode, btr);
1215 new_insn = gen_move_insn (btr_rtx, src);
1217 /* Insert target register initialization at head of basic block. */
1218 def->insn = emit_insn_after (new_insn, insp);
1220 df_set_regs_ever_live (btr, true);
1222 if (dump_file)
1223 fprintf (dump_file, "New pt is insn %d, inserted after insn %d\n",
1224 INSN_UID (def->insn), INSN_UID (insp));
1226 /* Delete the old target register initialization. */
1227 delete_insn (old_insn);
1229 /* Replace each use of the old target register by a use of the new target
1230 register. */
1231 for (user = def->uses; user != NULL; user = user->next)
1233 /* Some extra work here to ensure consistent modes, because
1234 it seems that a target register REG rtx can be given a different
1235 mode depending on the context (surely that should not be
1236 the case?). */
1237 rtx replacement_rtx;
1238 if (GET_MODE (user->use) == GET_MODE (btr_rtx)
1239 || GET_MODE (user->use) == VOIDmode)
1240 replacement_rtx = btr_rtx;
1241 else
1242 replacement_rtx = gen_rtx_REG (GET_MODE (user->use), btr);
1243 validate_replace_rtx (user->use, replacement_rtx, user->insn);
1244 user->use = replacement_rtx;
1248 /* We anticipate intra-block scheduling to be done. See if INSN could move
1249 up within BB by N_INSNS. */
1250 static int
1251 can_move_up (const_basic_block bb, const rtx_insn *insn, int n_insns)
1253 while (insn != BB_HEAD (bb) && n_insns > 0)
1255 insn = PREV_INSN (insn);
1256 /* ??? What if we have an anti-dependency that actually prevents the
1257 scheduler from doing the move? We'd like to re-allocate the register,
1258 but not necessarily put the load into another basic block. */
1259 if (INSN_P (insn))
1260 n_insns--;
1262 return n_insns <= 0;
1265 /* Attempt to migrate the target register definition DEF to an
1266 earlier point in the flowgraph.
1268 It is a precondition of this function that DEF is migratable:
1269 i.e. it has a constant source, and all uses are unambiguous.
1271 Only migrations that reduce the cost of DEF will be made.
1272 MIN_COST is the lower bound on the cost of the DEF after migration.
1273 If we migrate DEF so that its cost falls below MIN_COST,
1274 then we do not attempt to migrate further. The idea is that
1275 we migrate definitions in a priority order based on their cost,
1276 when the cost of this definition falls below MIN_COST, then
1277 there is another definition with cost == MIN_COST which now
1278 has a higher priority than this definition.
1280 Return nonzero if there may be benefit from attempting to
1281 migrate this DEF further (i.e. we have reduced the cost below
1282 MIN_COST, but we may be able to reduce it further).
1283 Return zero if no further migration is possible. */
1284 static int
1285 migrate_btr_def (btr_def def, int min_cost)
1287 bitmap live_range;
1288 HARD_REG_SET btrs_live_in_range;
1289 int btr_used_near_def = 0;
1290 int def_basic_block_freq;
1291 basic_block attempt;
1292 int give_up = 0;
1293 int def_moved = 0;
1294 btr_user user;
1295 int def_latency;
1297 if (dump_file)
1298 fprintf (dump_file,
1299 "Attempting to migrate pt from insn %d (cost = %d, min_cost = %d) ... ",
1300 INSN_UID (def->insn), def->cost, min_cost);
1302 if (!def->group || def->has_ambiguous_use)
1303 /* These defs are not migratable. */
1305 if (dump_file)
1306 fprintf (dump_file, "it's not migratable\n");
1307 return 0;
1310 if (!def->uses)
1311 /* We have combined this def with another in the same group, so
1312 no need to consider it further.
1315 if (dump_file)
1316 fprintf (dump_file, "it's already combined with another pt\n");
1317 return 0;
1320 btr_def_live_range (def, &btrs_live_in_range);
1321 live_range = BITMAP_ALLOC (NULL);
1322 bitmap_copy (live_range, def->live_range);
1324 #ifdef INSN_SCHEDULING
1325 def_latency = insn_default_latency (def->insn) * issue_rate;
1326 #else
1327 def_latency = issue_rate;
1328 #endif
1330 for (user = def->uses; user != NULL; user = user->next)
1332 if (user->bb == def->bb
1333 && user->luid > def->luid
1334 && (def->luid + def_latency) > user->luid
1335 && ! can_move_up (def->bb, def->insn,
1336 (def->luid + def_latency) - user->luid))
1338 btr_used_near_def = 1;
1339 break;
1343 def_basic_block_freq = basic_block_freq (def->bb);
1345 for (attempt = get_immediate_dominator (CDI_DOMINATORS, def->bb);
1346 !give_up && attempt && attempt != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1347 && def->cost >= min_cost;
1348 attempt = get_immediate_dominator (CDI_DOMINATORS, attempt))
1350 /* Try to move the instruction that sets the target register into
1351 basic block ATTEMPT. */
1352 int try_freq = basic_block_freq (attempt);
1353 edge_iterator ei;
1354 edge e;
1356 /* If ATTEMPT has abnormal edges, skip it. */
1357 FOR_EACH_EDGE (e, ei, attempt->succs)
1358 if (e->flags & EDGE_COMPLEX)
1359 break;
1360 if (e)
1361 continue;
1363 if (dump_file)
1364 fprintf (dump_file, "trying block %d ...", attempt->index);
1366 if (try_freq < def_basic_block_freq
1367 || (try_freq == def_basic_block_freq && btr_used_near_def))
1369 int btr;
1370 augment_live_range (live_range, &btrs_live_in_range, def->bb, attempt,
1371 flag_btr_bb_exclusive);
1372 if (dump_file)
1374 fprintf (dump_file, "Now btrs live in range are: ");
1375 dump_hard_reg_set (btrs_live_in_range);
1376 fprintf (dump_file, "\n");
1378 btr = choose_btr (btrs_live_in_range);
1379 if (btr != -1)
1381 move_btr_def (attempt, btr, def, live_range, &btrs_live_in_range);
1382 bitmap_copy (live_range, def->live_range);
1383 btr_used_near_def = 0;
1384 def_moved = 1;
1385 def_basic_block_freq = basic_block_freq (def->bb);
1387 else
1389 /* There are no free target registers available to move
1390 this far forward, so give up */
1391 give_up = 1;
1392 if (dump_file)
1393 fprintf (dump_file,
1394 "giving up because there are no free target registers\n");
1399 if (!def_moved)
1401 give_up = 1;
1402 if (dump_file)
1403 fprintf (dump_file, "failed to move\n");
1405 BITMAP_FREE (live_range);
1406 return !give_up;
1409 /* Attempt to move instructions that set target registers earlier
1410 in the flowgraph, away from their corresponding uses. */
1411 static void
1412 migrate_btr_defs (enum reg_class btr_class, int allow_callee_save)
1414 btr_heap_t all_btr_defs (LONG_MIN);
1415 int reg;
1417 gcc_obstack_init (&migrate_btrl_obstack);
1418 if (dump_file)
1420 int i;
1422 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
1424 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1425 fprintf (dump_file,
1426 "Basic block %d: count = %" PRId64
1427 " loop-depth = %d idom = %d\n",
1428 i, (int64_t) bb->count, bb_loop_depth (bb),
1429 get_immediate_dominator (CDI_DOMINATORS, bb)->index);
1433 CLEAR_HARD_REG_SET (all_btrs);
1434 for (first_btr = -1, reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
1435 if (TEST_HARD_REG_BIT (reg_class_contents[(int) btr_class], reg)
1436 && (allow_callee_save || call_used_regs[reg]
1437 || df_regs_ever_live_p (reg)))
1439 SET_HARD_REG_BIT (all_btrs, reg);
1440 last_btr = reg;
1441 if (first_btr < 0)
1442 first_btr = reg;
1445 btrs_live = XCNEWVEC (HARD_REG_SET, last_basic_block_for_fn (cfun));
1446 btrs_live_at_end = XCNEWVEC (HARD_REG_SET, last_basic_block_for_fn (cfun));
1448 build_btr_def_use_webs (&all_btr_defs);
1450 while (!all_btr_defs.empty ())
1452 int min_cost = -all_btr_defs.min_key ();
1453 btr_def def = all_btr_defs.extract_min ();
1454 if (migrate_btr_def (def, min_cost))
1456 all_btr_defs.insert (-def->cost, def);
1457 if (dump_file)
1459 fprintf (dump_file,
1460 "Putting insn %d back on queue with priority %d\n",
1461 INSN_UID (def->insn), def->cost);
1464 else
1465 BITMAP_FREE (def->live_range);
1468 free (btrs_live);
1469 free (btrs_live_at_end);
1470 obstack_free (&migrate_btrl_obstack, NULL);
1473 static void
1474 branch_target_load_optimize (bool after_prologue_epilogue_gen)
1476 enum reg_class klass
1477 = (enum reg_class) targetm.branch_target_register_class ();
1478 if (klass != NO_REGS)
1480 /* Initialize issue_rate. */
1481 if (targetm.sched.issue_rate)
1482 issue_rate = targetm.sched.issue_rate ();
1483 else
1484 issue_rate = 1;
1486 if (!after_prologue_epilogue_gen)
1488 /* Build the CFG for migrate_btr_defs. */
1489 #if 1
1490 /* This may or may not be needed, depending on where we
1491 run this phase. */
1492 cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
1493 #endif
1495 df_analyze ();
1498 /* Dominator info is also needed for migrate_btr_def. */
1499 calculate_dominance_info (CDI_DOMINATORS);
1500 migrate_btr_defs (klass,
1501 (targetm.branch_target_register_callee_saved
1502 (after_prologue_epilogue_gen)));
1504 free_dominance_info (CDI_DOMINATORS);
1508 namespace {
1510 const pass_data pass_data_branch_target_load_optimize1 =
1512 RTL_PASS, /* type */
1513 "btl1", /* name */
1514 OPTGROUP_NONE, /* optinfo_flags */
1515 TV_NONE, /* tv_id */
1516 0, /* properties_required */
1517 0, /* properties_provided */
1518 0, /* properties_destroyed */
1519 0, /* todo_flags_start */
1520 0, /* todo_flags_finish */
1523 class pass_branch_target_load_optimize1 : public rtl_opt_pass
1525 public:
1526 pass_branch_target_load_optimize1 (gcc::context *ctxt)
1527 : rtl_opt_pass (pass_data_branch_target_load_optimize1, ctxt)
1530 /* opt_pass methods: */
1531 virtual bool gate (function *) { return flag_branch_target_load_optimize; }
1532 virtual unsigned int execute (function *)
1534 branch_target_load_optimize (epilogue_completed);
1535 return 0;
1538 }; // class pass_branch_target_load_optimize1
1540 } // anon namespace
1542 rtl_opt_pass *
1543 make_pass_branch_target_load_optimize1 (gcc::context *ctxt)
1545 return new pass_branch_target_load_optimize1 (ctxt);
1549 namespace {
1551 const pass_data pass_data_branch_target_load_optimize2 =
1553 RTL_PASS, /* type */
1554 "btl2", /* name */
1555 OPTGROUP_NONE, /* optinfo_flags */
1556 TV_NONE, /* tv_id */
1557 0, /* properties_required */
1558 0, /* properties_provided */
1559 0, /* properties_destroyed */
1560 0, /* todo_flags_start */
1561 0, /* todo_flags_finish */
1564 class pass_branch_target_load_optimize2 : public rtl_opt_pass
1566 public:
1567 pass_branch_target_load_optimize2 (gcc::context *ctxt)
1568 : rtl_opt_pass (pass_data_branch_target_load_optimize2, ctxt)
1571 /* opt_pass methods: */
1572 virtual bool gate (function *)
1574 return (optimize > 0 && flag_branch_target_load_optimize2);
1577 virtual unsigned int execute (function *);
1579 }; // class pass_branch_target_load_optimize2
1581 unsigned int
1582 pass_branch_target_load_optimize2::execute (function *)
1584 static int warned = 0;
1586 /* Leave this a warning for now so that it is possible to experiment
1587 with running this pass twice. In 3.6, we should either make this
1588 an error, or use separate dump files. */
1589 if (flag_branch_target_load_optimize
1590 && flag_branch_target_load_optimize2
1591 && !warned)
1593 warning (0, "branch target register load optimization is not intended "
1594 "to be run twice");
1596 warned = 1;
1599 branch_target_load_optimize (epilogue_completed);
1600 return 0;
1603 } // anon namespace
1605 rtl_opt_pass *
1606 make_pass_branch_target_load_optimize2 (gcc::context *ctxt)
1608 return new pass_branch_target_load_optimize2 (ctxt);