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