1 /* Building internal representation for IRA.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "insn-config.h"
35 #include "diagnostic-core.h"
40 #include "sparseset.h"
42 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
44 static ira_copy_t
find_allocno_copy (ira_allocno_t
, ira_allocno_t
, rtx
,
45 ira_loop_tree_node_t
);
47 /* The root of the loop tree corresponding to the all function. */
48 ira_loop_tree_node_t ira_loop_tree_root
;
50 /* Height of the loop tree. */
51 int ira_loop_tree_height
;
53 /* All nodes representing basic blocks are referred through the
54 following array. We can not use basic block member `aux' for this
55 because it is used for insertion of insns on edges. */
56 ira_loop_tree_node_t ira_bb_nodes
;
58 /* All nodes representing loops are referred through the following
60 ira_loop_tree_node_t ira_loop_nodes
;
62 /* Map regno -> allocnos with given regno (see comments for
63 allocno member `next_regno_allocno'). */
64 ira_allocno_t
*ira_regno_allocno_map
;
66 /* Array of references to all allocnos. The order number of the
67 allocno corresponds to the index in the array. Removed allocnos
68 have NULL element value. */
69 ira_allocno_t
*ira_allocnos
;
71 /* Sizes of the previous array. */
74 /* Count of conflict record structures we've created, used when creating
78 /* Map a conflict id to its conflict record. */
79 ira_object_t
*ira_object_id_map
;
81 /* Array of references to all copies. The order number of the copy
82 corresponds to the index in the array. Removed copies have NULL
84 ira_copy_t
*ira_copies
;
86 /* Size of the previous array. */
91 /* LAST_BASIC_BLOCK before generating additional insns because of live
92 range splitting. Emitting insns on a critical edge creates a new
94 static int last_basic_block_before_change
;
96 /* The following function allocates the loop tree nodes. If LOOPS_P
97 is FALSE, the nodes corresponding to the loops (except the root
98 which corresponds the all function) will be not allocated but nodes
99 will still be allocated for basic blocks. */
101 create_loop_tree_nodes (bool loops_p
)
108 VEC (edge
, heap
) *edges
;
112 = ((struct ira_loop_tree_node
*)
113 ira_allocate (sizeof (struct ira_loop_tree_node
) * last_basic_block
));
114 last_basic_block_before_change
= last_basic_block
;
115 for (i
= 0; i
< (unsigned int) last_basic_block
; i
++)
117 ira_bb_nodes
[i
].regno_allocno_map
= NULL
;
118 memset (ira_bb_nodes
[i
].reg_pressure
, 0,
119 sizeof (ira_bb_nodes
[i
].reg_pressure
));
120 ira_bb_nodes
[i
].all_allocnos
= NULL
;
121 ira_bb_nodes
[i
].modified_regnos
= NULL
;
122 ira_bb_nodes
[i
].border_allocnos
= NULL
;
123 ira_bb_nodes
[i
].local_copies
= NULL
;
125 ira_loop_nodes
= ((struct ira_loop_tree_node
*)
126 ira_allocate (sizeof (struct ira_loop_tree_node
)
127 * VEC_length (loop_p
, ira_loops
.larray
)));
128 max_regno
= max_reg_num ();
129 FOR_EACH_VEC_ELT (loop_p
, ira_loops
.larray
, i
, loop
)
131 if (loop
!= ira_loops
.tree_root
)
133 ira_loop_nodes
[i
].regno_allocno_map
= NULL
;
137 FOR_EACH_EDGE (e
, ei
, loop
->header
->preds
)
138 if (e
->src
!= loop
->latch
139 && (e
->flags
& EDGE_ABNORMAL
) && EDGE_CRITICAL_P (e
))
146 edges
= get_loop_exit_edges (loop
);
147 FOR_EACH_VEC_ELT (edge
, edges
, j
, e
)
148 if ((e
->flags
& EDGE_ABNORMAL
) && EDGE_CRITICAL_P (e
))
153 VEC_free (edge
, heap
, edges
);
157 ira_loop_nodes
[i
].regno_allocno_map
158 = (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
) * max_regno
);
159 memset (ira_loop_nodes
[i
].regno_allocno_map
, 0,
160 sizeof (ira_allocno_t
) * max_regno
);
161 memset (ira_loop_nodes
[i
].reg_pressure
, 0,
162 sizeof (ira_loop_nodes
[i
].reg_pressure
));
163 ira_loop_nodes
[i
].all_allocnos
= ira_allocate_bitmap ();
164 ira_loop_nodes
[i
].modified_regnos
= ira_allocate_bitmap ();
165 ira_loop_nodes
[i
].border_allocnos
= ira_allocate_bitmap ();
166 ira_loop_nodes
[i
].local_copies
= ira_allocate_bitmap ();
170 /* The function returns TRUE if there are more one allocation
173 more_one_region_p (void)
178 FOR_EACH_VEC_ELT (loop_p
, ira_loops
.larray
, i
, loop
)
179 if (ira_loop_nodes
[i
].regno_allocno_map
!= NULL
180 && ira_loop_tree_root
!= &ira_loop_nodes
[i
])
185 /* Free the loop tree node of a loop. */
187 finish_loop_tree_node (ira_loop_tree_node_t loop
)
189 if (loop
->regno_allocno_map
!= NULL
)
191 ira_assert (loop
->bb
== NULL
);
192 ira_free_bitmap (loop
->local_copies
);
193 ira_free_bitmap (loop
->border_allocnos
);
194 ira_free_bitmap (loop
->modified_regnos
);
195 ira_free_bitmap (loop
->all_allocnos
);
196 ira_free (loop
->regno_allocno_map
);
197 loop
->regno_allocno_map
= NULL
;
201 /* Free the loop tree nodes. */
203 finish_loop_tree_nodes (void)
208 FOR_EACH_VEC_ELT (loop_p
, ira_loops
.larray
, i
, loop
)
209 finish_loop_tree_node (&ira_loop_nodes
[i
]);
210 ira_free (ira_loop_nodes
);
211 for (i
= 0; i
< (unsigned int) last_basic_block_before_change
; i
++)
213 if (ira_bb_nodes
[i
].local_copies
!= NULL
)
214 ira_free_bitmap (ira_bb_nodes
[i
].local_copies
);
215 if (ira_bb_nodes
[i
].border_allocnos
!= NULL
)
216 ira_free_bitmap (ira_bb_nodes
[i
].border_allocnos
);
217 if (ira_bb_nodes
[i
].modified_regnos
!= NULL
)
218 ira_free_bitmap (ira_bb_nodes
[i
].modified_regnos
);
219 if (ira_bb_nodes
[i
].all_allocnos
!= NULL
)
220 ira_free_bitmap (ira_bb_nodes
[i
].all_allocnos
);
221 if (ira_bb_nodes
[i
].regno_allocno_map
!= NULL
)
222 ira_free (ira_bb_nodes
[i
].regno_allocno_map
);
224 ira_free (ira_bb_nodes
);
229 /* The following recursive function adds LOOP to the loop tree
230 hierarchy. LOOP is added only once. */
232 add_loop_to_tree (struct loop
*loop
)
235 ira_loop_tree_node_t loop_node
, parent_node
;
237 /* We can not use loop node access macros here because of potential
238 checking and because the nodes are not initialized enough
240 if (loop_outer (loop
) != NULL
)
241 add_loop_to_tree (loop_outer (loop
));
242 if (ira_loop_nodes
[loop
->num
].regno_allocno_map
!= NULL
243 && ira_loop_nodes
[loop
->num
].children
== NULL
)
245 /* We have not added loop node to the tree yet. */
246 loop_node
= &ira_loop_nodes
[loop
->num
];
247 loop_node
->loop
= loop
;
248 loop_node
->bb
= NULL
;
249 for (parent
= loop_outer (loop
);
251 parent
= loop_outer (parent
))
252 if (ira_loop_nodes
[parent
->num
].regno_allocno_map
!= NULL
)
256 loop_node
->next
= NULL
;
257 loop_node
->subloop_next
= NULL
;
258 loop_node
->parent
= NULL
;
262 parent_node
= &ira_loop_nodes
[parent
->num
];
263 loop_node
->next
= parent_node
->children
;
264 parent_node
->children
= loop_node
;
265 loop_node
->subloop_next
= parent_node
->subloops
;
266 parent_node
->subloops
= loop_node
;
267 loop_node
->parent
= parent_node
;
272 /* The following recursive function sets up levels of nodes of the
273 tree given its root LOOP_NODE. The enumeration starts with LEVEL.
274 The function returns maximal value of level in the tree + 1. */
276 setup_loop_tree_level (ira_loop_tree_node_t loop_node
, int level
)
278 int height
, max_height
;
279 ira_loop_tree_node_t subloop_node
;
281 ira_assert (loop_node
->bb
== NULL
);
282 loop_node
->level
= level
;
283 max_height
= level
+ 1;
284 for (subloop_node
= loop_node
->subloops
;
285 subloop_node
!= NULL
;
286 subloop_node
= subloop_node
->subloop_next
)
288 ira_assert (subloop_node
->bb
== NULL
);
289 height
= setup_loop_tree_level (subloop_node
, level
+ 1);
290 if (height
> max_height
)
296 /* Create the loop tree. The algorithm is designed to provide correct
297 order of loops (they are ordered by their last loop BB) and basic
298 blocks in the chain formed by member next. */
300 form_loop_tree (void)
305 ira_loop_tree_node_t bb_node
, loop_node
;
308 /* We can not use loop/bb node access macros because of potential
309 checking and because the nodes are not initialized enough
311 FOR_EACH_VEC_ELT (loop_p
, ira_loops
.larray
, i
, loop
)
312 if (ira_loop_nodes
[i
].regno_allocno_map
!= NULL
)
314 ira_loop_nodes
[i
].children
= NULL
;
315 ira_loop_nodes
[i
].subloops
= NULL
;
319 bb_node
= &ira_bb_nodes
[bb
->index
];
321 bb_node
->loop
= NULL
;
322 bb_node
->subloops
= NULL
;
323 bb_node
->children
= NULL
;
324 bb_node
->subloop_next
= NULL
;
325 bb_node
->next
= NULL
;
326 for (parent
= bb
->loop_father
;
328 parent
= loop_outer (parent
))
329 if (ira_loop_nodes
[parent
->num
].regno_allocno_map
!= NULL
)
331 add_loop_to_tree (parent
);
332 loop_node
= &ira_loop_nodes
[parent
->num
];
333 bb_node
->next
= loop_node
->children
;
334 bb_node
->parent
= loop_node
;
335 loop_node
->children
= bb_node
;
337 ira_loop_tree_root
= IRA_LOOP_NODE_BY_INDEX (ira_loops
.tree_root
->num
);
338 ira_loop_tree_height
= setup_loop_tree_level (ira_loop_tree_root
, 0);
339 ira_assert (ira_loop_tree_root
->regno_allocno_map
!= NULL
);
344 /* Rebuild IRA_REGNO_ALLOCNO_MAP and REGNO_ALLOCNO_MAPs of the loop
347 rebuild_regno_allocno_maps (void)
350 int max_regno
, regno
;
352 ira_loop_tree_node_t loop_tree_node
;
354 ira_allocno_iterator ai
;
356 max_regno
= max_reg_num ();
357 FOR_EACH_VEC_ELT (loop_p
, ira_loops
.larray
, l
, loop
)
358 if (ira_loop_nodes
[l
].regno_allocno_map
!= NULL
)
360 ira_free (ira_loop_nodes
[l
].regno_allocno_map
);
361 ira_loop_nodes
[l
].regno_allocno_map
362 = (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
)
364 memset (ira_loop_nodes
[l
].regno_allocno_map
, 0,
365 sizeof (ira_allocno_t
) * max_regno
);
367 ira_free (ira_regno_allocno_map
);
368 ira_regno_allocno_map
369 = (ira_allocno_t
*) ira_allocate (max_regno
* sizeof (ira_allocno_t
));
370 memset (ira_regno_allocno_map
, 0, max_regno
* sizeof (ira_allocno_t
));
371 FOR_EACH_ALLOCNO (a
, ai
)
373 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
374 /* Caps are not in the regno allocno maps. */
376 regno
= ALLOCNO_REGNO (a
);
377 loop_tree_node
= ALLOCNO_LOOP_TREE_NODE (a
);
378 ALLOCNO_NEXT_REGNO_ALLOCNO (a
) = ira_regno_allocno_map
[regno
];
379 ira_regno_allocno_map
[regno
] = a
;
380 if (loop_tree_node
->regno_allocno_map
[regno
] == NULL
)
381 /* Remember that we can create temporary allocnos to break
382 cycles in register shuffle. */
383 loop_tree_node
->regno_allocno_map
[regno
] = a
;
388 /* Pools for allocnos, allocno live ranges and objects. */
389 static alloc_pool allocno_pool
, live_range_pool
, object_pool
;
391 /* Vec containing references to all created allocnos. It is a
392 container of array allocnos. */
393 static VEC(ira_allocno_t
,heap
) *allocno_vec
;
395 /* Vec containing references to all created ira_objects. It is a
396 container of ira_object_id_map. */
397 static VEC(ira_object_t
,heap
) *ira_object_id_map_vec
;
399 /* Initialize data concerning allocnos. */
401 initiate_allocnos (void)
404 = create_alloc_pool ("live ranges",
405 sizeof (struct live_range
), 100);
407 = create_alloc_pool ("allocnos", sizeof (struct ira_allocno
), 100);
409 = create_alloc_pool ("objects", sizeof (struct ira_object
), 100);
410 allocno_vec
= VEC_alloc (ira_allocno_t
, heap
, max_reg_num () * 2);
412 ira_allocnos_num
= 0;
414 ira_object_id_map_vec
415 = VEC_alloc (ira_object_t
, heap
, max_reg_num () * 2);
416 ira_object_id_map
= NULL
;
417 ira_regno_allocno_map
418 = (ira_allocno_t
*) ira_allocate (max_reg_num ()
419 * sizeof (ira_allocno_t
));
420 memset (ira_regno_allocno_map
, 0, max_reg_num () * sizeof (ira_allocno_t
));
423 /* Create and return an object corresponding to a new allocno A. */
425 ira_create_object (ira_allocno_t a
, int subword
)
427 enum reg_class aclass
= ALLOCNO_CLASS (a
);
428 ira_object_t obj
= (ira_object_t
) pool_alloc (object_pool
);
430 OBJECT_ALLOCNO (obj
) = a
;
431 OBJECT_SUBWORD (obj
) = subword
;
432 OBJECT_CONFLICT_ID (obj
) = ira_objects_num
;
433 OBJECT_CONFLICT_VEC_P (obj
) = false;
434 OBJECT_CONFLICT_ARRAY (obj
) = NULL
;
435 OBJECT_NUM_CONFLICTS (obj
) = 0;
436 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj
), ira_no_alloc_regs
);
437 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
), ira_no_alloc_regs
);
438 IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj
),
439 reg_class_contents
[aclass
]);
440 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
),
441 reg_class_contents
[aclass
]);
442 OBJECT_MIN (obj
) = INT_MAX
;
443 OBJECT_MAX (obj
) = -1;
444 OBJECT_LIVE_RANGES (obj
) = NULL
;
445 OBJECT_ADD_DATA (obj
) = NULL
;
447 VEC_safe_push (ira_object_t
, heap
, ira_object_id_map_vec
, obj
);
449 = VEC_address (ira_object_t
, ira_object_id_map_vec
);
450 ira_objects_num
= VEC_length (ira_object_t
, ira_object_id_map_vec
);
455 /* Create and return the allocno corresponding to REGNO in
456 LOOP_TREE_NODE. Add the allocno to the list of allocnos with the
457 same regno if CAP_P is FALSE. */
459 ira_create_allocno (int regno
, bool cap_p
,
460 ira_loop_tree_node_t loop_tree_node
)
464 a
= (ira_allocno_t
) pool_alloc (allocno_pool
);
465 ALLOCNO_REGNO (a
) = regno
;
466 ALLOCNO_LOOP_TREE_NODE (a
) = loop_tree_node
;
469 ALLOCNO_NEXT_REGNO_ALLOCNO (a
) = ira_regno_allocno_map
[regno
];
470 ira_regno_allocno_map
[regno
] = a
;
471 if (loop_tree_node
->regno_allocno_map
[regno
] == NULL
)
472 /* Remember that we can create temporary allocnos to break
473 cycles in register shuffle on region borders (see
475 loop_tree_node
->regno_allocno_map
[regno
] = a
;
477 ALLOCNO_CAP (a
) = NULL
;
478 ALLOCNO_CAP_MEMBER (a
) = NULL
;
479 ALLOCNO_NUM (a
) = ira_allocnos_num
;
480 bitmap_set_bit (loop_tree_node
->all_allocnos
, ALLOCNO_NUM (a
));
481 ALLOCNO_NREFS (a
) = 0;
482 ALLOCNO_FREQ (a
) = 0;
483 ALLOCNO_HARD_REGNO (a
) = -1;
484 ALLOCNO_CALL_FREQ (a
) = 0;
485 ALLOCNO_CALLS_CROSSED_NUM (a
) = 0;
487 ALLOCNO_NO_STACK_REG_P (a
) = false;
488 ALLOCNO_TOTAL_NO_STACK_REG_P (a
) = false;
490 ALLOCNO_DONT_REASSIGN_P (a
) = false;
491 ALLOCNO_BAD_SPILL_P (a
) = false;
492 ALLOCNO_ASSIGNED_P (a
) = false;
493 ALLOCNO_MODE (a
) = (regno
< 0 ? VOIDmode
: PSEUDO_REGNO_MODE (regno
));
494 ALLOCNO_COPIES (a
) = NULL
;
495 ALLOCNO_HARD_REG_COSTS (a
) = NULL
;
496 ALLOCNO_CONFLICT_HARD_REG_COSTS (a
) = NULL
;
497 ALLOCNO_UPDATED_HARD_REG_COSTS (a
) = NULL
;
498 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) = NULL
;
499 ALLOCNO_CLASS (a
) = NO_REGS
;
500 ALLOCNO_UPDATED_CLASS_COST (a
) = 0;
501 ALLOCNO_CLASS_COST (a
) = 0;
502 ALLOCNO_MEMORY_COST (a
) = 0;
503 ALLOCNO_UPDATED_MEMORY_COST (a
) = 0;
504 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
) = 0;
505 ALLOCNO_NUM_OBJECTS (a
) = 0;
507 ALLOCNO_ADD_DATA (a
) = NULL
;
508 VEC_safe_push (ira_allocno_t
, heap
, allocno_vec
, a
);
509 ira_allocnos
= VEC_address (ira_allocno_t
, allocno_vec
);
510 ira_allocnos_num
= VEC_length (ira_allocno_t
, allocno_vec
);
515 /* Set up register class for A and update its conflict hard
518 ira_set_allocno_class (ira_allocno_t a
, enum reg_class aclass
)
520 ira_allocno_object_iterator oi
;
523 ALLOCNO_CLASS (a
) = aclass
;
524 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
526 IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj
),
527 reg_class_contents
[aclass
]);
528 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
),
529 reg_class_contents
[aclass
]);
533 /* Determine the number of objects we should associate with allocno A
534 and allocate them. */
536 ira_create_allocno_objects (ira_allocno_t a
)
538 enum machine_mode mode
= ALLOCNO_MODE (a
);
539 enum reg_class aclass
= ALLOCNO_CLASS (a
);
540 int n
= ira_reg_class_max_nregs
[aclass
][mode
];
543 if (GET_MODE_SIZE (mode
) != 2 * UNITS_PER_WORD
|| n
!= 2)
546 ALLOCNO_NUM_OBJECTS (a
) = n
;
547 for (i
= 0; i
< n
; i
++)
548 ALLOCNO_OBJECT (a
, i
) = ira_create_object (a
, i
);
551 /* For each allocno, set ALLOCNO_NUM_OBJECTS and create the
552 ALLOCNO_OBJECT structures. This must be called after the allocno
553 classes are known. */
555 create_allocno_objects (void)
558 ira_allocno_iterator ai
;
560 FOR_EACH_ALLOCNO (a
, ai
)
561 ira_create_allocno_objects (a
);
564 /* Merge hard register conflict information for all objects associated with
565 allocno TO into the corresponding objects associated with FROM.
566 If TOTAL_ONLY is true, we only merge OBJECT_TOTAL_CONFLICT_HARD_REGS. */
568 merge_hard_reg_conflicts (ira_allocno_t from
, ira_allocno_t to
,
572 gcc_assert (ALLOCNO_NUM_OBJECTS (to
) == ALLOCNO_NUM_OBJECTS (from
));
573 for (i
= 0; i
< ALLOCNO_NUM_OBJECTS (to
); i
++)
575 ira_object_t from_obj
= ALLOCNO_OBJECT (from
, i
);
576 ira_object_t to_obj
= ALLOCNO_OBJECT (to
, i
);
579 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (to_obj
),
580 OBJECT_CONFLICT_HARD_REGS (from_obj
));
581 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (to_obj
),
582 OBJECT_TOTAL_CONFLICT_HARD_REGS (from_obj
));
585 if (!total_only
&& ALLOCNO_NO_STACK_REG_P (from
))
586 ALLOCNO_NO_STACK_REG_P (to
) = true;
587 if (ALLOCNO_TOTAL_NO_STACK_REG_P (from
))
588 ALLOCNO_TOTAL_NO_STACK_REG_P (to
) = true;
592 /* Update hard register conflict information for all objects associated with
593 A to include the regs in SET. */
595 ior_hard_reg_conflicts (ira_allocno_t a
, HARD_REG_SET
*set
)
597 ira_allocno_object_iterator i
;
600 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, i
)
602 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj
), *set
);
603 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
), *set
);
607 /* Return TRUE if a conflict vector with NUM elements is more
608 profitable than a conflict bit vector for OBJ. */
610 ira_conflict_vector_profitable_p (ira_object_t obj
, int num
)
613 int max
= OBJECT_MAX (obj
);
614 int min
= OBJECT_MIN (obj
);
617 /* We prefer a bit vector in such case because it does not result
621 nw
= (max
- min
+ IRA_INT_BITS
) / IRA_INT_BITS
;
622 return (2 * sizeof (ira_object_t
) * (num
+ 1)
623 < 3 * nw
* sizeof (IRA_INT_TYPE
));
626 /* Allocates and initialize the conflict vector of OBJ for NUM
627 conflicting objects. */
629 ira_allocate_conflict_vec (ira_object_t obj
, int num
)
634 ira_assert (OBJECT_CONFLICT_ARRAY (obj
) == NULL
);
635 num
++; /* for NULL end marker */
636 size
= sizeof (ira_object_t
) * num
;
637 OBJECT_CONFLICT_ARRAY (obj
) = ira_allocate (size
);
638 vec
= (ira_object_t
*) OBJECT_CONFLICT_ARRAY (obj
);
640 OBJECT_NUM_CONFLICTS (obj
) = 0;
641 OBJECT_CONFLICT_ARRAY_SIZE (obj
) = size
;
642 OBJECT_CONFLICT_VEC_P (obj
) = true;
645 /* Allocate and initialize the conflict bit vector of OBJ. */
647 allocate_conflict_bit_vec (ira_object_t obj
)
651 ira_assert (OBJECT_CONFLICT_ARRAY (obj
) == NULL
);
652 size
= ((OBJECT_MAX (obj
) - OBJECT_MIN (obj
) + IRA_INT_BITS
)
653 / IRA_INT_BITS
* sizeof (IRA_INT_TYPE
));
654 OBJECT_CONFLICT_ARRAY (obj
) = ira_allocate (size
);
655 memset (OBJECT_CONFLICT_ARRAY (obj
), 0, size
);
656 OBJECT_CONFLICT_ARRAY_SIZE (obj
) = size
;
657 OBJECT_CONFLICT_VEC_P (obj
) = false;
660 /* Allocate and initialize the conflict vector or conflict bit vector
661 of OBJ for NUM conflicting allocnos whatever is more profitable. */
663 ira_allocate_object_conflicts (ira_object_t obj
, int num
)
665 if (ira_conflict_vector_profitable_p (obj
, num
))
666 ira_allocate_conflict_vec (obj
, num
);
668 allocate_conflict_bit_vec (obj
);
671 /* Add OBJ2 to the conflicts of OBJ1. */
673 add_to_conflicts (ira_object_t obj1
, ira_object_t obj2
)
678 if (OBJECT_CONFLICT_VEC_P (obj1
))
680 ira_object_t
*vec
= OBJECT_CONFLICT_VEC (obj1
);
681 int curr_num
= OBJECT_NUM_CONFLICTS (obj1
);
683 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1
) < num
* sizeof (ira_object_t
))
685 ira_object_t
*newvec
;
686 size
= (3 * num
/ 2 + 1) * sizeof (ira_allocno_t
);
687 newvec
= (ira_object_t
*) ira_allocate (size
);
688 memcpy (newvec
, vec
, curr_num
* sizeof (ira_object_t
));
691 OBJECT_CONFLICT_ARRAY (obj1
) = vec
;
692 OBJECT_CONFLICT_ARRAY_SIZE (obj1
) = size
;
696 OBJECT_NUM_CONFLICTS (obj1
)++;
700 int nw
, added_head_nw
, id
;
701 IRA_INT_TYPE
*vec
= OBJECT_CONFLICT_BITVEC (obj1
);
703 id
= OBJECT_CONFLICT_ID (obj2
);
704 if (OBJECT_MIN (obj1
) > id
)
706 /* Expand head of the bit vector. */
707 added_head_nw
= (OBJECT_MIN (obj1
) - id
- 1) / IRA_INT_BITS
+ 1;
708 nw
= (OBJECT_MAX (obj1
) - OBJECT_MIN (obj1
)) / IRA_INT_BITS
+ 1;
709 size
= (nw
+ added_head_nw
) * sizeof (IRA_INT_TYPE
);
710 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1
) >= size
)
712 memmove ((char *) vec
+ added_head_nw
* sizeof (IRA_INT_TYPE
),
713 vec
, nw
* sizeof (IRA_INT_TYPE
));
714 memset (vec
, 0, added_head_nw
* sizeof (IRA_INT_TYPE
));
719 = (3 * (nw
+ added_head_nw
) / 2 + 1) * sizeof (IRA_INT_TYPE
);
720 vec
= (IRA_INT_TYPE
*) ira_allocate (size
);
721 memcpy ((char *) vec
+ added_head_nw
* sizeof (IRA_INT_TYPE
),
722 OBJECT_CONFLICT_ARRAY (obj1
), nw
* sizeof (IRA_INT_TYPE
));
723 memset (vec
, 0, added_head_nw
* sizeof (IRA_INT_TYPE
));
725 + (nw
+ added_head_nw
) * sizeof (IRA_INT_TYPE
),
726 0, size
- (nw
+ added_head_nw
) * sizeof (IRA_INT_TYPE
));
727 ira_free (OBJECT_CONFLICT_ARRAY (obj1
));
728 OBJECT_CONFLICT_ARRAY (obj1
) = vec
;
729 OBJECT_CONFLICT_ARRAY_SIZE (obj1
) = size
;
731 OBJECT_MIN (obj1
) -= added_head_nw
* IRA_INT_BITS
;
733 else if (OBJECT_MAX (obj1
) < id
)
735 nw
= (id
- OBJECT_MIN (obj1
)) / IRA_INT_BITS
+ 1;
736 size
= nw
* sizeof (IRA_INT_TYPE
);
737 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1
) < size
)
739 /* Expand tail of the bit vector. */
740 size
= (3 * nw
/ 2 + 1) * sizeof (IRA_INT_TYPE
);
741 vec
= (IRA_INT_TYPE
*) ira_allocate (size
);
742 memcpy (vec
, OBJECT_CONFLICT_ARRAY (obj1
), OBJECT_CONFLICT_ARRAY_SIZE (obj1
));
743 memset ((char *) vec
+ OBJECT_CONFLICT_ARRAY_SIZE (obj1
),
744 0, size
- OBJECT_CONFLICT_ARRAY_SIZE (obj1
));
745 ira_free (OBJECT_CONFLICT_ARRAY (obj1
));
746 OBJECT_CONFLICT_ARRAY (obj1
) = vec
;
747 OBJECT_CONFLICT_ARRAY_SIZE (obj1
) = size
;
749 OBJECT_MAX (obj1
) = id
;
751 SET_MINMAX_SET_BIT (vec
, id
, OBJECT_MIN (obj1
), OBJECT_MAX (obj1
));
755 /* Add OBJ1 to the conflicts of OBJ2 and vice versa. */
757 ira_add_conflict (ira_object_t obj1
, ira_object_t obj2
)
759 add_to_conflicts (obj1
, obj2
);
760 add_to_conflicts (obj2
, obj1
);
763 /* Clear all conflicts of OBJ. */
765 clear_conflicts (ira_object_t obj
)
767 if (OBJECT_CONFLICT_VEC_P (obj
))
769 OBJECT_NUM_CONFLICTS (obj
) = 0;
770 OBJECT_CONFLICT_VEC (obj
)[0] = NULL
;
772 else if (OBJECT_CONFLICT_ARRAY_SIZE (obj
) != 0)
776 nw
= (OBJECT_MAX (obj
) - OBJECT_MIN (obj
)) / IRA_INT_BITS
+ 1;
777 memset (OBJECT_CONFLICT_BITVEC (obj
), 0, nw
* sizeof (IRA_INT_TYPE
));
781 /* The array used to find duplications in conflict vectors of
783 static int *conflict_check
;
785 /* The value used to mark allocation presence in conflict vector of
786 the current allocno. */
787 static int curr_conflict_check_tick
;
789 /* Remove duplications in conflict vector of OBJ. */
791 compress_conflict_vec (ira_object_t obj
)
793 ira_object_t
*vec
, conflict_obj
;
796 ira_assert (OBJECT_CONFLICT_VEC_P (obj
));
797 vec
= OBJECT_CONFLICT_VEC (obj
);
798 curr_conflict_check_tick
++;
799 for (i
= j
= 0; (conflict_obj
= vec
[i
]) != NULL
; i
++)
801 int id
= OBJECT_CONFLICT_ID (conflict_obj
);
802 if (conflict_check
[id
] != curr_conflict_check_tick
)
804 conflict_check
[id
] = curr_conflict_check_tick
;
805 vec
[j
++] = conflict_obj
;
808 OBJECT_NUM_CONFLICTS (obj
) = j
;
812 /* Remove duplications in conflict vectors of all allocnos. */
814 compress_conflict_vecs (void)
817 ira_object_iterator oi
;
819 conflict_check
= (int *) ira_allocate (sizeof (int) * ira_objects_num
);
820 memset (conflict_check
, 0, sizeof (int) * ira_objects_num
);
821 curr_conflict_check_tick
= 0;
822 FOR_EACH_OBJECT (obj
, oi
)
824 if (OBJECT_CONFLICT_VEC_P (obj
))
825 compress_conflict_vec (obj
);
827 ira_free (conflict_check
);
830 /* This recursive function outputs allocno A and if it is a cap the
831 function outputs its members. */
833 ira_print_expanded_allocno (ira_allocno_t a
)
837 fprintf (ira_dump_file
, " a%d(r%d", ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
));
838 if ((bb
= ALLOCNO_LOOP_TREE_NODE (a
)->bb
) != NULL
)
839 fprintf (ira_dump_file
, ",b%d", bb
->index
);
841 fprintf (ira_dump_file
, ",l%d", ALLOCNO_LOOP_TREE_NODE (a
)->loop
->num
);
842 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
844 fprintf (ira_dump_file
, ":");
845 ira_print_expanded_allocno (ALLOCNO_CAP_MEMBER (a
));
847 fprintf (ira_dump_file
, ")");
850 /* Create and return the cap representing allocno A in the
853 create_cap_allocno (ira_allocno_t a
)
856 ira_loop_tree_node_t parent
;
857 enum reg_class aclass
;
859 parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
;
860 cap
= ira_create_allocno (ALLOCNO_REGNO (a
), true, parent
);
861 ALLOCNO_MODE (cap
) = ALLOCNO_MODE (a
);
862 aclass
= ALLOCNO_CLASS (a
);
863 ira_set_allocno_class (cap
, aclass
);
864 ira_create_allocno_objects (cap
);
865 ALLOCNO_CAP_MEMBER (cap
) = a
;
866 ALLOCNO_CAP (a
) = cap
;
867 ALLOCNO_CLASS_COST (cap
) = ALLOCNO_CLASS_COST (a
);
868 ALLOCNO_MEMORY_COST (cap
) = ALLOCNO_MEMORY_COST (a
);
869 ira_allocate_and_copy_costs
870 (&ALLOCNO_HARD_REG_COSTS (cap
), aclass
, ALLOCNO_HARD_REG_COSTS (a
));
871 ira_allocate_and_copy_costs
872 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (cap
), aclass
,
873 ALLOCNO_CONFLICT_HARD_REG_COSTS (a
));
874 ALLOCNO_BAD_SPILL_P (cap
) = ALLOCNO_BAD_SPILL_P (a
);
875 ALLOCNO_NREFS (cap
) = ALLOCNO_NREFS (a
);
876 ALLOCNO_FREQ (cap
) = ALLOCNO_FREQ (a
);
877 ALLOCNO_CALL_FREQ (cap
) = ALLOCNO_CALL_FREQ (a
);
879 merge_hard_reg_conflicts (a
, cap
, false);
881 ALLOCNO_CALLS_CROSSED_NUM (cap
) = ALLOCNO_CALLS_CROSSED_NUM (a
);
882 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
884 fprintf (ira_dump_file
, " Creating cap ");
885 ira_print_expanded_allocno (cap
);
886 fprintf (ira_dump_file
, "\n");
891 /* Create and return a live range for OBJECT with given attributes. */
893 ira_create_live_range (ira_object_t obj
, int start
, int finish
,
898 p
= (live_range_t
) pool_alloc (live_range_pool
);
906 /* Create a new live range for OBJECT and queue it at the head of its
909 ira_add_live_range_to_object (ira_object_t object
, int start
, int finish
)
912 p
= ira_create_live_range (object
, start
, finish
,
913 OBJECT_LIVE_RANGES (object
));
914 OBJECT_LIVE_RANGES (object
) = p
;
917 /* Copy allocno live range R and return the result. */
919 copy_live_range (live_range_t r
)
923 p
= (live_range_t
) pool_alloc (live_range_pool
);
928 /* Copy allocno live range list given by its head R and return the
931 ira_copy_live_range_list (live_range_t r
)
933 live_range_t p
, first
, last
;
937 for (first
= last
= NULL
; r
!= NULL
; r
= r
->next
)
939 p
= copy_live_range (r
);
949 /* Merge ranges R1 and R2 and returns the result. The function
950 maintains the order of ranges and tries to minimize number of the
953 ira_merge_live_ranges (live_range_t r1
, live_range_t r2
)
955 live_range_t first
, last
, temp
;
961 for (first
= last
= NULL
; r1
!= NULL
&& r2
!= NULL
;)
963 if (r1
->start
< r2
->start
)
969 if (r1
->start
<= r2
->finish
+ 1)
971 /* Intersected ranges: merge r1 and r2 into r1. */
972 r1
->start
= r2
->start
;
973 if (r1
->finish
< r2
->finish
)
974 r1
->finish
= r2
->finish
;
977 ira_finish_live_range (temp
);
980 /* To try to merge with subsequent ranges in r1. */
987 /* Add r1 to the result. */
998 /* To try to merge with subsequent ranges in r2. */
1010 ira_assert (r1
->next
== NULL
);
1012 else if (r2
!= NULL
)
1018 ira_assert (r2
->next
== NULL
);
1022 ira_assert (last
->next
== NULL
);
1027 /* Return TRUE if live ranges R1 and R2 intersect. */
1029 ira_live_ranges_intersect_p (live_range_t r1
, live_range_t r2
)
1031 /* Remember the live ranges are always kept ordered. */
1032 while (r1
!= NULL
&& r2
!= NULL
)
1034 if (r1
->start
> r2
->finish
)
1036 else if (r2
->start
> r1
->finish
)
1044 /* Free allocno live range R. */
1046 ira_finish_live_range (live_range_t r
)
1048 pool_free (live_range_pool
, r
);
1051 /* Free list of allocno live ranges starting with R. */
1053 ira_finish_live_range_list (live_range_t r
)
1055 live_range_t next_r
;
1057 for (; r
!= NULL
; r
= next_r
)
1060 ira_finish_live_range (r
);
1064 /* Free updated register costs of allocno A. */
1066 ira_free_allocno_updated_costs (ira_allocno_t a
)
1068 enum reg_class aclass
;
1070 aclass
= ALLOCNO_CLASS (a
);
1071 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) != NULL
)
1072 ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a
), aclass
);
1073 ALLOCNO_UPDATED_HARD_REG_COSTS (a
) = NULL
;
1074 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) != NULL
)
1075 ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
),
1077 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) = NULL
;
1080 /* Free and nullify all cost vectors allocated earlier for allocno
1083 ira_free_allocno_costs (ira_allocno_t a
)
1085 enum reg_class aclass
= ALLOCNO_CLASS (a
);
1087 ira_allocno_object_iterator oi
;
1089 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
1091 ira_finish_live_range_list (OBJECT_LIVE_RANGES (obj
));
1092 ira_object_id_map
[OBJECT_CONFLICT_ID (obj
)] = NULL
;
1093 if (OBJECT_CONFLICT_ARRAY (obj
) != NULL
)
1094 ira_free (OBJECT_CONFLICT_ARRAY (obj
));
1095 pool_free (object_pool
, obj
);
1098 ira_allocnos
[ALLOCNO_NUM (a
)] = NULL
;
1099 if (ALLOCNO_HARD_REG_COSTS (a
) != NULL
)
1100 ira_free_cost_vector (ALLOCNO_HARD_REG_COSTS (a
), aclass
);
1101 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a
) != NULL
)
1102 ira_free_cost_vector (ALLOCNO_CONFLICT_HARD_REG_COSTS (a
), aclass
);
1103 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) != NULL
)
1104 ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a
), aclass
);
1105 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) != NULL
)
1106 ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
),
1108 ALLOCNO_HARD_REG_COSTS (a
) = NULL
;
1109 ALLOCNO_CONFLICT_HARD_REG_COSTS (a
) = NULL
;
1110 ALLOCNO_UPDATED_HARD_REG_COSTS (a
) = NULL
;
1111 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) = NULL
;
1114 /* Free the memory allocated for allocno A. */
1116 finish_allocno (ira_allocno_t a
)
1118 ira_free_allocno_costs (a
);
1119 pool_free (allocno_pool
, a
);
1122 /* Free the memory allocated for all allocnos. */
1124 finish_allocnos (void)
1127 ira_allocno_iterator ai
;
1129 FOR_EACH_ALLOCNO (a
, ai
)
1131 ira_free (ira_regno_allocno_map
);
1132 VEC_free (ira_object_t
, heap
, ira_object_id_map_vec
);
1133 VEC_free (ira_allocno_t
, heap
, allocno_vec
);
1134 free_alloc_pool (allocno_pool
);
1135 free_alloc_pool (object_pool
);
1136 free_alloc_pool (live_range_pool
);
1141 /* Pools for copies. */
1142 static alloc_pool copy_pool
;
1144 /* Vec containing references to all created copies. It is a
1145 container of array ira_copies. */
1146 static VEC(ira_copy_t
,heap
) *copy_vec
;
1148 /* The function initializes data concerning allocno copies. */
1150 initiate_copies (void)
1153 = create_alloc_pool ("copies", sizeof (struct ira_allocno_copy
), 100);
1154 copy_vec
= VEC_alloc (ira_copy_t
, heap
, get_max_uid ());
1159 /* Return copy connecting A1 and A2 and originated from INSN of
1160 LOOP_TREE_NODE if any. */
1162 find_allocno_copy (ira_allocno_t a1
, ira_allocno_t a2
, rtx insn
,
1163 ira_loop_tree_node_t loop_tree_node
)
1165 ira_copy_t cp
, next_cp
;
1166 ira_allocno_t another_a
;
1168 for (cp
= ALLOCNO_COPIES (a1
); cp
!= NULL
; cp
= next_cp
)
1170 if (cp
->first
== a1
)
1172 next_cp
= cp
->next_first_allocno_copy
;
1173 another_a
= cp
->second
;
1175 else if (cp
->second
== a1
)
1177 next_cp
= cp
->next_second_allocno_copy
;
1178 another_a
= cp
->first
;
1182 if (another_a
== a2
&& cp
->insn
== insn
1183 && cp
->loop_tree_node
== loop_tree_node
)
1189 /* Create and return copy with given attributes LOOP_TREE_NODE, FIRST,
1190 SECOND, FREQ, CONSTRAINT_P, and INSN. */
1192 ira_create_copy (ira_allocno_t first
, ira_allocno_t second
, int freq
,
1193 bool constraint_p
, rtx insn
,
1194 ira_loop_tree_node_t loop_tree_node
)
1198 cp
= (ira_copy_t
) pool_alloc (copy_pool
);
1199 cp
->num
= ira_copies_num
;
1201 cp
->second
= second
;
1203 cp
->constraint_p
= constraint_p
;
1205 cp
->loop_tree_node
= loop_tree_node
;
1206 VEC_safe_push (ira_copy_t
, heap
, copy_vec
, cp
);
1207 ira_copies
= VEC_address (ira_copy_t
, copy_vec
);
1208 ira_copies_num
= VEC_length (ira_copy_t
, copy_vec
);
1212 /* Attach a copy CP to allocnos involved into the copy. */
1214 ira_add_allocno_copy_to_list (ira_copy_t cp
)
1216 ira_allocno_t first
= cp
->first
, second
= cp
->second
;
1218 cp
->prev_first_allocno_copy
= NULL
;
1219 cp
->prev_second_allocno_copy
= NULL
;
1220 cp
->next_first_allocno_copy
= ALLOCNO_COPIES (first
);
1221 if (cp
->next_first_allocno_copy
!= NULL
)
1223 if (cp
->next_first_allocno_copy
->first
== first
)
1224 cp
->next_first_allocno_copy
->prev_first_allocno_copy
= cp
;
1226 cp
->next_first_allocno_copy
->prev_second_allocno_copy
= cp
;
1228 cp
->next_second_allocno_copy
= ALLOCNO_COPIES (second
);
1229 if (cp
->next_second_allocno_copy
!= NULL
)
1231 if (cp
->next_second_allocno_copy
->second
== second
)
1232 cp
->next_second_allocno_copy
->prev_second_allocno_copy
= cp
;
1234 cp
->next_second_allocno_copy
->prev_first_allocno_copy
= cp
;
1236 ALLOCNO_COPIES (first
) = cp
;
1237 ALLOCNO_COPIES (second
) = cp
;
1240 /* Make a copy CP a canonical copy where number of the
1241 first allocno is less than the second one. */
1243 ira_swap_allocno_copy_ends_if_necessary (ira_copy_t cp
)
1248 if (ALLOCNO_NUM (cp
->first
) <= ALLOCNO_NUM (cp
->second
))
1252 cp
->first
= cp
->second
;
1255 temp_cp
= cp
->prev_first_allocno_copy
;
1256 cp
->prev_first_allocno_copy
= cp
->prev_second_allocno_copy
;
1257 cp
->prev_second_allocno_copy
= temp_cp
;
1259 temp_cp
= cp
->next_first_allocno_copy
;
1260 cp
->next_first_allocno_copy
= cp
->next_second_allocno_copy
;
1261 cp
->next_second_allocno_copy
= temp_cp
;
1264 /* Create (or update frequency if the copy already exists) and return
1265 the copy of allocnos FIRST and SECOND with frequency FREQ
1266 corresponding to move insn INSN (if any) and originated from
1269 ira_add_allocno_copy (ira_allocno_t first
, ira_allocno_t second
, int freq
,
1270 bool constraint_p
, rtx insn
,
1271 ira_loop_tree_node_t loop_tree_node
)
1275 if ((cp
= find_allocno_copy (first
, second
, insn
, loop_tree_node
)) != NULL
)
1280 cp
= ira_create_copy (first
, second
, freq
, constraint_p
, insn
,
1282 ira_assert (first
!= NULL
&& second
!= NULL
);
1283 ira_add_allocno_copy_to_list (cp
);
1284 ira_swap_allocno_copy_ends_if_necessary (cp
);
1288 /* Print info about copy CP into file F. */
1290 print_copy (FILE *f
, ira_copy_t cp
)
1292 fprintf (f
, " cp%d:a%d(r%d)<->a%d(r%d)@%d:%s\n", cp
->num
,
1293 ALLOCNO_NUM (cp
->first
), ALLOCNO_REGNO (cp
->first
),
1294 ALLOCNO_NUM (cp
->second
), ALLOCNO_REGNO (cp
->second
), cp
->freq
,
1296 ? "move" : cp
->constraint_p
? "constraint" : "shuffle");
1299 /* Print info about copy CP into stderr. */
1301 ira_debug_copy (ira_copy_t cp
)
1303 print_copy (stderr
, cp
);
1306 /* Print info about all copies into file F. */
1308 print_copies (FILE *f
)
1311 ira_copy_iterator ci
;
1313 FOR_EACH_COPY (cp
, ci
)
1317 /* Print info about all copies into stderr. */
1319 ira_debug_copies (void)
1321 print_copies (stderr
);
1324 /* Print info about copies involving allocno A into file F. */
1326 print_allocno_copies (FILE *f
, ira_allocno_t a
)
1328 ira_allocno_t another_a
;
1329 ira_copy_t cp
, next_cp
;
1331 fprintf (f
, " a%d(r%d):", ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
));
1332 for (cp
= ALLOCNO_COPIES (a
); cp
!= NULL
; cp
= next_cp
)
1336 next_cp
= cp
->next_first_allocno_copy
;
1337 another_a
= cp
->second
;
1339 else if (cp
->second
== a
)
1341 next_cp
= cp
->next_second_allocno_copy
;
1342 another_a
= cp
->first
;
1346 fprintf (f
, " cp%d:a%d(r%d)@%d", cp
->num
,
1347 ALLOCNO_NUM (another_a
), ALLOCNO_REGNO (another_a
), cp
->freq
);
1352 /* Print info about copies involving allocno A into stderr. */
1354 ira_debug_allocno_copies (ira_allocno_t a
)
1356 print_allocno_copies (stderr
, a
);
1359 /* The function frees memory allocated for copy CP. */
1361 finish_copy (ira_copy_t cp
)
1363 pool_free (copy_pool
, cp
);
1367 /* Free memory allocated for all copies. */
1369 finish_copies (void)
1372 ira_copy_iterator ci
;
1374 FOR_EACH_COPY (cp
, ci
)
1376 VEC_free (ira_copy_t
, heap
, copy_vec
);
1377 free_alloc_pool (copy_pool
);
1382 /* Pools for cost vectors. It is defined only for allocno classes. */
1383 static alloc_pool cost_vector_pool
[N_REG_CLASSES
];
1385 /* The function initiates work with hard register cost vectors. It
1386 creates allocation pool for each allocno class. */
1388 initiate_cost_vectors (void)
1391 enum reg_class aclass
;
1393 for (i
= 0; i
< ira_allocno_classes_num
; i
++)
1395 aclass
= ira_allocno_classes
[i
];
1396 cost_vector_pool
[aclass
]
1397 = create_alloc_pool ("cost vectors",
1398 sizeof (int) * ira_class_hard_regs_num
[aclass
],
1403 /* Allocate and return a cost vector VEC for ACLASS. */
1405 ira_allocate_cost_vector (reg_class_t aclass
)
1407 return (int *) pool_alloc (cost_vector_pool
[(int) aclass
]);
1410 /* Free a cost vector VEC for ACLASS. */
1412 ira_free_cost_vector (int *vec
, reg_class_t aclass
)
1414 ira_assert (vec
!= NULL
);
1415 pool_free (cost_vector_pool
[(int) aclass
], vec
);
1418 /* Finish work with hard register cost vectors. Release allocation
1419 pool for each allocno class. */
1421 finish_cost_vectors (void)
1424 enum reg_class aclass
;
1426 for (i
= 0; i
< ira_allocno_classes_num
; i
++)
1428 aclass
= ira_allocno_classes
[i
];
1429 free_alloc_pool (cost_vector_pool
[aclass
]);
1435 /* The current loop tree node and its regno allocno map. */
1436 ira_loop_tree_node_t ira_curr_loop_tree_node
;
1437 ira_allocno_t
*ira_curr_regno_allocno_map
;
1439 /* This recursive function traverses loop tree with root LOOP_NODE
1440 calling non-null functions PREORDER_FUNC and POSTORDER_FUNC
1441 correspondingly in preorder and postorder. The function sets up
1442 IRA_CURR_LOOP_TREE_NODE and IRA_CURR_REGNO_ALLOCNO_MAP. If BB_P,
1443 basic block nodes of LOOP_NODE is also processed (before its
1446 ira_traverse_loop_tree (bool bb_p
, ira_loop_tree_node_t loop_node
,
1447 void (*preorder_func
) (ira_loop_tree_node_t
),
1448 void (*postorder_func
) (ira_loop_tree_node_t
))
1450 ira_loop_tree_node_t subloop_node
;
1452 ira_assert (loop_node
->bb
== NULL
);
1453 ira_curr_loop_tree_node
= loop_node
;
1454 ira_curr_regno_allocno_map
= ira_curr_loop_tree_node
->regno_allocno_map
;
1456 if (preorder_func
!= NULL
)
1457 (*preorder_func
) (loop_node
);
1460 for (subloop_node
= loop_node
->children
;
1461 subloop_node
!= NULL
;
1462 subloop_node
= subloop_node
->next
)
1463 if (subloop_node
->bb
!= NULL
)
1465 if (preorder_func
!= NULL
)
1466 (*preorder_func
) (subloop_node
);
1468 if (postorder_func
!= NULL
)
1469 (*postorder_func
) (subloop_node
);
1472 for (subloop_node
= loop_node
->subloops
;
1473 subloop_node
!= NULL
;
1474 subloop_node
= subloop_node
->subloop_next
)
1476 ira_assert (subloop_node
->bb
== NULL
);
1477 ira_traverse_loop_tree (bb_p
, subloop_node
,
1478 preorder_func
, postorder_func
);
1481 ira_curr_loop_tree_node
= loop_node
;
1482 ira_curr_regno_allocno_map
= ira_curr_loop_tree_node
->regno_allocno_map
;
1484 if (postorder_func
!= NULL
)
1485 (*postorder_func
) (loop_node
);
1490 /* The basic block currently being processed. */
1491 static basic_block curr_bb
;
1493 /* This recursive function creates allocnos corresponding to
1494 pseudo-registers containing in X. True OUTPUT_P means that X is
1497 create_insn_allocnos (rtx x
, bool output_p
)
1501 enum rtx_code code
= GET_CODE (x
);
1507 if ((regno
= REGNO (x
)) >= FIRST_PSEUDO_REGISTER
)
1511 if ((a
= ira_curr_regno_allocno_map
[regno
]) == NULL
)
1512 a
= ira_create_allocno (regno
, false, ira_curr_loop_tree_node
);
1514 ALLOCNO_NREFS (a
)++;
1515 ALLOCNO_FREQ (a
) += REG_FREQ_FROM_BB (curr_bb
);
1517 bitmap_set_bit (ira_curr_loop_tree_node
->modified_regnos
, regno
);
1521 else if (code
== SET
)
1523 create_insn_allocnos (SET_DEST (x
), true);
1524 create_insn_allocnos (SET_SRC (x
), false);
1527 else if (code
== CLOBBER
)
1529 create_insn_allocnos (XEXP (x
, 0), true);
1532 else if (code
== MEM
)
1534 create_insn_allocnos (XEXP (x
, 0), false);
1537 else if (code
== PRE_DEC
|| code
== POST_DEC
|| code
== PRE_INC
||
1538 code
== POST_INC
|| code
== POST_MODIFY
|| code
== PRE_MODIFY
)
1540 create_insn_allocnos (XEXP (x
, 0), true);
1541 create_insn_allocnos (XEXP (x
, 0), false);
1545 fmt
= GET_RTX_FORMAT (code
);
1546 for (i
= GET_RTX_LENGTH (code
) - 1; i
>= 0; i
--)
1549 create_insn_allocnos (XEXP (x
, i
), output_p
);
1550 else if (fmt
[i
] == 'E')
1551 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
1552 create_insn_allocnos (XVECEXP (x
, i
, j
), output_p
);
1556 /* Create allocnos corresponding to pseudo-registers living in the
1557 basic block represented by the corresponding loop tree node
1560 create_bb_allocnos (ira_loop_tree_node_t bb_node
)
1567 curr_bb
= bb
= bb_node
->bb
;
1568 ira_assert (bb
!= NULL
);
1569 FOR_BB_INSNS_REVERSE (bb
, insn
)
1570 if (NONDEBUG_INSN_P (insn
))
1571 create_insn_allocnos (PATTERN (insn
), false);
1572 /* It might be a allocno living through from one subloop to
1574 EXECUTE_IF_SET_IN_REG_SET (DF_LR_IN (bb
), FIRST_PSEUDO_REGISTER
, i
, bi
)
1575 if (ira_curr_regno_allocno_map
[i
] == NULL
)
1576 ira_create_allocno (i
, false, ira_curr_loop_tree_node
);
1579 /* Create allocnos corresponding to pseudo-registers living on edge E
1580 (a loop entry or exit). Also mark the allocnos as living on the
1583 create_loop_allocnos (edge e
)
1586 bitmap live_in_regs
, border_allocnos
;
1588 ira_loop_tree_node_t parent
;
1590 live_in_regs
= DF_LR_IN (e
->dest
);
1591 border_allocnos
= ira_curr_loop_tree_node
->border_allocnos
;
1592 EXECUTE_IF_SET_IN_REG_SET (DF_LR_OUT (e
->src
),
1593 FIRST_PSEUDO_REGISTER
, i
, bi
)
1594 if (bitmap_bit_p (live_in_regs
, i
))
1596 if (ira_curr_regno_allocno_map
[i
] == NULL
)
1598 /* The order of creations is important for right
1599 ira_regno_allocno_map. */
1600 if ((parent
= ira_curr_loop_tree_node
->parent
) != NULL
1601 && parent
->regno_allocno_map
[i
] == NULL
)
1602 ira_create_allocno (i
, false, parent
);
1603 ira_create_allocno (i
, false, ira_curr_loop_tree_node
);
1605 bitmap_set_bit (border_allocnos
,
1606 ALLOCNO_NUM (ira_curr_regno_allocno_map
[i
]));
1610 /* Create allocnos corresponding to pseudo-registers living in loop
1611 represented by the corresponding loop tree node LOOP_NODE. This
1612 function is called by ira_traverse_loop_tree. */
1614 create_loop_tree_node_allocnos (ira_loop_tree_node_t loop_node
)
1616 if (loop_node
->bb
!= NULL
)
1617 create_bb_allocnos (loop_node
);
1618 else if (loop_node
!= ira_loop_tree_root
)
1623 VEC (edge
, heap
) *edges
;
1625 FOR_EACH_EDGE (e
, ei
, loop_node
->loop
->header
->preds
)
1626 if (e
->src
!= loop_node
->loop
->latch
)
1627 create_loop_allocnos (e
);
1629 edges
= get_loop_exit_edges (loop_node
->loop
);
1630 FOR_EACH_VEC_ELT (edge
, edges
, i
, e
)
1631 create_loop_allocnos (e
);
1632 VEC_free (edge
, heap
, edges
);
1636 /* Propagate information about allocnos modified inside the loop given
1637 by its LOOP_TREE_NODE to its parent. */
1639 propagate_modified_regnos (ira_loop_tree_node_t loop_tree_node
)
1641 if (loop_tree_node
== ira_loop_tree_root
)
1643 ira_assert (loop_tree_node
->bb
== NULL
);
1644 bitmap_ior_into (loop_tree_node
->parent
->modified_regnos
,
1645 loop_tree_node
->modified_regnos
);
1648 /* Propagate new info about allocno A (see comments about accumulated
1649 info in allocno definition) to the corresponding allocno on upper
1650 loop tree level. So allocnos on upper levels accumulate
1651 information about the corresponding allocnos in nested regions.
1652 The new info means allocno info finally calculated in this
1655 propagate_allocno_info (void)
1658 ira_allocno_t a
, parent_a
;
1659 ira_loop_tree_node_t parent
;
1660 enum reg_class aclass
;
1662 if (flag_ira_region
!= IRA_REGION_ALL
1663 && flag_ira_region
!= IRA_REGION_MIXED
)
1665 for (i
= max_reg_num () - 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
1666 for (a
= ira_regno_allocno_map
[i
];
1668 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
1669 if ((parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
) != NULL
1670 && (parent_a
= parent
->regno_allocno_map
[i
]) != NULL
1671 /* There are no caps yet at this point. So use
1672 border_allocnos to find allocnos for the propagation. */
1673 && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE (a
)->border_allocnos
,
1676 if (! ALLOCNO_BAD_SPILL_P (a
))
1677 ALLOCNO_BAD_SPILL_P (parent_a
) = false;
1678 ALLOCNO_NREFS (parent_a
) += ALLOCNO_NREFS (a
);
1679 ALLOCNO_FREQ (parent_a
) += ALLOCNO_FREQ (a
);
1680 ALLOCNO_CALL_FREQ (parent_a
) += ALLOCNO_CALL_FREQ (a
);
1681 merge_hard_reg_conflicts (a
, parent_a
, true);
1682 ALLOCNO_CALLS_CROSSED_NUM (parent_a
)
1683 += ALLOCNO_CALLS_CROSSED_NUM (a
);
1684 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a
)
1685 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
);
1686 aclass
= ALLOCNO_CLASS (a
);
1687 ira_assert (aclass
== ALLOCNO_CLASS (parent_a
));
1688 ira_allocate_and_accumulate_costs
1689 (&ALLOCNO_HARD_REG_COSTS (parent_a
), aclass
,
1690 ALLOCNO_HARD_REG_COSTS (a
));
1691 ira_allocate_and_accumulate_costs
1692 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a
),
1694 ALLOCNO_CONFLICT_HARD_REG_COSTS (a
));
1695 ALLOCNO_CLASS_COST (parent_a
)
1696 += ALLOCNO_CLASS_COST (a
);
1697 ALLOCNO_MEMORY_COST (parent_a
) += ALLOCNO_MEMORY_COST (a
);
1701 /* Create allocnos corresponding to pseudo-registers in the current
1702 function. Traverse the loop tree for this. */
1704 create_allocnos (void)
1706 /* We need to process BB first to correctly link allocnos by member
1707 next_regno_allocno. */
1708 ira_traverse_loop_tree (true, ira_loop_tree_root
,
1709 create_loop_tree_node_allocnos
, NULL
);
1711 ira_traverse_loop_tree (false, ira_loop_tree_root
, NULL
,
1712 propagate_modified_regnos
);
1717 /* The page contains function to remove some regions from a separate
1718 register allocation. We remove regions whose separate allocation
1719 will hardly improve the result. As a result we speed up regional
1720 register allocation. */
1722 /* The function changes the object in range list given by R to OBJ. */
1724 change_object_in_range_list (live_range_t r
, ira_object_t obj
)
1726 for (; r
!= NULL
; r
= r
->next
)
1730 /* Move all live ranges associated with allocno FROM to allocno TO. */
1732 move_allocno_live_ranges (ira_allocno_t from
, ira_allocno_t to
)
1735 int n
= ALLOCNO_NUM_OBJECTS (from
);
1737 gcc_assert (n
== ALLOCNO_NUM_OBJECTS (to
));
1739 for (i
= 0; i
< n
; i
++)
1741 ira_object_t from_obj
= ALLOCNO_OBJECT (from
, i
);
1742 ira_object_t to_obj
= ALLOCNO_OBJECT (to
, i
);
1743 live_range_t lr
= OBJECT_LIVE_RANGES (from_obj
);
1745 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
1747 fprintf (ira_dump_file
,
1748 " Moving ranges of a%dr%d to a%dr%d: ",
1749 ALLOCNO_NUM (from
), ALLOCNO_REGNO (from
),
1750 ALLOCNO_NUM (to
), ALLOCNO_REGNO (to
));
1751 ira_print_live_range_list (ira_dump_file
, lr
);
1753 change_object_in_range_list (lr
, to_obj
);
1754 OBJECT_LIVE_RANGES (to_obj
)
1755 = ira_merge_live_ranges (lr
, OBJECT_LIVE_RANGES (to_obj
));
1756 OBJECT_LIVE_RANGES (from_obj
) = NULL
;
1761 copy_allocno_live_ranges (ira_allocno_t from
, ira_allocno_t to
)
1764 int n
= ALLOCNO_NUM_OBJECTS (from
);
1766 gcc_assert (n
== ALLOCNO_NUM_OBJECTS (to
));
1768 for (i
= 0; i
< n
; i
++)
1770 ira_object_t from_obj
= ALLOCNO_OBJECT (from
, i
);
1771 ira_object_t to_obj
= ALLOCNO_OBJECT (to
, i
);
1772 live_range_t lr
= OBJECT_LIVE_RANGES (from_obj
);
1774 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
1776 fprintf (ira_dump_file
, " Copying ranges of a%dr%d to a%dr%d: ",
1777 ALLOCNO_NUM (from
), ALLOCNO_REGNO (from
),
1778 ALLOCNO_NUM (to
), ALLOCNO_REGNO (to
));
1779 ira_print_live_range_list (ira_dump_file
, lr
);
1781 lr
= ira_copy_live_range_list (lr
);
1782 change_object_in_range_list (lr
, to_obj
);
1783 OBJECT_LIVE_RANGES (to_obj
)
1784 = ira_merge_live_ranges (lr
, OBJECT_LIVE_RANGES (to_obj
));
1788 /* Return TRUE if NODE represents a loop with low register
1791 low_pressure_loop_node_p (ira_loop_tree_node_t node
)
1794 enum reg_class pclass
;
1796 if (node
->bb
!= NULL
)
1799 for (i
= 0; i
< ira_pressure_classes_num
; i
++)
1801 pclass
= ira_pressure_classes
[i
];
1802 if (node
->reg_pressure
[pclass
] > ira_available_class_regs
[pclass
]
1803 && ira_available_class_regs
[pclass
] > 1)
1810 /* Return TRUE if LOOP has a complex enter or exit edge. We don't
1811 form a region from such loop if the target use stack register
1812 because reg-stack.c can not deal with such edges. */
1814 loop_with_complex_edge_p (struct loop
*loop
)
1819 VEC (edge
, heap
) *edges
;
1821 FOR_EACH_EDGE (e
, ei
, loop
->header
->preds
)
1822 if (e
->flags
& EDGE_EH
)
1824 edges
= get_loop_exit_edges (loop
);
1825 FOR_EACH_VEC_ELT (edge
, edges
, i
, e
)
1826 if (e
->flags
& EDGE_COMPLEX
)
1832 /* Sort loops for marking them for removal. We put already marked
1833 loops first, then less frequent loops next, and then outer loops
1836 loop_compare_func (const void *v1p
, const void *v2p
)
1839 ira_loop_tree_node_t l1
= *(const ira_loop_tree_node_t
*) v1p
;
1840 ira_loop_tree_node_t l2
= *(const ira_loop_tree_node_t
*) v2p
;
1842 ira_assert (l1
->parent
!= NULL
&& l2
->parent
!= NULL
);
1843 if (l1
->to_remove_p
&& ! l2
->to_remove_p
)
1845 if (! l1
->to_remove_p
&& l2
->to_remove_p
)
1847 if ((diff
= l1
->loop
->header
->frequency
- l2
->loop
->header
->frequency
) != 0)
1849 if ((diff
= (int) loop_depth (l1
->loop
) - (int) loop_depth (l2
->loop
)) != 0)
1851 /* Make sorting stable. */
1852 return l1
->loop
->num
- l2
->loop
->num
;
1855 /* Mark loops which should be removed from regional allocation. We
1856 remove a loop with low register pressure inside another loop with
1857 register pressure. In this case a separate allocation of the loop
1858 hardly helps (for irregular register file architecture it could
1859 help by choosing a better hard register in the loop but we prefer
1860 faster allocation even in this case). We also remove cheap loops
1861 if there are more than IRA_MAX_LOOPS_NUM of them. Loop with EH
1862 exit or enter edges are removed too because the allocation might
1863 require put pseudo moves on the EH edges (we could still do this
1864 for pseudos with caller saved hard registers in some cases but it
1865 is impossible to say here or during top-down allocation pass what
1866 hard register the pseudos get finally). */
1868 mark_loops_for_removal (void)
1871 ira_loop_tree_node_t
*sorted_loops
;
1875 = (ira_loop_tree_node_t
*) ira_allocate (sizeof (ira_loop_tree_node_t
)
1876 * VEC_length (loop_p
,
1878 for (n
= i
= 0; VEC_iterate (loop_p
, ira_loops
.larray
, i
, loop
); i
++)
1879 if (ira_loop_nodes
[i
].regno_allocno_map
!= NULL
)
1881 if (ira_loop_nodes
[i
].parent
== NULL
)
1883 /* Don't remove the root. */
1884 ira_loop_nodes
[i
].to_remove_p
= false;
1887 sorted_loops
[n
++] = &ira_loop_nodes
[i
];
1888 ira_loop_nodes
[i
].to_remove_p
1889 = ((low_pressure_loop_node_p (ira_loop_nodes
[i
].parent
)
1890 && low_pressure_loop_node_p (&ira_loop_nodes
[i
]))
1892 || loop_with_complex_edge_p (ira_loop_nodes
[i
].loop
)
1896 qsort (sorted_loops
, n
, sizeof (ira_loop_tree_node_t
), loop_compare_func
);
1897 for (i
= 0; n
- i
+ 1 > IRA_MAX_LOOPS_NUM
; i
++)
1899 sorted_loops
[i
]->to_remove_p
= true;
1900 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
1903 " Mark loop %d (header %d, freq %d, depth %d) for removal (%s)\n",
1904 sorted_loops
[i
]->loop
->num
, sorted_loops
[i
]->loop
->header
->index
,
1905 sorted_loops
[i
]->loop
->header
->frequency
,
1906 loop_depth (sorted_loops
[i
]->loop
),
1907 low_pressure_loop_node_p (sorted_loops
[i
]->parent
)
1908 && low_pressure_loop_node_p (sorted_loops
[i
])
1909 ? "low pressure" : "cheap loop");
1911 ira_free (sorted_loops
);
1914 /* Mark all loops but root for removing. */
1916 mark_all_loops_for_removal (void)
1921 FOR_EACH_VEC_ELT (loop_p
, ira_loops
.larray
, i
, loop
)
1922 if (ira_loop_nodes
[i
].regno_allocno_map
!= NULL
)
1924 if (ira_loop_nodes
[i
].parent
== NULL
)
1926 /* Don't remove the root. */
1927 ira_loop_nodes
[i
].to_remove_p
= false;
1930 ira_loop_nodes
[i
].to_remove_p
= true;
1931 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
1934 " Mark loop %d (header %d, freq %d, depth %d) for removal\n",
1935 ira_loop_nodes
[i
].loop
->num
,
1936 ira_loop_nodes
[i
].loop
->header
->index
,
1937 ira_loop_nodes
[i
].loop
->header
->frequency
,
1938 loop_depth (ira_loop_nodes
[i
].loop
));
1942 /* Definition of vector of loop tree nodes. */
1943 DEF_VEC_P(ira_loop_tree_node_t
);
1944 DEF_VEC_ALLOC_P(ira_loop_tree_node_t
, heap
);
1946 /* Vec containing references to all removed loop tree nodes. */
1947 static VEC(ira_loop_tree_node_t
,heap
) *removed_loop_vec
;
1949 /* Vec containing references to all children of loop tree nodes. */
1950 static VEC(ira_loop_tree_node_t
,heap
) *children_vec
;
1952 /* Remove subregions of NODE if their separate allocation will not
1953 improve the result. */
1955 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_node_t node
)
1959 ira_loop_tree_node_t subnode
;
1961 remove_p
= node
->to_remove_p
;
1963 VEC_safe_push (ira_loop_tree_node_t
, heap
, children_vec
, node
);
1964 start
= VEC_length (ira_loop_tree_node_t
, children_vec
);
1965 for (subnode
= node
->children
; subnode
!= NULL
; subnode
= subnode
->next
)
1966 if (subnode
->bb
== NULL
)
1967 remove_uneccesary_loop_nodes_from_loop_tree (subnode
);
1969 VEC_safe_push (ira_loop_tree_node_t
, heap
, children_vec
, subnode
);
1970 node
->children
= node
->subloops
= NULL
;
1973 VEC_safe_push (ira_loop_tree_node_t
, heap
, removed_loop_vec
, node
);
1976 while (VEC_length (ira_loop_tree_node_t
, children_vec
) > start
)
1978 subnode
= VEC_pop (ira_loop_tree_node_t
, children_vec
);
1979 subnode
->parent
= node
;
1980 subnode
->next
= node
->children
;
1981 node
->children
= subnode
;
1982 if (subnode
->bb
== NULL
)
1984 subnode
->subloop_next
= node
->subloops
;
1985 node
->subloops
= subnode
;
1990 /* Return TRUE if NODE is inside PARENT. */
1992 loop_is_inside_p (ira_loop_tree_node_t node
, ira_loop_tree_node_t parent
)
1994 for (node
= node
->parent
; node
!= NULL
; node
= node
->parent
)
2000 /* Sort allocnos according to their order in regno allocno list. */
2002 regno_allocno_order_compare_func (const void *v1p
, const void *v2p
)
2004 ira_allocno_t a1
= *(const ira_allocno_t
*) v1p
;
2005 ira_allocno_t a2
= *(const ira_allocno_t
*) v2p
;
2006 ira_loop_tree_node_t n1
= ALLOCNO_LOOP_TREE_NODE (a1
);
2007 ira_loop_tree_node_t n2
= ALLOCNO_LOOP_TREE_NODE (a2
);
2009 if (loop_is_inside_p (n1
, n2
))
2011 else if (loop_is_inside_p (n2
, n1
))
2013 /* If allocnos are equally good, sort by allocno numbers, so that
2014 the results of qsort leave nothing to chance. We put allocnos
2015 with higher number first in the list because it is the original
2016 order for allocnos from loops on the same levels. */
2017 return ALLOCNO_NUM (a2
) - ALLOCNO_NUM (a1
);
2020 /* This array is used to sort allocnos to restore allocno order in
2021 the regno allocno list. */
2022 static ira_allocno_t
*regno_allocnos
;
2024 /* Restore allocno order for REGNO in the regno allocno list. */
2026 ira_rebuild_regno_allocno_list (int regno
)
2031 for (n
= 0, a
= ira_regno_allocno_map
[regno
];
2033 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
2034 regno_allocnos
[n
++] = a
;
2036 qsort (regno_allocnos
, n
, sizeof (ira_allocno_t
),
2037 regno_allocno_order_compare_func
);
2038 for (i
= 1; i
< n
; i
++)
2039 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos
[i
- 1]) = regno_allocnos
[i
];
2040 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos
[n
- 1]) = NULL
;
2041 ira_regno_allocno_map
[regno
] = regno_allocnos
[0];
2042 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
2043 fprintf (ira_dump_file
, " Rebuilding regno allocno list for %d\n", regno
);
2046 /* Propagate info from allocno FROM_A to allocno A. */
2048 propagate_some_info_from_allocno (ira_allocno_t a
, ira_allocno_t from_a
)
2050 enum reg_class aclass
;
2052 merge_hard_reg_conflicts (from_a
, a
, false);
2053 ALLOCNO_NREFS (a
) += ALLOCNO_NREFS (from_a
);
2054 ALLOCNO_FREQ (a
) += ALLOCNO_FREQ (from_a
);
2055 ALLOCNO_CALL_FREQ (a
) += ALLOCNO_CALL_FREQ (from_a
);
2056 ALLOCNO_CALLS_CROSSED_NUM (a
) += ALLOCNO_CALLS_CROSSED_NUM (from_a
);
2057 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
)
2058 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (from_a
);
2059 if (! ALLOCNO_BAD_SPILL_P (from_a
))
2060 ALLOCNO_BAD_SPILL_P (a
) = false;
2061 aclass
= ALLOCNO_CLASS (from_a
);
2062 ira_assert (aclass
== ALLOCNO_CLASS (a
));
2063 ira_allocate_and_accumulate_costs (&ALLOCNO_HARD_REG_COSTS (a
), aclass
,
2064 ALLOCNO_HARD_REG_COSTS (from_a
));
2065 ira_allocate_and_accumulate_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a
),
2067 ALLOCNO_CONFLICT_HARD_REG_COSTS (from_a
));
2068 ALLOCNO_CLASS_COST (a
) += ALLOCNO_CLASS_COST (from_a
);
2069 ALLOCNO_MEMORY_COST (a
) += ALLOCNO_MEMORY_COST (from_a
);
2072 /* Remove allocnos from loops removed from the allocation
2075 remove_unnecessary_allocnos (void)
2078 bool merged_p
, rebuild_p
;
2079 ira_allocno_t a
, prev_a
, next_a
, parent_a
;
2080 ira_loop_tree_node_t a_node
, parent
;
2083 regno_allocnos
= NULL
;
2084 for (regno
= max_reg_num () - 1; regno
>= FIRST_PSEUDO_REGISTER
; regno
--)
2087 for (prev_a
= NULL
, a
= ira_regno_allocno_map
[regno
];
2091 next_a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
);
2092 a_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2093 if (! a_node
->to_remove_p
)
2097 for (parent
= a_node
->parent
;
2098 (parent_a
= parent
->regno_allocno_map
[regno
]) == NULL
2099 && parent
->to_remove_p
;
2100 parent
= parent
->parent
)
2102 if (parent_a
== NULL
)
2104 /* There are no allocnos with the same regno in
2105 upper region -- just move the allocno to the
2108 ALLOCNO_LOOP_TREE_NODE (a
) = parent
;
2109 parent
->regno_allocno_map
[regno
] = a
;
2110 bitmap_set_bit (parent
->all_allocnos
, ALLOCNO_NUM (a
));
2115 /* Remove the allocno and update info of allocno in
2116 the upper region. */
2118 ira_regno_allocno_map
[regno
] = next_a
;
2120 ALLOCNO_NEXT_REGNO_ALLOCNO (prev_a
) = next_a
;
2121 move_allocno_live_ranges (a
, parent_a
);
2123 propagate_some_info_from_allocno (parent_a
, a
);
2124 /* Remove it from the corresponding regno allocno
2125 map to avoid info propagation of subsequent
2126 allocno into this already removed allocno. */
2127 a_node
->regno_allocno_map
[regno
] = NULL
;
2133 /* We need to restore the order in regno allocno list. */
2135 if (regno_allocnos
== NULL
)
2137 = (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
)
2138 * ira_allocnos_num
);
2139 ira_rebuild_regno_allocno_list (regno
);
2143 ira_rebuild_start_finish_chains ();
2144 if (regno_allocnos
!= NULL
)
2145 ira_free (regno_allocnos
);
2148 /* Remove allocnos from all loops but the root. */
2150 remove_low_level_allocnos (void)
2153 bool merged_p
, propagate_p
;
2154 ira_allocno_t a
, top_a
;
2155 ira_loop_tree_node_t a_node
, parent
;
2156 ira_allocno_iterator ai
;
2159 FOR_EACH_ALLOCNO (a
, ai
)
2161 a_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2162 if (a_node
== ira_loop_tree_root
|| ALLOCNO_CAP_MEMBER (a
) != NULL
)
2164 regno
= ALLOCNO_REGNO (a
);
2165 if ((top_a
= ira_loop_tree_root
->regno_allocno_map
[regno
]) == NULL
)
2167 ALLOCNO_LOOP_TREE_NODE (a
) = ira_loop_tree_root
;
2168 ira_loop_tree_root
->regno_allocno_map
[regno
] = a
;
2171 propagate_p
= a_node
->parent
->regno_allocno_map
[regno
] == NULL
;
2172 /* Remove the allocno and update info of allocno in the upper
2174 move_allocno_live_ranges (a
, top_a
);
2177 propagate_some_info_from_allocno (top_a
, a
);
2179 FOR_EACH_ALLOCNO (a
, ai
)
2181 a_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2182 if (a_node
== ira_loop_tree_root
)
2184 parent
= a_node
->parent
;
2185 regno
= ALLOCNO_REGNO (a
);
2186 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
2187 ira_assert (ALLOCNO_CAP (a
) != NULL
);
2188 else if (ALLOCNO_CAP (a
) == NULL
)
2189 ira_assert (parent
->regno_allocno_map
[regno
] != NULL
);
2191 FOR_EACH_ALLOCNO (a
, ai
)
2193 regno
= ALLOCNO_REGNO (a
);
2194 if (ira_loop_tree_root
->regno_allocno_map
[regno
] == a
)
2197 ira_allocno_object_iterator oi
;
2199 ira_regno_allocno_map
[regno
] = a
;
2200 ALLOCNO_NEXT_REGNO_ALLOCNO (a
) = NULL
;
2201 ALLOCNO_CAP_MEMBER (a
) = NULL
;
2202 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
2203 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj
),
2204 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
));
2206 if (ALLOCNO_TOTAL_NO_STACK_REG_P (a
))
2207 ALLOCNO_NO_STACK_REG_P (a
) = true;
2214 ira_rebuild_start_finish_chains ();
2217 /* Remove loops from consideration. We remove all loops except for
2218 root if ALL_P or loops for which a separate allocation will not
2219 improve the result. We have to do this after allocno creation and
2220 their costs and allocno class evaluation because only after that
2221 the register pressure can be known and is calculated. */
2223 remove_unnecessary_regions (bool all_p
)
2226 mark_all_loops_for_removal ();
2228 mark_loops_for_removal ();
2230 = VEC_alloc (ira_loop_tree_node_t
, heap
,
2231 last_basic_block
+ VEC_length (loop_p
, ira_loops
.larray
));
2233 = VEC_alloc (ira_loop_tree_node_t
, heap
,
2234 last_basic_block
+ VEC_length (loop_p
, ira_loops
.larray
));
2235 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_root
) ;
2236 VEC_free (ira_loop_tree_node_t
, heap
, children_vec
);
2238 remove_low_level_allocnos ();
2240 remove_unnecessary_allocnos ();
2241 while (VEC_length (ira_loop_tree_node_t
, removed_loop_vec
) > 0)
2242 finish_loop_tree_node (VEC_pop (ira_loop_tree_node_t
, removed_loop_vec
));
2243 VEC_free (ira_loop_tree_node_t
, heap
, removed_loop_vec
);
2248 /* At this point true value of allocno attribute bad_spill_p means
2249 that there is an insn where allocno occurs and where the allocno
2250 can not be used as memory. The function updates the attribute, now
2251 it can be true only for allocnos which can not be used as memory in
2252 an insn and in whose live ranges there is other allocno deaths.
2253 Spilling allocnos with true value will not improve the code because
2254 it will not make other allocnos colorable and additional reloads
2255 for the corresponding pseudo will be generated in reload pass for
2256 each insn it occurs.
2258 This is a trick mentioned in one classic article of Chaitin etc
2259 which is frequently omitted in other implementations of RA based on
2262 update_bad_spill_attribute (void)
2266 ira_allocno_iterator ai
;
2267 ira_allocno_object_iterator aoi
;
2270 enum reg_class aclass
;
2271 bitmap_head dead_points
[N_REG_CLASSES
];
2273 for (i
= 0; i
< ira_allocno_classes_num
; i
++)
2275 aclass
= ira_allocno_classes
[i
];
2276 bitmap_initialize (&dead_points
[aclass
], ®_obstack
);
2278 FOR_EACH_ALLOCNO (a
, ai
)
2280 aclass
= ALLOCNO_CLASS (a
);
2281 if (aclass
== NO_REGS
)
2283 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, aoi
)
2284 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
2285 bitmap_set_bit (&dead_points
[aclass
], r
->finish
);
2287 FOR_EACH_ALLOCNO (a
, ai
)
2289 aclass
= ALLOCNO_CLASS (a
);
2290 if (aclass
== NO_REGS
)
2292 if (! ALLOCNO_BAD_SPILL_P (a
))
2294 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, aoi
)
2296 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
2298 for (i
= r
->start
+ 1; i
< r
->finish
; i
++)
2299 if (bitmap_bit_p (&dead_points
[aclass
], i
))
2306 ALLOCNO_BAD_SPILL_P (a
) = false;
2311 for (i
= 0; i
< ira_allocno_classes_num
; i
++)
2313 aclass
= ira_allocno_classes
[i
];
2314 bitmap_clear (&dead_points
[aclass
]);
2320 /* Set up minimal and maximal live range points for allocnos. */
2322 setup_min_max_allocno_live_range_point (void)
2325 ira_allocno_t a
, parent_a
, cap
;
2326 ira_allocno_iterator ai
;
2327 #ifdef ENABLE_IRA_CHECKING
2328 ira_object_iterator oi
;
2332 ira_loop_tree_node_t parent
;
2334 FOR_EACH_ALLOCNO (a
, ai
)
2336 int n
= ALLOCNO_NUM_OBJECTS (a
);
2338 for (i
= 0; i
< n
; i
++)
2340 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
2341 r
= OBJECT_LIVE_RANGES (obj
);
2344 OBJECT_MAX (obj
) = r
->finish
;
2345 for (; r
->next
!= NULL
; r
= r
->next
)
2347 OBJECT_MIN (obj
) = r
->start
;
2350 for (i
= max_reg_num () - 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
2351 for (a
= ira_regno_allocno_map
[i
];
2353 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
2356 int n
= ALLOCNO_NUM_OBJECTS (a
);
2358 for (j
= 0; j
< n
; j
++)
2360 ira_object_t obj
= ALLOCNO_OBJECT (a
, j
);
2361 ira_object_t parent_obj
;
2363 if (OBJECT_MAX (obj
) < 0)
2365 ira_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
2366 /* Accumulation of range info. */
2367 if (ALLOCNO_CAP (a
) != NULL
)
2369 for (cap
= ALLOCNO_CAP (a
); cap
!= NULL
; cap
= ALLOCNO_CAP (cap
))
2371 ira_object_t cap_obj
= ALLOCNO_OBJECT (cap
, j
);
2372 if (OBJECT_MAX (cap_obj
) < OBJECT_MAX (obj
))
2373 OBJECT_MAX (cap_obj
) = OBJECT_MAX (obj
);
2374 if (OBJECT_MIN (cap_obj
) > OBJECT_MIN (obj
))
2375 OBJECT_MIN (cap_obj
) = OBJECT_MIN (obj
);
2379 if ((parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
) == NULL
)
2381 parent_a
= parent
->regno_allocno_map
[i
];
2382 parent_obj
= ALLOCNO_OBJECT (parent_a
, j
);
2383 if (OBJECT_MAX (parent_obj
) < OBJECT_MAX (obj
))
2384 OBJECT_MAX (parent_obj
) = OBJECT_MAX (obj
);
2385 if (OBJECT_MIN (parent_obj
) > OBJECT_MIN (obj
))
2386 OBJECT_MIN (parent_obj
) = OBJECT_MIN (obj
);
2389 #ifdef ENABLE_IRA_CHECKING
2390 FOR_EACH_OBJECT (obj
, oi
)
2392 if ((0 <= OBJECT_MIN (obj
) && OBJECT_MIN (obj
) <= ira_max_point
)
2393 && (0 <= OBJECT_MAX (obj
) && OBJECT_MAX (obj
) <= ira_max_point
))
2400 /* Sort allocnos according to their live ranges. Allocnos with
2401 smaller allocno class are put first unless we use priority
2402 coloring. Allocnos with the same class are ordered according
2403 their start (min). Allocnos with the same start are ordered
2404 according their finish (max). */
2406 object_range_compare_func (const void *v1p
, const void *v2p
)
2409 ira_object_t obj1
= *(const ira_object_t
*) v1p
;
2410 ira_object_t obj2
= *(const ira_object_t
*) v2p
;
2411 ira_allocno_t a1
= OBJECT_ALLOCNO (obj1
);
2412 ira_allocno_t a2
= OBJECT_ALLOCNO (obj2
);
2414 if ((diff
= OBJECT_MIN (obj1
) - OBJECT_MIN (obj2
)) != 0)
2416 if ((diff
= OBJECT_MAX (obj1
) - OBJECT_MAX (obj2
)) != 0)
2418 return ALLOCNO_NUM (a1
) - ALLOCNO_NUM (a2
);
2421 /* Sort ira_object_id_map and set up conflict id of allocnos. */
2423 sort_conflict_id_map (void)
2427 ira_allocno_iterator ai
;
2430 FOR_EACH_ALLOCNO (a
, ai
)
2432 ira_allocno_object_iterator oi
;
2435 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
2436 ira_object_id_map
[num
++] = obj
;
2438 qsort (ira_object_id_map
, num
, sizeof (ira_object_t
),
2439 object_range_compare_func
);
2440 for (i
= 0; i
< num
; i
++)
2442 ira_object_t obj
= ira_object_id_map
[i
];
2444 gcc_assert (obj
!= NULL
);
2445 OBJECT_CONFLICT_ID (obj
) = i
;
2447 for (i
= num
; i
< ira_objects_num
; i
++)
2448 ira_object_id_map
[i
] = NULL
;
2451 /* Set up minimal and maximal conflict ids of allocnos with which
2452 given allocno can conflict. */
2454 setup_min_max_conflict_allocno_ids (void)
2457 int i
, j
, min
, max
, start
, finish
, first_not_finished
, filled_area_start
;
2458 int *live_range_min
, *last_lived
;
2459 int word0_min
, word0_max
;
2461 ira_allocno_iterator ai
;
2463 live_range_min
= (int *) ira_allocate (sizeof (int) * ira_objects_num
);
2465 first_not_finished
= -1;
2466 for (i
= 0; i
< ira_objects_num
; i
++)
2468 ira_object_t obj
= ira_object_id_map
[i
];
2473 a
= OBJECT_ALLOCNO (obj
);
2477 aclass
= ALLOCNO_CLASS (a
);
2479 first_not_finished
= i
;
2483 start
= OBJECT_MIN (obj
);
2484 /* If we skip an allocno, the allocno with smaller ids will
2485 be also skipped because of the secondary sorting the
2486 range finishes (see function
2487 object_range_compare_func). */
2488 while (first_not_finished
< i
2489 && start
> OBJECT_MAX (ira_object_id_map
2490 [first_not_finished
]))
2491 first_not_finished
++;
2492 min
= first_not_finished
;
2495 /* We could increase min further in this case but it is good
2498 live_range_min
[i
] = OBJECT_MIN (obj
);
2499 OBJECT_MIN (obj
) = min
;
2501 last_lived
= (int *) ira_allocate (sizeof (int) * ira_max_point
);
2503 filled_area_start
= -1;
2504 for (i
= ira_objects_num
- 1; i
>= 0; i
--)
2506 ira_object_t obj
= ira_object_id_map
[i
];
2511 a
= OBJECT_ALLOCNO (obj
);
2514 aclass
= ALLOCNO_CLASS (a
);
2515 for (j
= 0; j
< ira_max_point
; j
++)
2517 filled_area_start
= ira_max_point
;
2519 min
= live_range_min
[i
];
2520 finish
= OBJECT_MAX (obj
);
2521 max
= last_lived
[finish
];
2523 /* We could decrease max further in this case but it is good
2525 max
= OBJECT_CONFLICT_ID (obj
) - 1;
2526 OBJECT_MAX (obj
) = max
;
2527 /* In filling, we can go further A range finish to recognize
2528 intersection quickly because if the finish of subsequently
2529 processed allocno (it has smaller conflict id) range is
2530 further A range finish than they are definitely intersected
2531 (the reason for this is the allocnos with bigger conflict id
2532 have their range starts not smaller than allocnos with
2534 for (j
= min
; j
< filled_area_start
; j
++)
2536 filled_area_start
= min
;
2538 ira_free (last_lived
);
2539 ira_free (live_range_min
);
2541 /* For allocnos with more than one object, we may later record extra conflicts in
2542 subobject 0 that we cannot really know about here.
2543 For now, simply widen the min/max range of these subobjects. */
2545 word0_min
= INT_MAX
;
2546 word0_max
= INT_MIN
;
2548 FOR_EACH_ALLOCNO (a
, ai
)
2550 int n
= ALLOCNO_NUM_OBJECTS (a
);
2555 obj0
= ALLOCNO_OBJECT (a
, 0);
2556 if (OBJECT_CONFLICT_ID (obj0
) < word0_min
)
2557 word0_min
= OBJECT_CONFLICT_ID (obj0
);
2558 if (OBJECT_CONFLICT_ID (obj0
) > word0_max
)
2559 word0_max
= OBJECT_CONFLICT_ID (obj0
);
2561 FOR_EACH_ALLOCNO (a
, ai
)
2563 int n
= ALLOCNO_NUM_OBJECTS (a
);
2568 obj0
= ALLOCNO_OBJECT (a
, 0);
2569 if (OBJECT_MIN (obj0
) > word0_min
)
2570 OBJECT_MIN (obj0
) = word0_min
;
2571 if (OBJECT_MAX (obj0
) < word0_max
)
2572 OBJECT_MAX (obj0
) = word0_max
;
2582 ira_allocno_iterator ai
;
2583 ira_loop_tree_node_t loop_tree_node
;
2585 FOR_EACH_ALLOCNO (a
, ai
)
2587 if (ALLOCNO_LOOP_TREE_NODE (a
) == ira_loop_tree_root
)
2589 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
2590 create_cap_allocno (a
);
2591 else if (ALLOCNO_CAP (a
) == NULL
)
2593 loop_tree_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2594 if (!bitmap_bit_p (loop_tree_node
->border_allocnos
, ALLOCNO_NUM (a
)))
2595 create_cap_allocno (a
);
2602 /* The page contains code transforming more one region internal
2603 representation (IR) to one region IR which is necessary for reload.
2604 This transformation is called IR flattening. We might just rebuild
2605 the IR for one region but we don't do it because it takes a lot of
2608 /* Map: regno -> allocnos which will finally represent the regno for
2609 IR with one region. */
2610 static ira_allocno_t
*regno_top_level_allocno_map
;
2612 /* Find the allocno that corresponds to A at a level one higher up in the
2613 loop tree. Returns NULL if A is a cap, or if it has no parent. */
2615 ira_parent_allocno (ira_allocno_t a
)
2617 ira_loop_tree_node_t parent
;
2619 if (ALLOCNO_CAP (a
) != NULL
)
2622 parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
;
2626 return parent
->regno_allocno_map
[ALLOCNO_REGNO (a
)];
2629 /* Find the allocno that corresponds to A at a level one higher up in the
2630 loop tree. If ALLOCNO_CAP is set for A, return that. */
2632 ira_parent_or_cap_allocno (ira_allocno_t a
)
2634 if (ALLOCNO_CAP (a
) != NULL
)
2635 return ALLOCNO_CAP (a
);
2637 return ira_parent_allocno (a
);
2640 /* Process all allocnos originated from pseudo REGNO and copy live
2641 ranges, hard reg conflicts, and allocno stack reg attributes from
2642 low level allocnos to final allocnos which are destinations of
2643 removed stores at a loop exit. Return true if we copied live
2646 copy_info_to_removed_store_destinations (int regno
)
2649 ira_allocno_t parent_a
= NULL
;
2650 ira_loop_tree_node_t parent
;
2654 for (a
= ira_regno_allocno_map
[regno
];
2656 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
2658 if (a
!= regno_top_level_allocno_map
[REGNO (allocno_emit_reg (a
))])
2659 /* This allocno will be removed. */
2662 /* Caps will be removed. */
2663 ira_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
2664 for (parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
;
2666 parent
= parent
->parent
)
2667 if ((parent_a
= parent
->regno_allocno_map
[regno
]) == NULL
2669 == regno_top_level_allocno_map
[REGNO
2670 (allocno_emit_reg (parent_a
))]
2671 && ALLOCNO_EMIT_DATA (parent_a
)->mem_optimized_dest_p
))
2673 if (parent
== NULL
|| parent_a
== NULL
)
2676 copy_allocno_live_ranges (a
, parent_a
);
2677 merge_hard_reg_conflicts (a
, parent_a
, true);
2679 ALLOCNO_CALL_FREQ (parent_a
) += ALLOCNO_CALL_FREQ (a
);
2680 ALLOCNO_CALLS_CROSSED_NUM (parent_a
)
2681 += ALLOCNO_CALLS_CROSSED_NUM (a
);
2682 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a
)
2683 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
);
2689 /* Flatten the IR. In other words, this function transforms IR as if
2690 it were built with one region (without loops). We could make it
2691 much simpler by rebuilding IR with one region, but unfortunately it
2692 takes a lot of time. MAX_REGNO_BEFORE_EMIT and
2693 IRA_MAX_POINT_BEFORE_EMIT are correspondingly MAX_REG_NUM () and
2694 IRA_MAX_POINT before emitting insns on the loop borders. */
2696 ira_flattening (int max_regno_before_emit
, int ira_max_point_before_emit
)
2701 bool new_pseudos_p
, merged_p
, mem_dest_p
;
2703 enum reg_class aclass
;
2704 ira_allocno_t a
, parent_a
, first
, second
, node_first
, node_second
;
2706 ira_loop_tree_node_t node
;
2708 ira_allocno_iterator ai
;
2709 ira_copy_iterator ci
;
2711 regno_top_level_allocno_map
2712 = (ira_allocno_t
*) ira_allocate (max_reg_num ()
2713 * sizeof (ira_allocno_t
));
2714 memset (regno_top_level_allocno_map
, 0,
2715 max_reg_num () * sizeof (ira_allocno_t
));
2716 new_pseudos_p
= merged_p
= false;
2717 FOR_EACH_ALLOCNO (a
, ai
)
2719 ira_allocno_object_iterator oi
;
2722 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
2723 /* Caps are not in the regno allocno maps and they are never
2724 will be transformed into allocnos existing after IR
2727 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
2728 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
),
2729 OBJECT_CONFLICT_HARD_REGS (obj
));
2731 ALLOCNO_TOTAL_NO_STACK_REG_P (a
) = ALLOCNO_NO_STACK_REG_P (a
);
2734 /* Fix final allocno attributes. */
2735 for (i
= max_regno_before_emit
- 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
2738 for (a
= ira_regno_allocno_map
[i
];
2740 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
2742 ira_emit_data_t parent_data
, data
= ALLOCNO_EMIT_DATA (a
);
2744 ira_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
2745 if (data
->somewhere_renamed_p
)
2746 new_pseudos_p
= true;
2747 parent_a
= ira_parent_allocno (a
);
2748 if (parent_a
== NULL
)
2750 ALLOCNO_COPIES (a
) = NULL
;
2751 regno_top_level_allocno_map
[REGNO (data
->reg
)] = a
;
2754 ira_assert (ALLOCNO_CAP_MEMBER (parent_a
) == NULL
);
2756 if (data
->mem_optimized_dest
!= NULL
)
2758 parent_data
= ALLOCNO_EMIT_DATA (parent_a
);
2759 if (REGNO (data
->reg
) == REGNO (parent_data
->reg
))
2761 merge_hard_reg_conflicts (a
, parent_a
, true);
2762 move_allocno_live_ranges (a
, parent_a
);
2764 parent_data
->mem_optimized_dest_p
2765 = (parent_data
->mem_optimized_dest_p
2766 || data
->mem_optimized_dest_p
);
2769 new_pseudos_p
= true;
2772 ALLOCNO_NREFS (parent_a
) -= ALLOCNO_NREFS (a
);
2773 ALLOCNO_FREQ (parent_a
) -= ALLOCNO_FREQ (a
);
2774 ALLOCNO_CALL_FREQ (parent_a
) -= ALLOCNO_CALL_FREQ (a
);
2775 ALLOCNO_CALLS_CROSSED_NUM (parent_a
)
2776 -= ALLOCNO_CALLS_CROSSED_NUM (a
);
2777 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a
)
2778 -= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
);
2779 ira_assert (ALLOCNO_CALLS_CROSSED_NUM (parent_a
) >= 0
2780 && ALLOCNO_NREFS (parent_a
) >= 0
2781 && ALLOCNO_FREQ (parent_a
) >= 0);
2782 aclass
= ALLOCNO_CLASS (parent_a
);
2783 hard_regs_num
= ira_class_hard_regs_num
[aclass
];
2784 if (ALLOCNO_HARD_REG_COSTS (a
) != NULL
2785 && ALLOCNO_HARD_REG_COSTS (parent_a
) != NULL
)
2786 for (j
= 0; j
< hard_regs_num
; j
++)
2787 ALLOCNO_HARD_REG_COSTS (parent_a
)[j
]
2788 -= ALLOCNO_HARD_REG_COSTS (a
)[j
];
2789 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a
) != NULL
2790 && ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a
) != NULL
)
2791 for (j
= 0; j
< hard_regs_num
; j
++)
2792 ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a
)[j
]
2793 -= ALLOCNO_CONFLICT_HARD_REG_COSTS (a
)[j
];
2794 ALLOCNO_CLASS_COST (parent_a
)
2795 -= ALLOCNO_CLASS_COST (a
);
2796 ALLOCNO_MEMORY_COST (parent_a
) -= ALLOCNO_MEMORY_COST (a
);
2797 parent_a
= ira_parent_allocno (parent_a
);
2798 if (parent_a
== NULL
)
2801 ALLOCNO_COPIES (a
) = NULL
;
2802 regno_top_level_allocno_map
[REGNO (data
->reg
)] = a
;
2804 if (mem_dest_p
&& copy_info_to_removed_store_destinations (i
))
2807 ira_assert (new_pseudos_p
|| ira_max_point_before_emit
== ira_max_point
);
2808 if (merged_p
|| ira_max_point_before_emit
!= ira_max_point
)
2809 ira_rebuild_start_finish_chains ();
2812 sparseset objects_live
;
2814 /* Rebuild conflicts. */
2815 FOR_EACH_ALLOCNO (a
, ai
)
2817 ira_allocno_object_iterator oi
;
2820 if (a
!= regno_top_level_allocno_map
[REGNO (allocno_emit_reg (a
))]
2821 || ALLOCNO_CAP_MEMBER (a
) != NULL
)
2823 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
2825 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
2826 ira_assert (r
->object
== obj
);
2827 clear_conflicts (obj
);
2830 objects_live
= sparseset_alloc (ira_objects_num
);
2831 for (i
= 0; i
< ira_max_point
; i
++)
2833 for (r
= ira_start_point_ranges
[i
]; r
!= NULL
; r
= r
->start_next
)
2835 ira_object_t obj
= r
->object
;
2837 a
= OBJECT_ALLOCNO (obj
);
2838 if (a
!= regno_top_level_allocno_map
[REGNO (allocno_emit_reg (a
))]
2839 || ALLOCNO_CAP_MEMBER (a
) != NULL
)
2842 aclass
= ALLOCNO_CLASS (a
);
2843 sparseset_set_bit (objects_live
, OBJECT_CONFLICT_ID (obj
));
2844 EXECUTE_IF_SET_IN_SPARSESET (objects_live
, n
)
2846 ira_object_t live_obj
= ira_object_id_map
[n
];
2847 ira_allocno_t live_a
= OBJECT_ALLOCNO (live_obj
);
2848 enum reg_class live_aclass
= ALLOCNO_CLASS (live_a
);
2850 if (ira_reg_classes_intersect_p
[aclass
][live_aclass
]
2851 /* Don't set up conflict for the allocno with itself. */
2853 ira_add_conflict (obj
, live_obj
);
2857 for (r
= ira_finish_point_ranges
[i
]; r
!= NULL
; r
= r
->finish_next
)
2858 sparseset_clear_bit (objects_live
, OBJECT_CONFLICT_ID (r
->object
));
2860 sparseset_free (objects_live
);
2861 compress_conflict_vecs ();
2863 /* Mark some copies for removing and change allocnos in the rest
2865 FOR_EACH_COPY (cp
, ci
)
2867 if (ALLOCNO_CAP_MEMBER (cp
->first
) != NULL
2868 || ALLOCNO_CAP_MEMBER (cp
->second
) != NULL
)
2870 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
2872 (ira_dump_file
, " Remove cp%d:%c%dr%d-%c%dr%d\n",
2873 cp
->num
, ALLOCNO_CAP_MEMBER (cp
->first
) != NULL
? 'c' : 'a',
2874 ALLOCNO_NUM (cp
->first
),
2875 REGNO (allocno_emit_reg (cp
->first
)),
2876 ALLOCNO_CAP_MEMBER (cp
->second
) != NULL
? 'c' : 'a',
2877 ALLOCNO_NUM (cp
->second
),
2878 REGNO (allocno_emit_reg (cp
->second
)));
2879 cp
->loop_tree_node
= NULL
;
2883 = regno_top_level_allocno_map
[REGNO (allocno_emit_reg (cp
->first
))];
2885 = regno_top_level_allocno_map
[REGNO (allocno_emit_reg (cp
->second
))];
2886 node
= cp
->loop_tree_node
;
2888 keep_p
= true; /* It copy generated in ira-emit.c. */
2891 /* Check that the copy was not propagated from level on
2892 which we will have different pseudos. */
2893 node_first
= node
->regno_allocno_map
[ALLOCNO_REGNO (cp
->first
)];
2894 node_second
= node
->regno_allocno_map
[ALLOCNO_REGNO (cp
->second
)];
2895 keep_p
= ((REGNO (allocno_emit_reg (first
))
2896 == REGNO (allocno_emit_reg (node_first
)))
2897 && (REGNO (allocno_emit_reg (second
))
2898 == REGNO (allocno_emit_reg (node_second
))));
2902 cp
->loop_tree_node
= ira_loop_tree_root
;
2904 cp
->second
= second
;
2908 cp
->loop_tree_node
= NULL
;
2909 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
2910 fprintf (ira_dump_file
, " Remove cp%d:a%dr%d-a%dr%d\n",
2911 cp
->num
, ALLOCNO_NUM (cp
->first
),
2912 REGNO (allocno_emit_reg (cp
->first
)),
2913 ALLOCNO_NUM (cp
->second
),
2914 REGNO (allocno_emit_reg (cp
->second
)));
2917 /* Remove unnecessary allocnos on lower levels of the loop tree. */
2918 FOR_EACH_ALLOCNO (a
, ai
)
2920 if (a
!= regno_top_level_allocno_map
[REGNO (allocno_emit_reg (a
))]
2921 || ALLOCNO_CAP_MEMBER (a
) != NULL
)
2923 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
2924 fprintf (ira_dump_file
, " Remove a%dr%d\n",
2925 ALLOCNO_NUM (a
), REGNO (allocno_emit_reg (a
)));
2929 ALLOCNO_LOOP_TREE_NODE (a
) = ira_loop_tree_root
;
2930 ALLOCNO_REGNO (a
) = REGNO (allocno_emit_reg (a
));
2931 ALLOCNO_CAP (a
) = NULL
;
2932 /* Restore updated costs for assignments from reload. */
2933 ALLOCNO_UPDATED_MEMORY_COST (a
) = ALLOCNO_MEMORY_COST (a
);
2934 ALLOCNO_UPDATED_CLASS_COST (a
) = ALLOCNO_CLASS_COST (a
);
2935 if (! ALLOCNO_ASSIGNED_P (a
))
2936 ira_free_allocno_updated_costs (a
);
2937 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
);
2938 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) == NULL
);
2940 /* Remove unnecessary copies. */
2941 FOR_EACH_COPY (cp
, ci
)
2943 if (cp
->loop_tree_node
== NULL
)
2945 ira_copies
[cp
->num
] = NULL
;
2950 (ALLOCNO_LOOP_TREE_NODE (cp
->first
) == ira_loop_tree_root
2951 && ALLOCNO_LOOP_TREE_NODE (cp
->second
) == ira_loop_tree_root
);
2952 ira_add_allocno_copy_to_list (cp
);
2953 ira_swap_allocno_copy_ends_if_necessary (cp
);
2955 rebuild_regno_allocno_maps ();
2956 if (ira_max_point
!= ira_max_point_before_emit
)
2957 ira_compress_allocno_live_ranges ();
2958 ira_free (regno_top_level_allocno_map
);
2963 #ifdef ENABLE_IRA_CHECKING
2964 /* Check creation of all allocnos. Allocnos on lower levels should
2965 have allocnos or caps on all upper levels. */
2967 check_allocno_creation (void)
2970 ira_allocno_iterator ai
;
2971 ira_loop_tree_node_t loop_tree_node
;
2973 FOR_EACH_ALLOCNO (a
, ai
)
2975 loop_tree_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2976 ira_assert (bitmap_bit_p (loop_tree_node
->all_allocnos
,
2978 if (loop_tree_node
== ira_loop_tree_root
)
2980 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
2981 ira_assert (ALLOCNO_CAP (a
) != NULL
);
2982 else if (ALLOCNO_CAP (a
) == NULL
)
2983 ira_assert (loop_tree_node
->parent
2984 ->regno_allocno_map
[ALLOCNO_REGNO (a
)] != NULL
2985 && bitmap_bit_p (loop_tree_node
->border_allocnos
,
2991 /* Identify allocnos which prefer a register class with a single hard register.
2992 Adjust ALLOCNO_CONFLICT_HARD_REG_COSTS so that conflicting allocnos are
2993 less likely to use the preferred singleton register. */
2995 update_conflict_hard_reg_costs (void)
2998 ira_allocno_iterator ai
;
3001 FOR_EACH_ALLOCNO (a
, ai
)
3003 reg_class_t aclass
= ALLOCNO_CLASS (a
);
3004 reg_class_t pref
= reg_preferred_class (ALLOCNO_REGNO (a
));
3006 if (reg_class_size
[(int) pref
] != 1)
3008 index
= ira_class_hard_reg_index
[(int) aclass
]
3009 [ira_class_hard_regs
[(int) pref
][0]];
3012 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a
) == NULL
3013 || ALLOCNO_HARD_REG_COSTS (a
) == NULL
)
3016 for (i
= ira_class_hard_regs_num
[(int) aclass
] - 1; i
>= 0; i
--)
3017 if (ALLOCNO_HARD_REG_COSTS (a
)[i
] > ALLOCNO_CLASS_COST (a
)
3018 && min
> ALLOCNO_HARD_REG_COSTS (a
)[i
])
3019 min
= ALLOCNO_HARD_REG_COSTS (a
)[i
];
3022 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a
),
3024 ALLOCNO_CONFLICT_HARD_REG_COSTS (a
)[index
]
3025 -= min
- ALLOCNO_CLASS_COST (a
);
3029 /* Create a internal representation (IR) for IRA (allocnos, copies,
3030 loop tree nodes). If LOOPS_P is FALSE the nodes corresponding to
3031 the loops (except the root which corresponds the all function) and
3032 correspondingly allocnos for the loops will be not created. Such
3033 parameter value is used for Chaitin-Briggs coloring. The function
3034 returns TRUE if we generate loop structure (besides nodes
3035 representing all function and the basic blocks) for regional
3036 allocation. A true return means that we really need to flatten IR
3037 before the reload. */
3039 ira_build (bool loops_p
)
3043 initiate_cost_vectors ();
3044 initiate_allocnos ();
3046 create_loop_tree_nodes (loops_p
);
3050 create_allocno_objects ();
3051 ira_create_allocno_live_ranges ();
3052 remove_unnecessary_regions (false);
3053 ira_compress_allocno_live_ranges ();
3054 update_bad_spill_attribute ();
3055 loops_p
= more_one_region_p ();
3058 propagate_allocno_info ();
3061 ira_tune_allocno_costs ();
3062 #ifdef ENABLE_IRA_CHECKING
3063 check_allocno_creation ();
3065 setup_min_max_allocno_live_range_point ();
3066 sort_conflict_id_map ();
3067 setup_min_max_conflict_allocno_ids ();
3068 ira_build_conflicts ();
3069 update_conflict_hard_reg_costs ();
3070 if (! ira_conflicts_p
)
3073 ira_allocno_iterator ai
;
3075 /* Remove all regions but root one. */
3078 remove_unnecessary_regions (true);
3081 /* We don't save hard registers around calls for fast allocation
3082 -- add caller clobbered registers as conflicting ones to
3083 allocno crossing calls. */
3084 FOR_EACH_ALLOCNO (a
, ai
)
3085 if (ALLOCNO_CALLS_CROSSED_NUM (a
) != 0)
3086 ior_hard_reg_conflicts (a
, &call_used_reg_set
);
3088 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
3089 print_copies (ira_dump_file
);
3090 if (internal_flag_ira_verbose
> 0 && ira_dump_file
!= NULL
)
3095 ira_allocno_iterator ai
;
3100 FOR_EACH_ALLOCNO (a
, ai
)
3102 int j
, nobj
= ALLOCNO_NUM_OBJECTS (a
);
3106 for (j
= 0; j
< nobj
; j
++)
3108 ira_object_t obj
= ALLOCNO_OBJECT (a
, j
);
3109 n
+= OBJECT_NUM_CONFLICTS (obj
);
3110 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
3114 fprintf (ira_dump_file
, " regions=%d, blocks=%d, points=%d\n",
3115 VEC_length (loop_p
, ira_loops
.larray
), n_basic_blocks
,
3117 fprintf (ira_dump_file
,
3118 " allocnos=%d (big %d), copies=%d, conflicts=%d, ranges=%d\n",
3119 ira_allocnos_num
, nr_big
, ira_copies_num
, n
, nr
);
3124 /* Release the data created by function ira_build. */
3128 finish_loop_tree_nodes ();
3131 finish_cost_vectors ();
3132 ira_finish_allocno_live_ranges ();