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)
1809 /* Sort loops for marking them for removal. We put already marked
1810 loops first, then less frequent loops next, and then outer loops
1813 loop_compare_func (const void *v1p
, const void *v2p
)
1816 ira_loop_tree_node_t l1
= *(const ira_loop_tree_node_t
*) v1p
;
1817 ira_loop_tree_node_t l2
= *(const ira_loop_tree_node_t
*) v2p
;
1819 ira_assert (l1
->parent
!= NULL
&& l2
->parent
!= NULL
);
1820 if (l1
->to_remove_p
&& ! l2
->to_remove_p
)
1822 if (! l1
->to_remove_p
&& l2
->to_remove_p
)
1824 if ((diff
= l1
->loop
->header
->frequency
- l2
->loop
->header
->frequency
) != 0)
1826 if ((diff
= (int) loop_depth (l1
->loop
) - (int) loop_depth (l2
->loop
)) != 0)
1828 /* Make sorting stable. */
1829 return l1
->loop
->num
- l2
->loop
->num
;
1833 /* Mark loops which should be removed from regional allocation. We
1834 remove a loop with low register pressure inside another loop with
1835 register pressure. In this case a separate allocation of the loop
1836 hardly helps (for irregular register file architecture it could
1837 help by choosing a better hard register in the loop but we prefer
1838 faster allocation even in this case). We also remove cheap loops
1839 if there are more than IRA_MAX_LOOPS_NUM of them. */
1841 mark_loops_for_removal (void)
1844 ira_loop_tree_node_t
*sorted_loops
;
1848 = (ira_loop_tree_node_t
*) ira_allocate (sizeof (ira_loop_tree_node_t
)
1849 * VEC_length (loop_p
,
1851 for (n
= i
= 0; VEC_iterate (loop_p
, ira_loops
.larray
, i
, loop
); i
++)
1852 if (ira_loop_nodes
[i
].regno_allocno_map
!= NULL
)
1854 if (ira_loop_nodes
[i
].parent
== NULL
)
1856 /* Don't remove the root. */
1857 ira_loop_nodes
[i
].to_remove_p
= false;
1860 sorted_loops
[n
++] = &ira_loop_nodes
[i
];
1861 ira_loop_nodes
[i
].to_remove_p
1862 = (low_pressure_loop_node_p (ira_loop_nodes
[i
].parent
)
1863 && low_pressure_loop_node_p (&ira_loop_nodes
[i
]));
1865 qsort (sorted_loops
, n
, sizeof (ira_loop_tree_node_t
), loop_compare_func
);
1866 for (i
= 0; n
- i
+ 1 > IRA_MAX_LOOPS_NUM
; i
++)
1868 sorted_loops
[i
]->to_remove_p
= true;
1869 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
1872 " Mark loop %d (header %d, freq %d, depth %d) for removal (%s)\n",
1873 sorted_loops
[i
]->loop
->num
, sorted_loops
[i
]->loop
->header
->index
,
1874 sorted_loops
[i
]->loop
->header
->frequency
,
1875 loop_depth (sorted_loops
[i
]->loop
),
1876 low_pressure_loop_node_p (sorted_loops
[i
]->parent
)
1877 && low_pressure_loop_node_p (sorted_loops
[i
])
1878 ? "low pressure" : "cheap loop");
1880 ira_free (sorted_loops
);
1883 /* Mark all loops but root for removing. */
1885 mark_all_loops_for_removal (void)
1890 FOR_EACH_VEC_ELT (loop_p
, ira_loops
.larray
, i
, loop
)
1891 if (ira_loop_nodes
[i
].regno_allocno_map
!= NULL
)
1893 if (ira_loop_nodes
[i
].parent
== NULL
)
1895 /* Don't remove the root. */
1896 ira_loop_nodes
[i
].to_remove_p
= false;
1899 ira_loop_nodes
[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\n",
1904 ira_loop_nodes
[i
].loop
->num
,
1905 ira_loop_nodes
[i
].loop
->header
->index
,
1906 ira_loop_nodes
[i
].loop
->header
->frequency
,
1907 loop_depth (ira_loop_nodes
[i
].loop
));
1911 /* Definition of vector of loop tree nodes. */
1912 DEF_VEC_P(ira_loop_tree_node_t
);
1913 DEF_VEC_ALLOC_P(ira_loop_tree_node_t
, heap
);
1915 /* Vec containing references to all removed loop tree nodes. */
1916 static VEC(ira_loop_tree_node_t
,heap
) *removed_loop_vec
;
1918 /* Vec containing references to all children of loop tree nodes. */
1919 static VEC(ira_loop_tree_node_t
,heap
) *children_vec
;
1921 /* Remove subregions of NODE if their separate allocation will not
1922 improve the result. */
1924 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_node_t node
)
1928 ira_loop_tree_node_t subnode
;
1930 remove_p
= node
->to_remove_p
;
1932 VEC_safe_push (ira_loop_tree_node_t
, heap
, children_vec
, node
);
1933 start
= VEC_length (ira_loop_tree_node_t
, children_vec
);
1934 for (subnode
= node
->children
; subnode
!= NULL
; subnode
= subnode
->next
)
1935 if (subnode
->bb
== NULL
)
1936 remove_uneccesary_loop_nodes_from_loop_tree (subnode
);
1938 VEC_safe_push (ira_loop_tree_node_t
, heap
, children_vec
, subnode
);
1939 node
->children
= node
->subloops
= NULL
;
1942 VEC_safe_push (ira_loop_tree_node_t
, heap
, removed_loop_vec
, node
);
1945 while (VEC_length (ira_loop_tree_node_t
, children_vec
) > start
)
1947 subnode
= VEC_pop (ira_loop_tree_node_t
, children_vec
);
1948 subnode
->parent
= node
;
1949 subnode
->next
= node
->children
;
1950 node
->children
= subnode
;
1951 if (subnode
->bb
== NULL
)
1953 subnode
->subloop_next
= node
->subloops
;
1954 node
->subloops
= subnode
;
1959 /* Return TRUE if NODE is inside PARENT. */
1961 loop_is_inside_p (ira_loop_tree_node_t node
, ira_loop_tree_node_t parent
)
1963 for (node
= node
->parent
; node
!= NULL
; node
= node
->parent
)
1969 /* Sort allocnos according to their order in regno allocno list. */
1971 regno_allocno_order_compare_func (const void *v1p
, const void *v2p
)
1973 ira_allocno_t a1
= *(const ira_allocno_t
*) v1p
;
1974 ira_allocno_t a2
= *(const ira_allocno_t
*) v2p
;
1975 ira_loop_tree_node_t n1
= ALLOCNO_LOOP_TREE_NODE (a1
);
1976 ira_loop_tree_node_t n2
= ALLOCNO_LOOP_TREE_NODE (a2
);
1978 if (loop_is_inside_p (n1
, n2
))
1980 else if (loop_is_inside_p (n2
, n1
))
1982 /* If allocnos are equally good, sort by allocno numbers, so that
1983 the results of qsort leave nothing to chance. We put allocnos
1984 with higher number first in the list because it is the original
1985 order for allocnos from loops on the same levels. */
1986 return ALLOCNO_NUM (a2
) - ALLOCNO_NUM (a1
);
1989 /* This array is used to sort allocnos to restore allocno order in
1990 the regno allocno list. */
1991 static ira_allocno_t
*regno_allocnos
;
1993 /* Restore allocno order for REGNO in the regno allocno list. */
1995 ira_rebuild_regno_allocno_list (int regno
)
2000 for (n
= 0, a
= ira_regno_allocno_map
[regno
];
2002 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
2003 regno_allocnos
[n
++] = a
;
2005 qsort (regno_allocnos
, n
, sizeof (ira_allocno_t
),
2006 regno_allocno_order_compare_func
);
2007 for (i
= 1; i
< n
; i
++)
2008 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos
[i
- 1]) = regno_allocnos
[i
];
2009 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos
[n
- 1]) = NULL
;
2010 ira_regno_allocno_map
[regno
] = regno_allocnos
[0];
2011 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
2012 fprintf (ira_dump_file
, " Rebuilding regno allocno list for %d\n", regno
);
2015 /* Propagate info from allocno FROM_A to allocno A. */
2017 propagate_some_info_from_allocno (ira_allocno_t a
, ira_allocno_t from_a
)
2019 enum reg_class aclass
;
2021 merge_hard_reg_conflicts (from_a
, a
, false);
2022 ALLOCNO_NREFS (a
) += ALLOCNO_NREFS (from_a
);
2023 ALLOCNO_FREQ (a
) += ALLOCNO_FREQ (from_a
);
2024 ALLOCNO_CALL_FREQ (a
) += ALLOCNO_CALL_FREQ (from_a
);
2025 ALLOCNO_CALLS_CROSSED_NUM (a
) += ALLOCNO_CALLS_CROSSED_NUM (from_a
);
2026 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
)
2027 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (from_a
);
2028 if (! ALLOCNO_BAD_SPILL_P (from_a
))
2029 ALLOCNO_BAD_SPILL_P (a
) = false;
2030 aclass
= ALLOCNO_CLASS (from_a
);
2031 ira_assert (aclass
== ALLOCNO_CLASS (a
));
2032 ira_allocate_and_accumulate_costs (&ALLOCNO_HARD_REG_COSTS (a
), aclass
,
2033 ALLOCNO_HARD_REG_COSTS (from_a
));
2034 ira_allocate_and_accumulate_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a
),
2036 ALLOCNO_CONFLICT_HARD_REG_COSTS (from_a
));
2037 ALLOCNO_CLASS_COST (a
) += ALLOCNO_CLASS_COST (from_a
);
2038 ALLOCNO_MEMORY_COST (a
) += ALLOCNO_MEMORY_COST (from_a
);
2041 /* Remove allocnos from loops removed from the allocation
2044 remove_unnecessary_allocnos (void)
2047 bool merged_p
, rebuild_p
;
2048 ira_allocno_t a
, prev_a
, next_a
, parent_a
;
2049 ira_loop_tree_node_t a_node
, parent
;
2052 regno_allocnos
= NULL
;
2053 for (regno
= max_reg_num () - 1; regno
>= FIRST_PSEUDO_REGISTER
; regno
--)
2056 for (prev_a
= NULL
, a
= ira_regno_allocno_map
[regno
];
2060 next_a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
);
2061 a_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2062 if (! a_node
->to_remove_p
)
2066 for (parent
= a_node
->parent
;
2067 (parent_a
= parent
->regno_allocno_map
[regno
]) == NULL
2068 && parent
->to_remove_p
;
2069 parent
= parent
->parent
)
2071 if (parent_a
== NULL
)
2073 /* There are no allocnos with the same regno in
2074 upper region -- just move the allocno to the
2077 ALLOCNO_LOOP_TREE_NODE (a
) = parent
;
2078 parent
->regno_allocno_map
[regno
] = a
;
2079 bitmap_set_bit (parent
->all_allocnos
, ALLOCNO_NUM (a
));
2084 /* Remove the allocno and update info of allocno in
2085 the upper region. */
2087 ira_regno_allocno_map
[regno
] = next_a
;
2089 ALLOCNO_NEXT_REGNO_ALLOCNO (prev_a
) = next_a
;
2090 move_allocno_live_ranges (a
, parent_a
);
2092 propagate_some_info_from_allocno (parent_a
, a
);
2093 /* Remove it from the corresponding regno allocno
2094 map to avoid info propagation of subsequent
2095 allocno into this already removed allocno. */
2096 a_node
->regno_allocno_map
[regno
] = NULL
;
2102 /* We need to restore the order in regno allocno list. */
2104 if (regno_allocnos
== NULL
)
2106 = (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
)
2107 * ira_allocnos_num
);
2108 ira_rebuild_regno_allocno_list (regno
);
2112 ira_rebuild_start_finish_chains ();
2113 if (regno_allocnos
!= NULL
)
2114 ira_free (regno_allocnos
);
2117 /* Remove allocnos from all loops but the root. */
2119 remove_low_level_allocnos (void)
2122 bool merged_p
, propagate_p
;
2123 ira_allocno_t a
, top_a
;
2124 ira_loop_tree_node_t a_node
, parent
;
2125 ira_allocno_iterator ai
;
2128 FOR_EACH_ALLOCNO (a
, ai
)
2130 a_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2131 if (a_node
== ira_loop_tree_root
|| ALLOCNO_CAP_MEMBER (a
) != NULL
)
2133 regno
= ALLOCNO_REGNO (a
);
2134 if ((top_a
= ira_loop_tree_root
->regno_allocno_map
[regno
]) == NULL
)
2136 ALLOCNO_LOOP_TREE_NODE (a
) = ira_loop_tree_root
;
2137 ira_loop_tree_root
->regno_allocno_map
[regno
] = a
;
2140 propagate_p
= a_node
->parent
->regno_allocno_map
[regno
] == NULL
;
2141 /* Remove the allocno and update info of allocno in the upper
2143 move_allocno_live_ranges (a
, top_a
);
2146 propagate_some_info_from_allocno (top_a
, a
);
2148 FOR_EACH_ALLOCNO (a
, ai
)
2150 a_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2151 if (a_node
== ira_loop_tree_root
)
2153 parent
= a_node
->parent
;
2154 regno
= ALLOCNO_REGNO (a
);
2155 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
2156 ira_assert (ALLOCNO_CAP (a
) != NULL
);
2157 else if (ALLOCNO_CAP (a
) == NULL
)
2158 ira_assert (parent
->regno_allocno_map
[regno
] != NULL
);
2160 FOR_EACH_ALLOCNO (a
, ai
)
2162 regno
= ALLOCNO_REGNO (a
);
2163 if (ira_loop_tree_root
->regno_allocno_map
[regno
] == a
)
2166 ira_allocno_object_iterator oi
;
2168 ira_regno_allocno_map
[regno
] = a
;
2169 ALLOCNO_NEXT_REGNO_ALLOCNO (a
) = NULL
;
2170 ALLOCNO_CAP_MEMBER (a
) = NULL
;
2171 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
2172 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj
),
2173 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
));
2175 if (ALLOCNO_TOTAL_NO_STACK_REG_P (a
))
2176 ALLOCNO_NO_STACK_REG_P (a
) = true;
2183 ira_rebuild_start_finish_chains ();
2186 /* Remove loops from consideration. We remove all loops except for
2187 root if ALL_P or loops for which a separate allocation will not
2188 improve the result. We have to do this after allocno creation and
2189 their costs and allocno class evaluation because only after that
2190 the register pressure can be known and is calculated. */
2192 remove_unnecessary_regions (bool all_p
)
2195 mark_all_loops_for_removal ();
2197 mark_loops_for_removal ();
2199 = VEC_alloc (ira_loop_tree_node_t
, heap
,
2200 last_basic_block
+ VEC_length (loop_p
, ira_loops
.larray
));
2202 = VEC_alloc (ira_loop_tree_node_t
, heap
,
2203 last_basic_block
+ VEC_length (loop_p
, ira_loops
.larray
));
2204 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_root
) ;
2205 VEC_free (ira_loop_tree_node_t
, heap
, children_vec
);
2207 remove_low_level_allocnos ();
2209 remove_unnecessary_allocnos ();
2210 while (VEC_length (ira_loop_tree_node_t
, removed_loop_vec
) > 0)
2211 finish_loop_tree_node (VEC_pop (ira_loop_tree_node_t
, removed_loop_vec
));
2212 VEC_free (ira_loop_tree_node_t
, heap
, removed_loop_vec
);
2217 /* At this point true value of allocno attribute bad_spill_p means
2218 that there is an insn where allocno occurs and where the allocno
2219 can not be used as memory. The function updates the attribute, now
2220 it can be true only for allocnos which can not be used as memory in
2221 an insn and in whose live ranges there is other allocno deaths.
2222 Spilling allocnos with true value will not improve the code because
2223 it will not make other allocnos colorable and additional reloads
2224 for the corresponding pseudo will be generated in reload pass for
2225 each insn it occurs.
2227 This is a trick mentioned in one classic article of Chaitin etc
2228 which is frequently omitted in other implementations of RA based on
2231 update_bad_spill_attribute (void)
2235 ira_allocno_iterator ai
;
2236 ira_allocno_object_iterator aoi
;
2239 enum reg_class aclass
;
2240 bitmap_head dead_points
[N_REG_CLASSES
];
2242 for (i
= 0; i
< ira_allocno_classes_num
; i
++)
2244 aclass
= ira_allocno_classes
[i
];
2245 bitmap_initialize (&dead_points
[aclass
], ®_obstack
);
2247 FOR_EACH_ALLOCNO (a
, ai
)
2249 aclass
= ALLOCNO_CLASS (a
);
2250 if (aclass
== NO_REGS
)
2252 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, aoi
)
2253 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
2254 bitmap_set_bit (&dead_points
[aclass
], r
->finish
);
2256 FOR_EACH_ALLOCNO (a
, ai
)
2258 aclass
= ALLOCNO_CLASS (a
);
2259 if (aclass
== NO_REGS
)
2261 if (! ALLOCNO_BAD_SPILL_P (a
))
2263 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, aoi
)
2265 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
2267 for (i
= r
->start
+ 1; i
< r
->finish
; i
++)
2268 if (bitmap_bit_p (&dead_points
[aclass
], i
))
2275 ALLOCNO_BAD_SPILL_P (a
) = false;
2280 for (i
= 0; i
< ira_allocno_classes_num
; i
++)
2282 aclass
= ira_allocno_classes
[i
];
2283 bitmap_clear (&dead_points
[aclass
]);
2289 /* Set up minimal and maximal live range points for allocnos. */
2291 setup_min_max_allocno_live_range_point (void)
2294 ira_allocno_t a
, parent_a
, cap
;
2295 ira_allocno_iterator ai
;
2296 #ifdef ENABLE_IRA_CHECKING
2297 ira_object_iterator oi
;
2301 ira_loop_tree_node_t parent
;
2303 FOR_EACH_ALLOCNO (a
, ai
)
2305 int n
= ALLOCNO_NUM_OBJECTS (a
);
2307 for (i
= 0; i
< n
; i
++)
2309 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
2310 r
= OBJECT_LIVE_RANGES (obj
);
2313 OBJECT_MAX (obj
) = r
->finish
;
2314 for (; r
->next
!= NULL
; r
= r
->next
)
2316 OBJECT_MIN (obj
) = r
->start
;
2319 for (i
= max_reg_num () - 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
2320 for (a
= ira_regno_allocno_map
[i
];
2322 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
2325 int n
= ALLOCNO_NUM_OBJECTS (a
);
2327 for (j
= 0; j
< n
; j
++)
2329 ira_object_t obj
= ALLOCNO_OBJECT (a
, j
);
2330 ira_object_t parent_obj
;
2332 if (OBJECT_MAX (obj
) < 0)
2334 ira_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
2335 /* Accumulation of range info. */
2336 if (ALLOCNO_CAP (a
) != NULL
)
2338 for (cap
= ALLOCNO_CAP (a
); cap
!= NULL
; cap
= ALLOCNO_CAP (cap
))
2340 ira_object_t cap_obj
= ALLOCNO_OBJECT (cap
, j
);
2341 if (OBJECT_MAX (cap_obj
) < OBJECT_MAX (obj
))
2342 OBJECT_MAX (cap_obj
) = OBJECT_MAX (obj
);
2343 if (OBJECT_MIN (cap_obj
) > OBJECT_MIN (obj
))
2344 OBJECT_MIN (cap_obj
) = OBJECT_MIN (obj
);
2348 if ((parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
) == NULL
)
2350 parent_a
= parent
->regno_allocno_map
[i
];
2351 parent_obj
= ALLOCNO_OBJECT (parent_a
, j
);
2352 if (OBJECT_MAX (parent_obj
) < OBJECT_MAX (obj
))
2353 OBJECT_MAX (parent_obj
) = OBJECT_MAX (obj
);
2354 if (OBJECT_MIN (parent_obj
) > OBJECT_MIN (obj
))
2355 OBJECT_MIN (parent_obj
) = OBJECT_MIN (obj
);
2358 #ifdef ENABLE_IRA_CHECKING
2359 FOR_EACH_OBJECT (obj
, oi
)
2361 if ((0 <= OBJECT_MIN (obj
) && OBJECT_MIN (obj
) <= ira_max_point
)
2362 && (0 <= OBJECT_MAX (obj
) && OBJECT_MAX (obj
) <= ira_max_point
))
2369 /* Sort allocnos according to their live ranges. Allocnos with
2370 smaller allocno class are put first unless we use priority
2371 coloring. Allocnos with the same class are ordered according
2372 their start (min). Allocnos with the same start are ordered
2373 according their finish (max). */
2375 object_range_compare_func (const void *v1p
, const void *v2p
)
2378 ira_object_t obj1
= *(const ira_object_t
*) v1p
;
2379 ira_object_t obj2
= *(const ira_object_t
*) v2p
;
2380 ira_allocno_t a1
= OBJECT_ALLOCNO (obj1
);
2381 ira_allocno_t a2
= OBJECT_ALLOCNO (obj2
);
2383 if ((diff
= OBJECT_MIN (obj1
) - OBJECT_MIN (obj2
)) != 0)
2385 if ((diff
= OBJECT_MAX (obj1
) - OBJECT_MAX (obj2
)) != 0)
2387 return ALLOCNO_NUM (a1
) - ALLOCNO_NUM (a2
);
2390 /* Sort ira_object_id_map and set up conflict id of allocnos. */
2392 sort_conflict_id_map (void)
2396 ira_allocno_iterator ai
;
2399 FOR_EACH_ALLOCNO (a
, ai
)
2401 ira_allocno_object_iterator oi
;
2404 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
2405 ira_object_id_map
[num
++] = obj
;
2407 qsort (ira_object_id_map
, num
, sizeof (ira_object_t
),
2408 object_range_compare_func
);
2409 for (i
= 0; i
< num
; i
++)
2411 ira_object_t obj
= ira_object_id_map
[i
];
2413 gcc_assert (obj
!= NULL
);
2414 OBJECT_CONFLICT_ID (obj
) = i
;
2416 for (i
= num
; i
< ira_objects_num
; i
++)
2417 ira_object_id_map
[i
] = NULL
;
2420 /* Set up minimal and maximal conflict ids of allocnos with which
2421 given allocno can conflict. */
2423 setup_min_max_conflict_allocno_ids (void)
2426 int i
, j
, min
, max
, start
, finish
, first_not_finished
, filled_area_start
;
2427 int *live_range_min
, *last_lived
;
2428 int word0_min
, word0_max
;
2430 ira_allocno_iterator ai
;
2432 live_range_min
= (int *) ira_allocate (sizeof (int) * ira_objects_num
);
2434 first_not_finished
= -1;
2435 for (i
= 0; i
< ira_objects_num
; i
++)
2437 ira_object_t obj
= ira_object_id_map
[i
];
2442 a
= OBJECT_ALLOCNO (obj
);
2446 aclass
= ALLOCNO_CLASS (a
);
2448 first_not_finished
= i
;
2452 start
= OBJECT_MIN (obj
);
2453 /* If we skip an allocno, the allocno with smaller ids will
2454 be also skipped because of the secondary sorting the
2455 range finishes (see function
2456 object_range_compare_func). */
2457 while (first_not_finished
< i
2458 && start
> OBJECT_MAX (ira_object_id_map
2459 [first_not_finished
]))
2460 first_not_finished
++;
2461 min
= first_not_finished
;
2464 /* We could increase min further in this case but it is good
2467 live_range_min
[i
] = OBJECT_MIN (obj
);
2468 OBJECT_MIN (obj
) = min
;
2470 last_lived
= (int *) ira_allocate (sizeof (int) * ira_max_point
);
2472 filled_area_start
= -1;
2473 for (i
= ira_objects_num
- 1; i
>= 0; i
--)
2475 ira_object_t obj
= ira_object_id_map
[i
];
2480 a
= OBJECT_ALLOCNO (obj
);
2483 aclass
= ALLOCNO_CLASS (a
);
2484 for (j
= 0; j
< ira_max_point
; j
++)
2486 filled_area_start
= ira_max_point
;
2488 min
= live_range_min
[i
];
2489 finish
= OBJECT_MAX (obj
);
2490 max
= last_lived
[finish
];
2492 /* We could decrease max further in this case but it is good
2494 max
= OBJECT_CONFLICT_ID (obj
) - 1;
2495 OBJECT_MAX (obj
) = max
;
2496 /* In filling, we can go further A range finish to recognize
2497 intersection quickly because if the finish of subsequently
2498 processed allocno (it has smaller conflict id) range is
2499 further A range finish than they are definitely intersected
2500 (the reason for this is the allocnos with bigger conflict id
2501 have their range starts not smaller than allocnos with
2503 for (j
= min
; j
< filled_area_start
; j
++)
2505 filled_area_start
= min
;
2507 ira_free (last_lived
);
2508 ira_free (live_range_min
);
2510 /* For allocnos with more than one object, we may later record extra conflicts in
2511 subobject 0 that we cannot really know about here.
2512 For now, simply widen the min/max range of these subobjects. */
2514 word0_min
= INT_MAX
;
2515 word0_max
= INT_MIN
;
2517 FOR_EACH_ALLOCNO (a
, ai
)
2519 int n
= ALLOCNO_NUM_OBJECTS (a
);
2524 obj0
= ALLOCNO_OBJECT (a
, 0);
2525 if (OBJECT_CONFLICT_ID (obj0
) < word0_min
)
2526 word0_min
= OBJECT_CONFLICT_ID (obj0
);
2527 if (OBJECT_CONFLICT_ID (obj0
) > word0_max
)
2528 word0_max
= OBJECT_CONFLICT_ID (obj0
);
2530 FOR_EACH_ALLOCNO (a
, ai
)
2532 int n
= ALLOCNO_NUM_OBJECTS (a
);
2537 obj0
= ALLOCNO_OBJECT (a
, 0);
2538 if (OBJECT_MIN (obj0
) > word0_min
)
2539 OBJECT_MIN (obj0
) = word0_min
;
2540 if (OBJECT_MAX (obj0
) < word0_max
)
2541 OBJECT_MAX (obj0
) = word0_max
;
2551 ira_allocno_iterator ai
;
2552 ira_loop_tree_node_t loop_tree_node
;
2554 FOR_EACH_ALLOCNO (a
, ai
)
2556 if (ALLOCNO_LOOP_TREE_NODE (a
) == ira_loop_tree_root
)
2558 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
2559 create_cap_allocno (a
);
2560 else if (ALLOCNO_CAP (a
) == NULL
)
2562 loop_tree_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2563 if (!bitmap_bit_p (loop_tree_node
->border_allocnos
, ALLOCNO_NUM (a
)))
2564 create_cap_allocno (a
);
2571 /* The page contains code transforming more one region internal
2572 representation (IR) to one region IR which is necessary for reload.
2573 This transformation is called IR flattening. We might just rebuild
2574 the IR for one region but we don't do it because it takes a lot of
2577 /* Map: regno -> allocnos which will finally represent the regno for
2578 IR with one region. */
2579 static ira_allocno_t
*regno_top_level_allocno_map
;
2581 /* Find the allocno that corresponds to A at a level one higher up in the
2582 loop tree. Returns NULL if A is a cap, or if it has no parent. */
2584 ira_parent_allocno (ira_allocno_t a
)
2586 ira_loop_tree_node_t parent
;
2588 if (ALLOCNO_CAP (a
) != NULL
)
2591 parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
;
2595 return parent
->regno_allocno_map
[ALLOCNO_REGNO (a
)];
2598 /* Find the allocno that corresponds to A at a level one higher up in the
2599 loop tree. If ALLOCNO_CAP is set for A, return that. */
2601 ira_parent_or_cap_allocno (ira_allocno_t a
)
2603 if (ALLOCNO_CAP (a
) != NULL
)
2604 return ALLOCNO_CAP (a
);
2606 return ira_parent_allocno (a
);
2609 /* Process all allocnos originated from pseudo REGNO and copy live
2610 ranges, hard reg conflicts, and allocno stack reg attributes from
2611 low level allocnos to final allocnos which are destinations of
2612 removed stores at a loop exit. Return true if we copied live
2615 copy_info_to_removed_store_destinations (int regno
)
2618 ira_allocno_t parent_a
= NULL
;
2619 ira_loop_tree_node_t parent
;
2623 for (a
= ira_regno_allocno_map
[regno
];
2625 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
2627 if (a
!= regno_top_level_allocno_map
[REGNO (allocno_emit_reg (a
))])
2628 /* This allocno will be removed. */
2631 /* Caps will be removed. */
2632 ira_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
2633 for (parent
= ALLOCNO_LOOP_TREE_NODE (a
)->parent
;
2635 parent
= parent
->parent
)
2636 if ((parent_a
= parent
->regno_allocno_map
[regno
]) == NULL
2638 == regno_top_level_allocno_map
[REGNO
2639 (allocno_emit_reg (parent_a
))]
2640 && ALLOCNO_EMIT_DATA (parent_a
)->mem_optimized_dest_p
))
2642 if (parent
== NULL
|| parent_a
== NULL
)
2645 copy_allocno_live_ranges (a
, parent_a
);
2646 merge_hard_reg_conflicts (a
, parent_a
, true);
2648 ALLOCNO_CALL_FREQ (parent_a
) += ALLOCNO_CALL_FREQ (a
);
2649 ALLOCNO_CALLS_CROSSED_NUM (parent_a
)
2650 += ALLOCNO_CALLS_CROSSED_NUM (a
);
2651 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a
)
2652 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
);
2658 /* Flatten the IR. In other words, this function transforms IR as if
2659 it were built with one region (without loops). We could make it
2660 much simpler by rebuilding IR with one region, but unfortunately it
2661 takes a lot of time. MAX_REGNO_BEFORE_EMIT and
2662 IRA_MAX_POINT_BEFORE_EMIT are correspondingly MAX_REG_NUM () and
2663 IRA_MAX_POINT before emitting insns on the loop borders. */
2665 ira_flattening (int max_regno_before_emit
, int ira_max_point_before_emit
)
2670 bool new_pseudos_p
, merged_p
, mem_dest_p
;
2672 enum reg_class aclass
;
2673 ira_allocno_t a
, parent_a
, first
, second
, node_first
, node_second
;
2675 ira_loop_tree_node_t node
;
2677 ira_allocno_iterator ai
;
2678 ira_copy_iterator ci
;
2680 regno_top_level_allocno_map
2681 = (ira_allocno_t
*) ira_allocate (max_reg_num ()
2682 * sizeof (ira_allocno_t
));
2683 memset (regno_top_level_allocno_map
, 0,
2684 max_reg_num () * sizeof (ira_allocno_t
));
2685 new_pseudos_p
= merged_p
= false;
2686 FOR_EACH_ALLOCNO (a
, ai
)
2688 ira_allocno_object_iterator oi
;
2691 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
2692 /* Caps are not in the regno allocno maps and they are never
2693 will be transformed into allocnos existing after IR
2696 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
2697 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
),
2698 OBJECT_CONFLICT_HARD_REGS (obj
));
2700 ALLOCNO_TOTAL_NO_STACK_REG_P (a
) = ALLOCNO_NO_STACK_REG_P (a
);
2703 /* Fix final allocno attributes. */
2704 for (i
= max_regno_before_emit
- 1; i
>= FIRST_PSEUDO_REGISTER
; i
--)
2707 for (a
= ira_regno_allocno_map
[i
];
2709 a
= ALLOCNO_NEXT_REGNO_ALLOCNO (a
))
2711 ira_emit_data_t parent_data
, data
= ALLOCNO_EMIT_DATA (a
);
2713 ira_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
2714 if (data
->somewhere_renamed_p
)
2715 new_pseudos_p
= true;
2716 parent_a
= ira_parent_allocno (a
);
2717 if (parent_a
== NULL
)
2719 ALLOCNO_COPIES (a
) = NULL
;
2720 regno_top_level_allocno_map
[REGNO (data
->reg
)] = a
;
2723 ira_assert (ALLOCNO_CAP_MEMBER (parent_a
) == NULL
);
2725 if (data
->mem_optimized_dest
!= NULL
)
2727 parent_data
= ALLOCNO_EMIT_DATA (parent_a
);
2728 if (REGNO (data
->reg
) == REGNO (parent_data
->reg
))
2730 merge_hard_reg_conflicts (a
, parent_a
, true);
2731 move_allocno_live_ranges (a
, parent_a
);
2733 parent_data
->mem_optimized_dest_p
2734 = (parent_data
->mem_optimized_dest_p
2735 || data
->mem_optimized_dest_p
);
2738 new_pseudos_p
= true;
2741 ALLOCNO_NREFS (parent_a
) -= ALLOCNO_NREFS (a
);
2742 ALLOCNO_FREQ (parent_a
) -= ALLOCNO_FREQ (a
);
2743 ALLOCNO_CALL_FREQ (parent_a
) -= ALLOCNO_CALL_FREQ (a
);
2744 ALLOCNO_CALLS_CROSSED_NUM (parent_a
)
2745 -= ALLOCNO_CALLS_CROSSED_NUM (a
);
2746 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a
)
2747 -= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
);
2748 ira_assert (ALLOCNO_CALLS_CROSSED_NUM (parent_a
) >= 0
2749 && ALLOCNO_NREFS (parent_a
) >= 0
2750 && ALLOCNO_FREQ (parent_a
) >= 0);
2751 aclass
= ALLOCNO_CLASS (parent_a
);
2752 hard_regs_num
= ira_class_hard_regs_num
[aclass
];
2753 if (ALLOCNO_HARD_REG_COSTS (a
) != NULL
2754 && ALLOCNO_HARD_REG_COSTS (parent_a
) != NULL
)
2755 for (j
= 0; j
< hard_regs_num
; j
++)
2756 ALLOCNO_HARD_REG_COSTS (parent_a
)[j
]
2757 -= ALLOCNO_HARD_REG_COSTS (a
)[j
];
2758 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a
) != NULL
2759 && ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a
) != NULL
)
2760 for (j
= 0; j
< hard_regs_num
; j
++)
2761 ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a
)[j
]
2762 -= ALLOCNO_CONFLICT_HARD_REG_COSTS (a
)[j
];
2763 ALLOCNO_CLASS_COST (parent_a
)
2764 -= ALLOCNO_CLASS_COST (a
);
2765 ALLOCNO_MEMORY_COST (parent_a
) -= ALLOCNO_MEMORY_COST (a
);
2766 parent_a
= ira_parent_allocno (parent_a
);
2767 if (parent_a
== NULL
)
2770 ALLOCNO_COPIES (a
) = NULL
;
2771 regno_top_level_allocno_map
[REGNO (data
->reg
)] = a
;
2773 if (mem_dest_p
&& copy_info_to_removed_store_destinations (i
))
2776 ira_assert (new_pseudos_p
|| ira_max_point_before_emit
== ira_max_point
);
2777 if (merged_p
|| ira_max_point_before_emit
!= ira_max_point
)
2778 ira_rebuild_start_finish_chains ();
2781 sparseset objects_live
;
2783 /* Rebuild conflicts. */
2784 FOR_EACH_ALLOCNO (a
, ai
)
2786 ira_allocno_object_iterator oi
;
2789 if (a
!= regno_top_level_allocno_map
[REGNO (allocno_emit_reg (a
))]
2790 || ALLOCNO_CAP_MEMBER (a
) != NULL
)
2792 FOR_EACH_ALLOCNO_OBJECT (a
, obj
, oi
)
2794 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
2795 ira_assert (r
->object
== obj
);
2796 clear_conflicts (obj
);
2799 objects_live
= sparseset_alloc (ira_objects_num
);
2800 for (i
= 0; i
< ira_max_point
; i
++)
2802 for (r
= ira_start_point_ranges
[i
]; r
!= NULL
; r
= r
->start_next
)
2804 ira_object_t obj
= r
->object
;
2806 a
= OBJECT_ALLOCNO (obj
);
2807 if (a
!= regno_top_level_allocno_map
[REGNO (allocno_emit_reg (a
))]
2808 || ALLOCNO_CAP_MEMBER (a
) != NULL
)
2811 aclass
= ALLOCNO_CLASS (a
);
2812 sparseset_set_bit (objects_live
, OBJECT_CONFLICT_ID (obj
));
2813 EXECUTE_IF_SET_IN_SPARSESET (objects_live
, n
)
2815 ira_object_t live_obj
= ira_object_id_map
[n
];
2816 ira_allocno_t live_a
= OBJECT_ALLOCNO (live_obj
);
2817 enum reg_class live_aclass
= ALLOCNO_CLASS (live_a
);
2819 if (ira_reg_classes_intersect_p
[aclass
][live_aclass
]
2820 /* Don't set up conflict for the allocno with itself. */
2822 ira_add_conflict (obj
, live_obj
);
2826 for (r
= ira_finish_point_ranges
[i
]; r
!= NULL
; r
= r
->finish_next
)
2827 sparseset_clear_bit (objects_live
, OBJECT_CONFLICT_ID (r
->object
));
2829 sparseset_free (objects_live
);
2830 compress_conflict_vecs ();
2832 /* Mark some copies for removing and change allocnos in the rest
2834 FOR_EACH_COPY (cp
, ci
)
2836 if (ALLOCNO_CAP_MEMBER (cp
->first
) != NULL
2837 || ALLOCNO_CAP_MEMBER (cp
->second
) != NULL
)
2839 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
2841 (ira_dump_file
, " Remove cp%d:%c%dr%d-%c%dr%d\n",
2842 cp
->num
, ALLOCNO_CAP_MEMBER (cp
->first
) != NULL
? 'c' : 'a',
2843 ALLOCNO_NUM (cp
->first
),
2844 REGNO (allocno_emit_reg (cp
->first
)),
2845 ALLOCNO_CAP_MEMBER (cp
->second
) != NULL
? 'c' : 'a',
2846 ALLOCNO_NUM (cp
->second
),
2847 REGNO (allocno_emit_reg (cp
->second
)));
2848 cp
->loop_tree_node
= NULL
;
2852 = regno_top_level_allocno_map
[REGNO (allocno_emit_reg (cp
->first
))];
2854 = regno_top_level_allocno_map
[REGNO (allocno_emit_reg (cp
->second
))];
2855 node
= cp
->loop_tree_node
;
2857 keep_p
= true; /* It copy generated in ira-emit.c. */
2860 /* Check that the copy was not propagated from level on
2861 which we will have different pseudos. */
2862 node_first
= node
->regno_allocno_map
[ALLOCNO_REGNO (cp
->first
)];
2863 node_second
= node
->regno_allocno_map
[ALLOCNO_REGNO (cp
->second
)];
2864 keep_p
= ((REGNO (allocno_emit_reg (first
))
2865 == REGNO (allocno_emit_reg (node_first
)))
2866 && (REGNO (allocno_emit_reg (second
))
2867 == REGNO (allocno_emit_reg (node_second
))));
2871 cp
->loop_tree_node
= ira_loop_tree_root
;
2873 cp
->second
= second
;
2877 cp
->loop_tree_node
= NULL
;
2878 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
2879 fprintf (ira_dump_file
, " Remove cp%d:a%dr%d-a%dr%d\n",
2880 cp
->num
, ALLOCNO_NUM (cp
->first
),
2881 REGNO (allocno_emit_reg (cp
->first
)),
2882 ALLOCNO_NUM (cp
->second
),
2883 REGNO (allocno_emit_reg (cp
->second
)));
2886 /* Remove unnecessary allocnos on lower levels of the loop tree. */
2887 FOR_EACH_ALLOCNO (a
, ai
)
2889 if (a
!= regno_top_level_allocno_map
[REGNO (allocno_emit_reg (a
))]
2890 || ALLOCNO_CAP_MEMBER (a
) != NULL
)
2892 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
2893 fprintf (ira_dump_file
, " Remove a%dr%d\n",
2894 ALLOCNO_NUM (a
), REGNO (allocno_emit_reg (a
)));
2898 ALLOCNO_LOOP_TREE_NODE (a
) = ira_loop_tree_root
;
2899 ALLOCNO_REGNO (a
) = REGNO (allocno_emit_reg (a
));
2900 ALLOCNO_CAP (a
) = NULL
;
2901 /* Restore updated costs for assignments from reload. */
2902 ALLOCNO_UPDATED_MEMORY_COST (a
) = ALLOCNO_MEMORY_COST (a
);
2903 ALLOCNO_UPDATED_CLASS_COST (a
) = ALLOCNO_CLASS_COST (a
);
2904 if (! ALLOCNO_ASSIGNED_P (a
))
2905 ira_free_allocno_updated_costs (a
);
2906 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
);
2907 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) == NULL
);
2909 /* Remove unnecessary copies. */
2910 FOR_EACH_COPY (cp
, ci
)
2912 if (cp
->loop_tree_node
== NULL
)
2914 ira_copies
[cp
->num
] = NULL
;
2919 (ALLOCNO_LOOP_TREE_NODE (cp
->first
) == ira_loop_tree_root
2920 && ALLOCNO_LOOP_TREE_NODE (cp
->second
) == ira_loop_tree_root
);
2921 ira_add_allocno_copy_to_list (cp
);
2922 ira_swap_allocno_copy_ends_if_necessary (cp
);
2924 rebuild_regno_allocno_maps ();
2925 if (ira_max_point
!= ira_max_point_before_emit
)
2926 ira_compress_allocno_live_ranges ();
2927 ira_free (regno_top_level_allocno_map
);
2932 #ifdef ENABLE_IRA_CHECKING
2933 /* Check creation of all allocnos. Allocnos on lower levels should
2934 have allocnos or caps on all upper levels. */
2936 check_allocno_creation (void)
2939 ira_allocno_iterator ai
;
2940 ira_loop_tree_node_t loop_tree_node
;
2942 FOR_EACH_ALLOCNO (a
, ai
)
2944 loop_tree_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2945 ira_assert (bitmap_bit_p (loop_tree_node
->all_allocnos
,
2947 if (loop_tree_node
== ira_loop_tree_root
)
2949 if (ALLOCNO_CAP_MEMBER (a
) != NULL
)
2950 ira_assert (ALLOCNO_CAP (a
) != NULL
);
2951 else if (ALLOCNO_CAP (a
) == NULL
)
2952 ira_assert (loop_tree_node
->parent
2953 ->regno_allocno_map
[ALLOCNO_REGNO (a
)] != NULL
2954 && bitmap_bit_p (loop_tree_node
->border_allocnos
,
2960 /* Identify allocnos which prefer a register class with a single hard register.
2961 Adjust ALLOCNO_CONFLICT_HARD_REG_COSTS so that conflicting allocnos are
2962 less likely to use the preferred singleton register. */
2964 update_conflict_hard_reg_costs (void)
2967 ira_allocno_iterator ai
;
2970 FOR_EACH_ALLOCNO (a
, ai
)
2972 reg_class_t aclass
= ALLOCNO_CLASS (a
);
2973 reg_class_t pref
= reg_preferred_class (ALLOCNO_REGNO (a
));
2975 if (reg_class_size
[(int) pref
] != 1)
2977 index
= ira_class_hard_reg_index
[(int) aclass
]
2978 [ira_class_hard_regs
[(int) pref
][0]];
2981 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a
) == NULL
2982 || ALLOCNO_HARD_REG_COSTS (a
) == NULL
)
2985 for (i
= ira_class_hard_regs_num
[(int) aclass
] - 1; i
>= 0; i
--)
2986 if (ALLOCNO_HARD_REG_COSTS (a
)[i
] > ALLOCNO_CLASS_COST (a
)
2987 && min
> ALLOCNO_HARD_REG_COSTS (a
)[i
])
2988 min
= ALLOCNO_HARD_REG_COSTS (a
)[i
];
2991 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a
),
2993 ALLOCNO_CONFLICT_HARD_REG_COSTS (a
)[index
]
2994 -= min
- ALLOCNO_CLASS_COST (a
);
2998 /* Create a internal representation (IR) for IRA (allocnos, copies,
2999 loop tree nodes). If LOOPS_P is FALSE the nodes corresponding to
3000 the loops (except the root which corresponds the all function) and
3001 correspondingly allocnos for the loops will be not created. Such
3002 parameter value is used for Chaitin-Briggs coloring. The function
3003 returns TRUE if we generate loop structure (besides nodes
3004 representing all function and the basic blocks) for regional
3005 allocation. A true return means that we really need to flatten IR
3006 before the reload. */
3008 ira_build (bool loops_p
)
3012 initiate_cost_vectors ();
3013 initiate_allocnos ();
3015 create_loop_tree_nodes (loops_p
);
3019 create_allocno_objects ();
3020 ira_create_allocno_live_ranges ();
3021 remove_unnecessary_regions (false);
3022 ira_compress_allocno_live_ranges ();
3023 update_bad_spill_attribute ();
3024 loops_p
= more_one_region_p ();
3027 propagate_allocno_info ();
3030 ira_tune_allocno_costs ();
3031 #ifdef ENABLE_IRA_CHECKING
3032 check_allocno_creation ();
3034 setup_min_max_allocno_live_range_point ();
3035 sort_conflict_id_map ();
3036 setup_min_max_conflict_allocno_ids ();
3037 ira_build_conflicts ();
3038 update_conflict_hard_reg_costs ();
3039 if (! ira_conflicts_p
)
3042 ira_allocno_iterator ai
;
3044 /* Remove all regions but root one. */
3047 remove_unnecessary_regions (true);
3050 /* We don't save hard registers around calls for fast allocation
3051 -- add caller clobbered registers as conflicting ones to
3052 allocno crossing calls. */
3053 FOR_EACH_ALLOCNO (a
, ai
)
3054 if (ALLOCNO_CALLS_CROSSED_NUM (a
) != 0)
3055 ior_hard_reg_conflicts (a
, &call_used_reg_set
);
3057 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
3058 print_copies (ira_dump_file
);
3059 if (internal_flag_ira_verbose
> 0 && ira_dump_file
!= NULL
)
3064 ira_allocno_iterator ai
;
3069 FOR_EACH_ALLOCNO (a
, ai
)
3071 int j
, nobj
= ALLOCNO_NUM_OBJECTS (a
);
3075 for (j
= 0; j
< nobj
; j
++)
3077 ira_object_t obj
= ALLOCNO_OBJECT (a
, j
);
3078 n
+= OBJECT_NUM_CONFLICTS (obj
);
3079 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
3083 fprintf (ira_dump_file
, " regions=%d, blocks=%d, points=%d\n",
3084 VEC_length (loop_p
, ira_loops
.larray
), n_basic_blocks
,
3086 fprintf (ira_dump_file
,
3087 " allocnos=%d (big %d), copies=%d, conflicts=%d, ranges=%d\n",
3088 ira_allocnos_num
, nr_big
, ira_copies_num
, n
, nr
);
3093 /* Release the data created by function ira_build. */
3097 finish_loop_tree_nodes ();
3100 finish_cost_vectors ();
3101 ira_finish_allocno_live_ranges ();