jit: Add checking for dereference of void *
[official-gcc.git] / gcc / bt-load.c
blob6084beb05677067279fdcded805916cba6ca8dff
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 "expr.h"
30 #include "flags.h"
31 #include "insn-attr.h"
32 #include "hashtab.h"
33 #include "hash-set.h"
34 #include "vec.h"
35 #include "machmode.h"
36 #include "input.h"
37 #include "function.h"
38 #include "except.h"
39 #include "tm_p.h"
40 #include "diagnostic-core.h"
41 #include "tree-pass.h"
42 #include "recog.h"
43 #include "dominance.h"
44 #include "cfg.h"
45 #include "cfgrtl.h"
46 #include "cfganal.h"
47 #include "cfgcleanup.h"
48 #include "predict.h"
49 #include "basic-block.h"
50 #include "df.h"
51 #include "cfgloop.h"
52 #include "rtl-iter.h"
53 #include "fibonacci_heap.h"
55 /* Target register optimizations - these are performed after reload. */
57 typedef struct btr_def_group_s
59 struct btr_def_group_s *next;
60 rtx src;
61 struct btr_def_s *members;
62 } *btr_def_group;
64 typedef struct btr_user_s
66 struct btr_user_s *next;
67 basic_block bb;
68 int luid;
69 rtx_insn *insn;
70 /* If INSN has a single use of a single branch register, then
71 USE points to it within INSN. If there is more than
72 one branch register use, or the use is in some way ambiguous,
73 then USE is NULL. */
74 rtx use;
75 int n_reaching_defs;
76 int first_reaching_def;
77 char other_use_this_block;
78 } *btr_user;
80 /* btr_def structs appear on three lists:
81 1. A list of all btr_def structures (head is
82 ALL_BTR_DEFS, linked by the NEXT field).
83 2. A list of branch reg definitions per basic block (head is
84 BB_BTR_DEFS[i], linked by the NEXT_THIS_BB field).
85 3. A list of all branch reg definitions belonging to the same
86 group (head is in a BTR_DEF_GROUP struct, linked by
87 NEXT_THIS_GROUP field). */
89 typedef struct btr_def_s
91 struct btr_def_s *next_this_bb;
92 struct btr_def_s *next_this_group;
93 basic_block bb;
94 int luid;
95 rtx_insn *insn;
96 int btr;
97 int cost;
98 /* For a branch register setting insn that has a constant
99 source (i.e. a label), group links together all the
100 insns with the same source. For other branch register
101 setting insns, group is NULL. */
102 btr_def_group group;
103 btr_user uses;
104 /* If this def has a reaching use which is not a simple use
105 in a branch instruction, then has_ambiguous_use will be true,
106 and we will not attempt to migrate this definition. */
107 char has_ambiguous_use;
108 /* live_range is an approximation to the true live range for this
109 def/use web, because it records the set of blocks that contain
110 the live range. There could be other live ranges for the same
111 branch register in that set of blocks, either in the block
112 containing the def (before the def), or in a block containing
113 a use (after the use). If there are such other live ranges, then
114 other_btr_uses_before_def or other_btr_uses_after_use must be set true
115 as appropriate. */
116 char other_btr_uses_before_def;
117 char other_btr_uses_after_use;
118 /* We set own_end when we have moved a definition into a dominator.
119 Thus, when a later combination removes this definition again, we know
120 to clear out trs_live_at_end again. */
121 char own_end;
122 bitmap live_range;
123 } *btr_def;
125 typedef fibonacci_heap <long, btr_def_s> btr_heap_t;
126 typedef fibonacci_node <long, btr_def_s> btr_heap_node_t;
128 static int issue_rate;
130 static int basic_block_freq (const_basic_block);
131 static int insn_sets_btr_p (const rtx_insn *, int, int *);
132 static void find_btr_def_group (btr_def_group *, btr_def);
133 static btr_def add_btr_def (btr_heap_t *, basic_block, int, rtx_insn *,
134 unsigned int, int, btr_def_group *);
135 static btr_user new_btr_user (basic_block, int, rtx_insn *);
136 static void dump_hard_reg_set (HARD_REG_SET);
137 static void dump_btrs_live (int);
138 static void note_other_use_this_block (unsigned int, btr_user);
139 static void compute_defs_uses_and_gen (btr_heap_t *, btr_def *,btr_user *,
140 sbitmap *, sbitmap *, HARD_REG_SET *);
141 static void compute_kill (sbitmap *, sbitmap *, HARD_REG_SET *);
142 static void compute_out (sbitmap *bb_out, sbitmap *, sbitmap *, int);
143 static void link_btr_uses (btr_def *, btr_user *, sbitmap *, sbitmap *, int);
144 static void build_btr_def_use_webs (btr_heap_t *);
145 static int block_at_edge_of_live_range_p (int, btr_def);
146 static void clear_btr_from_live_range (btr_def def);
147 static void add_btr_to_live_range (btr_def, int);
148 static void augment_live_range (bitmap, HARD_REG_SET *, basic_block,
149 basic_block, int);
150 static int choose_btr (HARD_REG_SET);
151 static void combine_btr_defs (btr_def, HARD_REG_SET *);
152 static void btr_def_live_range (btr_def, HARD_REG_SET *);
153 static void move_btr_def (basic_block, int, btr_def, bitmap, HARD_REG_SET *);
154 static int migrate_btr_def (btr_def, int);
155 static void migrate_btr_defs (enum reg_class, int);
156 static int can_move_up (const_basic_block, const rtx_insn *, int);
157 static void note_btr_set (rtx, const_rtx, void *);
159 /* The following code performs code motion of target load instructions
160 (instructions that set branch target registers), to move them
161 forward away from the branch instructions and out of loops (or,
162 more generally, from a more frequently executed place to a less
163 frequently executed place).
164 Moving target load instructions further in front of the branch
165 instruction that uses the target register value means that the hardware
166 has a better chance of preloading the instructions at the branch
167 target by the time the branch is reached. This avoids bubbles
168 when a taken branch needs to flush out the pipeline.
169 Moving target load instructions out of loops means they are executed
170 less frequently. */
172 /* An obstack to hold the def-use web data structures built up for
173 migrating branch target load instructions. */
174 static struct obstack migrate_btrl_obstack;
176 /* Array indexed by basic block number, giving the set of registers
177 live in that block. */
178 static HARD_REG_SET *btrs_live;
180 /* Array indexed by basic block number, giving the set of registers live at
181 the end of that block, including any uses by a final jump insn, if any. */
182 static HARD_REG_SET *btrs_live_at_end;
184 /* Set of all target registers that we are willing to allocate. */
185 static HARD_REG_SET all_btrs;
187 /* Provide lower and upper bounds for target register numbers, so that
188 we don't need to search through all the hard registers all the time. */
189 static int first_btr, last_btr;
193 /* Return an estimate of the frequency of execution of block bb. */
194 static int
195 basic_block_freq (const_basic_block bb)
197 return bb->frequency;
200 /* If X references (sets or reads) any branch target register, return one
201 such register. If EXCLUDEP is set, disregard any references within
202 that location. */
203 static rtx *
204 find_btr_use (rtx x, rtx *excludep = 0)
206 subrtx_ptr_iterator::array_type array;
207 FOR_EACH_SUBRTX_PTR (iter, array, &x, NONCONST)
209 rtx *loc = *iter;
210 if (loc == excludep)
211 iter.skip_subrtxes ();
212 else
214 const_rtx x = *loc;
215 if (REG_P (x)
216 && overlaps_hard_reg_set_p (all_btrs, GET_MODE (x), REGNO (x)))
217 return loc;
220 return 0;
223 /* Return true if insn is an instruction that sets a target register.
224 if CHECK_CONST is true, only return true if the source is constant.
225 If such a set is found and REGNO is nonzero, assign the register number
226 of the destination register to *REGNO. */
227 static int
228 insn_sets_btr_p (const rtx_insn *insn, int check_const, int *regno)
230 rtx set;
232 if (NONJUMP_INSN_P (insn)
233 && (set = single_set (insn)))
235 rtx dest = SET_DEST (set);
236 rtx src = SET_SRC (set);
238 if (GET_CODE (dest) == SUBREG)
239 dest = XEXP (dest, 0);
241 if (REG_P (dest)
242 && TEST_HARD_REG_BIT (all_btrs, REGNO (dest)))
244 gcc_assert (!find_btr_use (src));
246 if (!check_const || CONSTANT_P (src))
248 if (regno)
249 *regno = REGNO (dest);
250 return 1;
254 return 0;
257 /* Find the group that the target register definition DEF belongs
258 to in the list starting with *ALL_BTR_DEF_GROUPS. If no such
259 group exists, create one. Add def to the group. */
260 static void
261 find_btr_def_group (btr_def_group *all_btr_def_groups, btr_def def)
263 if (insn_sets_btr_p (def->insn, 1, NULL))
265 btr_def_group this_group;
266 rtx def_src = SET_SRC (single_set (def->insn));
268 /* ?? This linear search is an efficiency concern, particularly
269 as the search will almost always fail to find a match. */
270 for (this_group = *all_btr_def_groups;
271 this_group != NULL;
272 this_group = this_group->next)
273 if (rtx_equal_p (def_src, this_group->src))
274 break;
276 if (!this_group)
278 this_group = XOBNEW (&migrate_btrl_obstack, struct btr_def_group_s);
279 this_group->src = def_src;
280 this_group->members = NULL;
281 this_group->next = *all_btr_def_groups;
282 *all_btr_def_groups = this_group;
284 def->group = this_group;
285 def->next_this_group = this_group->members;
286 this_group->members = def;
288 else
289 def->group = NULL;
292 /* Create a new target register definition structure, for a definition in
293 block BB, instruction INSN, and insert it into ALL_BTR_DEFS. Return
294 the new definition. */
295 static btr_def
296 add_btr_def (btr_heap_t *all_btr_defs, basic_block bb, int insn_luid,
297 rtx_insn *insn,
298 unsigned int dest_reg, int other_btr_uses_before_def,
299 btr_def_group *all_btr_def_groups)
301 btr_def this_def = XOBNEW (&migrate_btrl_obstack, struct btr_def_s);
302 this_def->bb = bb;
303 this_def->luid = insn_luid;
304 this_def->insn = insn;
305 this_def->btr = dest_reg;
306 this_def->cost = basic_block_freq (bb);
307 this_def->has_ambiguous_use = 0;
308 this_def->other_btr_uses_before_def = other_btr_uses_before_def;
309 this_def->other_btr_uses_after_use = 0;
310 this_def->next_this_bb = NULL;
311 this_def->next_this_group = NULL;
312 this_def->uses = NULL;
313 this_def->live_range = NULL;
314 find_btr_def_group (all_btr_def_groups, this_def);
316 all_btr_defs->insert (-this_def->cost, this_def);
318 if (dump_file)
319 fprintf (dump_file,
320 "Found target reg definition: sets %u { bb %d, insn %d }%s priority %d\n",
321 dest_reg, bb->index, INSN_UID (insn),
322 (this_def->group ? "" : ":not const"), this_def->cost);
324 return this_def;
327 /* Create a new target register user structure, for a use in block BB,
328 instruction INSN. Return the new user. */
329 static btr_user
330 new_btr_user (basic_block bb, int insn_luid, rtx_insn *insn)
332 /* This instruction reads target registers. We need
333 to decide whether we can replace all target register
334 uses easily.
336 rtx *usep = find_btr_use (PATTERN (insn));
337 rtx use;
338 btr_user user = NULL;
340 if (usep)
342 int unambiguous_single_use;
344 /* We want to ensure that USE is the only use of a target
345 register in INSN, so that we know that to rewrite INSN to use
346 a different target register, all we have to do is replace USE. */
347 unambiguous_single_use = !find_btr_use (PATTERN (insn), usep);
348 if (!unambiguous_single_use)
349 usep = NULL;
351 use = usep ? *usep : NULL_RTX;
352 user = XOBNEW (&migrate_btrl_obstack, struct btr_user_s);
353 user->bb = bb;
354 user->luid = insn_luid;
355 user->insn = insn;
356 user->use = use;
357 user->other_use_this_block = 0;
358 user->next = NULL;
359 user->n_reaching_defs = 0;
360 user->first_reaching_def = -1;
362 if (dump_file)
364 fprintf (dump_file, "Uses target reg: { bb %d, insn %d }",
365 bb->index, INSN_UID (insn));
367 if (user->use)
368 fprintf (dump_file, ": unambiguous use of reg %d\n",
369 REGNO (user->use));
372 return user;
375 /* Write the contents of S to the dump file. */
376 static void
377 dump_hard_reg_set (HARD_REG_SET s)
379 int reg;
380 for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
381 if (TEST_HARD_REG_BIT (s, reg))
382 fprintf (dump_file, " %d", reg);
385 /* Write the set of target regs live in block BB to the dump file. */
386 static void
387 dump_btrs_live (int bb)
389 fprintf (dump_file, "BB%d live:", bb);
390 dump_hard_reg_set (btrs_live[bb]);
391 fprintf (dump_file, "\n");
394 /* REGNO is the number of a branch target register that is being used or
395 set. USERS_THIS_BB is a list of preceding branch target register users;
396 If any of them use the same register, set their other_use_this_block
397 flag. */
398 static void
399 note_other_use_this_block (unsigned int regno, btr_user users_this_bb)
401 btr_user user;
403 for (user = users_this_bb; user != NULL; user = user->next)
404 if (user->use && REGNO (user->use) == regno)
405 user->other_use_this_block = 1;
408 typedef struct {
409 btr_user users_this_bb;
410 HARD_REG_SET btrs_written_in_block;
411 HARD_REG_SET btrs_live_in_block;
412 sbitmap bb_gen;
413 sbitmap *btr_defset;
414 } defs_uses_info;
416 /* Called via note_stores or directly to register stores into /
417 clobbers of a branch target register DEST that are not recognized as
418 straightforward definitions. DATA points to information about the
419 current basic block that needs updating. */
420 static void
421 note_btr_set (rtx dest, const_rtx set ATTRIBUTE_UNUSED, void *data)
423 defs_uses_info *info = (defs_uses_info *) data;
424 int regno, end_regno;
426 if (!REG_P (dest))
427 return;
428 regno = REGNO (dest);
429 end_regno = END_HARD_REGNO (dest);
430 for (; regno < end_regno; regno++)
431 if (TEST_HARD_REG_BIT (all_btrs, regno))
433 note_other_use_this_block (regno, info->users_this_bb);
434 SET_HARD_REG_BIT (info->btrs_written_in_block, regno);
435 SET_HARD_REG_BIT (info->btrs_live_in_block, regno);
436 bitmap_and_compl (info->bb_gen, info->bb_gen,
437 info->btr_defset[regno - first_btr]);
441 static void
442 compute_defs_uses_and_gen (btr_heap_t *all_btr_defs, btr_def *def_array,
443 btr_user *use_array, sbitmap *btr_defset,
444 sbitmap *bb_gen, HARD_REG_SET *btrs_written)
446 /* Scan the code building up the set of all defs and all uses.
447 For each target register, build the set of defs of that register.
448 For each block, calculate the set of target registers
449 written in that block.
450 Also calculate the set of btrs ever live in that block.
452 int i;
453 int insn_luid = 0;
454 btr_def_group all_btr_def_groups = NULL;
455 defs_uses_info info;
457 bitmap_vector_clear (bb_gen, last_basic_block_for_fn (cfun));
458 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
460 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
461 int reg;
462 btr_def defs_this_bb = NULL;
463 rtx_insn *insn;
464 rtx_insn *last;
465 int can_throw = 0;
467 info.users_this_bb = NULL;
468 info.bb_gen = bb_gen[i];
469 info.btr_defset = btr_defset;
471 CLEAR_HARD_REG_SET (info.btrs_live_in_block);
472 CLEAR_HARD_REG_SET (info.btrs_written_in_block);
473 for (reg = first_btr; reg <= last_btr; reg++)
474 if (TEST_HARD_REG_BIT (all_btrs, reg)
475 && REGNO_REG_SET_P (df_get_live_in (bb), reg))
476 SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
478 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
479 insn != last;
480 insn = NEXT_INSN (insn), insn_luid++)
482 if (INSN_P (insn))
484 int regno;
485 int insn_uid = INSN_UID (insn);
487 if (insn_sets_btr_p (insn, 0, &regno))
489 btr_def def = add_btr_def (
490 all_btr_defs, bb, insn_luid, insn, regno,
491 TEST_HARD_REG_BIT (info.btrs_live_in_block, regno),
492 &all_btr_def_groups);
494 def_array[insn_uid] = def;
495 SET_HARD_REG_BIT (info.btrs_written_in_block, regno);
496 SET_HARD_REG_BIT (info.btrs_live_in_block, regno);
497 bitmap_and_compl (bb_gen[i], bb_gen[i],
498 btr_defset[regno - first_btr]);
499 bitmap_set_bit (bb_gen[i], insn_uid);
500 def->next_this_bb = defs_this_bb;
501 defs_this_bb = def;
502 bitmap_set_bit (btr_defset[regno - first_btr], insn_uid);
503 note_other_use_this_block (regno, info.users_this_bb);
505 /* Check for the blockage emitted by expand_nl_goto_receiver. */
506 else if (cfun->has_nonlocal_label
507 && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE)
509 btr_user user;
511 /* Do the equivalent of calling note_other_use_this_block
512 for every target register. */
513 for (user = info.users_this_bb; user != NULL;
514 user = user->next)
515 if (user->use)
516 user->other_use_this_block = 1;
517 IOR_HARD_REG_SET (info.btrs_written_in_block, all_btrs);
518 IOR_HARD_REG_SET (info.btrs_live_in_block, all_btrs);
519 bitmap_clear (info.bb_gen);
521 else
523 if (find_btr_use (PATTERN (insn)))
525 btr_user user = new_btr_user (bb, insn_luid, insn);
527 use_array[insn_uid] = user;
528 if (user->use)
529 SET_HARD_REG_BIT (info.btrs_live_in_block,
530 REGNO (user->use));
531 else
533 int reg;
534 for (reg = first_btr; reg <= last_btr; reg++)
535 if (TEST_HARD_REG_BIT (all_btrs, reg)
536 && refers_to_regno_p (reg, user->insn))
538 note_other_use_this_block (reg,
539 info.users_this_bb);
540 SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
542 note_stores (PATTERN (insn), note_btr_set, &info);
544 user->next = info.users_this_bb;
545 info.users_this_bb = user;
547 if (CALL_P (insn))
549 HARD_REG_SET *clobbered = &call_used_reg_set;
550 HARD_REG_SET call_saved;
551 rtx pat = PATTERN (insn);
552 int i;
554 /* Check for sibcall. */
555 if (GET_CODE (pat) == PARALLEL)
556 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
557 if (ANY_RETURN_P (XVECEXP (pat, 0, i)))
559 COMPL_HARD_REG_SET (call_saved,
560 call_used_reg_set);
561 clobbered = &call_saved;
564 for (regno = first_btr; regno <= last_btr; regno++)
565 if (TEST_HARD_REG_BIT (*clobbered, regno))
566 note_btr_set (regno_reg_rtx[regno], NULL_RTX, &info);
572 COPY_HARD_REG_SET (btrs_live[i], info.btrs_live_in_block);
573 COPY_HARD_REG_SET (btrs_written[i], info.btrs_written_in_block);
575 REG_SET_TO_HARD_REG_SET (btrs_live_at_end[i], df_get_live_out (bb));
576 /* If this block ends in a jump insn, add any uses or even clobbers
577 of branch target registers that it might have. */
578 for (insn = BB_END (bb); insn != BB_HEAD (bb) && ! INSN_P (insn); )
579 insn = PREV_INSN (insn);
580 /* ??? for the fall-through edge, it would make sense to insert the
581 btr set on the edge, but that would require to split the block
582 early on so that we can distinguish between dominance from the fall
583 through edge - which can use the call-clobbered registers - from
584 dominance by the throw edge. */
585 if (can_throw_internal (insn))
587 HARD_REG_SET tmp;
589 COPY_HARD_REG_SET (tmp, call_used_reg_set);
590 AND_HARD_REG_SET (tmp, all_btrs);
591 IOR_HARD_REG_SET (btrs_live_at_end[i], tmp);
592 can_throw = 1;
594 if (can_throw || JUMP_P (insn))
596 int regno;
598 for (regno = first_btr; regno <= last_btr; regno++)
599 if (refers_to_regno_p (regno, insn))
600 SET_HARD_REG_BIT (btrs_live_at_end[i], regno);
603 if (dump_file)
604 dump_btrs_live (i);
608 static void
609 compute_kill (sbitmap *bb_kill, sbitmap *btr_defset,
610 HARD_REG_SET *btrs_written)
612 int i;
613 int regno;
615 /* For each basic block, form the set BB_KILL - the set
616 of definitions that the block kills. */
617 bitmap_vector_clear (bb_kill, last_basic_block_for_fn (cfun));
618 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
620 for (regno = first_btr; regno <= last_btr; regno++)
621 if (TEST_HARD_REG_BIT (all_btrs, regno)
622 && TEST_HARD_REG_BIT (btrs_written[i], regno))
623 bitmap_ior (bb_kill[i], bb_kill[i],
624 btr_defset[regno - first_btr]);
628 static void
629 compute_out (sbitmap *bb_out, sbitmap *bb_gen, sbitmap *bb_kill, int max_uid)
631 /* Perform iterative dataflow:
632 Initially, for all blocks, BB_OUT = BB_GEN.
633 For each block,
634 BB_IN = union over predecessors of BB_OUT(pred)
635 BB_OUT = (BB_IN - BB_KILL) + BB_GEN
636 Iterate until the bb_out sets stop growing. */
637 int i;
638 int changed;
639 sbitmap bb_in = sbitmap_alloc (max_uid);
641 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
642 bitmap_copy (bb_out[i], bb_gen[i]);
644 changed = 1;
645 while (changed)
647 changed = 0;
648 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
650 bitmap_union_of_preds (bb_in, bb_out, BASIC_BLOCK_FOR_FN (cfun, i));
651 changed |= bitmap_ior_and_compl (bb_out[i], bb_gen[i],
652 bb_in, bb_kill[i]);
655 sbitmap_free (bb_in);
658 static void
659 link_btr_uses (btr_def *def_array, btr_user *use_array, sbitmap *bb_out,
660 sbitmap *btr_defset, int max_uid)
662 int i;
663 sbitmap reaching_defs = sbitmap_alloc (max_uid);
665 /* Link uses to the uses lists of all of their reaching defs.
666 Count up the number of reaching defs of each use. */
667 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
669 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
670 rtx_insn *insn;
671 rtx_insn *last;
673 bitmap_union_of_preds (reaching_defs, bb_out, BASIC_BLOCK_FOR_FN (cfun, i));
674 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
675 insn != last;
676 insn = NEXT_INSN (insn))
678 if (INSN_P (insn))
680 int insn_uid = INSN_UID (insn);
682 btr_def def = def_array[insn_uid];
683 btr_user user = use_array[insn_uid];
684 if (def != NULL)
686 /* Remove all reaching defs of regno except
687 for this one. */
688 bitmap_and_compl (reaching_defs, reaching_defs,
689 btr_defset[def->btr - first_btr]);
690 bitmap_set_bit (reaching_defs, insn_uid);
693 if (user != NULL)
695 /* Find all the reaching defs for this use. */
696 sbitmap reaching_defs_of_reg = sbitmap_alloc (max_uid);
697 unsigned int uid = 0;
698 sbitmap_iterator sbi;
700 if (user->use)
701 bitmap_and (
702 reaching_defs_of_reg,
703 reaching_defs,
704 btr_defset[REGNO (user->use) - first_btr]);
705 else
707 int reg;
709 bitmap_clear (reaching_defs_of_reg);
710 for (reg = first_btr; reg <= last_btr; reg++)
711 if (TEST_HARD_REG_BIT (all_btrs, reg)
712 && refers_to_regno_p (reg, user->insn))
713 bitmap_or_and (reaching_defs_of_reg,
714 reaching_defs_of_reg,
715 reaching_defs,
716 btr_defset[reg - first_btr]);
718 EXECUTE_IF_SET_IN_BITMAP (reaching_defs_of_reg, 0, uid, sbi)
720 btr_def def = def_array[uid];
722 /* We now know that def reaches user. */
724 if (dump_file)
725 fprintf (dump_file,
726 "Def in insn %d reaches use in insn %d\n",
727 uid, insn_uid);
729 user->n_reaching_defs++;
730 if (!user->use)
731 def->has_ambiguous_use = 1;
732 if (user->first_reaching_def != -1)
733 { /* There is more than one reaching def. This is
734 a rare case, so just give up on this def/use
735 web when it occurs. */
736 def->has_ambiguous_use = 1;
737 def_array[user->first_reaching_def]
738 ->has_ambiguous_use = 1;
739 if (dump_file)
740 fprintf (dump_file,
741 "(use %d has multiple reaching defs)\n",
742 insn_uid);
744 else
745 user->first_reaching_def = uid;
746 if (user->other_use_this_block)
747 def->other_btr_uses_after_use = 1;
748 user->next = def->uses;
749 def->uses = user;
751 sbitmap_free (reaching_defs_of_reg);
754 if (CALL_P (insn))
756 int regno;
758 for (regno = first_btr; regno <= last_btr; regno++)
759 if (TEST_HARD_REG_BIT (all_btrs, regno)
760 && TEST_HARD_REG_BIT (call_used_reg_set, regno))
761 bitmap_and_compl (reaching_defs, reaching_defs,
762 btr_defset[regno - first_btr]);
767 sbitmap_free (reaching_defs);
770 static void
771 build_btr_def_use_webs (btr_heap_t *all_btr_defs)
773 const int max_uid = get_max_uid ();
774 btr_def *def_array = XCNEWVEC (btr_def, max_uid);
775 btr_user *use_array = XCNEWVEC (btr_user, max_uid);
776 sbitmap *btr_defset = sbitmap_vector_alloc (
777 (last_btr - first_btr) + 1, max_uid);
778 sbitmap *bb_gen = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
779 max_uid);
780 HARD_REG_SET *btrs_written = XCNEWVEC (HARD_REG_SET,
781 last_basic_block_for_fn (cfun));
782 sbitmap *bb_kill;
783 sbitmap *bb_out;
785 bitmap_vector_clear (btr_defset, (last_btr - first_btr) + 1);
787 compute_defs_uses_and_gen (all_btr_defs, def_array, use_array, btr_defset,
788 bb_gen, btrs_written);
790 bb_kill = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), max_uid);
791 compute_kill (bb_kill, btr_defset, btrs_written);
792 free (btrs_written);
794 bb_out = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), max_uid);
795 compute_out (bb_out, bb_gen, bb_kill, max_uid);
797 sbitmap_vector_free (bb_gen);
798 sbitmap_vector_free (bb_kill);
800 link_btr_uses (def_array, use_array, bb_out, btr_defset, max_uid);
802 sbitmap_vector_free (bb_out);
803 sbitmap_vector_free (btr_defset);
804 free (use_array);
805 free (def_array);
808 /* Return true if basic block BB contains the start or end of the
809 live range of the definition DEF, AND there are other live
810 ranges of the same target register that include BB. */
811 static int
812 block_at_edge_of_live_range_p (int bb, btr_def def)
814 if (def->other_btr_uses_before_def
815 && BASIC_BLOCK_FOR_FN (cfun, bb) == def->bb)
816 return 1;
817 else if (def->other_btr_uses_after_use)
819 btr_user user;
820 for (user = def->uses; user != NULL; user = user->next)
821 if (BASIC_BLOCK_FOR_FN (cfun, bb) == user->bb)
822 return 1;
824 return 0;
827 /* We are removing the def/use web DEF. The target register
828 used in this web is therefore no longer live in the live range
829 of this web, so remove it from the live set of all basic blocks
830 in the live range of the web.
831 Blocks at the boundary of the live range may contain other live
832 ranges for the same target register, so we have to be careful
833 to remove the target register from the live set of these blocks
834 only if they do not contain other live ranges for the same register. */
835 static void
836 clear_btr_from_live_range (btr_def def)
838 unsigned bb;
839 bitmap_iterator bi;
841 EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
843 if ((!def->other_btr_uses_before_def
844 && !def->other_btr_uses_after_use)
845 || !block_at_edge_of_live_range_p (bb, def))
847 CLEAR_HARD_REG_BIT (btrs_live[bb], def->btr);
848 CLEAR_HARD_REG_BIT (btrs_live_at_end[bb], def->btr);
849 if (dump_file)
850 dump_btrs_live (bb);
853 if (def->own_end)
854 CLEAR_HARD_REG_BIT (btrs_live_at_end[def->bb->index], def->btr);
858 /* We are adding the def/use web DEF. Add the target register used
859 in this web to the live set of all of the basic blocks that contain
860 the live range of the web.
861 If OWN_END is set, also show that the register is live from our
862 definitions at the end of the basic block where it is defined. */
863 static void
864 add_btr_to_live_range (btr_def def, int own_end)
866 unsigned bb;
867 bitmap_iterator bi;
869 EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
871 SET_HARD_REG_BIT (btrs_live[bb], def->btr);
872 SET_HARD_REG_BIT (btrs_live_at_end[bb], def->btr);
873 if (dump_file)
874 dump_btrs_live (bb);
876 if (own_end)
878 SET_HARD_REG_BIT (btrs_live_at_end[def->bb->index], def->btr);
879 def->own_end = 1;
883 /* Update a live range to contain the basic block NEW_BLOCK, and all
884 blocks on paths between the existing live range and NEW_BLOCK.
885 HEAD is a block contained in the existing live range that dominates
886 all other blocks in the existing live range.
887 Also add to the set BTRS_LIVE_IN_RANGE all target registers that
888 are live in the blocks that we add to the live range.
889 If FULL_RANGE is set, include the full live range of NEW_BB;
890 otherwise, if NEW_BB dominates HEAD_BB, only add registers that
891 are life at the end of NEW_BB for NEW_BB itself.
892 It is a precondition that either NEW_BLOCK dominates HEAD,or
893 HEAD dom NEW_BLOCK. This is used to speed up the
894 implementation of this function. */
895 static void
896 augment_live_range (bitmap live_range, HARD_REG_SET *btrs_live_in_range,
897 basic_block head_bb, basic_block new_bb, int full_range)
899 basic_block *worklist, *tos;
901 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
903 if (dominated_by_p (CDI_DOMINATORS, new_bb, head_bb))
905 if (new_bb == head_bb)
907 if (full_range)
908 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[new_bb->index]);
909 free (tos);
910 return;
912 *tos++ = new_bb;
914 else
916 edge e;
917 edge_iterator ei;
918 int new_block = new_bb->index;
920 gcc_assert (dominated_by_p (CDI_DOMINATORS, head_bb, new_bb));
922 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[head_bb->index]);
923 bitmap_set_bit (live_range, new_block);
924 /* A previous btr migration could have caused a register to be
925 live just at the end of new_block which we need in full, so
926 use trs_live_at_end even if full_range is set. */
927 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live_at_end[new_block]);
928 if (full_range)
929 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[new_block]);
930 if (dump_file)
932 fprintf (dump_file,
933 "Adding end of block %d and rest of %d to live range\n",
934 new_block, head_bb->index);
935 fprintf (dump_file,"Now live btrs are ");
936 dump_hard_reg_set (*btrs_live_in_range);
937 fprintf (dump_file, "\n");
939 FOR_EACH_EDGE (e, ei, head_bb->preds)
940 *tos++ = e->src;
943 while (tos != worklist)
945 basic_block bb = *--tos;
946 if (!bitmap_bit_p (live_range, bb->index))
948 edge e;
949 edge_iterator ei;
951 bitmap_set_bit (live_range, bb->index);
952 IOR_HARD_REG_SET (*btrs_live_in_range,
953 btrs_live[bb->index]);
954 /* A previous btr migration could have caused a register to be
955 live just at the end of a block which we need in full. */
956 IOR_HARD_REG_SET (*btrs_live_in_range,
957 btrs_live_at_end[bb->index]);
958 if (dump_file)
960 fprintf (dump_file,
961 "Adding block %d to live range\n", bb->index);
962 fprintf (dump_file,"Now live btrs are ");
963 dump_hard_reg_set (*btrs_live_in_range);
964 fprintf (dump_file, "\n");
967 FOR_EACH_EDGE (e, ei, bb->preds)
969 basic_block pred = e->src;
970 if (!bitmap_bit_p (live_range, pred->index))
971 *tos++ = pred;
976 free (worklist);
979 /* Return the most desirable target register that is not in
980 the set USED_BTRS. */
981 static int
982 choose_btr (HARD_REG_SET used_btrs)
984 int i;
986 if (!hard_reg_set_subset_p (all_btrs, used_btrs))
987 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
989 #ifdef REG_ALLOC_ORDER
990 int regno = reg_alloc_order[i];
991 #else
992 int regno = i;
993 #endif
994 if (TEST_HARD_REG_BIT (all_btrs, regno)
995 && !TEST_HARD_REG_BIT (used_btrs, regno))
996 return regno;
998 return -1;
1001 /* Calculate the set of basic blocks that contain the live range of
1002 the def/use web DEF.
1003 Also calculate the set of target registers that are live at time
1004 in this live range, but ignore the live range represented by DEF
1005 when calculating this set. */
1006 static void
1007 btr_def_live_range (btr_def def, HARD_REG_SET *btrs_live_in_range)
1009 if (!def->live_range)
1011 btr_user user;
1013 def->live_range = BITMAP_ALLOC (NULL);
1015 bitmap_set_bit (def->live_range, def->bb->index);
1016 COPY_HARD_REG_SET (*btrs_live_in_range,
1017 (flag_btr_bb_exclusive
1018 ? btrs_live : btrs_live_at_end)[def->bb->index]);
1020 for (user = def->uses; user != NULL; user = user->next)
1021 augment_live_range (def->live_range, btrs_live_in_range,
1022 def->bb, user->bb,
1023 (flag_btr_bb_exclusive
1024 || user->insn != BB_END (def->bb)
1025 || !JUMP_P (user->insn)));
1027 else
1029 /* def->live_range is accurate, but we need to recompute
1030 the set of target registers live over it, because migration
1031 of other PT instructions may have affected it.
1033 unsigned bb;
1034 unsigned def_bb = flag_btr_bb_exclusive ? -1 : def->bb->index;
1035 bitmap_iterator bi;
1037 CLEAR_HARD_REG_SET (*btrs_live_in_range);
1038 EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
1040 IOR_HARD_REG_SET (*btrs_live_in_range,
1041 (def_bb == bb
1042 ? btrs_live_at_end : btrs_live) [bb]);
1045 if (!def->other_btr_uses_before_def &&
1046 !def->other_btr_uses_after_use)
1047 CLEAR_HARD_REG_BIT (*btrs_live_in_range, def->btr);
1050 /* Merge into the def/use web DEF any other def/use webs in the same
1051 group that are dominated by DEF, provided that there is a target
1052 register available to allocate to the merged web. */
1053 static void
1054 combine_btr_defs (btr_def def, HARD_REG_SET *btrs_live_in_range)
1056 btr_def other_def;
1058 for (other_def = def->group->members;
1059 other_def != NULL;
1060 other_def = other_def->next_this_group)
1062 if (other_def != def
1063 && other_def->uses != NULL
1064 && ! other_def->has_ambiguous_use
1065 && dominated_by_p (CDI_DOMINATORS, other_def->bb, def->bb))
1067 /* def->bb dominates the other def, so def and other_def could
1068 be combined. */
1069 /* Merge their live ranges, and get the set of
1070 target registers live over the merged range. */
1071 int btr;
1072 HARD_REG_SET combined_btrs_live;
1073 bitmap combined_live_range = BITMAP_ALLOC (NULL);
1074 btr_user user;
1076 if (other_def->live_range == NULL)
1078 HARD_REG_SET dummy_btrs_live_in_range;
1079 btr_def_live_range (other_def, &dummy_btrs_live_in_range);
1081 COPY_HARD_REG_SET (combined_btrs_live, *btrs_live_in_range);
1082 bitmap_copy (combined_live_range, def->live_range);
1084 for (user = other_def->uses; user != NULL; user = user->next)
1085 augment_live_range (combined_live_range, &combined_btrs_live,
1086 def->bb, user->bb,
1087 (flag_btr_bb_exclusive
1088 || user->insn != BB_END (def->bb)
1089 || !JUMP_P (user->insn)));
1091 btr = choose_btr (combined_btrs_live);
1092 if (btr != -1)
1094 /* We can combine them. */
1095 if (dump_file)
1096 fprintf (dump_file,
1097 "Combining def in insn %d with def in insn %d\n",
1098 INSN_UID (other_def->insn), INSN_UID (def->insn));
1100 def->btr = btr;
1101 user = other_def->uses;
1102 while (user != NULL)
1104 btr_user next = user->next;
1106 user->next = def->uses;
1107 def->uses = user;
1108 user = next;
1110 /* Combining def/use webs can make target registers live
1111 after uses where they previously were not. This means
1112 some REG_DEAD notes may no longer be correct. We could
1113 be more precise about this if we looked at the combined
1114 live range, but here I just delete any REG_DEAD notes
1115 in case they are no longer correct. */
1116 for (user = def->uses; user != NULL; user = user->next)
1117 remove_note (user->insn,
1118 find_regno_note (user->insn, REG_DEAD,
1119 REGNO (user->use)));
1120 clear_btr_from_live_range (other_def);
1121 other_def->uses = NULL;
1122 bitmap_copy (def->live_range, combined_live_range);
1123 if (other_def->btr == btr && other_def->other_btr_uses_after_use)
1124 def->other_btr_uses_after_use = 1;
1125 COPY_HARD_REG_SET (*btrs_live_in_range, combined_btrs_live);
1127 /* Delete the old target register initialization. */
1128 delete_insn (other_def->insn);
1131 BITMAP_FREE (combined_live_range);
1136 /* Move the definition DEF from its current position to basic
1137 block NEW_DEF_BB, and modify it to use branch target register BTR.
1138 Delete the old defining insn, and insert a new one in NEW_DEF_BB.
1139 Update all reaching uses of DEF in the RTL to use BTR.
1140 If this new position means that other defs in the
1141 same group can be combined with DEF then combine them. */
1142 static void
1143 move_btr_def (basic_block new_def_bb, int btr, btr_def def, bitmap live_range,
1144 HARD_REG_SET *btrs_live_in_range)
1146 /* We can move the instruction.
1147 Set a target register in block NEW_DEF_BB to the value
1148 needed for this target register definition.
1149 Replace all uses of the old target register definition by
1150 uses of the new definition. Delete the old definition. */
1151 basic_block b = new_def_bb;
1152 rtx_insn *insp = BB_HEAD (b);
1153 rtx_insn *old_insn = def->insn;
1154 rtx src;
1155 rtx btr_rtx;
1156 rtx_insn *new_insn;
1157 machine_mode btr_mode;
1158 btr_user user;
1159 rtx set;
1161 if (dump_file)
1162 fprintf(dump_file, "migrating to basic block %d, using reg %d\n",
1163 new_def_bb->index, btr);
1165 clear_btr_from_live_range (def);
1166 def->btr = btr;
1167 def->bb = new_def_bb;
1168 def->luid = 0;
1169 def->cost = basic_block_freq (new_def_bb);
1170 bitmap_copy (def->live_range, live_range);
1171 combine_btr_defs (def, btrs_live_in_range);
1172 btr = def->btr;
1173 def->other_btr_uses_before_def
1174 = TEST_HARD_REG_BIT (btrs_live[b->index], btr) ? 1 : 0;
1175 add_btr_to_live_range (def, 1);
1176 if (LABEL_P (insp))
1177 insp = NEXT_INSN (insp);
1178 /* N.B.: insp is expected to be NOTE_INSN_BASIC_BLOCK now. Some
1179 optimizations can result in insp being both first and last insn of
1180 its basic block. */
1181 /* ?? some assertions to check that insp is sensible? */
1183 if (def->other_btr_uses_before_def)
1185 insp = BB_END (b);
1186 for (insp = BB_END (b); ! INSN_P (insp); insp = PREV_INSN (insp))
1187 gcc_assert (insp != BB_HEAD (b));
1189 if (JUMP_P (insp) || can_throw_internal (insp))
1190 insp = PREV_INSN (insp);
1193 set = single_set (old_insn);
1194 src = SET_SRC (set);
1195 btr_mode = GET_MODE (SET_DEST (set));
1196 btr_rtx = gen_rtx_REG (btr_mode, btr);
1198 new_insn = as_a <rtx_insn *> (gen_move_insn (btr_rtx, src));
1200 /* Insert target register initialization at head of basic block. */
1201 def->insn = emit_insn_after (new_insn, insp);
1203 df_set_regs_ever_live (btr, true);
1205 if (dump_file)
1206 fprintf (dump_file, "New pt is insn %d, inserted after insn %d\n",
1207 INSN_UID (def->insn), INSN_UID (insp));
1209 /* Delete the old target register initialization. */
1210 delete_insn (old_insn);
1212 /* Replace each use of the old target register by a use of the new target
1213 register. */
1214 for (user = def->uses; user != NULL; user = user->next)
1216 /* Some extra work here to ensure consistent modes, because
1217 it seems that a target register REG rtx can be given a different
1218 mode depending on the context (surely that should not be
1219 the case?). */
1220 rtx replacement_rtx;
1221 if (GET_MODE (user->use) == GET_MODE (btr_rtx)
1222 || GET_MODE (user->use) == VOIDmode)
1223 replacement_rtx = btr_rtx;
1224 else
1225 replacement_rtx = gen_rtx_REG (GET_MODE (user->use), btr);
1226 validate_replace_rtx (user->use, replacement_rtx, user->insn);
1227 user->use = replacement_rtx;
1231 /* We anticipate intra-block scheduling to be done. See if INSN could move
1232 up within BB by N_INSNS. */
1233 static int
1234 can_move_up (const_basic_block bb, const rtx_insn *insn, int n_insns)
1236 while (insn != BB_HEAD (bb) && n_insns > 0)
1238 insn = PREV_INSN (insn);
1239 /* ??? What if we have an anti-dependency that actually prevents the
1240 scheduler from doing the move? We'd like to re-allocate the register,
1241 but not necessarily put the load into another basic block. */
1242 if (INSN_P (insn))
1243 n_insns--;
1245 return n_insns <= 0;
1248 /* Attempt to migrate the target register definition DEF to an
1249 earlier point in the flowgraph.
1251 It is a precondition of this function that DEF is migratable:
1252 i.e. it has a constant source, and all uses are unambiguous.
1254 Only migrations that reduce the cost of DEF will be made.
1255 MIN_COST is the lower bound on the cost of the DEF after migration.
1256 If we migrate DEF so that its cost falls below MIN_COST,
1257 then we do not attempt to migrate further. The idea is that
1258 we migrate definitions in a priority order based on their cost,
1259 when the cost of this definition falls below MIN_COST, then
1260 there is another definition with cost == MIN_COST which now
1261 has a higher priority than this definition.
1263 Return nonzero if there may be benefit from attempting to
1264 migrate this DEF further (i.e. we have reduced the cost below
1265 MIN_COST, but we may be able to reduce it further).
1266 Return zero if no further migration is possible. */
1267 static int
1268 migrate_btr_def (btr_def def, int min_cost)
1270 bitmap live_range;
1271 HARD_REG_SET btrs_live_in_range;
1272 int btr_used_near_def = 0;
1273 int def_basic_block_freq;
1274 basic_block attempt;
1275 int give_up = 0;
1276 int def_moved = 0;
1277 btr_user user;
1278 int def_latency;
1280 if (dump_file)
1281 fprintf (dump_file,
1282 "Attempting to migrate pt from insn %d (cost = %d, min_cost = %d) ... ",
1283 INSN_UID (def->insn), def->cost, min_cost);
1285 if (!def->group || def->has_ambiguous_use)
1286 /* These defs are not migratable. */
1288 if (dump_file)
1289 fprintf (dump_file, "it's not migratable\n");
1290 return 0;
1293 if (!def->uses)
1294 /* We have combined this def with another in the same group, so
1295 no need to consider it further.
1298 if (dump_file)
1299 fprintf (dump_file, "it's already combined with another pt\n");
1300 return 0;
1303 btr_def_live_range (def, &btrs_live_in_range);
1304 live_range = BITMAP_ALLOC (NULL);
1305 bitmap_copy (live_range, def->live_range);
1307 #ifdef INSN_SCHEDULING
1308 def_latency = insn_default_latency (def->insn) * issue_rate;
1309 #else
1310 def_latency = issue_rate;
1311 #endif
1313 for (user = def->uses; user != NULL; user = user->next)
1315 if (user->bb == def->bb
1316 && user->luid > def->luid
1317 && (def->luid + def_latency) > user->luid
1318 && ! can_move_up (def->bb, def->insn,
1319 (def->luid + def_latency) - user->luid))
1321 btr_used_near_def = 1;
1322 break;
1326 def_basic_block_freq = basic_block_freq (def->bb);
1328 for (attempt = get_immediate_dominator (CDI_DOMINATORS, def->bb);
1329 !give_up && attempt && attempt != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1330 && def->cost >= min_cost;
1331 attempt = get_immediate_dominator (CDI_DOMINATORS, attempt))
1333 /* Try to move the instruction that sets the target register into
1334 basic block ATTEMPT. */
1335 int try_freq = basic_block_freq (attempt);
1336 edge_iterator ei;
1337 edge e;
1339 /* If ATTEMPT has abnormal edges, skip it. */
1340 FOR_EACH_EDGE (e, ei, attempt->succs)
1341 if (e->flags & EDGE_COMPLEX)
1342 break;
1343 if (e)
1344 continue;
1346 if (dump_file)
1347 fprintf (dump_file, "trying block %d ...", attempt->index);
1349 if (try_freq < def_basic_block_freq
1350 || (try_freq == def_basic_block_freq && btr_used_near_def))
1352 int btr;
1353 augment_live_range (live_range, &btrs_live_in_range, def->bb, attempt,
1354 flag_btr_bb_exclusive);
1355 if (dump_file)
1357 fprintf (dump_file, "Now btrs live in range are: ");
1358 dump_hard_reg_set (btrs_live_in_range);
1359 fprintf (dump_file, "\n");
1361 btr = choose_btr (btrs_live_in_range);
1362 if (btr != -1)
1364 move_btr_def (attempt, btr, def, live_range, &btrs_live_in_range);
1365 bitmap_copy (live_range, def->live_range);
1366 btr_used_near_def = 0;
1367 def_moved = 1;
1368 def_basic_block_freq = basic_block_freq (def->bb);
1370 else
1372 /* There are no free target registers available to move
1373 this far forward, so give up */
1374 give_up = 1;
1375 if (dump_file)
1376 fprintf (dump_file,
1377 "giving up because there are no free target registers\n");
1382 if (!def_moved)
1384 give_up = 1;
1385 if (dump_file)
1386 fprintf (dump_file, "failed to move\n");
1388 BITMAP_FREE (live_range);
1389 return !give_up;
1392 /* Attempt to move instructions that set target registers earlier
1393 in the flowgraph, away from their corresponding uses. */
1394 static void
1395 migrate_btr_defs (enum reg_class btr_class, int allow_callee_save)
1397 btr_heap_t all_btr_defs (LONG_MIN);
1398 int reg;
1400 gcc_obstack_init (&migrate_btrl_obstack);
1401 if (dump_file)
1403 int i;
1405 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
1407 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1408 fprintf (dump_file,
1409 "Basic block %d: count = %" PRId64
1410 " loop-depth = %d idom = %d\n",
1411 i, (int64_t) bb->count, bb_loop_depth (bb),
1412 get_immediate_dominator (CDI_DOMINATORS, bb)->index);
1416 CLEAR_HARD_REG_SET (all_btrs);
1417 for (first_btr = -1, reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
1418 if (TEST_HARD_REG_BIT (reg_class_contents[(int) btr_class], reg)
1419 && (allow_callee_save || call_used_regs[reg]
1420 || df_regs_ever_live_p (reg)))
1422 SET_HARD_REG_BIT (all_btrs, reg);
1423 last_btr = reg;
1424 if (first_btr < 0)
1425 first_btr = reg;
1428 btrs_live = XCNEWVEC (HARD_REG_SET, last_basic_block_for_fn (cfun));
1429 btrs_live_at_end = XCNEWVEC (HARD_REG_SET, last_basic_block_for_fn (cfun));
1431 build_btr_def_use_webs (&all_btr_defs);
1433 while (!all_btr_defs.empty ())
1435 int min_cost = -all_btr_defs.min_key ();
1436 btr_def def = all_btr_defs.extract_min ();
1437 if (migrate_btr_def (def, min_cost))
1439 all_btr_defs.insert (-def->cost, def);
1440 if (dump_file)
1442 fprintf (dump_file,
1443 "Putting insn %d back on queue with priority %d\n",
1444 INSN_UID (def->insn), def->cost);
1447 else
1448 BITMAP_FREE (def->live_range);
1451 free (btrs_live);
1452 free (btrs_live_at_end);
1453 obstack_free (&migrate_btrl_obstack, NULL);
1456 static void
1457 branch_target_load_optimize (bool after_prologue_epilogue_gen)
1459 enum reg_class klass
1460 = (enum reg_class) targetm.branch_target_register_class ();
1461 if (klass != NO_REGS)
1463 /* Initialize issue_rate. */
1464 if (targetm.sched.issue_rate)
1465 issue_rate = targetm.sched.issue_rate ();
1466 else
1467 issue_rate = 1;
1469 if (!after_prologue_epilogue_gen)
1471 /* Build the CFG for migrate_btr_defs. */
1472 #if 1
1473 /* This may or may not be needed, depending on where we
1474 run this phase. */
1475 cleanup_cfg (optimize ? CLEANUP_EXPENSIVE : 0);
1476 #endif
1478 df_analyze ();
1481 /* Dominator info is also needed for migrate_btr_def. */
1482 calculate_dominance_info (CDI_DOMINATORS);
1483 migrate_btr_defs (klass,
1484 (targetm.branch_target_register_callee_saved
1485 (after_prologue_epilogue_gen)));
1487 free_dominance_info (CDI_DOMINATORS);
1491 namespace {
1493 const pass_data pass_data_branch_target_load_optimize1 =
1495 RTL_PASS, /* type */
1496 "btl1", /* name */
1497 OPTGROUP_NONE, /* optinfo_flags */
1498 TV_NONE, /* tv_id */
1499 0, /* properties_required */
1500 0, /* properties_provided */
1501 0, /* properties_destroyed */
1502 0, /* todo_flags_start */
1503 0, /* todo_flags_finish */
1506 class pass_branch_target_load_optimize1 : public rtl_opt_pass
1508 public:
1509 pass_branch_target_load_optimize1 (gcc::context *ctxt)
1510 : rtl_opt_pass (pass_data_branch_target_load_optimize1, ctxt)
1513 /* opt_pass methods: */
1514 virtual bool gate (function *) { return flag_branch_target_load_optimize; }
1515 virtual unsigned int execute (function *)
1517 branch_target_load_optimize (epilogue_completed);
1518 return 0;
1521 }; // class pass_branch_target_load_optimize1
1523 } // anon namespace
1525 rtl_opt_pass *
1526 make_pass_branch_target_load_optimize1 (gcc::context *ctxt)
1528 return new pass_branch_target_load_optimize1 (ctxt);
1532 namespace {
1534 const pass_data pass_data_branch_target_load_optimize2 =
1536 RTL_PASS, /* type */
1537 "btl2", /* name */
1538 OPTGROUP_NONE, /* optinfo_flags */
1539 TV_NONE, /* tv_id */
1540 0, /* properties_required */
1541 0, /* properties_provided */
1542 0, /* properties_destroyed */
1543 0, /* todo_flags_start */
1544 0, /* todo_flags_finish */
1547 class pass_branch_target_load_optimize2 : public rtl_opt_pass
1549 public:
1550 pass_branch_target_load_optimize2 (gcc::context *ctxt)
1551 : rtl_opt_pass (pass_data_branch_target_load_optimize2, ctxt)
1554 /* opt_pass methods: */
1555 virtual bool gate (function *)
1557 return (optimize > 0 && flag_branch_target_load_optimize2);
1560 virtual unsigned int execute (function *);
1562 }; // class pass_branch_target_load_optimize2
1564 unsigned int
1565 pass_branch_target_load_optimize2::execute (function *)
1567 static int warned = 0;
1569 /* Leave this a warning for now so that it is possible to experiment
1570 with running this pass twice. In 3.6, we should either make this
1571 an error, or use separate dump files. */
1572 if (flag_branch_target_load_optimize
1573 && flag_branch_target_load_optimize2
1574 && !warned)
1576 warning (0, "branch target register load optimization is not intended "
1577 "to be run twice");
1579 warned = 1;
1582 branch_target_load_optimize (epilogue_completed);
1583 return 0;
1586 } // anon namespace
1588 rtl_opt_pass *
1589 make_pass_branch_target_load_optimize2 (gcc::context *ctxt)
1591 return new pass_branch_target_load_optimize2 (ctxt);