1 /* Building internal representation for IRA.
2 Copyright (C) 2006-2013 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
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
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/>. */
23 #include "coretypes.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
32 #include "insn-config.h"
34 #include "diagnostic-core.h"
38 #include "sparseset.h"
40 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
42 static ira_copy_t
find_allocno_copy (ira_allocno_t
, ira_allocno_t
, rtx
,
43 ira_loop_tree_node_t
);
45 /* The root of the loop tree corresponding to the all function. */
46 ira_loop_tree_node_t ira_loop_tree_root
;
48 /* Height of the loop tree. */
49 int ira_loop_tree_height
;
51 /* All nodes representing basic blocks are referred through the
52 following array. We can not use basic block member `aux' for this
53 because it is used for insertion of insns on edges. */
54 ira_loop_tree_node_t ira_bb_nodes
;
56 /* All nodes representing loops are referred through the following
58 ira_loop_tree_node_t ira_loop_nodes
;
60 /* Map regno -> allocnos with given regno (see comments for
61 allocno member `next_regno_allocno'). */
62 ira_allocno_t
*ira_regno_allocno_map
;
64 /* Array of references to all allocnos. The order number of the
65 allocno corresponds to the index in the array. Removed allocnos
66 have NULL element value. */
67 ira_allocno_t
*ira_allocnos
;
69 /* Sizes of the previous array. */
72 /* Count of conflict record structures we've created, used when creating
76 /* Map a conflict id to its conflict record. */
77 ira_object_t
*ira_object_id_map
;
79 /* Array of references to all copies. The order number of the copy
80 corresponds to the index in the array. Removed copies have NULL
82 ira_copy_t
*ira_copies
;
84 /* Size of the previous array. */
89 /* LAST_BASIC_BLOCK before generating additional insns because of live
90 range splitting. Emitting insns on a critical edge creates a new
92 static int last_basic_block_before_change
;
94 /* Initialize some members in loop tree node NODE. Use LOOP_NUM for
95 the member loop_num. */
97 init_loop_tree_node (struct ira_loop_tree_node
*node
, int loop_num
)
99 int max_regno
= max_reg_num ();
101 node
->regno_allocno_map
102 = (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
) * max_regno
);
103 memset (node
->regno_allocno_map
, 0, sizeof (ira_allocno_t
) * max_regno
);
104 memset (node
->reg_pressure
, 0, sizeof (node
->reg_pressure
));
105 node
->all_allocnos
= ira_allocate_bitmap ();
106 node
->modified_regnos
= ira_allocate_bitmap ();
107 node
->border_allocnos
= ira_allocate_bitmap ();
108 node
->local_copies
= ira_allocate_bitmap ();
109 node
->loop_num
= loop_num
;
110 node
->children
= NULL
;
111 node
->subloops
= NULL
;
115 /* The following function allocates the loop tree nodes. If
116 CURRENT_LOOPS is NULL, the nodes corresponding to the loops (except
117 the root which corresponds the all function) will be not allocated
118 but nodes will still be allocated for basic blocks. */
120 create_loop_tree_nodes (void)
130 = ((struct ira_loop_tree_node
*)
131 ira_allocate (sizeof (struct ira_loop_tree_node
) * last_basic_block
));
132 last_basic_block_before_change
= last_basic_block
;
133 for (i
= 0; i
< (unsigned int) last_basic_block
; i
++)
135 ira_bb_nodes
[i
].regno_allocno_map
= NULL
;
136 memset (ira_bb_nodes
[i
].reg_pressure
, 0,
137 sizeof (ira_bb_nodes
[i
].reg_pressure
));
138 ira_bb_nodes
[i
].all_allocnos
= NULL
;
139 ira_bb_nodes
[i
].modified_regnos
= NULL
;
140 ira_bb_nodes
[i
].border_allocnos
= NULL
;
141 ira_bb_nodes
[i
].local_copies
= NULL
;
143 if (current_loops
== NULL
)
145 ira_loop_nodes
= ((struct ira_loop_tree_node
*)
146 ira_allocate (sizeof (struct ira_loop_tree_node
)));
147 init_loop_tree_node (ira_loop_nodes
, 0);
150 ira_loop_nodes
= ((struct ira_loop_tree_node
*)
151 ira_allocate (sizeof (struct ira_loop_tree_node
)
152 * number_of_loops ()));
153 FOR_EACH_VEC_SAFE_ELT (get_loops (), i
, loop
)
155 if (loop_outer (loop
) != NULL
)
157 ira_loop_nodes
[i
].regno_allocno_map
= NULL
;
159 FOR_EACH_EDGE (e
, ei
, loop
->header
->preds
)
160 if (e
->src
!= loop
->latch
161 && (e
->flags
& EDGE_ABNORMAL
) && EDGE_CRITICAL_P (e
))
168 edges
= get_loop_exit_edges (loop
);
169 FOR_EACH_VEC_ELT (edges
, j
, e
)
170 if ((e
->flags
& EDGE_ABNORMAL
) && EDGE_CRITICAL_P (e
))
179 init_loop_tree_node (&ira_loop_nodes
[i
], loop
->num
);
183 /* The function returns TRUE if there are more one allocation
186 more_one_region_p (void)
191 if (current_loops
!= NULL
)
192 FOR_EACH_VEC_SAFE_ELT (get_loops (), i
, loop
)
193 if (ira_loop_nodes
[i
].regno_allocno_map
!= NULL
194 && ira_loop_tree_root
!= &ira_loop_nodes
[i
])
199 /* Free the loop tree node of a loop. */
201 finish_loop_tree_node (ira_loop_tree_node_t loop
)
203 if (loop
->regno_allocno_map
!= NULL
)
205 ira_assert (loop
->bb
== NULL
);
206 ira_free_bitmap (loop
->local_copies
);
207 ira_free_bitmap (loop
->border_allocnos
);
208 ira_free_bitmap (loop
->modified_regnos
);
209 ira_free_bitmap (loop
->all_allocnos
);
210 ira_free (loop
->regno_allocno_map
);
211 loop
->regno_allocno_map
= NULL
;
215 /* Free the loop tree nodes. */
217 finish_loop_tree_nodes (void)
222 if (current_loops
== NULL
)
223 finish_loop_tree_node (&ira_loop_nodes
[0]);
225 FOR_EACH_VEC_SAFE_ELT (get_loops (), i
, loop
)
226 finish_loop_tree_node (&ira_loop_nodes
[i
]);
227 ira_free (ira_loop_nodes
);
228 for (i
= 0; i
< (unsigned int) last_basic_block_before_change
; i
++)
230 if (ira_bb_nodes
[i
].local_copies
!= NULL
)
231 ira_free_bitmap (ira_bb_nodes
[i
].local_copies
);
232 if (ira_bb_nodes
[i
].border_allocnos
!= NULL
)
233 ira_free_bitmap (ira_bb_nodes
[i
].border_allocnos
);
234 if (ira_bb_nodes
[i
].modified_regnos
!= NULL
)
235 ira_free_bitmap (ira_bb_nodes
[i
].modified_regnos
);
236 if (ira_bb_nodes
[i
].all_allocnos
!= NULL
)
237 ira_free_bitmap (ira_bb_nodes
[i
].all_allocnos
);
238 if (ira_bb_nodes
[i
].regno_allocno_map
!= NULL
)
239 ira_free (ira_bb_nodes
[i
].regno_allocno_map
);
241 ira_free (ira_bb_nodes
);
246 /* The following recursive function adds LOOP to the loop tree
247 hierarchy. LOOP is added only once. If LOOP is NULL we adding
248 loop designating the whole function when CFG loops are not
251 add_loop_to_tree (struct loop
*loop
)
255 ira_loop_tree_node_t loop_node
, parent_node
;
257 /* We can not use loop node access macros here because of potential
258 checking and because the nodes are not initialized enough
260 if (loop
!= NULL
&& loop_outer (loop
) != NULL
)
261 add_loop_to_tree (loop_outer (loop
));
262 loop_num
= loop
!= NULL
? loop
->num
: 0;
263 if (ira_loop_nodes
[loop_num
].regno_allocno_map
!= NULL
264 && ira_loop_nodes
[loop_num
].children
== NULL
)
266 /* We have not added loop node to the tree yet. */
267 loop_node
= &ira_loop_nodes
[loop_num
];
268 loop_node
->loop
= loop
;
269 loop_node
->bb
= NULL
;
274 for (parent
= loop_outer (loop
);
276 parent
= loop_outer (parent
))
277 if (ira_loop_nodes
[parent
->num
].regno_allocno_map
!= NULL
)
282 loop_node
->next
= NULL
;
283 loop_node
->subloop_next
= NULL
;
284 loop_node
->parent
= NULL
;
288 parent_node
= &ira_loop_nodes
[parent
->num
];
289 loop_node
->next
= parent_node
->children
;
290 parent_node
->children
= loop_node
;
291 loop_node
->subloop_next
= parent_node
->subloops
;
292 parent_node
->subloops
= loop_node
;
293 loop_node
->parent
= parent_node
;
298 /* The following recursive function sets up levels of nodes of the
299 tree given its root LOOP_NODE. The enumeration starts with LEVEL.
300 The function returns maximal value of level in the tree + 1. */
302 setup_loop_tree_level (ira_loop_tree_node_t loop_node
, int level
)
304 int height
, max_height
;
305 ira_loop_tree_node_t subloop_node
;
307 ira_assert (loop_node
->bb
== NULL
);
308 loop_node
->level
= level
;
309 max_height
= level
+ 1;
310 for (subloop_node
= loop_node
->subloops
;
311 subloop_node
!= NULL
;
312 subloop_node
= subloop_node
->subloop_next
)
314 ira_assert (subloop_node
->bb
== NULL
);
315 height
= setup_loop_tree_level (subloop_node
, level
+ 1);
316 if (height
> max_height
)
322 /* Create the loop tree. The algorithm is designed to provide correct
323 order of loops (they are ordered by their last loop BB) and basic
324 blocks in the chain formed by member next. */
326 form_loop_tree (void)
330 ira_loop_tree_node_t bb_node
, loop_node
;
332 /* We can not use loop/bb node access macros because of potential
333 checking and because the nodes are not initialized enough
337 bb_node
= &ira_bb_nodes
[bb
->index
];
339 bb_node
->loop
= NULL
;
340 bb_node
->subloops
= NULL
;
341 bb_node
->children
= NULL
;
342 bb_node
->subloop_next
= NULL
;
343 bb_node
->next
= NULL
;
344 if (current_loops
== NULL
)
348 for (parent
= bb
->loop_father
;
350 parent
= loop_outer (parent
))
351 if (ira_loop_nodes
[parent
->num
].regno_allocno_map
!= NULL
)
354 add_loop_to_tree (parent
);
355 loop_node
= &ira_loop_nodes
[parent
== NULL
? 0 : parent
->num
];
356 bb_node
->next
= loop_node
->children
;
357 bb_node
->parent
= loop_node
;
358 loop_node
->children
= bb_node
;
360 ira_loop_tree_root
= IRA_LOOP_NODE_BY_INDEX (0);
361 ira_loop_tree_height
= setup_loop_tree_level (ira_loop_tree_root
, 0);
362 ira_assert (ira_loop_tree_root
->regno_allocno_map
!= NULL
);
367 /* Rebuild IRA_REGNO_ALLOCNO_MAP and REGNO_ALLOCNO_MAPs of the loop
370 rebuild_regno_allocno_maps (void)
373 int max_regno
, regno
;
375 ira_loop_tree_node_t loop_tree_node
;
377 ira_allocno_iterator ai
;
379 ira_assert (current_loops
!= NULL
);
380 max_regno
= max_reg_num ();
381 FOR_EACH_VEC_SAFE_ELT (get_loops (), l
, loop
)
382 if (ira_loop_nodes
[l
].regno_allocno_map
!= NULL
)
384 ira_free (ira_loop_nodes
[l
].regno_allocno_map
);
385 ira_loop_nodes
[l
].regno_allocno_map
386 = (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
)
388 memset (ira_loop_nodes
[l
].regno_allocno_map
, 0,
389 sizeof (ira_allocno_t
) * max_regno
);
391 ira_free (ira_regno_allocno_map
);
392 ira_regno_allocno_map
393 = (ira_allocno_t
*) ira_allocate (max_regno
* sizeof (ira_allocno_t
));
394 memset (ira_regno_allocno_map
, 0, max_regno
* sizeof (ira_allocno_t
));
395 FOR_EACH_ALLOCNO (a
, ai
)
397 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
398 /* Caps are not in the regno allocno maps. */
400 regno
= ALLOCNO_REGNO (a
);
401 loop_tree_node
= ALLOCNO_LOOP_TREE_NODE (a
);
402 ALLOCNO_NEXT_REGNO_ALLOCNO (a
) = ira_regno_allocno_map
[regno
];
403 ira_regno_allocno_map
[regno
] = a
;
404 if (loop_tree_node
->regno_allocno_map
[regno
] == NULL
)
405 /* Remember that we can create temporary allocnos to break
406 cycles in register shuffle. */
407 loop_tree_node
->regno_allocno_map
[regno
] = a
;
412 /* Pools for allocnos, allocno live ranges and objects. */
413 static alloc_pool allocno_pool
, live_range_pool
, object_pool
;
415 /* Vec containing references to all created allocnos. It is a
416 container of array allocnos. */
417 static vec
<ira_allocno_t
> allocno_vec
;
419 /* Vec containing references to all created ira_objects. It is a
420 container of ira_object_id_map. */
421 static vec
<ira_object_t
> ira_object_id_map_vec
;
423 /* Initialize data concerning allocnos. */
425 initiate_allocnos (void)
428 = create_alloc_pool ("live ranges",
429 sizeof (struct live_range
), 100);
431 = create_alloc_pool ("allocnos", sizeof (struct ira_allocno
), 100);
433 = create_alloc_pool ("objects", sizeof (struct ira_object
), 100);
434 allocno_vec
.create (max_reg_num () * 2);
436 ira_allocnos_num
= 0;
438 ira_object_id_map_vec
.create (max_reg_num () * 2);
439 ira_object_id_map
= NULL
;
440 ira_regno_allocno_map
441 = (ira_allocno_t
*) ira_allocate (max_reg_num ()
442 * sizeof (ira_allocno_t
));
443 memset (ira_regno_allocno_map
, 0, max_reg_num () * sizeof (ira_allocno_t
));
446 /* Create and return an object corresponding to a new allocno A. */
448 ira_create_object (ira_allocno_t a
, int subword
)
450 enum reg_class aclass
= ALLOCNO_CLASS (a
);
451 ira_object_t obj
= (ira_object_t
) pool_alloc (object_pool
);
453 OBJECT_ALLOCNO (obj
) = a
;
454 OBJECT_SUBWORD (obj
) = subword
;
455 OBJECT_CONFLICT_ID (obj
) = ira_objects_num
;
456 OBJECT_CONFLICT_VEC_P (obj
) = false;
457 OBJECT_CONFLICT_ARRAY (obj
) = NULL
;
458 OBJECT_NUM_CONFLICTS (obj
) = 0;
459 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj
), ira_no_alloc_regs
);
460 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
), ira_no_alloc_regs
);
461 IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj
),
462 reg_class_contents
[aclass
]);
463 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
),
464 reg_class_contents
[aclass
]);
465 OBJECT_MIN (obj
) = INT_MAX
;
466 OBJECT_MAX (obj
) = -1;
467 OBJECT_LIVE_RANGES (obj
) = NULL
;
469 ira_object_id_map_vec
.safe_push (obj
);
471 = ira_object_id_map_vec
.address ();
472 ira_objects_num
= ira_object_id_map_vec
.length ();
477 /* Create and return the allocno corresponding to REGNO in
478 LOOP_TREE_NODE. Add the allocno to the list of allocnos with the
479 same regno if CAP_P is FALSE. */
481 ira_create_allocno (int regno
, bool cap_p
,
482 ira_loop_tree_node_t loop_tree_node
)
486 a
= (ira_allocno_t
) pool_alloc (allocno_pool
);
487 ALLOCNO_REGNO (a
) = regno
;
488 ALLOCNO_LOOP_TREE_NODE (a
) = loop_tree_node
;
491 ALLOCNO_NEXT_REGNO_ALLOCNO (a
) = ira_regno_allocno_map
[regno
];
492 ira_regno_allocno_map
[regno
] = a
;
493 if (loop_tree_node
->regno_allocno_map
[regno
] == NULL
)
494 /* Remember that we can create temporary allocnos to break
495 cycles in register shuffle on region borders (see
497 loop_tree_node
->regno_allocno_map
[regno
] = a
;
499 ALLOCNO_CAP (a
) = NULL
;
500 ALLOCNO_CAP_MEMBER (a
) = NULL
;
501 ALLOCNO_NUM (a
) = ira_allocnos_num
;
502 bitmap_set_bit (loop_tree_node
->all_allocnos
, ALLOCNO_NUM (a
));
503 ALLOCNO_NREFS (a
) = 0;
504 ALLOCNO_FREQ (a
) = 0;
505 ALLOCNO_HARD_REGNO (a
) = -1;
506 ALLOCNO_CALL_FREQ (a
) = 0;
507 ALLOCNO_CALLS_CROSSED_NUM (a
) = 0;
508 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a
) = 0;
510 ALLOCNO_NO_STACK_REG_P (a
) = false;
511 ALLOCNO_TOTAL_NO_STACK_REG_P (a
) = false;
513 ALLOCNO_DONT_REASSIGN_P (a
) = false;
514 ALLOCNO_BAD_SPILL_P (a
) = false;
515 ALLOCNO_ASSIGNED_P (a
) = false;
516 ALLOCNO_MODE (a
) = (regno
< 0 ? VOIDmode
: PSEUDO_REGNO_MODE (regno
));
517 ALLOCNO_COPIES (a
) = NULL
;
518 ALLOCNO_HARD_REG_COSTS (a
) = NULL
;
519 ALLOCNO_CONFLICT_HARD_REG_COSTS (a
) = NULL
;
520 ALLOCNO_UPDATED_HARD_REG_COSTS (a
) = NULL
;
521 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) = NULL
;
522 ALLOCNO_CLASS (a
) = NO_REGS
;
523 ALLOCNO_UPDATED_CLASS_COST (a
) = 0;
524 ALLOCNO_CLASS_COST (a
) = 0;
525 ALLOCNO_MEMORY_COST (a
) = 0;
526 ALLOCNO_UPDATED_MEMORY_COST (a
) = 0;
527 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
) = 0;
528 ALLOCNO_NUM_OBJECTS (a
) = 0;
530 ALLOCNO_ADD_DATA (a
) = NULL
;
531 allocno_vec
.safe_push (a
);
532 ira_allocnos
= allocno_vec
.address ();
533 ira_allocnos_num
= allocno_vec
.length ();
538 /* Set up register class for A and update its conflict hard
541 ira_set_allocno_class (ira_allocno_t a
, enum reg_class aclass
)
543 ira_allocno_object_iterator oi
;
546 ALLOCNO_CLASS (a
) = aclass
;
547 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
549 IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj
),
550 reg_class_contents
[aclass
]);
551 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
),
552 reg_class_contents
[aclass
]);
556 /* Determine the number of objects we should associate with allocno A
557 and allocate them. */
559 ira_create_allocno_objects (ira_allocno_t a
)
561 enum machine_mode mode
= ALLOCNO_MODE (a
);
562 enum reg_class aclass
= ALLOCNO_CLASS (a
);
563 int n
= ira_reg_class_max_nregs
[aclass
][mode
];
566 if (GET_MODE_SIZE (mode
) != 2 * UNITS_PER_WORD
|| n
!= 2)
569 ALLOCNO_NUM_OBJECTS (a
) = n
;
570 for (i
= 0; i
< n
; i
++)
571 ALLOCNO_OBJECT (a
, i
) = ira_create_object (a
, i
);
574 /* For each allocno, set ALLOCNO_NUM_OBJECTS and create the
575 ALLOCNO_OBJECT structures. This must be called after the allocno
576 classes are known. */
578 create_allocno_objects (void)
581 ira_allocno_iterator ai
;
583 FOR_EACH_ALLOCNO (a
, ai
)
584 ira_create_allocno_objects (a
);
587 /* Merge hard register conflict information for all objects associated with
588 allocno TO into the corresponding objects associated with FROM.
589 If TOTAL_ONLY is true, we only merge OBJECT_TOTAL_CONFLICT_HARD_REGS. */
591 merge_hard_reg_conflicts (ira_allocno_t from
, ira_allocno_t to
,
595 gcc_assert (ALLOCNO_NUM_OBJECTS (to
) == ALLOCNO_NUM_OBJECTS (from
));
596 for (i
= 0; i
< ALLOCNO_NUM_OBJECTS (to
); i
++)
598 ira_object_t from_obj
= ALLOCNO_OBJECT (from
, i
);
599 ira_object_t to_obj
= ALLOCNO_OBJECT (to
, i
);
602 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (to_obj
),
603 OBJECT_CONFLICT_HARD_REGS (from_obj
));
604 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (to_obj
),
605 OBJECT_TOTAL_CONFLICT_HARD_REGS (from_obj
));
608 if (!total_only
&& ALLOCNO_NO_STACK_REG_P (from
))
609 ALLOCNO_NO_STACK_REG_P (to
) = true;
610 if (ALLOCNO_TOTAL_NO_STACK_REG_P (from
))
611 ALLOCNO_TOTAL_NO_STACK_REG_P (to
) = true;
615 /* Update hard register conflict information for all objects associated with
616 A to include the regs in SET. */
618 ior_hard_reg_conflicts (ira_allocno_t a
, HARD_REG_SET
*set
)
620 ira_allocno_object_iterator i
;
623 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, i
)
625 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj
), *set
);
626 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
), *set
);
630 /* Return TRUE if a conflict vector with NUM elements is more
631 profitable than a conflict bit vector for OBJ. */
633 ira_conflict_vector_profitable_p (ira_object_t obj
, int num
)
636 int max
= OBJECT_MAX (obj
);
637 int min
= OBJECT_MIN (obj
);
640 /* We prefer a bit vector in such case because it does not result
644 nw
= (max
- min
+ IRA_INT_BITS
) / IRA_INT_BITS
;
645 return (2 * sizeof (ira_object_t
) * (num
+ 1)
646 < 3 * nw
* sizeof (IRA_INT_TYPE
));
649 /* Allocates and initialize the conflict vector of OBJ for NUM
650 conflicting objects. */
652 ira_allocate_conflict_vec (ira_object_t obj
, int num
)
657 ira_assert (OBJECT_CONFLICT_ARRAY (obj
) == NULL
);
658 num
++; /* for NULL end marker */
659 size
= sizeof (ira_object_t
) * num
;
660 OBJECT_CONFLICT_ARRAY (obj
) = ira_allocate (size
);
661 vec
= (ira_object_t
*) OBJECT_CONFLICT_ARRAY (obj
);
663 OBJECT_NUM_CONFLICTS (obj
) = 0;
664 OBJECT_CONFLICT_ARRAY_SIZE (obj
) = size
;
665 OBJECT_CONFLICT_VEC_P (obj
) = true;
668 /* Allocate and initialize the conflict bit vector of OBJ. */
670 allocate_conflict_bit_vec (ira_object_t obj
)
674 ira_assert (OBJECT_CONFLICT_ARRAY (obj
) == NULL
);
675 size
= ((OBJECT_MAX (obj
) - OBJECT_MIN (obj
) + IRA_INT_BITS
)
676 / IRA_INT_BITS
* sizeof (IRA_INT_TYPE
));
677 OBJECT_CONFLICT_ARRAY (obj
) = ira_allocate (size
);
678 memset (OBJECT_CONFLICT_ARRAY (obj
), 0, size
);
679 OBJECT_CONFLICT_ARRAY_SIZE (obj
) = size
;
680 OBJECT_CONFLICT_VEC_P (obj
) = false;
683 /* Allocate and initialize the conflict vector or conflict bit vector
684 of OBJ for NUM conflicting allocnos whatever is more profitable. */
686 ira_allocate_object_conflicts (ira_object_t obj
, int num
)
688 if (ira_conflict_vector_profitable_p (obj
, num
))
689 ira_allocate_conflict_vec (obj
, num
);
691 allocate_conflict_bit_vec (obj
);
694 /* Add OBJ2 to the conflicts of OBJ1. */
696 add_to_conflicts (ira_object_t obj1
, ira_object_t obj2
)
701 if (OBJECT_CONFLICT_VEC_P (obj1
))
703 ira_object_t
*vec
= OBJECT_CONFLICT_VEC (obj1
);
704 int curr_num
= OBJECT_NUM_CONFLICTS (obj1
);
706 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1
) < num
* sizeof (ira_object_t
))
708 ira_object_t
*newvec
;
709 size
= (3 * num
/ 2 + 1) * sizeof (ira_allocno_t
);
710 newvec
= (ira_object_t
*) ira_allocate (size
);
711 memcpy (newvec
, vec
, curr_num
* sizeof (ira_object_t
));
714 OBJECT_CONFLICT_ARRAY (obj1
) = vec
;
715 OBJECT_CONFLICT_ARRAY_SIZE (obj1
) = size
;
719 OBJECT_NUM_CONFLICTS (obj1
)++;
723 int nw
, added_head_nw
, id
;
724 IRA_INT_TYPE
*vec
= OBJECT_CONFLICT_BITVEC (obj1
);
726 id
= OBJECT_CONFLICT_ID (obj2
);
727 if (OBJECT_MIN (obj1
) > id
)
729 /* Expand head of the bit vector. */
730 added_head_nw
= (OBJECT_MIN (obj1
) - id
- 1) / IRA_INT_BITS
+ 1;
731 nw
= (OBJECT_MAX (obj1
) - OBJECT_MIN (obj1
)) / IRA_INT_BITS
+ 1;
732 size
= (nw
+ added_head_nw
) * sizeof (IRA_INT_TYPE
);
733 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1
) >= size
)
735 memmove ((char *) vec
+ added_head_nw
* sizeof (IRA_INT_TYPE
),
736 vec
, nw
* sizeof (IRA_INT_TYPE
));
737 memset (vec
, 0, added_head_nw
* sizeof (IRA_INT_TYPE
));
742 = (3 * (nw
+ added_head_nw
) / 2 + 1) * sizeof (IRA_INT_TYPE
);
743 vec
= (IRA_INT_TYPE
*) ira_allocate (size
);
744 memcpy ((char *) vec
+ added_head_nw
* sizeof (IRA_INT_TYPE
),
745 OBJECT_CONFLICT_ARRAY (obj1
), nw
* sizeof (IRA_INT_TYPE
));
746 memset (vec
, 0, added_head_nw
* sizeof (IRA_INT_TYPE
));
748 + (nw
+ added_head_nw
) * sizeof (IRA_INT_TYPE
),
749 0, size
- (nw
+ added_head_nw
) * sizeof (IRA_INT_TYPE
));
750 ira_free (OBJECT_CONFLICT_ARRAY (obj1
));
751 OBJECT_CONFLICT_ARRAY (obj1
) = vec
;
752 OBJECT_CONFLICT_ARRAY_SIZE (obj1
) = size
;
754 OBJECT_MIN (obj1
) -= added_head_nw
* IRA_INT_BITS
;
756 else if (OBJECT_MAX (obj1
) < id
)
758 nw
= (id
- OBJECT_MIN (obj1
)) / IRA_INT_BITS
+ 1;
759 size
= nw
* sizeof (IRA_INT_TYPE
);
760 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1
) < size
)
762 /* Expand tail of the bit vector. */
763 size
= (3 * nw
/ 2 + 1) * sizeof (IRA_INT_TYPE
);
764 vec
= (IRA_INT_TYPE
*) ira_allocate (size
);
765 memcpy (vec
, OBJECT_CONFLICT_ARRAY (obj1
), OBJECT_CONFLICT_ARRAY_SIZE (obj1
));
766 memset ((char *) vec
+ OBJECT_CONFLICT_ARRAY_SIZE (obj1
),
767 0, size
- OBJECT_CONFLICT_ARRAY_SIZE (obj1
));
768 ira_free (OBJECT_CONFLICT_ARRAY (obj1
));
769 OBJECT_CONFLICT_ARRAY (obj1
) = vec
;
770 OBJECT_CONFLICT_ARRAY_SIZE (obj1
) = size
;
772 OBJECT_MAX (obj1
) = id
;
774 SET_MINMAX_SET_BIT (vec
, id
, OBJECT_MIN (obj1
), OBJECT_MAX (obj1
));
778 /* Add OBJ1 to the conflicts of OBJ2 and vice versa. */
780 ira_add_conflict (ira_object_t obj1
, ira_object_t obj2
)
782 add_to_conflicts (obj1
, obj2
);
783 add_to_conflicts (obj2
, obj1
);
786 /* Clear all conflicts of OBJ. */
788 clear_conflicts (ira_object_t obj
)
790 if (OBJECT_CONFLICT_VEC_P (obj
))
792 OBJECT_NUM_CONFLICTS (obj
) = 0;
793 OBJECT_CONFLICT_VEC (obj
)[0] = NULL
;
795 else if (OBJECT_CONFLICT_ARRAY_SIZE (obj
) != 0)
799 nw
= (OBJECT_MAX (obj
) - OBJECT_MIN (obj
)) / IRA_INT_BITS
+ 1;
800 memset (OBJECT_CONFLICT_BITVEC (obj
), 0, nw
* sizeof (IRA_INT_TYPE
));
804 /* The array used to find duplications in conflict vectors of
806 static int *conflict_check
;
808 /* The value used to mark allocation presence in conflict vector of
809 the current allocno. */
810 static int curr_conflict_check_tick
;
812 /* Remove duplications in conflict vector of OBJ. */
814 compress_conflict_vec (ira_object_t obj
)
816 ira_object_t
*vec
, conflict_obj
;
819 ira_assert (OBJECT_CONFLICT_VEC_P (obj
));
820 vec
= OBJECT_CONFLICT_VEC (obj
);
821 curr_conflict_check_tick
++;
822 for (i
= j
= 0; (conflict_obj
= vec
[i
]) != NULL
; i
++)
824 int id
= OBJECT_CONFLICT_ID (conflict_obj
);
825 if (conflict_check
[id
] != curr_conflict_check_tick
)
827 conflict_check
[id
] = curr_conflict_check_tick
;
828 vec
[j
++] = conflict_obj
;
831 OBJECT_NUM_CONFLICTS (obj
) = j
;
835 /* Remove duplications in conflict vectors of all allocnos. */
837 compress_conflict_vecs (void)
840 ira_object_iterator oi
;
842 conflict_check
= (int *) ira_allocate (sizeof (int) * ira_objects_num
);
843 memset (conflict_check
, 0, sizeof (int) * ira_objects_num
);
844 curr_conflict_check_tick
= 0;
845 FOR_EACH_OBJECT (obj
, oi
)
847 if (OBJECT_CONFLICT_VEC_P (obj
))
848 compress_conflict_vec (obj
);
850 ira_free (conflict_check
);
853 /* This recursive function outputs allocno A and if it is a cap the
854 function outputs its members. */
856 ira_print_expanded_allocno (ira_allocno_t a
)
860 fprintf (ira_dump_file
, " a%d(r%d", ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
));
861 if ((bb
= ALLOCNO_LOOP_TREE_NODE (a
)->bb
) != NULL
)
862 fprintf (ira_dump_file
, ",b%d", bb
->index
);
864 fprintf (ira_dump_file
, ",l%d", ALLOCNO_LOOP_TREE_NODE (a
)->loop_num
);
865 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
867 fprintf (ira_dump_file
, ":");
868 ira_print_expanded_allocno (ALLOCNO_CAP_MEMBER (a
));
870 fprintf (ira_dump_file
, ")");
873 /* Create and return the cap representing allocno A in the
876 create_cap_allocno (ira_allocno_t a
)
879 ira_loop_tree_node_t parent
;
880 enum reg_class aclass
;
882 parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
;
883 cap
= ira_create_allocno (ALLOCNO_REGNO (a
), true, parent
);
884 ALLOCNO_MODE (cap
) = ALLOCNO_MODE (a
);
885 aclass
= ALLOCNO_CLASS (a
);
886 ira_set_allocno_class (cap
, aclass
);
887 ira_create_allocno_objects (cap
);
888 ALLOCNO_CAP_MEMBER (cap
) = a
;
889 ALLOCNO_CAP (a
) = cap
;
890 ALLOCNO_CLASS_COST (cap
) = ALLOCNO_CLASS_COST (a
);
891 ALLOCNO_MEMORY_COST (cap
) = ALLOCNO_MEMORY_COST (a
);
892 ira_allocate_and_copy_costs
893 (&ALLOCNO_HARD_REG_COSTS (cap
), aclass
, ALLOCNO_HARD_REG_COSTS (a
));
894 ira_allocate_and_copy_costs
895 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (cap
), aclass
,
896 ALLOCNO_CONFLICT_HARD_REG_COSTS (a
));
897 ALLOCNO_BAD_SPILL_P (cap
) = ALLOCNO_BAD_SPILL_P (a
);
898 ALLOCNO_NREFS (cap
) = ALLOCNO_NREFS (a
);
899 ALLOCNO_FREQ (cap
) = ALLOCNO_FREQ (a
);
900 ALLOCNO_CALL_FREQ (cap
) = ALLOCNO_CALL_FREQ (a
);
902 merge_hard_reg_conflicts (a
, cap
, false);
904 ALLOCNO_CALLS_CROSSED_NUM (cap
) = ALLOCNO_CALLS_CROSSED_NUM (a
);
905 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (cap
) = ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a
);
906 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
908 fprintf (ira_dump_file
, " Creating cap ");
909 ira_print_expanded_allocno (cap
);
910 fprintf (ira_dump_file
, "\n");
915 /* Create and return a live range for OBJECT with given attributes. */
917 ira_create_live_range (ira_object_t obj
, int start
, int finish
,
922 p
= (live_range_t
) pool_alloc (live_range_pool
);
930 /* Create a new live range for OBJECT and queue it at the head of its
933 ira_add_live_range_to_object (ira_object_t object
, int start
, int finish
)
936 p
= ira_create_live_range (object
, start
, finish
,
937 OBJECT_LIVE_RANGES (object
));
938 OBJECT_LIVE_RANGES (object
) = p
;
941 /* Copy allocno live range R and return the result. */
943 copy_live_range (live_range_t r
)
947 p
= (live_range_t
) pool_alloc (live_range_pool
);
952 /* Copy allocno live range list given by its head R and return the
955 ira_copy_live_range_list (live_range_t r
)
957 live_range_t p
, first
, last
;
961 for (first
= last
= NULL
; r
!= NULL
; r
= r
->next
)
963 p
= copy_live_range (r
);
973 /* Merge ranges R1 and R2 and returns the result. The function
974 maintains the order of ranges and tries to minimize number of the
977 ira_merge_live_ranges (live_range_t r1
, live_range_t r2
)
979 live_range_t first
, last
, temp
;
985 for (first
= last
= NULL
; r1
!= NULL
&& r2
!= NULL
;)
987 if (r1
->start
< r2
->start
)
993 if (r1
->start
<= r2
->finish
+ 1)
995 /* Intersected ranges: merge r1 and r2 into r1. */
996 r1
->start
= r2
->start
;
997 if (r1
->finish
< r2
->finish
)
998 r1
->finish
= r2
->finish
;
1001 ira_finish_live_range (temp
);
1004 /* To try to merge with subsequent ranges in r1. */
1011 /* Add r1 to the result. */
1022 /* To try to merge with subsequent ranges in r2. */
1034 ira_assert (r1
->next
== NULL
);
1036 else if (r2
!= NULL
)
1042 ira_assert (r2
->next
== NULL
);
1046 ira_assert (last
->next
== NULL
);
1051 /* Return TRUE if live ranges R1 and R2 intersect. */
1053 ira_live_ranges_intersect_p (live_range_t r1
, live_range_t r2
)
1055 /* Remember the live ranges are always kept ordered. */
1056 while (r1
!= NULL
&& r2
!= NULL
)
1058 if (r1
->start
> r2
->finish
)
1060 else if (r2
->start
> r1
->finish
)
1068 /* Free allocno live range R. */
1070 ira_finish_live_range (live_range_t r
)
1072 pool_free (live_range_pool
, r
);
1075 /* Free list of allocno live ranges starting with R. */
1077 ira_finish_live_range_list (live_range_t r
)
1079 live_range_t next_r
;
1081 for (; r
!= NULL
; r
= next_r
)
1084 ira_finish_live_range (r
);
1088 /* Free updated register costs of allocno A. */
1090 ira_free_allocno_updated_costs (ira_allocno_t a
)
1092 enum reg_class aclass
;
1094 aclass
= ALLOCNO_CLASS (a
);
1095 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) != NULL
)
1096 ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a
), aclass
);
1097 ALLOCNO_UPDATED_HARD_REG_COSTS (a
) = NULL
;
1098 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) != NULL
)
1099 ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
),
1101 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) = NULL
;
1104 /* Free and nullify all cost vectors allocated earlier for allocno
1107 ira_free_allocno_costs (ira_allocno_t a
)
1109 enum reg_class aclass
= ALLOCNO_CLASS (a
);
1111 ira_allocno_object_iterator oi
;
1113 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
1115 ira_finish_live_range_list (OBJECT_LIVE_RANGES (obj
));
1116 ira_object_id_map
[OBJECT_CONFLICT_ID (obj
)] = NULL
;
1117 if (OBJECT_CONFLICT_ARRAY (obj
) != NULL
)
1118 ira_free (OBJECT_CONFLICT_ARRAY (obj
));
1119 pool_free (object_pool
, obj
);
1122 ira_allocnos
[ALLOCNO_NUM (a
)] = NULL
;
1123 if (ALLOCNO_HARD_REG_COSTS (a
) != NULL
)
1124 ira_free_cost_vector (ALLOCNO_HARD_REG_COSTS (a
), aclass
);
1125 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a
) != NULL
)
1126 ira_free_cost_vector (ALLOCNO_CONFLICT_HARD_REG_COSTS (a
), aclass
);
1127 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) != NULL
)
1128 ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a
), aclass
);
1129 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) != NULL
)
1130 ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
),
1132 ALLOCNO_HARD_REG_COSTS (a
) = NULL
;
1133 ALLOCNO_CONFLICT_HARD_REG_COSTS (a
) = NULL
;
1134 ALLOCNO_UPDATED_HARD_REG_COSTS (a
) = NULL
;
1135 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) = NULL
;
1138 /* Free the memory allocated for allocno A. */
1140 finish_allocno (ira_allocno_t a
)
1142 ira_free_allocno_costs (a
);
1143 pool_free (allocno_pool
, a
);
1146 /* Free the memory allocated for all allocnos. */
1148 finish_allocnos (void)
1151 ira_allocno_iterator ai
;
1153 FOR_EACH_ALLOCNO (a
, ai
)
1155 ira_free (ira_regno_allocno_map
);
1156 ira_object_id_map_vec
.release ();
1157 allocno_vec
.release ();
1158 free_alloc_pool (allocno_pool
);
1159 free_alloc_pool (object_pool
);
1160 free_alloc_pool (live_range_pool
);
1165 /* Pools for copies. */
1166 static alloc_pool copy_pool
;
1168 /* Vec containing references to all created copies. It is a
1169 container of array ira_copies. */
1170 static vec
<ira_copy_t
> copy_vec
;
1172 /* The function initializes data concerning allocno copies. */
1174 initiate_copies (void)
1177 = create_alloc_pool ("copies", sizeof (struct ira_allocno_copy
), 100);
1178 copy_vec
.create (get_max_uid ());
1183 /* Return copy connecting A1 and A2 and originated from INSN of
1184 LOOP_TREE_NODE if any. */
1186 find_allocno_copy (ira_allocno_t a1
, ira_allocno_t a2
, rtx insn
,
1187 ira_loop_tree_node_t loop_tree_node
)
1189 ira_copy_t cp
, next_cp
;
1190 ira_allocno_t another_a
;
1192 for (cp
= ALLOCNO_COPIES (a1
); cp
!= NULL
; cp
= next_cp
)
1194 if (cp
->first
== a1
)
1196 next_cp
= cp
->next_first_allocno_copy
;
1197 another_a
= cp
->second
;
1199 else if (cp
->second
== a1
)
1201 next_cp
= cp
->next_second_allocno_copy
;
1202 another_a
= cp
->first
;
1206 if (another_a
== a2
&& cp
->insn
== insn
1207 && cp
->loop_tree_node
== loop_tree_node
)
1213 /* Create and return copy with given attributes LOOP_TREE_NODE, FIRST,
1214 SECOND, FREQ, CONSTRAINT_P, and INSN. */
1216 ira_create_copy (ira_allocno_t first
, ira_allocno_t second
, int freq
,
1217 bool constraint_p
, rtx insn
,
1218 ira_loop_tree_node_t loop_tree_node
)
1222 cp
= (ira_copy_t
) pool_alloc (copy_pool
);
1223 cp
->num
= ira_copies_num
;
1225 cp
->second
= second
;
1227 cp
->constraint_p
= constraint_p
;
1229 cp
->loop_tree_node
= loop_tree_node
;
1230 copy_vec
.safe_push (cp
);
1231 ira_copies
= copy_vec
.address ();
1232 ira_copies_num
= copy_vec
.length ();
1236 /* Attach a copy CP to allocnos involved into the copy. */
1238 ira_add_allocno_copy_to_list (ira_copy_t cp
)
1240 ira_allocno_t first
= cp
->first
, second
= cp
->second
;
1242 cp
->prev_first_allocno_copy
= NULL
;
1243 cp
->prev_second_allocno_copy
= NULL
;
1244 cp
->next_first_allocno_copy
= ALLOCNO_COPIES (first
);
1245 if (cp
->next_first_allocno_copy
!= NULL
)
1247 if (cp
->next_first_allocno_copy
->first
== first
)
1248 cp
->next_first_allocno_copy
->prev_first_allocno_copy
= cp
;
1250 cp
->next_first_allocno_copy
->prev_second_allocno_copy
= cp
;
1252 cp
->next_second_allocno_copy
= ALLOCNO_COPIES (second
);
1253 if (cp
->next_second_allocno_copy
!= NULL
)
1255 if (cp
->next_second_allocno_copy
->second
== second
)
1256 cp
->next_second_allocno_copy
->prev_second_allocno_copy
= cp
;
1258 cp
->next_second_allocno_copy
->prev_first_allocno_copy
= cp
;
1260 ALLOCNO_COPIES (first
) = cp
;
1261 ALLOCNO_COPIES (second
) = cp
;
1264 /* Make a copy CP a canonical copy where number of the
1265 first allocno is less than the second one. */
1267 ira_swap_allocno_copy_ends_if_necessary (ira_copy_t cp
)
1272 if (ALLOCNO_NUM (cp
->first
) <= ALLOCNO_NUM (cp
->second
))
1276 cp
->first
= cp
->second
;
1279 temp_cp
= cp
->prev_first_allocno_copy
;
1280 cp
->prev_first_allocno_copy
= cp
->prev_second_allocno_copy
;
1281 cp
->prev_second_allocno_copy
= temp_cp
;
1283 temp_cp
= cp
->next_first_allocno_copy
;
1284 cp
->next_first_allocno_copy
= cp
->next_second_allocno_copy
;
1285 cp
->next_second_allocno_copy
= temp_cp
;
1288 /* Create (or update frequency if the copy already exists) and return
1289 the copy of allocnos FIRST and SECOND with frequency FREQ
1290 corresponding to move insn INSN (if any) and originated from
1293 ira_add_allocno_copy (ira_allocno_t first
, ira_allocno_t second
, int freq
,
1294 bool constraint_p
, rtx insn
,
1295 ira_loop_tree_node_t loop_tree_node
)
1299 if ((cp
= find_allocno_copy (first
, second
, insn
, loop_tree_node
)) != NULL
)
1304 cp
= ira_create_copy (first
, second
, freq
, constraint_p
, insn
,
1306 ira_assert (first
!= NULL
&& second
!= NULL
);
1307 ira_add_allocno_copy_to_list (cp
);
1308 ira_swap_allocno_copy_ends_if_necessary (cp
);
1312 /* Print info about copy CP into file F. */
1314 print_copy (FILE *f
, ira_copy_t cp
)
1316 fprintf (f
, " cp%d:a%d(r%d)<->a%d(r%d)@%d:%s\n", cp
->num
,
1317 ALLOCNO_NUM (cp
->first
), ALLOCNO_REGNO (cp
->first
),
1318 ALLOCNO_NUM (cp
->second
), ALLOCNO_REGNO (cp
->second
), cp
->freq
,
1320 ? "move" : cp
->constraint_p
? "constraint" : "shuffle");
1323 /* Print info about copy CP into stderr. */
1325 ira_debug_copy (ira_copy_t cp
)
1327 print_copy (stderr
, cp
);
1330 /* Print info about all copies into file F. */
1332 print_copies (FILE *f
)
1335 ira_copy_iterator ci
;
1337 FOR_EACH_COPY (cp
, ci
)
1341 /* Print info about all copies into stderr. */
1343 ira_debug_copies (void)
1345 print_copies (stderr
);
1348 /* Print info about copies involving allocno A into file F. */
1350 print_allocno_copies (FILE *f
, ira_allocno_t a
)
1352 ira_allocno_t another_a
;
1353 ira_copy_t cp
, next_cp
;
1355 fprintf (f
, " a%d(r%d):", ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
));
1356 for (cp
= ALLOCNO_COPIES (a
); cp
!= NULL
; cp
= next_cp
)
1360 next_cp
= cp
->next_first_allocno_copy
;
1361 another_a
= cp
->second
;
1363 else if (cp
->second
== a
)
1365 next_cp
= cp
->next_second_allocno_copy
;
1366 another_a
= cp
->first
;
1370 fprintf (f
, " cp%d:a%d(r%d)@%d", cp
->num
,
1371 ALLOCNO_NUM (another_a
), ALLOCNO_REGNO (another_a
), cp
->freq
);
1376 /* Print info about copies involving allocno A into stderr. */
1378 ira_debug_allocno_copies (ira_allocno_t a
)
1380 print_allocno_copies (stderr
, a
);
1383 /* The function frees memory allocated for copy CP. */
1385 finish_copy (ira_copy_t cp
)
1387 pool_free (copy_pool
, cp
);
1391 /* Free memory allocated for all copies. */
1393 finish_copies (void)
1396 ira_copy_iterator ci
;
1398 FOR_EACH_COPY (cp
, ci
)
1400 copy_vec
.release ();
1401 free_alloc_pool (copy_pool
);
1406 /* Pools for cost vectors. It is defined only for allocno classes. */
1407 static alloc_pool cost_vector_pool
[N_REG_CLASSES
];
1409 /* The function initiates work with hard register cost vectors. It
1410 creates allocation pool for each allocno class. */
1412 initiate_cost_vectors (void)
1415 enum reg_class aclass
;
1417 for (i
= 0; i
< ira_allocno_classes_num
; i
++)
1419 aclass
= ira_allocno_classes
[i
];
1420 cost_vector_pool
[aclass
]
1421 = create_alloc_pool ("cost vectors",
1422 sizeof (int) * ira_class_hard_regs_num
[aclass
],
1427 /* Allocate and return a cost vector VEC for ACLASS. */
1429 ira_allocate_cost_vector (reg_class_t aclass
)
1431 return (int *) pool_alloc (cost_vector_pool
[(int) aclass
]);
1434 /* Free a cost vector VEC for ACLASS. */
1436 ira_free_cost_vector (int *vec
, reg_class_t aclass
)
1438 ira_assert (vec
!= NULL
);
1439 pool_free (cost_vector_pool
[(int) aclass
], vec
);
1442 /* Finish work with hard register cost vectors. Release allocation
1443 pool for each allocno class. */
1445 finish_cost_vectors (void)
1448 enum reg_class aclass
;
1450 for (i
= 0; i
< ira_allocno_classes_num
; i
++)
1452 aclass
= ira_allocno_classes
[i
];
1453 free_alloc_pool (cost_vector_pool
[aclass
]);
1459 /* Compute a post-ordering of the reverse control flow of the loop body
1460 designated by the children nodes of LOOP_NODE, whose body nodes in
1461 pre-order are input as LOOP_PREORDER. Return a VEC with a post-order
1462 of the reverse loop body.
1464 For the post-order of the reverse CFG, we visit the basic blocks in
1465 LOOP_PREORDER array in the reverse order of where they appear.
1466 This is important: We do not just want to compute a post-order of
1467 the reverse CFG, we want to make a best-guess for a visiting order that
1468 minimizes the number of chain elements per allocno live range. If the
1469 blocks would be visited in a different order, we would still compute a
1470 correct post-ordering but it would be less likely that two nodes
1471 connected by an edge in the CFG are neighbours in the topsort. */
1473 static vec
<ira_loop_tree_node_t
>
1474 ira_loop_tree_body_rev_postorder (ira_loop_tree_node_t loop_node ATTRIBUTE_UNUSED
,
1475 vec
<ira_loop_tree_node_t
> loop_preorder
)
1477 vec
<ira_loop_tree_node_t
> topsort_nodes
= vNULL
;
1478 unsigned int n_loop_preorder
;
1480 n_loop_preorder
= loop_preorder
.length ();
1481 if (n_loop_preorder
!= 0)
1483 ira_loop_tree_node_t subloop_node
;
1485 vec
<ira_loop_tree_node_t
> dfs_stack
;
1487 /* This is a bit of strange abuse of the BB_VISITED flag: We use
1488 the flag to mark blocks we still have to visit to add them to
1489 our post-order. Define an alias to avoid confusion. */
1490 #define BB_TO_VISIT BB_VISITED
1492 FOR_EACH_VEC_ELT (loop_preorder
, i
, subloop_node
)
1494 gcc_checking_assert (! (subloop_node
->bb
->flags
& BB_TO_VISIT
));
1495 subloop_node
->bb
->flags
|= BB_TO_VISIT
;
1498 topsort_nodes
.create (n_loop_preorder
);
1499 dfs_stack
.create (n_loop_preorder
);
1501 FOR_EACH_VEC_ELT_REVERSE (loop_preorder
, i
, subloop_node
)
1503 if (! (subloop_node
->bb
->flags
& BB_TO_VISIT
))
1506 subloop_node
->bb
->flags
&= ~BB_TO_VISIT
;
1507 dfs_stack
.quick_push (subloop_node
);
1508 while (! dfs_stack
.is_empty ())
1513 ira_loop_tree_node_t n
= dfs_stack
.last ();
1514 FOR_EACH_EDGE (e
, ei
, n
->bb
->preds
)
1516 ira_loop_tree_node_t pred_node
;
1517 basic_block pred_bb
= e
->src
;
1519 if (e
->src
== ENTRY_BLOCK_PTR
)
1522 pred_node
= IRA_BB_NODE_BY_INDEX (pred_bb
->index
);
1524 && (pred_node
->bb
->flags
& BB_TO_VISIT
))
1526 pred_node
->bb
->flags
&= ~BB_TO_VISIT
;
1527 dfs_stack
.quick_push (pred_node
);
1530 if (n
== dfs_stack
.last ())
1533 topsort_nodes
.quick_push (n
);
1539 dfs_stack
.release ();
1542 gcc_assert (topsort_nodes
.length () == n_loop_preorder
);
1543 return topsort_nodes
;
1546 /* The current loop tree node and its regno allocno map. */
1547 ira_loop_tree_node_t ira_curr_loop_tree_node
;
1548 ira_allocno_t
*ira_curr_regno_allocno_map
;
1550 /* This recursive function traverses loop tree with root LOOP_NODE
1551 calling non-null functions PREORDER_FUNC and POSTORDER_FUNC
1552 correspondingly in preorder and postorder. The function sets up
1553 IRA_CURR_LOOP_TREE_NODE and IRA_CURR_REGNO_ALLOCNO_MAP. If BB_P,
1554 basic block nodes of LOOP_NODE is also processed (before its
1557 If BB_P is set and POSTORDER_FUNC is given, the basic blocks in
1558 the loop are passed in the *reverse* post-order of the *reverse*
1559 CFG. This is only used by ira_create_allocno_live_ranges, which
1560 wants to visit basic blocks in this order to minimize the number
1561 of elements per live range chain.
1562 Note that the loop tree nodes are still visited in the normal,
1563 forward post-order of the loop tree. */
1566 ira_traverse_loop_tree (bool bb_p
, ira_loop_tree_node_t loop_node
,
1567 void (*preorder_func
) (ira_loop_tree_node_t
),
1568 void (*postorder_func
) (ira_loop_tree_node_t
))
1570 ira_loop_tree_node_t subloop_node
;
1572 ira_assert (loop_node
->bb
== NULL
);
1573 ira_curr_loop_tree_node
= loop_node
;
1574 ira_curr_regno_allocno_map
= ira_curr_loop_tree_node
->regno_allocno_map
;
1576 if (preorder_func
!= NULL
)
1577 (*preorder_func
) (loop_node
);
1581 vec
<ira_loop_tree_node_t
>
1582 loop_preorder
= vNULL
;
1585 /* Add all nodes to the set of nodes to visit. The IRA loop tree
1586 is set up such that nodes in the loop body appear in a pre-order
1587 of their place in the CFG. */
1588 for (subloop_node
= loop_node
->children
;
1589 subloop_node
!= NULL
;
1590 subloop_node
= subloop_node
->next
)
1591 if (subloop_node
->bb
!= NULL
)
1592 loop_preorder
.safe_push (subloop_node
);
1594 if (preorder_func
!= NULL
)
1595 FOR_EACH_VEC_ELT (loop_preorder
, i
, subloop_node
)
1596 (*preorder_func
) (subloop_node
);
1598 if (postorder_func
!= NULL
)
1600 vec
<ira_loop_tree_node_t
> loop_rev_postorder
=
1601 ira_loop_tree_body_rev_postorder (loop_node
, loop_preorder
);
1602 FOR_EACH_VEC_ELT_REVERSE (loop_rev_postorder
, i
, subloop_node
)
1603 (*postorder_func
) (subloop_node
);
1604 loop_rev_postorder
.release ();
1607 loop_preorder
.release ();
1610 for (subloop_node
= loop_node
->subloops
;
1611 subloop_node
!= NULL
;
1612 subloop_node
= subloop_node
->subloop_next
)
1614 ira_assert (subloop_node
->bb
== NULL
);
1615 ira_traverse_loop_tree (bb_p
, subloop_node
,
1616 preorder_func
, postorder_func
);
1619 ira_curr_loop_tree_node
= loop_node
;
1620 ira_curr_regno_allocno_map
= ira_curr_loop_tree_node
->regno_allocno_map
;
1622 if (postorder_func
!= NULL
)
1623 (*postorder_func
) (loop_node
);
1628 /* The basic block currently being processed. */
1629 static basic_block curr_bb
;
1631 /* This recursive function creates allocnos corresponding to
1632 pseudo-registers containing in X. True OUTPUT_P means that X is
1635 create_insn_allocnos (rtx x
, bool output_p
)
1639 enum rtx_code code
= GET_CODE (x
);
1645 if ((regno
= REGNO (x
)) >= FIRST_PSEUDO_REGISTER
)
1649 if ((a
= ira_curr_regno_allocno_map
[regno
]) == NULL
)
1650 a
= ira_create_allocno (regno
, false, ira_curr_loop_tree_node
);
1652 ALLOCNO_NREFS (a
)++;
1653 ALLOCNO_FREQ (a
) += REG_FREQ_FROM_BB (curr_bb
);
1655 bitmap_set_bit (ira_curr_loop_tree_node
->modified_regnos
, regno
);
1659 else if (code
== SET
)
1661 create_insn_allocnos (SET_DEST (x
), true);
1662 create_insn_allocnos (SET_SRC (x
), false);
1665 else if (code
== CLOBBER
)
1667 create_insn_allocnos (XEXP (x
, 0), true);
1670 else if (code
== MEM
)
1672 create_insn_allocnos (XEXP (x
, 0), false);
1675 else if (code
== PRE_DEC
|| code
== POST_DEC
|| code
== PRE_INC
||
1676 code
== POST_INC
|| code
== POST_MODIFY
|| code
== PRE_MODIFY
)
1678 create_insn_allocnos (XEXP (x
, 0), true);
1679 create_insn_allocnos (XEXP (x
, 0), false);
1683 fmt
= GET_RTX_FORMAT (code
);
1684 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1687 create_insn_allocnos (XEXP (x
, i
), output_p
);
1688 else if (fmt
[i
] == 'E')
1689 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1690 create_insn_allocnos (XVECEXP (x
, i
, j
), output_p
);
1694 /* Create allocnos corresponding to pseudo-registers living in the
1695 basic block represented by the corresponding loop tree node
1698 create_bb_allocnos (ira_loop_tree_node_t bb_node
)
1705 curr_bb
= bb
= bb_node
->bb
;
1706 ira_assert (bb
!= NULL
);
1707 FOR_BB_INSNS_REVERSE (bb
, insn
)
1708 if (NONDEBUG_INSN_P (insn
))
1709 create_insn_allocnos (PATTERN (insn
), false);
1710 /* It might be a allocno living through from one subloop to
1712 EXECUTE_IF_SET_IN_REG_SET (df_get_live_in (bb
), FIRST_PSEUDO_REGISTER
, i
, bi
)
1713 if (ira_curr_regno_allocno_map
[i
] == NULL
)
1714 ira_create_allocno (i
, false, ira_curr_loop_tree_node
);
1717 /* Create allocnos corresponding to pseudo-registers living on edge E
1718 (a loop entry or exit). Also mark the allocnos as living on the
1721 create_loop_allocnos (edge e
)
1724 bitmap live_in_regs
, border_allocnos
;
1726 ira_loop_tree_node_t parent
;
1728 live_in_regs
= df_get_live_in (e
->dest
);
1729 border_allocnos
= ira_curr_loop_tree_node
->border_allocnos
;
1730 EXECUTE_IF_SET_IN_REG_SET (df_get_live_out (e
->src
),
1731 FIRST_PSEUDO_REGISTER
, i
, bi
)
1732 if (bitmap_bit_p (live_in_regs
, i
))
1734 if (ira_curr_regno_allocno_map
[i
] == NULL
)
1736 /* The order of creations is important for right
1737 ira_regno_allocno_map. */
1738 if ((parent
= ira_curr_loop_tree_node
->parent
) != NULL
1739 && parent
->regno_allocno_map
[i
] == NULL
)
1740 ira_create_allocno (i
, false, parent
);
1741 ira_create_allocno (i
, false, ira_curr_loop_tree_node
);
1743 bitmap_set_bit (border_allocnos
,
1744 ALLOCNO_NUM (ira_curr_regno_allocno_map
[i
]));
1748 /* Create allocnos corresponding to pseudo-registers living in loop
1749 represented by the corresponding loop tree node LOOP_NODE. This
1750 function is called by ira_traverse_loop_tree. */
1752 create_loop_tree_node_allocnos (ira_loop_tree_node_t loop_node
)
1754 if (loop_node
->bb
!= NULL
)
1755 create_bb_allocnos (loop_node
);
1756 else if (loop_node
!= ira_loop_tree_root
)
1763 ira_assert (current_loops
!= NULL
);
1764 FOR_EACH_EDGE (e
, ei
, loop_node
->loop
->header
->preds
)
1765 if (e
->src
!= loop_node
->loop
->latch
)
1766 create_loop_allocnos (e
);
1768 edges
= get_loop_exit_edges (loop_node
->loop
);
1769 FOR_EACH_VEC_ELT (edges
, i
, e
)
1770 create_loop_allocnos (e
);
1775 /* Propagate information about allocnos modified inside the loop given
1776 by its LOOP_TREE_NODE to its parent. */
1778 propagate_modified_regnos (ira_loop_tree_node_t loop_tree_node
)
1780 if (loop_tree_node
== ira_loop_tree_root
)
1782 ira_assert (loop_tree_node
->bb
== NULL
);
1783 bitmap_ior_into (loop_tree_node
->parent
->modified_regnos
,
1784 loop_tree_node
->modified_regnos
);
1787 /* Propagate new info about allocno A (see comments about accumulated
1788 info in allocno definition) to the corresponding allocno on upper
1789 loop tree level. So allocnos on upper levels accumulate
1790 information about the corresponding allocnos in nested regions.
1791 The new info means allocno info finally calculated in this
1794 propagate_allocno_info (void)
1797 ira_allocno_t a
, parent_a
;
1798 ira_loop_tree_node_t parent
;
1799 enum reg_class aclass
;
1801 if (flag_ira_region
!= IRA_REGION_ALL
1802 && flag_ira_region
!= IRA_REGION_MIXED
)
1804 for (i
= max_reg_num () - 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
1805 for (a
= ira_regno_allocno_map
[i
];
1807 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
1808 if ((parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
) != NULL
1809 && (parent_a
= parent
->regno_allocno_map
[i
]) != NULL
1810 /* There are no caps yet at this point. So use
1811 border_allocnos to find allocnos for the propagation. */
1812 && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE (a
)->border_allocnos
,
1815 if (! ALLOCNO_BAD_SPILL_P (a
))
1816 ALLOCNO_BAD_SPILL_P (parent_a
) = false;
1817 ALLOCNO_NREFS (parent_a
) += ALLOCNO_NREFS (a
);
1818 ALLOCNO_FREQ (parent_a
) += ALLOCNO_FREQ (a
);
1819 ALLOCNO_CALL_FREQ (parent_a
) += ALLOCNO_CALL_FREQ (a
);
1820 merge_hard_reg_conflicts (a
, parent_a
, true);
1821 ALLOCNO_CALLS_CROSSED_NUM (parent_a
)
1822 += ALLOCNO_CALLS_CROSSED_NUM (a
);
1823 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a
)
1824 += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a
);
1825 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a
)
1826 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
);
1827 aclass
= ALLOCNO_CLASS (a
);
1828 ira_assert (aclass
== ALLOCNO_CLASS (parent_a
));
1829 ira_allocate_and_accumulate_costs
1830 (&ALLOCNO_HARD_REG_COSTS (parent_a
), aclass
,
1831 ALLOCNO_HARD_REG_COSTS (a
));
1832 ira_allocate_and_accumulate_costs
1833 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a
),
1835 ALLOCNO_CONFLICT_HARD_REG_COSTS (a
));
1836 ALLOCNO_CLASS_COST (parent_a
)
1837 += ALLOCNO_CLASS_COST (a
);
1838 ALLOCNO_MEMORY_COST (parent_a
) += ALLOCNO_MEMORY_COST (a
);
1842 /* Create allocnos corresponding to pseudo-registers in the current
1843 function. Traverse the loop tree for this. */
1845 create_allocnos (void)
1847 /* We need to process BB first to correctly link allocnos by member
1848 next_regno_allocno. */
1849 ira_traverse_loop_tree (true, ira_loop_tree_root
,
1850 create_loop_tree_node_allocnos
, NULL
);
1852 ira_traverse_loop_tree (false, ira_loop_tree_root
, NULL
,
1853 propagate_modified_regnos
);
1858 /* The page contains function to remove some regions from a separate
1859 register allocation. We remove regions whose separate allocation
1860 will hardly improve the result. As a result we speed up regional
1861 register allocation. */
1863 /* The function changes the object in range list given by R to OBJ. */
1865 change_object_in_range_list (live_range_t r
, ira_object_t obj
)
1867 for (; r
!= NULL
; r
= r
->next
)
1871 /* Move all live ranges associated with allocno FROM to allocno TO. */
1873 move_allocno_live_ranges (ira_allocno_t from
, ira_allocno_t to
)
1876 int n
= ALLOCNO_NUM_OBJECTS (from
);
1878 gcc_assert (n
== ALLOCNO_NUM_OBJECTS (to
));
1880 for (i
= 0; i
< n
; i
++)
1882 ira_object_t from_obj
= ALLOCNO_OBJECT (from
, i
);
1883 ira_object_t to_obj
= ALLOCNO_OBJECT (to
, i
);
1884 live_range_t lr
= OBJECT_LIVE_RANGES (from_obj
);
1886 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
1888 fprintf (ira_dump_file
,
1889 " Moving ranges of a%dr%d to a%dr%d: ",
1890 ALLOCNO_NUM (from
), ALLOCNO_REGNO (from
),
1891 ALLOCNO_NUM (to
), ALLOCNO_REGNO (to
));
1892 ira_print_live_range_list (ira_dump_file
, lr
);
1894 change_object_in_range_list (lr
, to_obj
);
1895 OBJECT_LIVE_RANGES (to_obj
)
1896 = ira_merge_live_ranges (lr
, OBJECT_LIVE_RANGES (to_obj
));
1897 OBJECT_LIVE_RANGES (from_obj
) = NULL
;
1902 copy_allocno_live_ranges (ira_allocno_t from
, ira_allocno_t to
)
1905 int n
= ALLOCNO_NUM_OBJECTS (from
);
1907 gcc_assert (n
== ALLOCNO_NUM_OBJECTS (to
));
1909 for (i
= 0; i
< n
; i
++)
1911 ira_object_t from_obj
= ALLOCNO_OBJECT (from
, i
);
1912 ira_object_t to_obj
= ALLOCNO_OBJECT (to
, i
);
1913 live_range_t lr
= OBJECT_LIVE_RANGES (from_obj
);
1915 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
1917 fprintf (ira_dump_file
, " Copying ranges of a%dr%d to a%dr%d: ",
1918 ALLOCNO_NUM (from
), ALLOCNO_REGNO (from
),
1919 ALLOCNO_NUM (to
), ALLOCNO_REGNO (to
));
1920 ira_print_live_range_list (ira_dump_file
, lr
);
1922 lr
= ira_copy_live_range_list (lr
);
1923 change_object_in_range_list (lr
, to_obj
);
1924 OBJECT_LIVE_RANGES (to_obj
)
1925 = ira_merge_live_ranges (lr
, OBJECT_LIVE_RANGES (to_obj
));
1929 /* Return TRUE if NODE represents a loop with low register
1932 low_pressure_loop_node_p (ira_loop_tree_node_t node
)
1935 enum reg_class pclass
;
1937 if (node
->bb
!= NULL
)
1940 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1942 pclass
= ira_pressure_classes
[i
];
1943 if (node
->reg_pressure
[pclass
] > ira_class_hard_regs_num
[pclass
]
1944 && ira_class_hard_regs_num
[pclass
] > 1)
1951 /* Return TRUE if LOOP has a complex enter or exit edge. We don't
1952 form a region from such loop if the target use stack register
1953 because reg-stack.c can not deal with such edges. */
1955 loop_with_complex_edge_p (struct loop
*loop
)
1963 FOR_EACH_EDGE (e
, ei
, loop
->header
->preds
)
1964 if (e
->flags
& EDGE_EH
)
1966 edges
= get_loop_exit_edges (loop
);
1968 FOR_EACH_VEC_ELT (edges
, i
, e
)
1969 if (e
->flags
& EDGE_COMPLEX
)
1979 /* Sort loops for marking them for removal. We put already marked
1980 loops first, then less frequent loops next, and then outer loops
1983 loop_compare_func (const void *v1p
, const void *v2p
)
1986 ira_loop_tree_node_t l1
= *(const ira_loop_tree_node_t
*) v1p
;
1987 ira_loop_tree_node_t l2
= *(const ira_loop_tree_node_t
*) v2p
;
1989 ira_assert (l1
->parent
!= NULL
&& l2
->parent
!= NULL
);
1990 if (l1
->to_remove_p
&& ! l2
->to_remove_p
)
1992 if (! l1
->to_remove_p
&& l2
->to_remove_p
)
1994 if ((diff
= l1
->loop
->header
->frequency
- l2
->loop
->header
->frequency
) != 0)
1996 if ((diff
= (int) loop_depth (l1
->loop
) - (int) loop_depth (l2
->loop
)) != 0)
1998 /* Make sorting stable. */
1999 return l1
->loop_num
- l2
->loop_num
;
2002 /* Mark loops which should be removed from regional allocation. We
2003 remove a loop with low register pressure inside another loop with
2004 register pressure. In this case a separate allocation of the loop
2005 hardly helps (for irregular register file architecture it could
2006 help by choosing a better hard register in the loop but we prefer
2007 faster allocation even in this case). We also remove cheap loops
2008 if there are more than IRA_MAX_LOOPS_NUM of them. Loop with EH
2009 exit or enter edges are removed too because the allocation might
2010 require put pseudo moves on the EH edges (we could still do this
2011 for pseudos with caller saved hard registers in some cases but it
2012 is impossible to say here or during top-down allocation pass what
2013 hard register the pseudos get finally). */
2015 mark_loops_for_removal (void)
2018 ira_loop_tree_node_t
*sorted_loops
;
2021 ira_assert (current_loops
!= NULL
);
2023 = (ira_loop_tree_node_t
*) ira_allocate (sizeof (ira_loop_tree_node_t
)
2024 * number_of_loops ());
2025 for (n
= i
= 0; vec_safe_iterate (get_loops (), i
, &loop
); i
++)
2026 if (ira_loop_nodes
[i
].regno_allocno_map
!= NULL
)
2028 if (ira_loop_nodes
[i
].parent
== NULL
)
2030 /* Don't remove the root. */
2031 ira_loop_nodes
[i
].to_remove_p
= false;
2034 sorted_loops
[n
++] = &ira_loop_nodes
[i
];
2035 ira_loop_nodes
[i
].to_remove_p
2036 = ((low_pressure_loop_node_p (ira_loop_nodes
[i
].parent
)
2037 && low_pressure_loop_node_p (&ira_loop_nodes
[i
]))
2039 || loop_with_complex_edge_p (ira_loop_nodes
[i
].loop
)
2043 qsort (sorted_loops
, n
, sizeof (ira_loop_tree_node_t
), loop_compare_func
);
2044 for (i
= 0; n
- i
+ 1 > IRA_MAX_LOOPS_NUM
; i
++)
2046 sorted_loops
[i
]->to_remove_p
= true;
2047 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
2050 " Mark loop %d (header %d, freq %d, depth %d) for removal (%s)\n",
2051 sorted_loops
[i
]->loop_num
, sorted_loops
[i
]->loop
->header
->index
,
2052 sorted_loops
[i
]->loop
->header
->frequency
,
2053 loop_depth (sorted_loops
[i
]->loop
),
2054 low_pressure_loop_node_p (sorted_loops
[i
]->parent
)
2055 && low_pressure_loop_node_p (sorted_loops
[i
])
2056 ? "low pressure" : "cheap loop");
2058 ira_free (sorted_loops
);
2061 /* Mark all loops but root for removing. */
2063 mark_all_loops_for_removal (void)
2068 ira_assert (current_loops
!= NULL
);
2069 FOR_EACH_VEC_SAFE_ELT (get_loops (), i
, loop
)
2070 if (ira_loop_nodes
[i
].regno_allocno_map
!= NULL
)
2072 if (ira_loop_nodes
[i
].parent
== NULL
)
2074 /* Don't remove the root. */
2075 ira_loop_nodes
[i
].to_remove_p
= false;
2078 ira_loop_nodes
[i
].to_remove_p
= true;
2079 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
2082 " Mark loop %d (header %d, freq %d, depth %d) for removal\n",
2083 ira_loop_nodes
[i
].loop_num
,
2084 ira_loop_nodes
[i
].loop
->header
->index
,
2085 ira_loop_nodes
[i
].loop
->header
->frequency
,
2086 loop_depth (ira_loop_nodes
[i
].loop
));
2090 /* Definition of vector of loop tree nodes. */
2092 /* Vec containing references to all removed loop tree nodes. */
2093 static vec
<ira_loop_tree_node_t
> removed_loop_vec
;
2095 /* Vec containing references to all children of loop tree nodes. */
2096 static vec
<ira_loop_tree_node_t
> children_vec
;
2098 /* Remove subregions of NODE if their separate allocation will not
2099 improve the result. */
2101 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_node_t node
)
2105 ira_loop_tree_node_t subnode
;
2107 remove_p
= node
->to_remove_p
;
2109 children_vec
.safe_push (node
);
2110 start
= children_vec
.length ();
2111 for (subnode
= node
->children
; subnode
!= NULL
; subnode
= subnode
->next
)
2112 if (subnode
->bb
== NULL
)
2113 remove_uneccesary_loop_nodes_from_loop_tree (subnode
);
2115 children_vec
.safe_push (subnode
);
2116 node
->children
= node
->subloops
= NULL
;
2119 removed_loop_vec
.safe_push (node
);
2122 while (children_vec
.length () > start
)
2124 subnode
= children_vec
.pop ();
2125 subnode
->parent
= node
;
2126 subnode
->next
= node
->children
;
2127 node
->children
= subnode
;
2128 if (subnode
->bb
== NULL
)
2130 subnode
->subloop_next
= node
->subloops
;
2131 node
->subloops
= subnode
;
2136 /* Return TRUE if NODE is inside PARENT. */
2138 loop_is_inside_p (ira_loop_tree_node_t node
, ira_loop_tree_node_t parent
)
2140 for (node
= node
->parent
; node
!= NULL
; node
= node
->parent
)
2146 /* Sort allocnos according to their order in regno allocno list. */
2148 regno_allocno_order_compare_func (const void *v1p
, const void *v2p
)
2150 ira_allocno_t a1
= *(const ira_allocno_t
*) v1p
;
2151 ira_allocno_t a2
= *(const ira_allocno_t
*) v2p
;
2152 ira_loop_tree_node_t n1
= ALLOCNO_LOOP_TREE_NODE (a1
);
2153 ira_loop_tree_node_t n2
= ALLOCNO_LOOP_TREE_NODE (a2
);
2155 if (loop_is_inside_p (n1
, n2
))
2157 else if (loop_is_inside_p (n2
, n1
))
2159 /* If allocnos are equally good, sort by allocno numbers, so that
2160 the results of qsort leave nothing to chance. We put allocnos
2161 with higher number first in the list because it is the original
2162 order for allocnos from loops on the same levels. */
2163 return ALLOCNO_NUM (a2
) - ALLOCNO_NUM (a1
);
2166 /* This array is used to sort allocnos to restore allocno order in
2167 the regno allocno list. */
2168 static ira_allocno_t
*regno_allocnos
;
2170 /* Restore allocno order for REGNO in the regno allocno list. */
2172 ira_rebuild_regno_allocno_list (int regno
)
2177 for (n
= 0, a
= ira_regno_allocno_map
[regno
];
2179 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
2180 regno_allocnos
[n
++] = a
;
2182 qsort (regno_allocnos
, n
, sizeof (ira_allocno_t
),
2183 regno_allocno_order_compare_func
);
2184 for (i
= 1; i
< n
; i
++)
2185 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos
[i
- 1]) = regno_allocnos
[i
];
2186 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos
[n
- 1]) = NULL
;
2187 ira_regno_allocno_map
[regno
] = regno_allocnos
[0];
2188 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
2189 fprintf (ira_dump_file
, " Rebuilding regno allocno list for %d\n", regno
);
2192 /* Propagate info from allocno FROM_A to allocno A. */
2194 propagate_some_info_from_allocno (ira_allocno_t a
, ira_allocno_t from_a
)
2196 enum reg_class aclass
;
2198 merge_hard_reg_conflicts (from_a
, a
, false);
2199 ALLOCNO_NREFS (a
) += ALLOCNO_NREFS (from_a
);
2200 ALLOCNO_FREQ (a
) += ALLOCNO_FREQ (from_a
);
2201 ALLOCNO_CALL_FREQ (a
) += ALLOCNO_CALL_FREQ (from_a
);
2202 ALLOCNO_CALLS_CROSSED_NUM (a
) += ALLOCNO_CALLS_CROSSED_NUM (from_a
);
2203 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a
)
2204 += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (from_a
);
2205 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
)
2206 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (from_a
);
2207 if (! ALLOCNO_BAD_SPILL_P (from_a
))
2208 ALLOCNO_BAD_SPILL_P (a
) = false;
2209 aclass
= ALLOCNO_CLASS (from_a
);
2210 ira_assert (aclass
== ALLOCNO_CLASS (a
));
2211 ira_allocate_and_accumulate_costs (&ALLOCNO_HARD_REG_COSTS (a
), aclass
,
2212 ALLOCNO_HARD_REG_COSTS (from_a
));
2213 ira_allocate_and_accumulate_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a
),
2215 ALLOCNO_CONFLICT_HARD_REG_COSTS (from_a
));
2216 ALLOCNO_CLASS_COST (a
) += ALLOCNO_CLASS_COST (from_a
);
2217 ALLOCNO_MEMORY_COST (a
) += ALLOCNO_MEMORY_COST (from_a
);
2220 /* Remove allocnos from loops removed from the allocation
2223 remove_unnecessary_allocnos (void)
2226 bool merged_p
, rebuild_p
;
2227 ira_allocno_t a
, prev_a
, next_a
, parent_a
;
2228 ira_loop_tree_node_t a_node
, parent
;
2231 regno_allocnos
= NULL
;
2232 for (regno
= max_reg_num () - 1; regno
>= FIRST_PSEUDO_REGISTER
; regno
--)
2235 for (prev_a
= NULL
, a
= ira_regno_allocno_map
[regno
];
2239 next_a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
);
2240 a_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2241 if (! a_node
->to_remove_p
)
2245 for (parent
= a_node
->parent
;
2246 (parent_a
= parent
->regno_allocno_map
[regno
]) == NULL
2247 && parent
->to_remove_p
;
2248 parent
= parent
->parent
)
2250 if (parent_a
== NULL
)
2252 /* There are no allocnos with the same regno in
2253 upper region -- just move the allocno to the
2256 ALLOCNO_LOOP_TREE_NODE (a
) = parent
;
2257 parent
->regno_allocno_map
[regno
] = a
;
2258 bitmap_set_bit (parent
->all_allocnos
, ALLOCNO_NUM (a
));
2263 /* Remove the allocno and update info of allocno in
2264 the upper region. */
2266 ira_regno_allocno_map
[regno
] = next_a
;
2268 ALLOCNO_NEXT_REGNO_ALLOCNO (prev_a
) = next_a
;
2269 move_allocno_live_ranges (a
, parent_a
);
2271 propagate_some_info_from_allocno (parent_a
, a
);
2272 /* Remove it from the corresponding regno allocno
2273 map to avoid info propagation of subsequent
2274 allocno into this already removed allocno. */
2275 a_node
->regno_allocno_map
[regno
] = NULL
;
2281 /* We need to restore the order in regno allocno list. */
2283 if (regno_allocnos
== NULL
)
2285 = (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
)
2286 * ira_allocnos_num
);
2287 ira_rebuild_regno_allocno_list (regno
);
2291 ira_rebuild_start_finish_chains ();
2292 if (regno_allocnos
!= NULL
)
2293 ira_free (regno_allocnos
);
2296 /* Remove allocnos from all loops but the root. */
2298 remove_low_level_allocnos (void)
2301 bool merged_p
, propagate_p
;
2302 ira_allocno_t a
, top_a
;
2303 ira_loop_tree_node_t a_node
, parent
;
2304 ira_allocno_iterator ai
;
2307 FOR_EACH_ALLOCNO (a
, ai
)
2309 a_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2310 if (a_node
== ira_loop_tree_root
|| ALLOCNO_CAP_MEMBER (a
) != NULL
)
2312 regno
= ALLOCNO_REGNO (a
);
2313 if ((top_a
= ira_loop_tree_root
->regno_allocno_map
[regno
]) == NULL
)
2315 ALLOCNO_LOOP_TREE_NODE (a
) = ira_loop_tree_root
;
2316 ira_loop_tree_root
->regno_allocno_map
[regno
] = a
;
2319 propagate_p
= a_node
->parent
->regno_allocno_map
[regno
] == NULL
;
2320 /* Remove the allocno and update info of allocno in the upper
2322 move_allocno_live_ranges (a
, top_a
);
2325 propagate_some_info_from_allocno (top_a
, a
);
2327 FOR_EACH_ALLOCNO (a
, ai
)
2329 a_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2330 if (a_node
== ira_loop_tree_root
)
2332 parent
= a_node
->parent
;
2333 regno
= ALLOCNO_REGNO (a
);
2334 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
2335 ira_assert (ALLOCNO_CAP (a
) != NULL
);
2336 else if (ALLOCNO_CAP (a
) == NULL
)
2337 ira_assert (parent
->regno_allocno_map
[regno
] != NULL
);
2339 FOR_EACH_ALLOCNO (a
, ai
)
2341 regno
= ALLOCNO_REGNO (a
);
2342 if (ira_loop_tree_root
->regno_allocno_map
[regno
] == a
)
2345 ira_allocno_object_iterator oi
;
2347 ira_regno_allocno_map
[regno
] = a
;
2348 ALLOCNO_NEXT_REGNO_ALLOCNO (a
) = NULL
;
2349 ALLOCNO_CAP_MEMBER (a
) = NULL
;
2350 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
2351 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj
),
2352 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
));
2354 if (ALLOCNO_TOTAL_NO_STACK_REG_P (a
))
2355 ALLOCNO_NO_STACK_REG_P (a
) = true;
2362 ira_rebuild_start_finish_chains ();
2365 /* Remove loops from consideration. We remove all loops except for
2366 root if ALL_P or loops for which a separate allocation will not
2367 improve the result. We have to do this after allocno creation and
2368 their costs and allocno class evaluation because only after that
2369 the register pressure can be known and is calculated. */
2371 remove_unnecessary_regions (bool all_p
)
2373 if (current_loops
== NULL
)
2376 mark_all_loops_for_removal ();
2378 mark_loops_for_removal ();
2379 children_vec
.create(last_basic_block
+ number_of_loops ());
2380 removed_loop_vec
.create(last_basic_block
+ number_of_loops ());
2381 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_root
);
2382 children_vec
.release ();
2384 remove_low_level_allocnos ();
2386 remove_unnecessary_allocnos ();
2387 while (removed_loop_vec
.length () > 0)
2388 finish_loop_tree_node (removed_loop_vec
.pop ());
2389 removed_loop_vec
.release ();
2394 /* At this point true value of allocno attribute bad_spill_p means
2395 that there is an insn where allocno occurs and where the allocno
2396 can not be used as memory. The function updates the attribute, now
2397 it can be true only for allocnos which can not be used as memory in
2398 an insn and in whose live ranges there is other allocno deaths.
2399 Spilling allocnos with true value will not improve the code because
2400 it will not make other allocnos colorable and additional reloads
2401 for the corresponding pseudo will be generated in reload pass for
2402 each insn it occurs.
2404 This is a trick mentioned in one classic article of Chaitin etc
2405 which is frequently omitted in other implementations of RA based on
2408 update_bad_spill_attribute (void)
2412 ira_allocno_iterator ai
;
2413 ira_allocno_object_iterator aoi
;
2416 enum reg_class aclass
;
2417 bitmap_head dead_points
[N_REG_CLASSES
];
2419 for (i
= 0; i
< ira_allocno_classes_num
; i
++)
2421 aclass
= ira_allocno_classes
[i
];
2422 bitmap_initialize (&dead_points
[aclass
], ®_obstack
);
2424 FOR_EACH_ALLOCNO (a
, ai
)
2426 aclass
= ALLOCNO_CLASS (a
);
2427 if (aclass
== NO_REGS
)
2429 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, aoi
)
2430 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
2431 bitmap_set_bit (&dead_points
[aclass
], r
->finish
);
2433 FOR_EACH_ALLOCNO (a
, ai
)
2435 aclass
= ALLOCNO_CLASS (a
);
2436 if (aclass
== NO_REGS
)
2438 if (! ALLOCNO_BAD_SPILL_P (a
))
2440 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, aoi
)
2442 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
2444 for (i
= r
->start
+ 1; i
< r
->finish
; i
++)
2445 if (bitmap_bit_p (&dead_points
[aclass
], i
))
2452 ALLOCNO_BAD_SPILL_P (a
) = false;
2457 for (i
= 0; i
< ira_allocno_classes_num
; i
++)
2459 aclass
= ira_allocno_classes
[i
];
2460 bitmap_clear (&dead_points
[aclass
]);
2466 /* Set up minimal and maximal live range points for allocnos. */
2468 setup_min_max_allocno_live_range_point (void)
2471 ira_allocno_t a
, parent_a
, cap
;
2472 ira_allocno_iterator ai
;
2473 #ifdef ENABLE_IRA_CHECKING
2474 ira_object_iterator oi
;
2478 ira_loop_tree_node_t parent
;
2480 FOR_EACH_ALLOCNO (a
, ai
)
2482 int n
= ALLOCNO_NUM_OBJECTS (a
);
2484 for (i
= 0; i
< n
; i
++)
2486 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
2487 r
= OBJECT_LIVE_RANGES (obj
);
2490 OBJECT_MAX (obj
) = r
->finish
;
2491 for (; r
->next
!= NULL
; r
= r
->next
)
2493 OBJECT_MIN (obj
) = r
->start
;
2496 for (i
= max_reg_num () - 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
2497 for (a
= ira_regno_allocno_map
[i
];
2499 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
2502 int n
= ALLOCNO_NUM_OBJECTS (a
);
2504 for (j
= 0; j
< n
; j
++)
2506 ira_object_t obj
= ALLOCNO_OBJECT (a
, j
);
2507 ira_object_t parent_obj
;
2509 if (OBJECT_MAX (obj
) < 0)
2511 ira_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
2512 /* Accumulation of range info. */
2513 if (ALLOCNO_CAP (a
) != NULL
)
2515 for (cap
= ALLOCNO_CAP (a
); cap
!= NULL
; cap
= ALLOCNO_CAP (cap
))
2517 ira_object_t cap_obj
= ALLOCNO_OBJECT (cap
, j
);
2518 if (OBJECT_MAX (cap_obj
) < OBJECT_MAX (obj
))
2519 OBJECT_MAX (cap_obj
) = OBJECT_MAX (obj
);
2520 if (OBJECT_MIN (cap_obj
) > OBJECT_MIN (obj
))
2521 OBJECT_MIN (cap_obj
) = OBJECT_MIN (obj
);
2525 if ((parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
) == NULL
)
2527 parent_a
= parent
->regno_allocno_map
[i
];
2528 parent_obj
= ALLOCNO_OBJECT (parent_a
, j
);
2529 if (OBJECT_MAX (parent_obj
) < OBJECT_MAX (obj
))
2530 OBJECT_MAX (parent_obj
) = OBJECT_MAX (obj
);
2531 if (OBJECT_MIN (parent_obj
) > OBJECT_MIN (obj
))
2532 OBJECT_MIN (parent_obj
) = OBJECT_MIN (obj
);
2535 #ifdef ENABLE_IRA_CHECKING
2536 FOR_EACH_OBJECT (obj
, oi
)
2538 if ((0 <= OBJECT_MIN (obj
) && OBJECT_MIN (obj
) <= ira_max_point
)
2539 && (0 <= OBJECT_MAX (obj
) && OBJECT_MAX (obj
) <= ira_max_point
))
2546 /* Sort allocnos according to their live ranges. Allocnos with
2547 smaller allocno class are put first unless we use priority
2548 coloring. Allocnos with the same class are ordered according
2549 their start (min). Allocnos with the same start are ordered
2550 according their finish (max). */
2552 object_range_compare_func (const void *v1p
, const void *v2p
)
2555 ira_object_t obj1
= *(const ira_object_t
*) v1p
;
2556 ira_object_t obj2
= *(const ira_object_t
*) v2p
;
2557 ira_allocno_t a1
= OBJECT_ALLOCNO (obj1
);
2558 ira_allocno_t a2
= OBJECT_ALLOCNO (obj2
);
2560 if ((diff
= OBJECT_MIN (obj1
) - OBJECT_MIN (obj2
)) != 0)
2562 if ((diff
= OBJECT_MAX (obj1
) - OBJECT_MAX (obj2
)) != 0)
2564 return ALLOCNO_NUM (a1
) - ALLOCNO_NUM (a2
);
2567 /* Sort ira_object_id_map and set up conflict id of allocnos. */
2569 sort_conflict_id_map (void)
2573 ira_allocno_iterator ai
;
2576 FOR_EACH_ALLOCNO (a
, ai
)
2578 ira_allocno_object_iterator oi
;
2581 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
2582 ira_object_id_map
[num
++] = obj
;
2584 qsort (ira_object_id_map
, num
, sizeof (ira_object_t
),
2585 object_range_compare_func
);
2586 for (i
= 0; i
< num
; i
++)
2588 ira_object_t obj
= ira_object_id_map
[i
];
2590 gcc_assert (obj
!= NULL
);
2591 OBJECT_CONFLICT_ID (obj
) = i
;
2593 for (i
= num
; i
< ira_objects_num
; i
++)
2594 ira_object_id_map
[i
] = NULL
;
2597 /* Set up minimal and maximal conflict ids of allocnos with which
2598 given allocno can conflict. */
2600 setup_min_max_conflict_allocno_ids (void)
2603 int i
, j
, min
, max
, start
, finish
, first_not_finished
, filled_area_start
;
2604 int *live_range_min
, *last_lived
;
2605 int word0_min
, word0_max
;
2607 ira_allocno_iterator ai
;
2609 live_range_min
= (int *) ira_allocate (sizeof (int) * ira_objects_num
);
2611 first_not_finished
= -1;
2612 for (i
= 0; i
< ira_objects_num
; i
++)
2614 ira_object_t obj
= ira_object_id_map
[i
];
2619 a
= OBJECT_ALLOCNO (obj
);
2623 aclass
= ALLOCNO_CLASS (a
);
2625 first_not_finished
= i
;
2629 start
= OBJECT_MIN (obj
);
2630 /* If we skip an allocno, the allocno with smaller ids will
2631 be also skipped because of the secondary sorting the
2632 range finishes (see function
2633 object_range_compare_func). */
2634 while (first_not_finished
< i
2635 && start
> OBJECT_MAX (ira_object_id_map
2636 [first_not_finished
]))
2637 first_not_finished
++;
2638 min
= first_not_finished
;
2641 /* We could increase min further in this case but it is good
2644 live_range_min
[i
] = OBJECT_MIN (obj
);
2645 OBJECT_MIN (obj
) = min
;
2647 last_lived
= (int *) ira_allocate (sizeof (int) * ira_max_point
);
2649 filled_area_start
= -1;
2650 for (i
= ira_objects_num
- 1; i
>= 0; i
--)
2652 ira_object_t obj
= ira_object_id_map
[i
];
2657 a
= OBJECT_ALLOCNO (obj
);
2660 aclass
= ALLOCNO_CLASS (a
);
2661 for (j
= 0; j
< ira_max_point
; j
++)
2663 filled_area_start
= ira_max_point
;
2665 min
= live_range_min
[i
];
2666 finish
= OBJECT_MAX (obj
);
2667 max
= last_lived
[finish
];
2669 /* We could decrease max further in this case but it is good
2671 max
= OBJECT_CONFLICT_ID (obj
) - 1;
2672 OBJECT_MAX (obj
) = max
;
2673 /* In filling, we can go further A range finish to recognize
2674 intersection quickly because if the finish of subsequently
2675 processed allocno (it has smaller conflict id) range is
2676 further A range finish than they are definitely intersected
2677 (the reason for this is the allocnos with bigger conflict id
2678 have their range starts not smaller than allocnos with
2680 for (j
= min
; j
< filled_area_start
; j
++)
2682 filled_area_start
= min
;
2684 ira_free (last_lived
);
2685 ira_free (live_range_min
);
2687 /* For allocnos with more than one object, we may later record extra conflicts in
2688 subobject 0 that we cannot really know about here.
2689 For now, simply widen the min/max range of these subobjects. */
2691 word0_min
= INT_MAX
;
2692 word0_max
= INT_MIN
;
2694 FOR_EACH_ALLOCNO (a
, ai
)
2696 int n
= ALLOCNO_NUM_OBJECTS (a
);
2701 obj0
= ALLOCNO_OBJECT (a
, 0);
2702 if (OBJECT_CONFLICT_ID (obj0
) < word0_min
)
2703 word0_min
= OBJECT_CONFLICT_ID (obj0
);
2704 if (OBJECT_CONFLICT_ID (obj0
) > word0_max
)
2705 word0_max
= OBJECT_CONFLICT_ID (obj0
);
2707 FOR_EACH_ALLOCNO (a
, ai
)
2709 int n
= ALLOCNO_NUM_OBJECTS (a
);
2714 obj0
= ALLOCNO_OBJECT (a
, 0);
2715 if (OBJECT_MIN (obj0
) > word0_min
)
2716 OBJECT_MIN (obj0
) = word0_min
;
2717 if (OBJECT_MAX (obj0
) < word0_max
)
2718 OBJECT_MAX (obj0
) = word0_max
;
2728 ira_allocno_iterator ai
;
2729 ira_loop_tree_node_t loop_tree_node
;
2731 FOR_EACH_ALLOCNO (a
, ai
)
2733 if (ALLOCNO_LOOP_TREE_NODE (a
) == ira_loop_tree_root
)
2735 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
2736 create_cap_allocno (a
);
2737 else if (ALLOCNO_CAP (a
) == NULL
)
2739 loop_tree_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2740 if (!bitmap_bit_p (loop_tree_node
->border_allocnos
, ALLOCNO_NUM (a
)))
2741 create_cap_allocno (a
);
2748 /* The page contains code transforming more one region internal
2749 representation (IR) to one region IR which is necessary for reload.
2750 This transformation is called IR flattening. We might just rebuild
2751 the IR for one region but we don't do it because it takes a lot of
2754 /* Map: regno -> allocnos which will finally represent the regno for
2755 IR with one region. */
2756 static ira_allocno_t
*regno_top_level_allocno_map
;
2758 /* Find the allocno that corresponds to A at a level one higher up in the
2759 loop tree. Returns NULL if A is a cap, or if it has no parent. */
2761 ira_parent_allocno (ira_allocno_t a
)
2763 ira_loop_tree_node_t parent
;
2765 if (ALLOCNO_CAP (a
) != NULL
)
2768 parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
;
2772 return parent
->regno_allocno_map
[ALLOCNO_REGNO (a
)];
2775 /* Find the allocno that corresponds to A at a level one higher up in the
2776 loop tree. If ALLOCNO_CAP is set for A, return that. */
2778 ira_parent_or_cap_allocno (ira_allocno_t a
)
2780 if (ALLOCNO_CAP (a
) != NULL
)
2781 return ALLOCNO_CAP (a
);
2783 return ira_parent_allocno (a
);
2786 /* Process all allocnos originated from pseudo REGNO and copy live
2787 ranges, hard reg conflicts, and allocno stack reg attributes from
2788 low level allocnos to final allocnos which are destinations of
2789 removed stores at a loop exit. Return true if we copied live
2792 copy_info_to_removed_store_destinations (int regno
)
2795 ira_allocno_t parent_a
= NULL
;
2796 ira_loop_tree_node_t parent
;
2800 for (a
= ira_regno_allocno_map
[regno
];
2802 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
2804 if (a
!= regno_top_level_allocno_map
[REGNO (allocno_emit_reg (a
))])
2805 /* This allocno will be removed. */
2808 /* Caps will be removed. */
2809 ira_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
2810 for (parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
;
2812 parent
= parent
->parent
)
2813 if ((parent_a
= parent
->regno_allocno_map
[regno
]) == NULL
2815 == regno_top_level_allocno_map
[REGNO
2816 (allocno_emit_reg (parent_a
))]
2817 && ALLOCNO_EMIT_DATA (parent_a
)->mem_optimized_dest_p
))
2819 if (parent
== NULL
|| parent_a
== NULL
)
2822 copy_allocno_live_ranges (a
, parent_a
);
2823 merge_hard_reg_conflicts (a
, parent_a
, true);
2825 ALLOCNO_CALL_FREQ (parent_a
) += ALLOCNO_CALL_FREQ (a
);
2826 ALLOCNO_CALLS_CROSSED_NUM (parent_a
)
2827 += ALLOCNO_CALLS_CROSSED_NUM (a
);
2828 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a
)
2829 += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a
);
2830 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a
)
2831 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
);
2837 /* Flatten the IR. In other words, this function transforms IR as if
2838 it were built with one region (without loops). We could make it
2839 much simpler by rebuilding IR with one region, but unfortunately it
2840 takes a lot of time. MAX_REGNO_BEFORE_EMIT and
2841 IRA_MAX_POINT_BEFORE_EMIT are correspondingly MAX_REG_NUM () and
2842 IRA_MAX_POINT before emitting insns on the loop borders. */
2844 ira_flattening (int max_regno_before_emit
, int ira_max_point_before_emit
)
2849 bool new_pseudos_p
, merged_p
, mem_dest_p
;
2851 enum reg_class aclass
;
2852 ira_allocno_t a
, parent_a
, first
, second
, node_first
, node_second
;
2854 ira_loop_tree_node_t node
;
2856 ira_allocno_iterator ai
;
2857 ira_copy_iterator ci
;
2859 regno_top_level_allocno_map
2860 = (ira_allocno_t
*) ira_allocate (max_reg_num ()
2861 * sizeof (ira_allocno_t
));
2862 memset (regno_top_level_allocno_map
, 0,
2863 max_reg_num () * sizeof (ira_allocno_t
));
2864 new_pseudos_p
= merged_p
= false;
2865 FOR_EACH_ALLOCNO (a
, ai
)
2867 ira_allocno_object_iterator oi
;
2870 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
2871 /* Caps are not in the regno allocno maps and they are never
2872 will be transformed into allocnos existing after IR
2875 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
2876 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
),
2877 OBJECT_CONFLICT_HARD_REGS (obj
));
2879 ALLOCNO_TOTAL_NO_STACK_REG_P (a
) = ALLOCNO_NO_STACK_REG_P (a
);
2882 /* Fix final allocno attributes. */
2883 for (i
= max_regno_before_emit
- 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
2886 for (a
= ira_regno_allocno_map
[i
];
2888 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
2890 ira_emit_data_t parent_data
, data
= ALLOCNO_EMIT_DATA (a
);
2892 ira_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
2893 if (data
->somewhere_renamed_p
)
2894 new_pseudos_p
= true;
2895 parent_a
= ira_parent_allocno (a
);
2896 if (parent_a
== NULL
)
2898 ALLOCNO_COPIES (a
) = NULL
;
2899 regno_top_level_allocno_map
[REGNO (data
->reg
)] = a
;
2902 ira_assert (ALLOCNO_CAP_MEMBER (parent_a
) == NULL
);
2904 if (data
->mem_optimized_dest
!= NULL
)
2906 parent_data
= ALLOCNO_EMIT_DATA (parent_a
);
2907 if (REGNO (data
->reg
) == REGNO (parent_data
->reg
))
2909 merge_hard_reg_conflicts (a
, parent_a
, true);
2910 move_allocno_live_ranges (a
, parent_a
);
2912 parent_data
->mem_optimized_dest_p
2913 = (parent_data
->mem_optimized_dest_p
2914 || data
->mem_optimized_dest_p
);
2917 new_pseudos_p
= true;
2920 ALLOCNO_NREFS (parent_a
) -= ALLOCNO_NREFS (a
);
2921 ALLOCNO_FREQ (parent_a
) -= ALLOCNO_FREQ (a
);
2922 ALLOCNO_CALL_FREQ (parent_a
) -= ALLOCNO_CALL_FREQ (a
);
2923 ALLOCNO_CALLS_CROSSED_NUM (parent_a
)
2924 -= ALLOCNO_CALLS_CROSSED_NUM (a
);
2925 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a
)
2926 -= ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a
);
2927 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a
)
2928 -= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
);
2929 ira_assert (ALLOCNO_CALLS_CROSSED_NUM (parent_a
) >= 0
2930 && ALLOCNO_NREFS (parent_a
) >= 0
2931 && ALLOCNO_FREQ (parent_a
) >= 0);
2932 aclass
= ALLOCNO_CLASS (parent_a
);
2933 hard_regs_num
= ira_class_hard_regs_num
[aclass
];
2934 if (ALLOCNO_HARD_REG_COSTS (a
) != NULL
2935 && ALLOCNO_HARD_REG_COSTS (parent_a
) != NULL
)
2936 for (j
= 0; j
< hard_regs_num
; j
++)
2937 ALLOCNO_HARD_REG_COSTS (parent_a
)[j
]
2938 -= ALLOCNO_HARD_REG_COSTS (a
)[j
];
2939 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a
) != NULL
2940 && ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a
) != NULL
)
2941 for (j
= 0; j
< hard_regs_num
; j
++)
2942 ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a
)[j
]
2943 -= ALLOCNO_CONFLICT_HARD_REG_COSTS (a
)[j
];
2944 ALLOCNO_CLASS_COST (parent_a
)
2945 -= ALLOCNO_CLASS_COST (a
);
2946 ALLOCNO_MEMORY_COST (parent_a
) -= ALLOCNO_MEMORY_COST (a
);
2947 parent_a
= ira_parent_allocno (parent_a
);
2948 if (parent_a
== NULL
)
2951 ALLOCNO_COPIES (a
) = NULL
;
2952 regno_top_level_allocno_map
[REGNO (data
->reg
)] = a
;
2954 if (mem_dest_p
&& copy_info_to_removed_store_destinations (i
))
2957 ira_assert (new_pseudos_p
|| ira_max_point_before_emit
== ira_max_point
);
2958 if (merged_p
|| ira_max_point_before_emit
!= ira_max_point
)
2959 ira_rebuild_start_finish_chains ();
2962 sparseset objects_live
;
2964 /* Rebuild conflicts. */
2965 FOR_EACH_ALLOCNO (a
, ai
)
2967 ira_allocno_object_iterator oi
;
2970 if (a
!= regno_top_level_allocno_map
[REGNO (allocno_emit_reg (a
))]
2971 || ALLOCNO_CAP_MEMBER (a
) != NULL
)
2973 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
2975 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
2976 ira_assert (r
->object
== obj
);
2977 clear_conflicts (obj
);
2980 objects_live
= sparseset_alloc (ira_objects_num
);
2981 for (i
= 0; i
< ira_max_point
; i
++)
2983 for (r
= ira_start_point_ranges
[i
]; r
!= NULL
; r
= r
->start_next
)
2985 ira_object_t obj
= r
->object
;
2987 a
= OBJECT_ALLOCNO (obj
);
2988 if (a
!= regno_top_level_allocno_map
[REGNO (allocno_emit_reg (a
))]
2989 || ALLOCNO_CAP_MEMBER (a
) != NULL
)
2992 aclass
= ALLOCNO_CLASS (a
);
2993 sparseset_set_bit (objects_live
, OBJECT_CONFLICT_ID (obj
));
2994 EXECUTE_IF_SET_IN_SPARSESET (objects_live
, n
)
2996 ira_object_t live_obj
= ira_object_id_map
[n
];
2997 ira_allocno_t live_a
= OBJECT_ALLOCNO (live_obj
);
2998 enum reg_class live_aclass
= ALLOCNO_CLASS (live_a
);
3000 if (ira_reg_classes_intersect_p
[aclass
][live_aclass
]
3001 /* Don't set up conflict for the allocno with itself. */
3003 ira_add_conflict (obj
, live_obj
);
3007 for (r
= ira_finish_point_ranges
[i
]; r
!= NULL
; r
= r
->finish_next
)
3008 sparseset_clear_bit (objects_live
, OBJECT_CONFLICT_ID (r
->object
));
3010 sparseset_free (objects_live
);
3011 compress_conflict_vecs ();
3013 /* Mark some copies for removing and change allocnos in the rest
3015 FOR_EACH_COPY (cp
, ci
)
3017 if (ALLOCNO_CAP_MEMBER (cp
->first
) != NULL
3018 || ALLOCNO_CAP_MEMBER (cp
->second
) != NULL
)
3020 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
3022 (ira_dump_file
, " Remove cp%d:%c%dr%d-%c%dr%d\n",
3023 cp
->num
, ALLOCNO_CAP_MEMBER (cp
->first
) != NULL
? 'c' : 'a',
3024 ALLOCNO_NUM (cp
->first
),
3025 REGNO (allocno_emit_reg (cp
->first
)),
3026 ALLOCNO_CAP_MEMBER (cp
->second
) != NULL
? 'c' : 'a',
3027 ALLOCNO_NUM (cp
->second
),
3028 REGNO (allocno_emit_reg (cp
->second
)));
3029 cp
->loop_tree_node
= NULL
;
3033 = regno_top_level_allocno_map
[REGNO (allocno_emit_reg (cp
->first
))];
3035 = regno_top_level_allocno_map
[REGNO (allocno_emit_reg (cp
->second
))];
3036 node
= cp
->loop_tree_node
;
3038 keep_p
= true; /* It copy generated in ira-emit.c. */
3041 /* Check that the copy was not propagated from level on
3042 which we will have different pseudos. */
3043 node_first
= node
->regno_allocno_map
[ALLOCNO_REGNO (cp
->first
)];
3044 node_second
= node
->regno_allocno_map
[ALLOCNO_REGNO (cp
->second
)];
3045 keep_p
= ((REGNO (allocno_emit_reg (first
))
3046 == REGNO (allocno_emit_reg (node_first
)))
3047 && (REGNO (allocno_emit_reg (second
))
3048 == REGNO (allocno_emit_reg (node_second
))));
3052 cp
->loop_tree_node
= ira_loop_tree_root
;
3054 cp
->second
= second
;
3058 cp
->loop_tree_node
= NULL
;
3059 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
3060 fprintf (ira_dump_file
, " Remove cp%d:a%dr%d-a%dr%d\n",
3061 cp
->num
, ALLOCNO_NUM (cp
->first
),
3062 REGNO (allocno_emit_reg (cp
->first
)),
3063 ALLOCNO_NUM (cp
->second
),
3064 REGNO (allocno_emit_reg (cp
->second
)));
3067 /* Remove unnecessary allocnos on lower levels of the loop tree. */
3068 FOR_EACH_ALLOCNO (a
, ai
)
3070 if (a
!= regno_top_level_allocno_map
[REGNO (allocno_emit_reg (a
))]
3071 || ALLOCNO_CAP_MEMBER (a
) != NULL
)
3073 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
3074 fprintf (ira_dump_file
, " Remove a%dr%d\n",
3075 ALLOCNO_NUM (a
), REGNO (allocno_emit_reg (a
)));
3079 ALLOCNO_LOOP_TREE_NODE (a
) = ira_loop_tree_root
;
3080 ALLOCNO_REGNO (a
) = REGNO (allocno_emit_reg (a
));
3081 ALLOCNO_CAP (a
) = NULL
;
3082 /* Restore updated costs for assignments from reload. */
3083 ALLOCNO_UPDATED_MEMORY_COST (a
) = ALLOCNO_MEMORY_COST (a
);
3084 ALLOCNO_UPDATED_CLASS_COST (a
) = ALLOCNO_CLASS_COST (a
);
3085 if (! ALLOCNO_ASSIGNED_P (a
))
3086 ira_free_allocno_updated_costs (a
);
3087 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
);
3088 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) == NULL
);
3090 /* Remove unnecessary copies. */
3091 FOR_EACH_COPY (cp
, ci
)
3093 if (cp
->loop_tree_node
== NULL
)
3095 ira_copies
[cp
->num
] = NULL
;
3100 (ALLOCNO_LOOP_TREE_NODE (cp
->first
) == ira_loop_tree_root
3101 && ALLOCNO_LOOP_TREE_NODE (cp
->second
) == ira_loop_tree_root
);
3102 ira_add_allocno_copy_to_list (cp
);
3103 ira_swap_allocno_copy_ends_if_necessary (cp
);
3105 rebuild_regno_allocno_maps ();
3106 if (ira_max_point
!= ira_max_point_before_emit
)
3107 ira_compress_allocno_live_ranges ();
3108 ira_free (regno_top_level_allocno_map
);
3113 #ifdef ENABLE_IRA_CHECKING
3114 /* Check creation of all allocnos. Allocnos on lower levels should
3115 have allocnos or caps on all upper levels. */
3117 check_allocno_creation (void)
3120 ira_allocno_iterator ai
;
3121 ira_loop_tree_node_t loop_tree_node
;
3123 FOR_EACH_ALLOCNO (a
, ai
)
3125 loop_tree_node
= ALLOCNO_LOOP_TREE_NODE (a
);
3126 ira_assert (bitmap_bit_p (loop_tree_node
->all_allocnos
,
3128 if (loop_tree_node
== ira_loop_tree_root
)
3130 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
3131 ira_assert (ALLOCNO_CAP (a
) != NULL
);
3132 else if (ALLOCNO_CAP (a
) == NULL
)
3133 ira_assert (loop_tree_node
->parent
3134 ->regno_allocno_map
[ALLOCNO_REGNO (a
)] != NULL
3135 && bitmap_bit_p (loop_tree_node
->border_allocnos
,
3141 /* Identify allocnos which prefer a register class with a single hard register.
3142 Adjust ALLOCNO_CONFLICT_HARD_REG_COSTS so that conflicting allocnos are
3143 less likely to use the preferred singleton register. */
3145 update_conflict_hard_reg_costs (void)
3148 ira_allocno_iterator ai
;
3151 FOR_EACH_ALLOCNO (a
, ai
)
3153 reg_class_t aclass
= ALLOCNO_CLASS (a
);
3154 reg_class_t pref
= reg_preferred_class (ALLOCNO_REGNO (a
));
3155 int singleton
= ira_class_singleton
[pref
][ALLOCNO_MODE (a
)];
3158 index
= ira_class_hard_reg_index
[(int) aclass
][singleton
];
3161 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a
) == NULL
3162 || ALLOCNO_HARD_REG_COSTS (a
) == NULL
)
3165 for (i
= ira_class_hard_regs_num
[(int) aclass
] - 1; i
>= 0; i
--)
3166 if (ALLOCNO_HARD_REG_COSTS (a
)[i
] > ALLOCNO_CLASS_COST (a
)
3167 && min
> ALLOCNO_HARD_REG_COSTS (a
)[i
])
3168 min
= ALLOCNO_HARD_REG_COSTS (a
)[i
];
3171 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a
),
3173 ALLOCNO_CONFLICT_HARD_REG_COSTS (a
)[index
]
3174 -= min
- ALLOCNO_CLASS_COST (a
);
3178 /* Create a internal representation (IR) for IRA (allocnos, copies,
3179 loop tree nodes). The function returns TRUE if we generate loop
3180 structure (besides nodes representing all function and the basic
3181 blocks) for regional allocation. A true return means that we
3182 really need to flatten IR before the reload. */
3189 initiate_cost_vectors ();
3190 initiate_allocnos ();
3192 create_loop_tree_nodes ();
3196 create_allocno_objects ();
3197 ira_create_allocno_live_ranges ();
3198 remove_unnecessary_regions (false);
3199 ira_compress_allocno_live_ranges ();
3200 update_bad_spill_attribute ();
3201 loops_p
= more_one_region_p ();
3204 propagate_allocno_info ();
3207 ira_tune_allocno_costs ();
3208 #ifdef ENABLE_IRA_CHECKING
3209 check_allocno_creation ();
3211 setup_min_max_allocno_live_range_point ();
3212 sort_conflict_id_map ();
3213 setup_min_max_conflict_allocno_ids ();
3214 ira_build_conflicts ();
3215 update_conflict_hard_reg_costs ();
3216 if (! ira_conflicts_p
)
3219 ira_allocno_iterator ai
;
3221 /* Remove all regions but root one. */
3224 remove_unnecessary_regions (true);
3227 /* We don't save hard registers around calls for fast allocation
3228 -- add caller clobbered registers as conflicting ones to
3229 allocno crossing calls. */
3230 FOR_EACH_ALLOCNO (a
, ai
)
3231 if (ALLOCNO_CALLS_CROSSED_NUM (a
) != 0)
3232 ior_hard_reg_conflicts (a
, &call_used_reg_set
);
3234 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
3235 print_copies (ira_dump_file
);
3236 if (internal_flag_ira_verbose
> 0 && ira_dump_file
!= NULL
)
3241 ira_allocno_iterator ai
;
3246 FOR_EACH_ALLOCNO (a
, ai
)
3248 int j
, nobj
= ALLOCNO_NUM_OBJECTS (a
);
3252 for (j
= 0; j
< nobj
; j
++)
3254 ira_object_t obj
= ALLOCNO_OBJECT (a
, j
);
3255 n
+= OBJECT_NUM_CONFLICTS (obj
);
3256 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
3260 fprintf (ira_dump_file
, " regions=%d, blocks=%d, points=%d\n",
3261 current_loops
== NULL
? 1 : number_of_loops (),
3262 n_basic_blocks
, ira_max_point
);
3263 fprintf (ira_dump_file
,
3264 " allocnos=%d (big %d), copies=%d, conflicts=%d, ranges=%d\n",
3265 ira_allocnos_num
, nr_big
, ira_copies_num
, n
, nr
);
3270 /* Release the data created by function ira_build. */
3274 finish_loop_tree_nodes ();
3277 finish_cost_vectors ();
3278 ira_finish_allocno_live_ranges ();