decl.c (gnat_to_gnu_entity): For a derived untagged type that renames discriminants...
[gcc.git] / gcc / bt-load.c
1
2 /* Perform branch target register load optimizations.
3 Copyright (C) 2001-2014 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "hard-reg-set.h"
27 #include "regs.h"
28 #include "fibheap.h"
29 #include "target.h"
30 #include "expr.h"
31 #include "flags.h"
32 #include "insn-attr.h"
33 #include "hashtab.h"
34 #include "hash-set.h"
35 #include "vec.h"
36 #include "machmode.h"
37 #include "input.h"
38 #include "function.h"
39 #include "except.h"
40 #include "tm_p.h"
41 #include "diagnostic-core.h"
42 #include "tree-pass.h"
43 #include "recog.h"
44 #include "dominance.h"
45 #include "cfg.h"
46 #include "cfgrtl.h"
47 #include "cfganal.h"
48 #include "cfgcleanup.h"
49 #include "predict.h"
50 #include "basic-block.h"
51 #include "df.h"
52 #include "cfgloop.h"
53 #include "rtl-iter.h"
54
55 /* Target register optimizations - these are performed after reload. */
56
57 typedef struct btr_def_group_s
58 {
59 struct btr_def_group_s *next;
60 rtx src;
61 struct btr_def_s *members;
62 } *btr_def_group;
63
64 typedef struct btr_user_s
65 {
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;
79
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). */
88
89 typedef struct btr_def_s
90 {
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;
124
125 static int issue_rate;
126
127 static int basic_block_freq (const_basic_block);
128 static int insn_sets_btr_p (const rtx_insn *, int, int *);
129 static void find_btr_def_group (btr_def_group *, btr_def);
130 static btr_def add_btr_def (fibheap_t, basic_block, int, rtx_insn *,
131 unsigned int, int, btr_def_group *);
132 static btr_user new_btr_user (basic_block, int, rtx_insn *);
133 static void dump_hard_reg_set (HARD_REG_SET);
134 static void dump_btrs_live (int);
135 static void note_other_use_this_block (unsigned int, btr_user);
136 static void compute_defs_uses_and_gen (fibheap_t, btr_def *,btr_user *,
137 sbitmap *, sbitmap *, HARD_REG_SET *);
138 static void compute_kill (sbitmap *, sbitmap *, HARD_REG_SET *);
139 static void compute_out (sbitmap *bb_out, sbitmap *, sbitmap *, int);
140 static void link_btr_uses (btr_def *, btr_user *, sbitmap *, sbitmap *, int);
141 static void build_btr_def_use_webs (fibheap_t);
142 static int block_at_edge_of_live_range_p (int, btr_def);
143 static void clear_btr_from_live_range (btr_def def);
144 static void add_btr_to_live_range (btr_def, int);
145 static void augment_live_range (bitmap, HARD_REG_SET *, basic_block,
146 basic_block, int);
147 static int choose_btr (HARD_REG_SET);
148 static void combine_btr_defs (btr_def, HARD_REG_SET *);
149 static void btr_def_live_range (btr_def, HARD_REG_SET *);
150 static void move_btr_def (basic_block, int, btr_def, bitmap, HARD_REG_SET *);
151 static int migrate_btr_def (btr_def, int);
152 static void migrate_btr_defs (enum reg_class, int);
153 static int can_move_up (const_basic_block, const rtx_insn *, int);
154 static void note_btr_set (rtx, const_rtx, void *);
155 \f
156 /* The following code performs code motion of target load instructions
157 (instructions that set branch target registers), to move them
158 forward away from the branch instructions and out of loops (or,
159 more generally, from a more frequently executed place to a less
160 frequently executed place).
161 Moving target load instructions further in front of the branch
162 instruction that uses the target register value means that the hardware
163 has a better chance of preloading the instructions at the branch
164 target by the time the branch is reached. This avoids bubbles
165 when a taken branch needs to flush out the pipeline.
166 Moving target load instructions out of loops means they are executed
167 less frequently. */
168
169 /* An obstack to hold the def-use web data structures built up for
170 migrating branch target load instructions. */
171 static struct obstack migrate_btrl_obstack;
172
173 /* Array indexed by basic block number, giving the set of registers
174 live in that block. */
175 static HARD_REG_SET *btrs_live;
176
177 /* Array indexed by basic block number, giving the set of registers live at
178 the end of that block, including any uses by a final jump insn, if any. */
179 static HARD_REG_SET *btrs_live_at_end;
180
181 /* Set of all target registers that we are willing to allocate. */
182 static HARD_REG_SET all_btrs;
183
184 /* Provide lower and upper bounds for target register numbers, so that
185 we don't need to search through all the hard registers all the time. */
186 static int first_btr, last_btr;
187
188
189
190 /* Return an estimate of the frequency of execution of block bb. */
191 static int
192 basic_block_freq (const_basic_block bb)
193 {
194 return bb->frequency;
195 }
196
197 /* If X references (sets or reads) any branch target register, return one
198 such register. If EXCLUDEP is set, disregard any references within
199 that location. */
200 static rtx *
201 find_btr_use (rtx x, rtx *excludep = 0)
202 {
203 subrtx_ptr_iterator::array_type array;
204 FOR_EACH_SUBRTX_PTR (iter, array, &x, NONCONST)
205 {
206 rtx *loc = *iter;
207 if (loc == excludep)
208 iter.skip_subrtxes ();
209 else
210 {
211 const_rtx x = *loc;
212 if (REG_P (x)
213 && overlaps_hard_reg_set_p (all_btrs, GET_MODE (x), REGNO (x)))
214 return loc;
215 }
216 }
217 return 0;
218 }
219
220 /* Return true if insn is an instruction that sets a target register.
221 if CHECK_CONST is true, only return true if the source is constant.
222 If such a set is found and REGNO is nonzero, assign the register number
223 of the destination register to *REGNO. */
224 static int
225 insn_sets_btr_p (const rtx_insn *insn, int check_const, int *regno)
226 {
227 rtx set;
228
229 if (NONJUMP_INSN_P (insn)
230 && (set = single_set (insn)))
231 {
232 rtx dest = SET_DEST (set);
233 rtx src = SET_SRC (set);
234
235 if (GET_CODE (dest) == SUBREG)
236 dest = XEXP (dest, 0);
237
238 if (REG_P (dest)
239 && TEST_HARD_REG_BIT (all_btrs, REGNO (dest)))
240 {
241 gcc_assert (!find_btr_use (src));
242
243 if (!check_const || CONSTANT_P (src))
244 {
245 if (regno)
246 *regno = REGNO (dest);
247 return 1;
248 }
249 }
250 }
251 return 0;
252 }
253
254 /* Find the group that the target register definition DEF belongs
255 to in the list starting with *ALL_BTR_DEF_GROUPS. If no such
256 group exists, create one. Add def to the group. */
257 static void
258 find_btr_def_group (btr_def_group *all_btr_def_groups, btr_def def)
259 {
260 if (insn_sets_btr_p (def->insn, 1, NULL))
261 {
262 btr_def_group this_group;
263 rtx def_src = SET_SRC (single_set (def->insn));
264
265 /* ?? This linear search is an efficiency concern, particularly
266 as the search will almost always fail to find a match. */
267 for (this_group = *all_btr_def_groups;
268 this_group != NULL;
269 this_group = this_group->next)
270 if (rtx_equal_p (def_src, this_group->src))
271 break;
272
273 if (!this_group)
274 {
275 this_group = XOBNEW (&migrate_btrl_obstack, struct btr_def_group_s);
276 this_group->src = def_src;
277 this_group->members = NULL;
278 this_group->next = *all_btr_def_groups;
279 *all_btr_def_groups = this_group;
280 }
281 def->group = this_group;
282 def->next_this_group = this_group->members;
283 this_group->members = def;
284 }
285 else
286 def->group = NULL;
287 }
288
289 /* Create a new target register definition structure, for a definition in
290 block BB, instruction INSN, and insert it into ALL_BTR_DEFS. Return
291 the new definition. */
292 static btr_def
293 add_btr_def (fibheap_t all_btr_defs, basic_block bb, int insn_luid,
294 rtx_insn *insn,
295 unsigned int dest_reg, int other_btr_uses_before_def,
296 btr_def_group *all_btr_def_groups)
297 {
298 btr_def this_def = XOBNEW (&migrate_btrl_obstack, struct btr_def_s);
299 this_def->bb = bb;
300 this_def->luid = insn_luid;
301 this_def->insn = insn;
302 this_def->btr = dest_reg;
303 this_def->cost = basic_block_freq (bb);
304 this_def->has_ambiguous_use = 0;
305 this_def->other_btr_uses_before_def = other_btr_uses_before_def;
306 this_def->other_btr_uses_after_use = 0;
307 this_def->next_this_bb = NULL;
308 this_def->next_this_group = NULL;
309 this_def->uses = NULL;
310 this_def->live_range = NULL;
311 find_btr_def_group (all_btr_def_groups, this_def);
312
313 fibheap_insert (all_btr_defs, -this_def->cost, this_def);
314
315 if (dump_file)
316 fprintf (dump_file,
317 "Found target reg definition: sets %u { bb %d, insn %d }%s priority %d\n",
318 dest_reg, bb->index, INSN_UID (insn),
319 (this_def->group ? "" : ":not const"), this_def->cost);
320
321 return this_def;
322 }
323
324 /* Create a new target register user structure, for a use in block BB,
325 instruction INSN. Return the new user. */
326 static btr_user
327 new_btr_user (basic_block bb, int insn_luid, rtx_insn *insn)
328 {
329 /* This instruction reads target registers. We need
330 to decide whether we can replace all target register
331 uses easily.
332 */
333 rtx *usep = find_btr_use (PATTERN (insn));
334 rtx use;
335 btr_user user = NULL;
336
337 if (usep)
338 {
339 int unambiguous_single_use;
340
341 /* We want to ensure that USE is the only use of a target
342 register in INSN, so that we know that to rewrite INSN to use
343 a different target register, all we have to do is replace USE. */
344 unambiguous_single_use = !find_btr_use (PATTERN (insn), usep);
345 if (!unambiguous_single_use)
346 usep = NULL;
347 }
348 use = usep ? *usep : NULL_RTX;
349 user = XOBNEW (&migrate_btrl_obstack, struct btr_user_s);
350 user->bb = bb;
351 user->luid = insn_luid;
352 user->insn = insn;
353 user->use = use;
354 user->other_use_this_block = 0;
355 user->next = NULL;
356 user->n_reaching_defs = 0;
357 user->first_reaching_def = -1;
358
359 if (dump_file)
360 {
361 fprintf (dump_file, "Uses target reg: { bb %d, insn %d }",
362 bb->index, INSN_UID (insn));
363
364 if (user->use)
365 fprintf (dump_file, ": unambiguous use of reg %d\n",
366 REGNO (user->use));
367 }
368
369 return user;
370 }
371
372 /* Write the contents of S to the dump file. */
373 static void
374 dump_hard_reg_set (HARD_REG_SET s)
375 {
376 int reg;
377 for (reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
378 if (TEST_HARD_REG_BIT (s, reg))
379 fprintf (dump_file, " %d", reg);
380 }
381
382 /* Write the set of target regs live in block BB to the dump file. */
383 static void
384 dump_btrs_live (int bb)
385 {
386 fprintf (dump_file, "BB%d live:", bb);
387 dump_hard_reg_set (btrs_live[bb]);
388 fprintf (dump_file, "\n");
389 }
390
391 /* REGNO is the number of a branch target register that is being used or
392 set. USERS_THIS_BB is a list of preceding branch target register users;
393 If any of them use the same register, set their other_use_this_block
394 flag. */
395 static void
396 note_other_use_this_block (unsigned int regno, btr_user users_this_bb)
397 {
398 btr_user user;
399
400 for (user = users_this_bb; user != NULL; user = user->next)
401 if (user->use && REGNO (user->use) == regno)
402 user->other_use_this_block = 1;
403 }
404
405 typedef struct {
406 btr_user users_this_bb;
407 HARD_REG_SET btrs_written_in_block;
408 HARD_REG_SET btrs_live_in_block;
409 sbitmap bb_gen;
410 sbitmap *btr_defset;
411 } defs_uses_info;
412
413 /* Called via note_stores or directly to register stores into /
414 clobbers of a branch target register DEST that are not recognized as
415 straightforward definitions. DATA points to information about the
416 current basic block that needs updating. */
417 static void
418 note_btr_set (rtx dest, const_rtx set ATTRIBUTE_UNUSED, void *data)
419 {
420 defs_uses_info *info = (defs_uses_info *) data;
421 int regno, end_regno;
422
423 if (!REG_P (dest))
424 return;
425 regno = REGNO (dest);
426 end_regno = END_HARD_REGNO (dest);
427 for (; regno < end_regno; regno++)
428 if (TEST_HARD_REG_BIT (all_btrs, regno))
429 {
430 note_other_use_this_block (regno, info->users_this_bb);
431 SET_HARD_REG_BIT (info->btrs_written_in_block, regno);
432 SET_HARD_REG_BIT (info->btrs_live_in_block, regno);
433 bitmap_and_compl (info->bb_gen, info->bb_gen,
434 info->btr_defset[regno - first_btr]);
435 }
436 }
437
438 static void
439 compute_defs_uses_and_gen (fibheap_t all_btr_defs, btr_def *def_array,
440 btr_user *use_array, sbitmap *btr_defset,
441 sbitmap *bb_gen, HARD_REG_SET *btrs_written)
442 {
443 /* Scan the code building up the set of all defs and all uses.
444 For each target register, build the set of defs of that register.
445 For each block, calculate the set of target registers
446 written in that block.
447 Also calculate the set of btrs ever live in that block.
448 */
449 int i;
450 int insn_luid = 0;
451 btr_def_group all_btr_def_groups = NULL;
452 defs_uses_info info;
453
454 bitmap_vector_clear (bb_gen, last_basic_block_for_fn (cfun));
455 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
456 {
457 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
458 int reg;
459 btr_def defs_this_bb = NULL;
460 rtx_insn *insn;
461 rtx_insn *last;
462 int can_throw = 0;
463
464 info.users_this_bb = NULL;
465 info.bb_gen = bb_gen[i];
466 info.btr_defset = btr_defset;
467
468 CLEAR_HARD_REG_SET (info.btrs_live_in_block);
469 CLEAR_HARD_REG_SET (info.btrs_written_in_block);
470 for (reg = first_btr; reg <= last_btr; reg++)
471 if (TEST_HARD_REG_BIT (all_btrs, reg)
472 && REGNO_REG_SET_P (df_get_live_in (bb), reg))
473 SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
474
475 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
476 insn != last;
477 insn = NEXT_INSN (insn), insn_luid++)
478 {
479 if (INSN_P (insn))
480 {
481 int regno;
482 int insn_uid = INSN_UID (insn);
483
484 if (insn_sets_btr_p (insn, 0, &regno))
485 {
486 btr_def def = add_btr_def (
487 all_btr_defs, bb, insn_luid, insn, regno,
488 TEST_HARD_REG_BIT (info.btrs_live_in_block, regno),
489 &all_btr_def_groups);
490
491 def_array[insn_uid] = def;
492 SET_HARD_REG_BIT (info.btrs_written_in_block, regno);
493 SET_HARD_REG_BIT (info.btrs_live_in_block, regno);
494 bitmap_and_compl (bb_gen[i], bb_gen[i],
495 btr_defset[regno - first_btr]);
496 bitmap_set_bit (bb_gen[i], insn_uid);
497 def->next_this_bb = defs_this_bb;
498 defs_this_bb = def;
499 bitmap_set_bit (btr_defset[regno - first_btr], insn_uid);
500 note_other_use_this_block (regno, info.users_this_bb);
501 }
502 /* Check for the blockage emitted by expand_nl_goto_receiver. */
503 else if (cfun->has_nonlocal_label
504 && GET_CODE (PATTERN (insn)) == UNSPEC_VOLATILE)
505 {
506 btr_user user;
507
508 /* Do the equivalent of calling note_other_use_this_block
509 for every target register. */
510 for (user = info.users_this_bb; user != NULL;
511 user = user->next)
512 if (user->use)
513 user->other_use_this_block = 1;
514 IOR_HARD_REG_SET (info.btrs_written_in_block, all_btrs);
515 IOR_HARD_REG_SET (info.btrs_live_in_block, all_btrs);
516 bitmap_clear (info.bb_gen);
517 }
518 else
519 {
520 if (find_btr_use (PATTERN (insn)))
521 {
522 btr_user user = new_btr_user (bb, insn_luid, insn);
523
524 use_array[insn_uid] = user;
525 if (user->use)
526 SET_HARD_REG_BIT (info.btrs_live_in_block,
527 REGNO (user->use));
528 else
529 {
530 int reg;
531 for (reg = first_btr; reg <= last_btr; reg++)
532 if (TEST_HARD_REG_BIT (all_btrs, reg)
533 && refers_to_regno_p (reg, reg + 1, user->insn,
534 NULL))
535 {
536 note_other_use_this_block (reg,
537 info.users_this_bb);
538 SET_HARD_REG_BIT (info.btrs_live_in_block, reg);
539 }
540 note_stores (PATTERN (insn), note_btr_set, &info);
541 }
542 user->next = info.users_this_bb;
543 info.users_this_bb = user;
544 }
545 if (CALL_P (insn))
546 {
547 HARD_REG_SET *clobbered = &call_used_reg_set;
548 HARD_REG_SET call_saved;
549 rtx pat = PATTERN (insn);
550 int i;
551
552 /* Check for sibcall. */
553 if (GET_CODE (pat) == PARALLEL)
554 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
555 if (ANY_RETURN_P (XVECEXP (pat, 0, i)))
556 {
557 COMPL_HARD_REG_SET (call_saved,
558 call_used_reg_set);
559 clobbered = &call_saved;
560 }
561
562 for (regno = first_btr; regno <= last_btr; regno++)
563 if (TEST_HARD_REG_BIT (*clobbered, regno))
564 note_btr_set (regno_reg_rtx[regno], NULL_RTX, &info);
565 }
566 }
567 }
568 }
569
570 COPY_HARD_REG_SET (btrs_live[i], info.btrs_live_in_block);
571 COPY_HARD_REG_SET (btrs_written[i], info.btrs_written_in_block);
572
573 REG_SET_TO_HARD_REG_SET (btrs_live_at_end[i], df_get_live_out (bb));
574 /* If this block ends in a jump insn, add any uses or even clobbers
575 of branch target registers that it might have. */
576 for (insn = BB_END (bb); insn != BB_HEAD (bb) && ! INSN_P (insn); )
577 insn = PREV_INSN (insn);
578 /* ??? for the fall-through edge, it would make sense to insert the
579 btr set on the edge, but that would require to split the block
580 early on so that we can distinguish between dominance from the fall
581 through edge - which can use the call-clobbered registers - from
582 dominance by the throw edge. */
583 if (can_throw_internal (insn))
584 {
585 HARD_REG_SET tmp;
586
587 COPY_HARD_REG_SET (tmp, call_used_reg_set);
588 AND_HARD_REG_SET (tmp, all_btrs);
589 IOR_HARD_REG_SET (btrs_live_at_end[i], tmp);
590 can_throw = 1;
591 }
592 if (can_throw || JUMP_P (insn))
593 {
594 int regno;
595
596 for (regno = first_btr; regno <= last_btr; regno++)
597 if (refers_to_regno_p (regno, regno+1, insn, NULL))
598 SET_HARD_REG_BIT (btrs_live_at_end[i], regno);
599 }
600
601 if (dump_file)
602 dump_btrs_live (i);
603 }
604 }
605
606 static void
607 compute_kill (sbitmap *bb_kill, sbitmap *btr_defset,
608 HARD_REG_SET *btrs_written)
609 {
610 int i;
611 int regno;
612
613 /* For each basic block, form the set BB_KILL - the set
614 of definitions that the block kills. */
615 bitmap_vector_clear (bb_kill, last_basic_block_for_fn (cfun));
616 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
617 {
618 for (regno = first_btr; regno <= last_btr; regno++)
619 if (TEST_HARD_REG_BIT (all_btrs, regno)
620 && TEST_HARD_REG_BIT (btrs_written[i], regno))
621 bitmap_ior (bb_kill[i], bb_kill[i],
622 btr_defset[regno - first_btr]);
623 }
624 }
625
626 static void
627 compute_out (sbitmap *bb_out, sbitmap *bb_gen, sbitmap *bb_kill, int max_uid)
628 {
629 /* Perform iterative dataflow:
630 Initially, for all blocks, BB_OUT = BB_GEN.
631 For each block,
632 BB_IN = union over predecessors of BB_OUT(pred)
633 BB_OUT = (BB_IN - BB_KILL) + BB_GEN
634 Iterate until the bb_out sets stop growing. */
635 int i;
636 int changed;
637 sbitmap bb_in = sbitmap_alloc (max_uid);
638
639 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
640 bitmap_copy (bb_out[i], bb_gen[i]);
641
642 changed = 1;
643 while (changed)
644 {
645 changed = 0;
646 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
647 {
648 bitmap_union_of_preds (bb_in, bb_out, BASIC_BLOCK_FOR_FN (cfun, i));
649 changed |= bitmap_ior_and_compl (bb_out[i], bb_gen[i],
650 bb_in, bb_kill[i]);
651 }
652 }
653 sbitmap_free (bb_in);
654 }
655
656 static void
657 link_btr_uses (btr_def *def_array, btr_user *use_array, sbitmap *bb_out,
658 sbitmap *btr_defset, int max_uid)
659 {
660 int i;
661 sbitmap reaching_defs = sbitmap_alloc (max_uid);
662
663 /* Link uses to the uses lists of all of their reaching defs.
664 Count up the number of reaching defs of each use. */
665 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
666 {
667 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
668 rtx_insn *insn;
669 rtx_insn *last;
670
671 bitmap_union_of_preds (reaching_defs, bb_out, BASIC_BLOCK_FOR_FN (cfun, i));
672 for (insn = BB_HEAD (bb), last = NEXT_INSN (BB_END (bb));
673 insn != last;
674 insn = NEXT_INSN (insn))
675 {
676 if (INSN_P (insn))
677 {
678 int insn_uid = INSN_UID (insn);
679
680 btr_def def = def_array[insn_uid];
681 btr_user user = use_array[insn_uid];
682 if (def != NULL)
683 {
684 /* Remove all reaching defs of regno except
685 for this one. */
686 bitmap_and_compl (reaching_defs, reaching_defs,
687 btr_defset[def->btr - first_btr]);
688 bitmap_set_bit (reaching_defs, insn_uid);
689 }
690
691 if (user != NULL)
692 {
693 /* Find all the reaching defs for this use. */
694 sbitmap reaching_defs_of_reg = sbitmap_alloc (max_uid);
695 unsigned int uid = 0;
696 sbitmap_iterator sbi;
697
698 if (user->use)
699 bitmap_and (
700 reaching_defs_of_reg,
701 reaching_defs,
702 btr_defset[REGNO (user->use) - first_btr]);
703 else
704 {
705 int reg;
706
707 bitmap_clear (reaching_defs_of_reg);
708 for (reg = first_btr; reg <= last_btr; reg++)
709 if (TEST_HARD_REG_BIT (all_btrs, reg)
710 && refers_to_regno_p (reg, reg + 1, user->insn,
711 NULL))
712 bitmap_or_and (reaching_defs_of_reg,
713 reaching_defs_of_reg,
714 reaching_defs,
715 btr_defset[reg - first_btr]);
716 }
717 EXECUTE_IF_SET_IN_BITMAP (reaching_defs_of_reg, 0, uid, sbi)
718 {
719 btr_def def = def_array[uid];
720
721 /* We now know that def reaches user. */
722
723 if (dump_file)
724 fprintf (dump_file,
725 "Def in insn %d reaches use in insn %d\n",
726 uid, insn_uid);
727
728 user->n_reaching_defs++;
729 if (!user->use)
730 def->has_ambiguous_use = 1;
731 if (user->first_reaching_def != -1)
732 { /* There is more than one reaching def. This is
733 a rare case, so just give up on this def/use
734 web when it occurs. */
735 def->has_ambiguous_use = 1;
736 def_array[user->first_reaching_def]
737 ->has_ambiguous_use = 1;
738 if (dump_file)
739 fprintf (dump_file,
740 "(use %d has multiple reaching defs)\n",
741 insn_uid);
742 }
743 else
744 user->first_reaching_def = uid;
745 if (user->other_use_this_block)
746 def->other_btr_uses_after_use = 1;
747 user->next = def->uses;
748 def->uses = user;
749 }
750 sbitmap_free (reaching_defs_of_reg);
751 }
752
753 if (CALL_P (insn))
754 {
755 int regno;
756
757 for (regno = first_btr; regno <= last_btr; regno++)
758 if (TEST_HARD_REG_BIT (all_btrs, regno)
759 && TEST_HARD_REG_BIT (call_used_reg_set, regno))
760 bitmap_and_compl (reaching_defs, reaching_defs,
761 btr_defset[regno - first_btr]);
762 }
763 }
764 }
765 }
766 sbitmap_free (reaching_defs);
767 }
768
769 static void
770 build_btr_def_use_webs (fibheap_t all_btr_defs)
771 {
772 const int max_uid = get_max_uid ();
773 btr_def *def_array = XCNEWVEC (btr_def, max_uid);
774 btr_user *use_array = XCNEWVEC (btr_user, max_uid);
775 sbitmap *btr_defset = sbitmap_vector_alloc (
776 (last_btr - first_btr) + 1, max_uid);
777 sbitmap *bb_gen = sbitmap_vector_alloc (last_basic_block_for_fn (cfun),
778 max_uid);
779 HARD_REG_SET *btrs_written = XCNEWVEC (HARD_REG_SET,
780 last_basic_block_for_fn (cfun));
781 sbitmap *bb_kill;
782 sbitmap *bb_out;
783
784 bitmap_vector_clear (btr_defset, (last_btr - first_btr) + 1);
785
786 compute_defs_uses_and_gen (all_btr_defs, def_array, use_array, btr_defset,
787 bb_gen, btrs_written);
788
789 bb_kill = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), max_uid);
790 compute_kill (bb_kill, btr_defset, btrs_written);
791 free (btrs_written);
792
793 bb_out = sbitmap_vector_alloc (last_basic_block_for_fn (cfun), max_uid);
794 compute_out (bb_out, bb_gen, bb_kill, max_uid);
795
796 sbitmap_vector_free (bb_gen);
797 sbitmap_vector_free (bb_kill);
798
799 link_btr_uses (def_array, use_array, bb_out, btr_defset, max_uid);
800
801 sbitmap_vector_free (bb_out);
802 sbitmap_vector_free (btr_defset);
803 free (use_array);
804 free (def_array);
805 }
806
807 /* Return true if basic block BB contains the start or end of the
808 live range of the definition DEF, AND there are other live
809 ranges of the same target register that include BB. */
810 static int
811 block_at_edge_of_live_range_p (int bb, btr_def def)
812 {
813 if (def->other_btr_uses_before_def
814 && BASIC_BLOCK_FOR_FN (cfun, bb) == def->bb)
815 return 1;
816 else if (def->other_btr_uses_after_use)
817 {
818 btr_user user;
819 for (user = def->uses; user != NULL; user = user->next)
820 if (BASIC_BLOCK_FOR_FN (cfun, bb) == user->bb)
821 return 1;
822 }
823 return 0;
824 }
825
826 /* We are removing the def/use web DEF. The target register
827 used in this web is therefore no longer live in the live range
828 of this web, so remove it from the live set of all basic blocks
829 in the live range of the web.
830 Blocks at the boundary of the live range may contain other live
831 ranges for the same target register, so we have to be careful
832 to remove the target register from the live set of these blocks
833 only if they do not contain other live ranges for the same register. */
834 static void
835 clear_btr_from_live_range (btr_def def)
836 {
837 unsigned bb;
838 bitmap_iterator bi;
839
840 EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
841 {
842 if ((!def->other_btr_uses_before_def
843 && !def->other_btr_uses_after_use)
844 || !block_at_edge_of_live_range_p (bb, def))
845 {
846 CLEAR_HARD_REG_BIT (btrs_live[bb], def->btr);
847 CLEAR_HARD_REG_BIT (btrs_live_at_end[bb], def->btr);
848 if (dump_file)
849 dump_btrs_live (bb);
850 }
851 }
852 if (def->own_end)
853 CLEAR_HARD_REG_BIT (btrs_live_at_end[def->bb->index], def->btr);
854 }
855
856
857 /* We are adding the def/use web DEF. Add the target register used
858 in this web to the live set of all of the basic blocks that contain
859 the live range of the web.
860 If OWN_END is set, also show that the register is live from our
861 definitions at the end of the basic block where it is defined. */
862 static void
863 add_btr_to_live_range (btr_def def, int own_end)
864 {
865 unsigned bb;
866 bitmap_iterator bi;
867
868 EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
869 {
870 SET_HARD_REG_BIT (btrs_live[bb], def->btr);
871 SET_HARD_REG_BIT (btrs_live_at_end[bb], def->btr);
872 if (dump_file)
873 dump_btrs_live (bb);
874 }
875 if (own_end)
876 {
877 SET_HARD_REG_BIT (btrs_live_at_end[def->bb->index], def->btr);
878 def->own_end = 1;
879 }
880 }
881
882 /* Update a live range to contain the basic block NEW_BLOCK, and all
883 blocks on paths between the existing live range and NEW_BLOCK.
884 HEAD is a block contained in the existing live range that dominates
885 all other blocks in the existing live range.
886 Also add to the set BTRS_LIVE_IN_RANGE all target registers that
887 are live in the blocks that we add to the live range.
888 If FULL_RANGE is set, include the full live range of NEW_BB;
889 otherwise, if NEW_BB dominates HEAD_BB, only add registers that
890 are life at the end of NEW_BB for NEW_BB itself.
891 It is a precondition that either NEW_BLOCK dominates HEAD,or
892 HEAD dom NEW_BLOCK. This is used to speed up the
893 implementation of this function. */
894 static void
895 augment_live_range (bitmap live_range, HARD_REG_SET *btrs_live_in_range,
896 basic_block head_bb, basic_block new_bb, int full_range)
897 {
898 basic_block *worklist, *tos;
899
900 tos = worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) + 1);
901
902 if (dominated_by_p (CDI_DOMINATORS, new_bb, head_bb))
903 {
904 if (new_bb == head_bb)
905 {
906 if (full_range)
907 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[new_bb->index]);
908 free (tos);
909 return;
910 }
911 *tos++ = new_bb;
912 }
913 else
914 {
915 edge e;
916 edge_iterator ei;
917 int new_block = new_bb->index;
918
919 gcc_assert (dominated_by_p (CDI_DOMINATORS, head_bb, new_bb));
920
921 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[head_bb->index]);
922 bitmap_set_bit (live_range, new_block);
923 /* A previous btr migration could have caused a register to be
924 live just at the end of new_block which we need in full, so
925 use trs_live_at_end even if full_range is set. */
926 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live_at_end[new_block]);
927 if (full_range)
928 IOR_HARD_REG_SET (*btrs_live_in_range, btrs_live[new_block]);
929 if (dump_file)
930 {
931 fprintf (dump_file,
932 "Adding end of block %d and rest of %d to live range\n",
933 new_block, head_bb->index);
934 fprintf (dump_file,"Now live btrs are ");
935 dump_hard_reg_set (*btrs_live_in_range);
936 fprintf (dump_file, "\n");
937 }
938 FOR_EACH_EDGE (e, ei, head_bb->preds)
939 *tos++ = e->src;
940 }
941
942 while (tos != worklist)
943 {
944 basic_block bb = *--tos;
945 if (!bitmap_bit_p (live_range, bb->index))
946 {
947 edge e;
948 edge_iterator ei;
949
950 bitmap_set_bit (live_range, bb->index);
951 IOR_HARD_REG_SET (*btrs_live_in_range,
952 btrs_live[bb->index]);
953 /* A previous btr migration could have caused a register to be
954 live just at the end of a block which we need in full. */
955 IOR_HARD_REG_SET (*btrs_live_in_range,
956 btrs_live_at_end[bb->index]);
957 if (dump_file)
958 {
959 fprintf (dump_file,
960 "Adding block %d to live range\n", bb->index);
961 fprintf (dump_file,"Now live btrs are ");
962 dump_hard_reg_set (*btrs_live_in_range);
963 fprintf (dump_file, "\n");
964 }
965
966 FOR_EACH_EDGE (e, ei, bb->preds)
967 {
968 basic_block pred = e->src;
969 if (!bitmap_bit_p (live_range, pred->index))
970 *tos++ = pred;
971 }
972 }
973 }
974
975 free (worklist);
976 }
977
978 /* Return the most desirable target register that is not in
979 the set USED_BTRS. */
980 static int
981 choose_btr (HARD_REG_SET used_btrs)
982 {
983 int i;
984
985 if (!hard_reg_set_subset_p (all_btrs, used_btrs))
986 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
987 {
988 #ifdef REG_ALLOC_ORDER
989 int regno = reg_alloc_order[i];
990 #else
991 int regno = i;
992 #endif
993 if (TEST_HARD_REG_BIT (all_btrs, regno)
994 && !TEST_HARD_REG_BIT (used_btrs, regno))
995 return regno;
996 }
997 return -1;
998 }
999
1000 /* Calculate the set of basic blocks that contain the live range of
1001 the def/use web DEF.
1002 Also calculate the set of target registers that are live at time
1003 in this live range, but ignore the live range represented by DEF
1004 when calculating this set. */
1005 static void
1006 btr_def_live_range (btr_def def, HARD_REG_SET *btrs_live_in_range)
1007 {
1008 if (!def->live_range)
1009 {
1010 btr_user user;
1011
1012 def->live_range = BITMAP_ALLOC (NULL);
1013
1014 bitmap_set_bit (def->live_range, def->bb->index);
1015 COPY_HARD_REG_SET (*btrs_live_in_range,
1016 (flag_btr_bb_exclusive
1017 ? btrs_live : btrs_live_at_end)[def->bb->index]);
1018
1019 for (user = def->uses; user != NULL; user = user->next)
1020 augment_live_range (def->live_range, btrs_live_in_range,
1021 def->bb, user->bb,
1022 (flag_btr_bb_exclusive
1023 || user->insn != BB_END (def->bb)
1024 || !JUMP_P (user->insn)));
1025 }
1026 else
1027 {
1028 /* def->live_range is accurate, but we need to recompute
1029 the set of target registers live over it, because migration
1030 of other PT instructions may have affected it.
1031 */
1032 unsigned bb;
1033 unsigned def_bb = flag_btr_bb_exclusive ? -1 : def->bb->index;
1034 bitmap_iterator bi;
1035
1036 CLEAR_HARD_REG_SET (*btrs_live_in_range);
1037 EXECUTE_IF_SET_IN_BITMAP (def->live_range, 0, bb, bi)
1038 {
1039 IOR_HARD_REG_SET (*btrs_live_in_range,
1040 (def_bb == bb
1041 ? btrs_live_at_end : btrs_live) [bb]);
1042 }
1043 }
1044 if (!def->other_btr_uses_before_def &&
1045 !def->other_btr_uses_after_use)
1046 CLEAR_HARD_REG_BIT (*btrs_live_in_range, def->btr);
1047 }
1048
1049 /* Merge into the def/use web DEF any other def/use webs in the same
1050 group that are dominated by DEF, provided that there is a target
1051 register available to allocate to the merged web. */
1052 static void
1053 combine_btr_defs (btr_def def, HARD_REG_SET *btrs_live_in_range)
1054 {
1055 btr_def other_def;
1056
1057 for (other_def = def->group->members;
1058 other_def != NULL;
1059 other_def = other_def->next_this_group)
1060 {
1061 if (other_def != def
1062 && other_def->uses != NULL
1063 && ! other_def->has_ambiguous_use
1064 && dominated_by_p (CDI_DOMINATORS, other_def->bb, def->bb))
1065 {
1066 /* def->bb dominates the other def, so def and other_def could
1067 be combined. */
1068 /* Merge their live ranges, and get the set of
1069 target registers live over the merged range. */
1070 int btr;
1071 HARD_REG_SET combined_btrs_live;
1072 bitmap combined_live_range = BITMAP_ALLOC (NULL);
1073 btr_user user;
1074
1075 if (other_def->live_range == NULL)
1076 {
1077 HARD_REG_SET dummy_btrs_live_in_range;
1078 btr_def_live_range (other_def, &dummy_btrs_live_in_range);
1079 }
1080 COPY_HARD_REG_SET (combined_btrs_live, *btrs_live_in_range);
1081 bitmap_copy (combined_live_range, def->live_range);
1082
1083 for (user = other_def->uses; user != NULL; user = user->next)
1084 augment_live_range (combined_live_range, &combined_btrs_live,
1085 def->bb, user->bb,
1086 (flag_btr_bb_exclusive
1087 || user->insn != BB_END (def->bb)
1088 || !JUMP_P (user->insn)));
1089
1090 btr = choose_btr (combined_btrs_live);
1091 if (btr != -1)
1092 {
1093 /* We can combine them. */
1094 if (dump_file)
1095 fprintf (dump_file,
1096 "Combining def in insn %d with def in insn %d\n",
1097 INSN_UID (other_def->insn), INSN_UID (def->insn));
1098
1099 def->btr = btr;
1100 user = other_def->uses;
1101 while (user != NULL)
1102 {
1103 btr_user next = user->next;
1104
1105 user->next = def->uses;
1106 def->uses = user;
1107 user = next;
1108 }
1109 /* Combining def/use webs can make target registers live
1110 after uses where they previously were not. This means
1111 some REG_DEAD notes may no longer be correct. We could
1112 be more precise about this if we looked at the combined
1113 live range, but here I just delete any REG_DEAD notes
1114 in case they are no longer correct. */
1115 for (user = def->uses; user != NULL; user = user->next)
1116 remove_note (user->insn,
1117 find_regno_note (user->insn, REG_DEAD,
1118 REGNO (user->use)));
1119 clear_btr_from_live_range (other_def);
1120 other_def->uses = NULL;
1121 bitmap_copy (def->live_range, combined_live_range);
1122 if (other_def->btr == btr && other_def->other_btr_uses_after_use)
1123 def->other_btr_uses_after_use = 1;
1124 COPY_HARD_REG_SET (*btrs_live_in_range, combined_btrs_live);
1125
1126 /* Delete the old target register initialization. */
1127 delete_insn (other_def->insn);
1128
1129 }
1130 BITMAP_FREE (combined_live_range);
1131 }
1132 }
1133 }
1134
1135 /* Move the definition DEF from its current position to basic
1136 block NEW_DEF_BB, and modify it to use branch target register BTR.
1137 Delete the old defining insn, and insert a new one in NEW_DEF_BB.
1138 Update all reaching uses of DEF in the RTL to use BTR.
1139 If this new position means that other defs in the
1140 same group can be combined with DEF then combine them. */
1141 static void
1142 move_btr_def (basic_block new_def_bb, int btr, btr_def def, bitmap live_range,
1143 HARD_REG_SET *btrs_live_in_range)
1144 {
1145 /* We can move the instruction.
1146 Set a target register in block NEW_DEF_BB to the value
1147 needed for this target register definition.
1148 Replace all uses of the old target register definition by
1149 uses of the new definition. Delete the old definition. */
1150 basic_block b = new_def_bb;
1151 rtx_insn *insp = BB_HEAD (b);
1152 rtx_insn *old_insn = def->insn;
1153 rtx src;
1154 rtx btr_rtx;
1155 rtx_insn *new_insn;
1156 machine_mode btr_mode;
1157 btr_user user;
1158 rtx set;
1159
1160 if (dump_file)
1161 fprintf(dump_file, "migrating to basic block %d, using reg %d\n",
1162 new_def_bb->index, btr);
1163
1164 clear_btr_from_live_range (def);
1165 def->btr = btr;
1166 def->bb = new_def_bb;
1167 def->luid = 0;
1168 def->cost = basic_block_freq (new_def_bb);
1169 bitmap_copy (def->live_range, live_range);
1170 combine_btr_defs (def, btrs_live_in_range);
1171 btr = def->btr;
1172 def->other_btr_uses_before_def
1173 = TEST_HARD_REG_BIT (btrs_live[b->index], btr) ? 1 : 0;
1174 add_btr_to_live_range (def, 1);
1175 if (LABEL_P (insp))
1176 insp = NEXT_INSN (insp);
1177 /* N.B.: insp is expected to be NOTE_INSN_BASIC_BLOCK now. Some
1178 optimizations can result in insp being both first and last insn of
1179 its basic block. */
1180 /* ?? some assertions to check that insp is sensible? */
1181
1182 if (def->other_btr_uses_before_def)
1183 {
1184 insp = BB_END (b);
1185 for (insp = BB_END (b); ! INSN_P (insp); insp = PREV_INSN (insp))
1186 gcc_assert (insp != BB_HEAD (b));
1187
1188 if (JUMP_P (insp) || can_throw_internal (insp))
1189 insp = PREV_INSN (insp);
1190 }
1191
1192 set = single_set (old_insn);
1193 src = SET_SRC (set);
1194 btr_mode = GET_MODE (SET_DEST (set));
1195 btr_rtx = gen_rtx_REG (btr_mode, btr);
1196
1197 new_insn = as_a <rtx_insn *> (gen_move_insn (btr_rtx, src));
1198
1199 /* Insert target register initialization at head of basic block. */
1200 def->insn = emit_insn_after (new_insn, insp);
1201
1202 df_set_regs_ever_live (btr, true);
1203
1204 if (dump_file)
1205 fprintf (dump_file, "New pt is insn %d, inserted after insn %d\n",
1206 INSN_UID (def->insn), INSN_UID (insp));
1207
1208 /* Delete the old target register initialization. */
1209 delete_insn (old_insn);
1210
1211 /* Replace each use of the old target register by a use of the new target
1212 register. */
1213 for (user = def->uses; user != NULL; user = user->next)
1214 {
1215 /* Some extra work here to ensure consistent modes, because
1216 it seems that a target register REG rtx can be given a different
1217 mode depending on the context (surely that should not be
1218 the case?). */
1219 rtx replacement_rtx;
1220 if (GET_MODE (user->use) == GET_MODE (btr_rtx)
1221 || GET_MODE (user->use) == VOIDmode)
1222 replacement_rtx = btr_rtx;
1223 else
1224 replacement_rtx = gen_rtx_REG (GET_MODE (user->use), btr);
1225 validate_replace_rtx (user->use, replacement_rtx, user->insn);
1226 user->use = replacement_rtx;
1227 }
1228 }
1229
1230 /* We anticipate intra-block scheduling to be done. See if INSN could move
1231 up within BB by N_INSNS. */
1232 static int
1233 can_move_up (const_basic_block bb, const rtx_insn *insn, int n_insns)
1234 {
1235 while (insn != BB_HEAD (bb) && n_insns > 0)
1236 {
1237 insn = PREV_INSN (insn);
1238 /* ??? What if we have an anti-dependency that actually prevents the
1239 scheduler from doing the move? We'd like to re-allocate the register,
1240 but not necessarily put the load into another basic block. */
1241 if (INSN_P (insn))
1242 n_insns--;
1243 }
1244 return n_insns <= 0;
1245 }
1246
1247 /* Attempt to migrate the target register definition DEF to an
1248 earlier point in the flowgraph.
1249
1250 It is a precondition of this function that DEF is migratable:
1251 i.e. it has a constant source, and all uses are unambiguous.
1252
1253 Only migrations that reduce the cost of DEF will be made.
1254 MIN_COST is the lower bound on the cost of the DEF after migration.
1255 If we migrate DEF so that its cost falls below MIN_COST,
1256 then we do not attempt to migrate further. The idea is that
1257 we migrate definitions in a priority order based on their cost,
1258 when the cost of this definition falls below MIN_COST, then
1259 there is another definition with cost == MIN_COST which now
1260 has a higher priority than this definition.
1261
1262 Return nonzero if there may be benefit from attempting to
1263 migrate this DEF further (i.e. we have reduced the cost below
1264 MIN_COST, but we may be able to reduce it further).
1265 Return zero if no further migration is possible. */
1266 static int
1267 migrate_btr_def (btr_def def, int min_cost)
1268 {
1269 bitmap live_range;
1270 HARD_REG_SET btrs_live_in_range;
1271 int btr_used_near_def = 0;
1272 int def_basic_block_freq;
1273 basic_block attempt;
1274 int give_up = 0;
1275 int def_moved = 0;
1276 btr_user user;
1277 int def_latency;
1278
1279 if (dump_file)
1280 fprintf (dump_file,
1281 "Attempting to migrate pt from insn %d (cost = %d, min_cost = %d) ... ",
1282 INSN_UID (def->insn), def->cost, min_cost);
1283
1284 if (!def->group || def->has_ambiguous_use)
1285 /* These defs are not migratable. */
1286 {
1287 if (dump_file)
1288 fprintf (dump_file, "it's not migratable\n");
1289 return 0;
1290 }
1291
1292 if (!def->uses)
1293 /* We have combined this def with another in the same group, so
1294 no need to consider it further.
1295 */
1296 {
1297 if (dump_file)
1298 fprintf (dump_file, "it's already combined with another pt\n");
1299 return 0;
1300 }
1301
1302 btr_def_live_range (def, &btrs_live_in_range);
1303 live_range = BITMAP_ALLOC (NULL);
1304 bitmap_copy (live_range, def->live_range);
1305
1306 #ifdef INSN_SCHEDULING
1307 def_latency = insn_default_latency (def->insn) * issue_rate;
1308 #else
1309 def_latency = issue_rate;
1310 #endif
1311
1312 for (user = def->uses; user != NULL; user = user->next)
1313 {
1314 if (user->bb == def->bb
1315 && user->luid > def->luid
1316 && (def->luid + def_latency) > user->luid
1317 && ! can_move_up (def->bb, def->insn,
1318 (def->luid + def_latency) - user->luid))
1319 {
1320 btr_used_near_def = 1;
1321 break;
1322 }
1323 }
1324
1325 def_basic_block_freq = basic_block_freq (def->bb);
1326
1327 for (attempt = get_immediate_dominator (CDI_DOMINATORS, def->bb);
1328 !give_up && attempt && attempt != ENTRY_BLOCK_PTR_FOR_FN (cfun)
1329 && def->cost >= min_cost;
1330 attempt = get_immediate_dominator (CDI_DOMINATORS, attempt))
1331 {
1332 /* Try to move the instruction that sets the target register into
1333 basic block ATTEMPT. */
1334 int try_freq = basic_block_freq (attempt);
1335 edge_iterator ei;
1336 edge e;
1337
1338 /* If ATTEMPT has abnormal edges, skip it. */
1339 FOR_EACH_EDGE (e, ei, attempt->succs)
1340 if (e->flags & EDGE_COMPLEX)
1341 break;
1342 if (e)
1343 continue;
1344
1345 if (dump_file)
1346 fprintf (dump_file, "trying block %d ...", attempt->index);
1347
1348 if (try_freq < def_basic_block_freq
1349 || (try_freq == def_basic_block_freq && btr_used_near_def))
1350 {
1351 int btr;
1352 augment_live_range (live_range, &btrs_live_in_range, def->bb, attempt,
1353 flag_btr_bb_exclusive);
1354 if (dump_file)
1355 {
1356 fprintf (dump_file, "Now btrs live in range are: ");
1357 dump_hard_reg_set (btrs_live_in_range);
1358 fprintf (dump_file, "\n");
1359 }
1360 btr = choose_btr (btrs_live_in_range);
1361 if (btr != -1)
1362 {
1363 move_btr_def (attempt, btr, def, live_range, &btrs_live_in_range);
1364 bitmap_copy (live_range, def->live_range);
1365 btr_used_near_def = 0;
1366 def_moved = 1;
1367 def_basic_block_freq = basic_block_freq (def->bb);
1368 }
1369 else
1370 {
1371 /* There are no free target registers available to move
1372 this far forward, so give up */
1373 give_up = 1;
1374 if (dump_file)
1375 fprintf (dump_file,
1376 "giving up because there are no free target registers\n");
1377 }
1378
1379 }
1380 }
1381 if (!def_moved)
1382 {
1383 give_up = 1;
1384 if (dump_file)
1385 fprintf (dump_file, "failed to move\n");
1386 }
1387 BITMAP_FREE (live_range);
1388 return !give_up;
1389 }
1390
1391 /* Attempt to move instructions that set target registers earlier
1392 in the flowgraph, away from their corresponding uses. */
1393 static void
1394 migrate_btr_defs (enum reg_class btr_class, int allow_callee_save)
1395 {
1396 fibheap_t all_btr_defs = fibheap_new ();
1397 int reg;
1398
1399 gcc_obstack_init (&migrate_btrl_obstack);
1400 if (dump_file)
1401 {
1402 int i;
1403
1404 for (i = NUM_FIXED_BLOCKS; i < last_basic_block_for_fn (cfun); i++)
1405 {
1406 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
1407 fprintf (dump_file,
1408 "Basic block %d: count = %" PRId64
1409 " loop-depth = %d idom = %d\n",
1410 i, (int64_t) bb->count, bb_loop_depth (bb),
1411 get_immediate_dominator (CDI_DOMINATORS, bb)->index);
1412 }
1413 }
1414
1415 CLEAR_HARD_REG_SET (all_btrs);
1416 for (first_btr = -1, reg = 0; reg < FIRST_PSEUDO_REGISTER; reg++)
1417 if (TEST_HARD_REG_BIT (reg_class_contents[(int) btr_class], reg)
1418 && (allow_callee_save || call_used_regs[reg]
1419 || df_regs_ever_live_p (reg)))
1420 {
1421 SET_HARD_REG_BIT (all_btrs, reg);
1422 last_btr = reg;
1423 if (first_btr < 0)
1424 first_btr = reg;
1425 }
1426
1427 btrs_live = XCNEWVEC (HARD_REG_SET, last_basic_block_for_fn (cfun));
1428 btrs_live_at_end = XCNEWVEC (HARD_REG_SET, last_basic_block_for_fn (cfun));
1429
1430 build_btr_def_use_webs (all_btr_defs);
1431
1432 while (!fibheap_empty (all_btr_defs))
1433 {
1434 btr_def def = (btr_def) fibheap_extract_min (all_btr_defs);
1435 int min_cost = -fibheap_min_key (all_btr_defs);
1436 if (migrate_btr_def (def, min_cost))
1437 {
1438 fibheap_insert (all_btr_defs, -def->cost, (void *) def);
1439 if (dump_file)
1440 {
1441 fprintf (dump_file,
1442 "Putting insn %d back on queue with priority %d\n",
1443 INSN_UID (def->insn), def->cost);
1444 }
1445 }
1446 else
1447 BITMAP_FREE (def->live_range);
1448 }
1449
1450 free (btrs_live);
1451 free (btrs_live_at_end);
1452 obstack_free (&migrate_btrl_obstack, NULL);
1453 fibheap_delete (all_btr_defs);
1454 }
1455
1456 static void
1457 branch_target_load_optimize (bool after_prologue_epilogue_gen)
1458 {
1459 enum reg_class klass
1460 = (enum reg_class) targetm.branch_target_register_class ();
1461 if (klass != NO_REGS)
1462 {
1463 /* Initialize issue_rate. */
1464 if (targetm.sched.issue_rate)
1465 issue_rate = targetm.sched.issue_rate ();
1466 else
1467 issue_rate = 1;
1468
1469 if (!after_prologue_epilogue_gen)
1470 {
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
1477 }
1478 df_analyze ();
1479
1480
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)));
1486
1487 free_dominance_info (CDI_DOMINATORS);
1488 }
1489 }
1490 \f
1491 namespace {
1492
1493 const pass_data pass_data_branch_target_load_optimize1 =
1494 {
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 */
1504 };
1505
1506 class pass_branch_target_load_optimize1 : public rtl_opt_pass
1507 {
1508 public:
1509 pass_branch_target_load_optimize1 (gcc::context *ctxt)
1510 : rtl_opt_pass (pass_data_branch_target_load_optimize1, ctxt)
1511 {}
1512
1513 /* opt_pass methods: */
1514 virtual bool gate (function *) { return flag_branch_target_load_optimize; }
1515 virtual unsigned int execute (function *)
1516 {
1517 branch_target_load_optimize (epilogue_completed);
1518 return 0;
1519 }
1520
1521 }; // class pass_branch_target_load_optimize1
1522
1523 } // anon namespace
1524
1525 rtl_opt_pass *
1526 make_pass_branch_target_load_optimize1 (gcc::context *ctxt)
1527 {
1528 return new pass_branch_target_load_optimize1 (ctxt);
1529 }
1530
1531
1532 namespace {
1533
1534 const pass_data pass_data_branch_target_load_optimize2 =
1535 {
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 */
1545 };
1546
1547 class pass_branch_target_load_optimize2 : public rtl_opt_pass
1548 {
1549 public:
1550 pass_branch_target_load_optimize2 (gcc::context *ctxt)
1551 : rtl_opt_pass (pass_data_branch_target_load_optimize2, ctxt)
1552 {}
1553
1554 /* opt_pass methods: */
1555 virtual bool gate (function *)
1556 {
1557 return (optimize > 0 && flag_branch_target_load_optimize2);
1558 }
1559
1560 virtual unsigned int execute (function *);
1561
1562 }; // class pass_branch_target_load_optimize2
1563
1564 unsigned int
1565 pass_branch_target_load_optimize2::execute (function *)
1566 {
1567 static int warned = 0;
1568
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)
1575 {
1576 warning (0, "branch target register load optimization is not intended "
1577 "to be run twice");
1578
1579 warned = 1;
1580 }
1581
1582 branch_target_load_optimize (epilogue_completed);
1583 return 0;
1584 }
1585
1586 } // anon namespace
1587
1588 rtl_opt_pass *
1589 make_pass_branch_target_load_optimize2 (gcc::context *ctxt)
1590 {
1591 return new pass_branch_target_load_optimize2 (ctxt);
1592 }