Change use to type-based pool allocator in ira-build.c.
[official-gcc.git] / gcc / ira-build.c
blob2de7d34a29223c0b74562fafcf6077b45740fe1d
1 /* Building internal representation for IRA.
2 Copyright (C) 2006-2015 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "target.h"
28 #include "regs.h"
29 #include "flags.h"
30 #include "hard-reg-set.h"
31 #include "predict.h"
32 #include "vec.h"
33 #include "hashtab.h"
34 #include "hash-set.h"
35 #include "machmode.h"
36 #include "input.h"
37 #include "function.h"
38 #include "dominance.h"
39 #include "cfg.h"
40 #include "basic-block.h"
41 #include "insn-config.h"
42 #include "recog.h"
43 #include "diagnostic-core.h"
44 #include "params.h"
45 #include "df.h"
46 #include "reload.h"
47 #include "sparseset.h"
48 #include "ira-int.h"
49 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
51 static ira_copy_t find_allocno_copy (ira_allocno_t, ira_allocno_t, rtx_insn *,
52 ira_loop_tree_node_t);
54 /* The root of the loop tree corresponding to the all function. */
55 ira_loop_tree_node_t ira_loop_tree_root;
57 /* Height of the loop tree. */
58 int ira_loop_tree_height;
60 /* All nodes representing basic blocks are referred through the
61 following array. We can not use basic block member `aux' for this
62 because it is used for insertion of insns on edges. */
63 ira_loop_tree_node_t ira_bb_nodes;
65 /* All nodes representing loops are referred through the following
66 array. */
67 ira_loop_tree_node_t ira_loop_nodes;
69 /* And size of the ira_loop_nodes array. */
70 unsigned int ira_loop_nodes_count;
72 /* Map regno -> allocnos with given regno (see comments for
73 allocno member `next_regno_allocno'). */
74 ira_allocno_t *ira_regno_allocno_map;
76 /* Array of references to all allocnos. The order number of the
77 allocno corresponds to the index in the array. Removed allocnos
78 have NULL element value. */
79 ira_allocno_t *ira_allocnos;
81 /* Sizes of the previous array. */
82 int ira_allocnos_num;
84 /* Count of conflict record structures we've created, used when creating
85 a new conflict id. */
86 int ira_objects_num;
88 /* Map a conflict id to its conflict record. */
89 ira_object_t *ira_object_id_map;
91 /* Array of references to all allocno preferences. The order number
92 of the preference corresponds to the index in the array. */
93 ira_pref_t *ira_prefs;
95 /* Size of the previous array. */
96 int ira_prefs_num;
98 /* Array of references to all copies. The order number of the copy
99 corresponds to the index in the array. Removed copies have NULL
100 element value. */
101 ira_copy_t *ira_copies;
103 /* Size of the previous array. */
104 int ira_copies_num;
108 /* LAST_BASIC_BLOCK before generating additional insns because of live
109 range splitting. Emitting insns on a critical edge creates a new
110 basic block. */
111 static int last_basic_block_before_change;
113 /* Initialize some members in loop tree node NODE. Use LOOP_NUM for
114 the member loop_num. */
115 static void
116 init_loop_tree_node (struct ira_loop_tree_node *node, int loop_num)
118 int max_regno = max_reg_num ();
120 node->regno_allocno_map
121 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t) * max_regno);
122 memset (node->regno_allocno_map, 0, sizeof (ira_allocno_t) * max_regno);
123 memset (node->reg_pressure, 0, sizeof (node->reg_pressure));
124 node->all_allocnos = ira_allocate_bitmap ();
125 node->modified_regnos = ira_allocate_bitmap ();
126 node->border_allocnos = ira_allocate_bitmap ();
127 node->local_copies = ira_allocate_bitmap ();
128 node->loop_num = loop_num;
129 node->children = NULL;
130 node->subloops = NULL;
134 /* The following function allocates the loop tree nodes. If
135 CURRENT_LOOPS is NULL, the nodes corresponding to the loops (except
136 the root which corresponds the all function) will be not allocated
137 but nodes will still be allocated for basic blocks. */
138 static void
139 create_loop_tree_nodes (void)
141 unsigned int i, j;
142 bool skip_p;
143 edge_iterator ei;
144 edge e;
145 vec<edge> edges;
146 loop_p loop;
148 ira_bb_nodes
149 = ((struct ira_loop_tree_node *)
150 ira_allocate (sizeof (struct ira_loop_tree_node)
151 * last_basic_block_for_fn (cfun)));
152 last_basic_block_before_change = last_basic_block_for_fn (cfun);
153 for (i = 0; i < (unsigned int) last_basic_block_for_fn (cfun); i++)
155 ira_bb_nodes[i].regno_allocno_map = NULL;
156 memset (ira_bb_nodes[i].reg_pressure, 0,
157 sizeof (ira_bb_nodes[i].reg_pressure));
158 ira_bb_nodes[i].all_allocnos = NULL;
159 ira_bb_nodes[i].modified_regnos = NULL;
160 ira_bb_nodes[i].border_allocnos = NULL;
161 ira_bb_nodes[i].local_copies = NULL;
163 if (current_loops == NULL)
165 ira_loop_nodes_count = 1;
166 ira_loop_nodes = ((struct ira_loop_tree_node *)
167 ira_allocate (sizeof (struct ira_loop_tree_node)));
168 init_loop_tree_node (ira_loop_nodes, 0);
169 return;
171 ira_loop_nodes_count = number_of_loops (cfun);
172 ira_loop_nodes = ((struct ira_loop_tree_node *)
173 ira_allocate (sizeof (struct ira_loop_tree_node)
174 * ira_loop_nodes_count));
175 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
177 if (loop_outer (loop) != NULL)
179 ira_loop_nodes[i].regno_allocno_map = NULL;
180 skip_p = false;
181 FOR_EACH_EDGE (e, ei, loop->header->preds)
182 if (e->src != loop->latch
183 && (e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
185 skip_p = true;
186 break;
188 if (skip_p)
189 continue;
190 edges = get_loop_exit_edges (loop);
191 FOR_EACH_VEC_ELT (edges, j, e)
192 if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
194 skip_p = true;
195 break;
197 edges.release ();
198 if (skip_p)
199 continue;
201 init_loop_tree_node (&ira_loop_nodes[i], loop->num);
205 /* The function returns TRUE if there are more one allocation
206 region. */
207 static bool
208 more_one_region_p (void)
210 unsigned int i;
211 loop_p loop;
213 if (current_loops != NULL)
214 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
215 if (ira_loop_nodes[i].regno_allocno_map != NULL
216 && ira_loop_tree_root != &ira_loop_nodes[i])
217 return true;
218 return false;
221 /* Free the loop tree node of a loop. */
222 static void
223 finish_loop_tree_node (ira_loop_tree_node_t loop)
225 if (loop->regno_allocno_map != NULL)
227 ira_assert (loop->bb == NULL);
228 ira_free_bitmap (loop->local_copies);
229 ira_free_bitmap (loop->border_allocnos);
230 ira_free_bitmap (loop->modified_regnos);
231 ira_free_bitmap (loop->all_allocnos);
232 ira_free (loop->regno_allocno_map);
233 loop->regno_allocno_map = NULL;
237 /* Free the loop tree nodes. */
238 static void
239 finish_loop_tree_nodes (void)
241 unsigned int i;
243 for (i = 0; i < ira_loop_nodes_count; i++)
244 finish_loop_tree_node (&ira_loop_nodes[i]);
245 ira_free (ira_loop_nodes);
246 for (i = 0; i < (unsigned int) last_basic_block_before_change; i++)
248 if (ira_bb_nodes[i].local_copies != NULL)
249 ira_free_bitmap (ira_bb_nodes[i].local_copies);
250 if (ira_bb_nodes[i].border_allocnos != NULL)
251 ira_free_bitmap (ira_bb_nodes[i].border_allocnos);
252 if (ira_bb_nodes[i].modified_regnos != NULL)
253 ira_free_bitmap (ira_bb_nodes[i].modified_regnos);
254 if (ira_bb_nodes[i].all_allocnos != NULL)
255 ira_free_bitmap (ira_bb_nodes[i].all_allocnos);
256 if (ira_bb_nodes[i].regno_allocno_map != NULL)
257 ira_free (ira_bb_nodes[i].regno_allocno_map);
259 ira_free (ira_bb_nodes);
264 /* The following recursive function adds LOOP to the loop tree
265 hierarchy. LOOP is added only once. If LOOP is NULL we adding
266 loop designating the whole function when CFG loops are not
267 built. */
268 static void
269 add_loop_to_tree (struct loop *loop)
271 int loop_num;
272 struct loop *parent;
273 ira_loop_tree_node_t loop_node, parent_node;
275 /* We can not use loop node access macros here because of potential
276 checking and because the nodes are not initialized enough
277 yet. */
278 if (loop != NULL && loop_outer (loop) != NULL)
279 add_loop_to_tree (loop_outer (loop));
280 loop_num = loop != NULL ? loop->num : 0;
281 if (ira_loop_nodes[loop_num].regno_allocno_map != NULL
282 && ira_loop_nodes[loop_num].children == NULL)
284 /* We have not added loop node to the tree yet. */
285 loop_node = &ira_loop_nodes[loop_num];
286 loop_node->loop = loop;
287 loop_node->bb = NULL;
288 if (loop == NULL)
289 parent = NULL;
290 else
292 for (parent = loop_outer (loop);
293 parent != NULL;
294 parent = loop_outer (parent))
295 if (ira_loop_nodes[parent->num].regno_allocno_map != NULL)
296 break;
298 if (parent == NULL)
300 loop_node->next = NULL;
301 loop_node->subloop_next = NULL;
302 loop_node->parent = NULL;
304 else
306 parent_node = &ira_loop_nodes[parent->num];
307 loop_node->next = parent_node->children;
308 parent_node->children = loop_node;
309 loop_node->subloop_next = parent_node->subloops;
310 parent_node->subloops = loop_node;
311 loop_node->parent = parent_node;
316 /* The following recursive function sets up levels of nodes of the
317 tree given its root LOOP_NODE. The enumeration starts with LEVEL.
318 The function returns maximal value of level in the tree + 1. */
319 static int
320 setup_loop_tree_level (ira_loop_tree_node_t loop_node, int level)
322 int height, max_height;
323 ira_loop_tree_node_t subloop_node;
325 ira_assert (loop_node->bb == NULL);
326 loop_node->level = level;
327 max_height = level + 1;
328 for (subloop_node = loop_node->subloops;
329 subloop_node != NULL;
330 subloop_node = subloop_node->subloop_next)
332 ira_assert (subloop_node->bb == NULL);
333 height = setup_loop_tree_level (subloop_node, level + 1);
334 if (height > max_height)
335 max_height = height;
337 return max_height;
340 /* Create the loop tree. The algorithm is designed to provide correct
341 order of loops (they are ordered by their last loop BB) and basic
342 blocks in the chain formed by member next. */
343 static void
344 form_loop_tree (void)
346 basic_block bb;
347 struct loop *parent;
348 ira_loop_tree_node_t bb_node, loop_node;
350 /* We can not use loop/bb node access macros because of potential
351 checking and because the nodes are not initialized enough
352 yet. */
353 FOR_EACH_BB_FN (bb, cfun)
355 bb_node = &ira_bb_nodes[bb->index];
356 bb_node->bb = bb;
357 bb_node->loop = NULL;
358 bb_node->subloops = NULL;
359 bb_node->children = NULL;
360 bb_node->subloop_next = NULL;
361 bb_node->next = NULL;
362 if (current_loops == NULL)
363 parent = NULL;
364 else
366 for (parent = bb->loop_father;
367 parent != NULL;
368 parent = loop_outer (parent))
369 if (ira_loop_nodes[parent->num].regno_allocno_map != NULL)
370 break;
372 add_loop_to_tree (parent);
373 loop_node = &ira_loop_nodes[parent == NULL ? 0 : parent->num];
374 bb_node->next = loop_node->children;
375 bb_node->parent = loop_node;
376 loop_node->children = bb_node;
378 ira_loop_tree_root = IRA_LOOP_NODE_BY_INDEX (0);
379 ira_loop_tree_height = setup_loop_tree_level (ira_loop_tree_root, 0);
380 ira_assert (ira_loop_tree_root->regno_allocno_map != NULL);
385 /* Rebuild IRA_REGNO_ALLOCNO_MAP and REGNO_ALLOCNO_MAPs of the loop
386 tree nodes. */
387 static void
388 rebuild_regno_allocno_maps (void)
390 unsigned int l;
391 int max_regno, regno;
392 ira_allocno_t a;
393 ira_loop_tree_node_t loop_tree_node;
394 loop_p loop;
395 ira_allocno_iterator ai;
397 ira_assert (current_loops != NULL);
398 max_regno = max_reg_num ();
399 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), l, loop)
400 if (ira_loop_nodes[l].regno_allocno_map != NULL)
402 ira_free (ira_loop_nodes[l].regno_allocno_map);
403 ira_loop_nodes[l].regno_allocno_map
404 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
405 * max_regno);
406 memset (ira_loop_nodes[l].regno_allocno_map, 0,
407 sizeof (ira_allocno_t) * max_regno);
409 ira_free (ira_regno_allocno_map);
410 ira_regno_allocno_map
411 = (ira_allocno_t *) ira_allocate (max_regno * sizeof (ira_allocno_t));
412 memset (ira_regno_allocno_map, 0, max_regno * sizeof (ira_allocno_t));
413 FOR_EACH_ALLOCNO (a, ai)
415 if (ALLOCNO_CAP_MEMBER (a) != NULL)
416 /* Caps are not in the regno allocno maps. */
417 continue;
418 regno = ALLOCNO_REGNO (a);
419 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
420 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = ira_regno_allocno_map[regno];
421 ira_regno_allocno_map[regno] = a;
422 if (loop_tree_node->regno_allocno_map[regno] == NULL)
423 /* Remember that we can create temporary allocnos to break
424 cycles in register shuffle. */
425 loop_tree_node->regno_allocno_map[regno] = a;
430 /* Pools for allocnos, allocno live ranges and objects. */
431 static alloc_pool allocno_pool, live_range_pool, object_pool;
433 /* Vec containing references to all created allocnos. It is a
434 container of array allocnos. */
435 static vec<ira_allocno_t> allocno_vec;
437 /* Vec containing references to all created ira_objects. It is a
438 container of ira_object_id_map. */
439 static vec<ira_object_t> ira_object_id_map_vec;
441 /* Initialize data concerning allocnos. */
442 static void
443 initiate_allocnos (void)
445 live_range_pool
446 = create_alloc_pool ("live ranges",
447 sizeof (struct live_range), 100);
448 allocno_pool
449 = create_alloc_pool ("allocnos", sizeof (struct ira_allocno), 100);
450 object_pool
451 = create_alloc_pool ("objects", sizeof (struct ira_object), 100);
452 allocno_vec.create (max_reg_num () * 2);
453 ira_allocnos = NULL;
454 ira_allocnos_num = 0;
455 ira_objects_num = 0;
456 ira_object_id_map_vec.create (max_reg_num () * 2);
457 ira_object_id_map = NULL;
458 ira_regno_allocno_map
459 = (ira_allocno_t *) ira_allocate (max_reg_num ()
460 * sizeof (ira_allocno_t));
461 memset (ira_regno_allocno_map, 0, max_reg_num () * sizeof (ira_allocno_t));
464 /* Create and return an object corresponding to a new allocno A. */
465 static ira_object_t
466 ira_create_object (ira_allocno_t a, int subword)
468 enum reg_class aclass = ALLOCNO_CLASS (a);
469 ira_object_t obj = (ira_object_t) pool_alloc (object_pool);
471 OBJECT_ALLOCNO (obj) = a;
472 OBJECT_SUBWORD (obj) = subword;
473 OBJECT_CONFLICT_ID (obj) = ira_objects_num;
474 OBJECT_CONFLICT_VEC_P (obj) = false;
475 OBJECT_CONFLICT_ARRAY (obj) = NULL;
476 OBJECT_NUM_CONFLICTS (obj) = 0;
477 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj), ira_no_alloc_regs);
478 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), ira_no_alloc_regs);
479 IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
480 reg_class_contents[aclass]);
481 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
482 reg_class_contents[aclass]);
483 OBJECT_MIN (obj) = INT_MAX;
484 OBJECT_MAX (obj) = -1;
485 OBJECT_LIVE_RANGES (obj) = NULL;
487 ira_object_id_map_vec.safe_push (obj);
488 ira_object_id_map
489 = ira_object_id_map_vec.address ();
490 ira_objects_num = ira_object_id_map_vec.length ();
492 return obj;
495 /* Create and return the allocno corresponding to REGNO in
496 LOOP_TREE_NODE. Add the allocno to the list of allocnos with the
497 same regno if CAP_P is FALSE. */
498 ira_allocno_t
499 ira_create_allocno (int regno, bool cap_p,
500 ira_loop_tree_node_t loop_tree_node)
502 ira_allocno_t a;
504 a = (ira_allocno_t) pool_alloc (allocno_pool);
505 ALLOCNO_REGNO (a) = regno;
506 ALLOCNO_LOOP_TREE_NODE (a) = loop_tree_node;
507 if (! cap_p)
509 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = ira_regno_allocno_map[regno];
510 ira_regno_allocno_map[regno] = a;
511 if (loop_tree_node->regno_allocno_map[regno] == NULL)
512 /* Remember that we can create temporary allocnos to break
513 cycles in register shuffle on region borders (see
514 ira-emit.c). */
515 loop_tree_node->regno_allocno_map[regno] = a;
517 ALLOCNO_CAP (a) = NULL;
518 ALLOCNO_CAP_MEMBER (a) = NULL;
519 ALLOCNO_NUM (a) = ira_allocnos_num;
520 bitmap_set_bit (loop_tree_node->all_allocnos, ALLOCNO_NUM (a));
521 ALLOCNO_NREFS (a) = 0;
522 ALLOCNO_FREQ (a) = 0;
523 ALLOCNO_HARD_REGNO (a) = -1;
524 ALLOCNO_CALL_FREQ (a) = 0;
525 ALLOCNO_CALLS_CROSSED_NUM (a) = 0;
526 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a) = 0;
527 CLEAR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
528 #ifdef STACK_REGS
529 ALLOCNO_NO_STACK_REG_P (a) = false;
530 ALLOCNO_TOTAL_NO_STACK_REG_P (a) = false;
531 #endif
532 ALLOCNO_DONT_REASSIGN_P (a) = false;
533 ALLOCNO_BAD_SPILL_P (a) = false;
534 ALLOCNO_ASSIGNED_P (a) = false;
535 ALLOCNO_MODE (a) = (regno < 0 ? VOIDmode : PSEUDO_REGNO_MODE (regno));
536 ALLOCNO_WMODE (a) = ALLOCNO_MODE (a);
537 ALLOCNO_PREFS (a) = NULL;
538 ALLOCNO_COPIES (a) = NULL;
539 ALLOCNO_HARD_REG_COSTS (a) = NULL;
540 ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
541 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
542 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
543 ALLOCNO_CLASS (a) = NO_REGS;
544 ALLOCNO_UPDATED_CLASS_COST (a) = 0;
545 ALLOCNO_CLASS_COST (a) = 0;
546 ALLOCNO_MEMORY_COST (a) = 0;
547 ALLOCNO_UPDATED_MEMORY_COST (a) = 0;
548 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) = 0;
549 ALLOCNO_NUM_OBJECTS (a) = 0;
551 ALLOCNO_ADD_DATA (a) = NULL;
552 allocno_vec.safe_push (a);
553 ira_allocnos = allocno_vec.address ();
554 ira_allocnos_num = allocno_vec.length ();
556 return a;
559 /* Set up register class for A and update its conflict hard
560 registers. */
561 void
562 ira_set_allocno_class (ira_allocno_t a, enum reg_class aclass)
564 ira_allocno_object_iterator oi;
565 ira_object_t obj;
567 ALLOCNO_CLASS (a) = aclass;
568 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
570 IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
571 reg_class_contents[aclass]);
572 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
573 reg_class_contents[aclass]);
577 /* Determine the number of objects we should associate with allocno A
578 and allocate them. */
579 void
580 ira_create_allocno_objects (ira_allocno_t a)
582 machine_mode mode = ALLOCNO_MODE (a);
583 enum reg_class aclass = ALLOCNO_CLASS (a);
584 int n = ira_reg_class_max_nregs[aclass][mode];
585 int i;
587 if (GET_MODE_SIZE (mode) != 2 * UNITS_PER_WORD || n != 2)
588 n = 1;
590 ALLOCNO_NUM_OBJECTS (a) = n;
591 for (i = 0; i < n; i++)
592 ALLOCNO_OBJECT (a, i) = ira_create_object (a, i);
595 /* For each allocno, set ALLOCNO_NUM_OBJECTS and create the
596 ALLOCNO_OBJECT structures. This must be called after the allocno
597 classes are known. */
598 static void
599 create_allocno_objects (void)
601 ira_allocno_t a;
602 ira_allocno_iterator ai;
604 FOR_EACH_ALLOCNO (a, ai)
605 ira_create_allocno_objects (a);
608 /* Merge hard register conflict information for all objects associated with
609 allocno TO into the corresponding objects associated with FROM.
610 If TOTAL_ONLY is true, we only merge OBJECT_TOTAL_CONFLICT_HARD_REGS. */
611 static void
612 merge_hard_reg_conflicts (ira_allocno_t from, ira_allocno_t to,
613 bool total_only)
615 int i;
616 gcc_assert (ALLOCNO_NUM_OBJECTS (to) == ALLOCNO_NUM_OBJECTS (from));
617 for (i = 0; i < ALLOCNO_NUM_OBJECTS (to); i++)
619 ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
620 ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
622 if (!total_only)
623 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (to_obj),
624 OBJECT_CONFLICT_HARD_REGS (from_obj));
625 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (to_obj),
626 OBJECT_TOTAL_CONFLICT_HARD_REGS (from_obj));
628 #ifdef STACK_REGS
629 if (!total_only && ALLOCNO_NO_STACK_REG_P (from))
630 ALLOCNO_NO_STACK_REG_P (to) = true;
631 if (ALLOCNO_TOTAL_NO_STACK_REG_P (from))
632 ALLOCNO_TOTAL_NO_STACK_REG_P (to) = true;
633 #endif
636 /* Update hard register conflict information for all objects associated with
637 A to include the regs in SET. */
638 void
639 ior_hard_reg_conflicts (ira_allocno_t a, HARD_REG_SET *set)
641 ira_allocno_object_iterator i;
642 ira_object_t obj;
644 FOR_EACH_ALLOCNO_OBJECT (a, obj, i)
646 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj), *set);
647 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), *set);
651 /* Return TRUE if a conflict vector with NUM elements is more
652 profitable than a conflict bit vector for OBJ. */
653 bool
654 ira_conflict_vector_profitable_p (ira_object_t obj, int num)
656 int nw;
657 int max = OBJECT_MAX (obj);
658 int min = OBJECT_MIN (obj);
660 if (max < min)
661 /* We prefer a bit vector in such case because it does not result
662 in allocation. */
663 return false;
665 nw = (max - min + IRA_INT_BITS) / IRA_INT_BITS;
666 return (2 * sizeof (ira_object_t) * (num + 1)
667 < 3 * nw * sizeof (IRA_INT_TYPE));
670 /* Allocates and initialize the conflict vector of OBJ for NUM
671 conflicting objects. */
672 void
673 ira_allocate_conflict_vec (ira_object_t obj, int num)
675 int size;
676 ira_object_t *vec;
678 ira_assert (OBJECT_CONFLICT_ARRAY (obj) == NULL);
679 num++; /* for NULL end marker */
680 size = sizeof (ira_object_t) * num;
681 OBJECT_CONFLICT_ARRAY (obj) = ira_allocate (size);
682 vec = (ira_object_t *) OBJECT_CONFLICT_ARRAY (obj);
683 vec[0] = NULL;
684 OBJECT_NUM_CONFLICTS (obj) = 0;
685 OBJECT_CONFLICT_ARRAY_SIZE (obj) = size;
686 OBJECT_CONFLICT_VEC_P (obj) = true;
689 /* Allocate and initialize the conflict bit vector of OBJ. */
690 static void
691 allocate_conflict_bit_vec (ira_object_t obj)
693 unsigned int size;
695 ira_assert (OBJECT_CONFLICT_ARRAY (obj) == NULL);
696 size = ((OBJECT_MAX (obj) - OBJECT_MIN (obj) + IRA_INT_BITS)
697 / IRA_INT_BITS * sizeof (IRA_INT_TYPE));
698 OBJECT_CONFLICT_ARRAY (obj) = ira_allocate (size);
699 memset (OBJECT_CONFLICT_ARRAY (obj), 0, size);
700 OBJECT_CONFLICT_ARRAY_SIZE (obj) = size;
701 OBJECT_CONFLICT_VEC_P (obj) = false;
704 /* Allocate and initialize the conflict vector or conflict bit vector
705 of OBJ for NUM conflicting allocnos whatever is more profitable. */
706 void
707 ira_allocate_object_conflicts (ira_object_t obj, int num)
709 if (ira_conflict_vector_profitable_p (obj, num))
710 ira_allocate_conflict_vec (obj, num);
711 else
712 allocate_conflict_bit_vec (obj);
715 /* Add OBJ2 to the conflicts of OBJ1. */
716 static void
717 add_to_conflicts (ira_object_t obj1, ira_object_t obj2)
719 int num;
720 unsigned int size;
722 if (OBJECT_CONFLICT_VEC_P (obj1))
724 ira_object_t *vec = OBJECT_CONFLICT_VEC (obj1);
725 int curr_num = OBJECT_NUM_CONFLICTS (obj1);
726 num = curr_num + 2;
727 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) < num * sizeof (ira_object_t))
729 ira_object_t *newvec;
730 size = (3 * num / 2 + 1) * sizeof (ira_allocno_t);
731 newvec = (ira_object_t *) ira_allocate (size);
732 memcpy (newvec, vec, curr_num * sizeof (ira_object_t));
733 ira_free (vec);
734 vec = newvec;
735 OBJECT_CONFLICT_ARRAY (obj1) = vec;
736 OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
738 vec[num - 2] = obj2;
739 vec[num - 1] = NULL;
740 OBJECT_NUM_CONFLICTS (obj1)++;
742 else
744 int nw, added_head_nw, id;
745 IRA_INT_TYPE *vec = OBJECT_CONFLICT_BITVEC (obj1);
747 id = OBJECT_CONFLICT_ID (obj2);
748 if (OBJECT_MIN (obj1) > id)
750 /* Expand head of the bit vector. */
751 added_head_nw = (OBJECT_MIN (obj1) - id - 1) / IRA_INT_BITS + 1;
752 nw = (OBJECT_MAX (obj1) - OBJECT_MIN (obj1)) / IRA_INT_BITS + 1;
753 size = (nw + added_head_nw) * sizeof (IRA_INT_TYPE);
754 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) >= size)
756 memmove ((char *) vec + added_head_nw * sizeof (IRA_INT_TYPE),
757 vec, nw * sizeof (IRA_INT_TYPE));
758 memset (vec, 0, added_head_nw * sizeof (IRA_INT_TYPE));
760 else
762 size
763 = (3 * (nw + added_head_nw) / 2 + 1) * sizeof (IRA_INT_TYPE);
764 vec = (IRA_INT_TYPE *) ira_allocate (size);
765 memcpy ((char *) vec + added_head_nw * sizeof (IRA_INT_TYPE),
766 OBJECT_CONFLICT_ARRAY (obj1), nw * sizeof (IRA_INT_TYPE));
767 memset (vec, 0, added_head_nw * sizeof (IRA_INT_TYPE));
768 memset ((char *) vec
769 + (nw + added_head_nw) * sizeof (IRA_INT_TYPE),
770 0, size - (nw + added_head_nw) * sizeof (IRA_INT_TYPE));
771 ira_free (OBJECT_CONFLICT_ARRAY (obj1));
772 OBJECT_CONFLICT_ARRAY (obj1) = vec;
773 OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
775 OBJECT_MIN (obj1) -= added_head_nw * IRA_INT_BITS;
777 else if (OBJECT_MAX (obj1) < id)
779 nw = (id - OBJECT_MIN (obj1)) / IRA_INT_BITS + 1;
780 size = nw * sizeof (IRA_INT_TYPE);
781 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) < size)
783 /* Expand tail of the bit vector. */
784 size = (3 * nw / 2 + 1) * sizeof (IRA_INT_TYPE);
785 vec = (IRA_INT_TYPE *) ira_allocate (size);
786 memcpy (vec, OBJECT_CONFLICT_ARRAY (obj1), OBJECT_CONFLICT_ARRAY_SIZE (obj1));
787 memset ((char *) vec + OBJECT_CONFLICT_ARRAY_SIZE (obj1),
788 0, size - OBJECT_CONFLICT_ARRAY_SIZE (obj1));
789 ira_free (OBJECT_CONFLICT_ARRAY (obj1));
790 OBJECT_CONFLICT_ARRAY (obj1) = vec;
791 OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
793 OBJECT_MAX (obj1) = id;
795 SET_MINMAX_SET_BIT (vec, id, OBJECT_MIN (obj1), OBJECT_MAX (obj1));
799 /* Add OBJ1 to the conflicts of OBJ2 and vice versa. */
800 static void
801 ira_add_conflict (ira_object_t obj1, ira_object_t obj2)
803 add_to_conflicts (obj1, obj2);
804 add_to_conflicts (obj2, obj1);
807 /* Clear all conflicts of OBJ. */
808 static void
809 clear_conflicts (ira_object_t obj)
811 if (OBJECT_CONFLICT_VEC_P (obj))
813 OBJECT_NUM_CONFLICTS (obj) = 0;
814 OBJECT_CONFLICT_VEC (obj)[0] = NULL;
816 else if (OBJECT_CONFLICT_ARRAY_SIZE (obj) != 0)
818 int nw;
820 nw = (OBJECT_MAX (obj) - OBJECT_MIN (obj)) / IRA_INT_BITS + 1;
821 memset (OBJECT_CONFLICT_BITVEC (obj), 0, nw * sizeof (IRA_INT_TYPE));
825 /* The array used to find duplications in conflict vectors of
826 allocnos. */
827 static int *conflict_check;
829 /* The value used to mark allocation presence in conflict vector of
830 the current allocno. */
831 static int curr_conflict_check_tick;
833 /* Remove duplications in conflict vector of OBJ. */
834 static void
835 compress_conflict_vec (ira_object_t obj)
837 ira_object_t *vec, conflict_obj;
838 int i, j;
840 ira_assert (OBJECT_CONFLICT_VEC_P (obj));
841 vec = OBJECT_CONFLICT_VEC (obj);
842 curr_conflict_check_tick++;
843 for (i = j = 0; (conflict_obj = vec[i]) != NULL; i++)
845 int id = OBJECT_CONFLICT_ID (conflict_obj);
846 if (conflict_check[id] != curr_conflict_check_tick)
848 conflict_check[id] = curr_conflict_check_tick;
849 vec[j++] = conflict_obj;
852 OBJECT_NUM_CONFLICTS (obj) = j;
853 vec[j] = NULL;
856 /* Remove duplications in conflict vectors of all allocnos. */
857 static void
858 compress_conflict_vecs (void)
860 ira_object_t obj;
861 ira_object_iterator oi;
863 conflict_check = (int *) ira_allocate (sizeof (int) * ira_objects_num);
864 memset (conflict_check, 0, sizeof (int) * ira_objects_num);
865 curr_conflict_check_tick = 0;
866 FOR_EACH_OBJECT (obj, oi)
868 if (OBJECT_CONFLICT_VEC_P (obj))
869 compress_conflict_vec (obj);
871 ira_free (conflict_check);
874 /* This recursive function outputs allocno A and if it is a cap the
875 function outputs its members. */
876 void
877 ira_print_expanded_allocno (ira_allocno_t a)
879 basic_block bb;
881 fprintf (ira_dump_file, " a%d(r%d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
882 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
883 fprintf (ira_dump_file, ",b%d", bb->index);
884 else
885 fprintf (ira_dump_file, ",l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
886 if (ALLOCNO_CAP_MEMBER (a) != NULL)
888 fprintf (ira_dump_file, ":");
889 ira_print_expanded_allocno (ALLOCNO_CAP_MEMBER (a));
891 fprintf (ira_dump_file, ")");
894 /* Create and return the cap representing allocno A in the
895 parent loop. */
896 static ira_allocno_t
897 create_cap_allocno (ira_allocno_t a)
899 ira_allocno_t cap;
900 ira_loop_tree_node_t parent;
901 enum reg_class aclass;
903 parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
904 cap = ira_create_allocno (ALLOCNO_REGNO (a), true, parent);
905 ALLOCNO_MODE (cap) = ALLOCNO_MODE (a);
906 ALLOCNO_WMODE (cap) = ALLOCNO_WMODE (a);
907 aclass = ALLOCNO_CLASS (a);
908 ira_set_allocno_class (cap, aclass);
909 ira_create_allocno_objects (cap);
910 ALLOCNO_CAP_MEMBER (cap) = a;
911 ALLOCNO_CAP (a) = cap;
912 ALLOCNO_CLASS_COST (cap) = ALLOCNO_CLASS_COST (a);
913 ALLOCNO_MEMORY_COST (cap) = ALLOCNO_MEMORY_COST (a);
914 ira_allocate_and_copy_costs
915 (&ALLOCNO_HARD_REG_COSTS (cap), aclass, ALLOCNO_HARD_REG_COSTS (a));
916 ira_allocate_and_copy_costs
917 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (cap), aclass,
918 ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
919 ALLOCNO_BAD_SPILL_P (cap) = ALLOCNO_BAD_SPILL_P (a);
920 ALLOCNO_NREFS (cap) = ALLOCNO_NREFS (a);
921 ALLOCNO_FREQ (cap) = ALLOCNO_FREQ (a);
922 ALLOCNO_CALL_FREQ (cap) = ALLOCNO_CALL_FREQ (a);
924 merge_hard_reg_conflicts (a, cap, false);
926 ALLOCNO_CALLS_CROSSED_NUM (cap) = ALLOCNO_CALLS_CROSSED_NUM (a);
927 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (cap) = ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
928 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (cap),
929 ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
930 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
932 fprintf (ira_dump_file, " Creating cap ");
933 ira_print_expanded_allocno (cap);
934 fprintf (ira_dump_file, "\n");
936 return cap;
939 /* Create and return a live range for OBJECT with given attributes. */
940 live_range_t
941 ira_create_live_range (ira_object_t obj, int start, int finish,
942 live_range_t next)
944 live_range_t p;
946 p = (live_range_t) pool_alloc (live_range_pool);
947 p->object = obj;
948 p->start = start;
949 p->finish = finish;
950 p->next = next;
951 return p;
954 /* Create a new live range for OBJECT and queue it at the head of its
955 live range list. */
956 void
957 ira_add_live_range_to_object (ira_object_t object, int start, int finish)
959 live_range_t p;
960 p = ira_create_live_range (object, start, finish,
961 OBJECT_LIVE_RANGES (object));
962 OBJECT_LIVE_RANGES (object) = p;
965 /* Copy allocno live range R and return the result. */
966 static live_range_t
967 copy_live_range (live_range_t r)
969 live_range_t p;
971 p = (live_range_t) pool_alloc (live_range_pool);
972 *p = *r;
973 return p;
976 /* Copy allocno live range list given by its head R and return the
977 result. */
978 live_range_t
979 ira_copy_live_range_list (live_range_t r)
981 live_range_t p, first, last;
983 if (r == NULL)
984 return NULL;
985 for (first = last = NULL; r != NULL; r = r->next)
987 p = copy_live_range (r);
988 if (first == NULL)
989 first = p;
990 else
991 last->next = p;
992 last = p;
994 return first;
997 /* Merge ranges R1 and R2 and returns the result. The function
998 maintains the order of ranges and tries to minimize number of the
999 result ranges. */
1000 live_range_t
1001 ira_merge_live_ranges (live_range_t r1, live_range_t r2)
1003 live_range_t first, last;
1005 if (r1 == NULL)
1006 return r2;
1007 if (r2 == NULL)
1008 return r1;
1009 for (first = last = NULL; r1 != NULL && r2 != NULL;)
1011 if (r1->start < r2->start)
1012 std::swap (r1, r2);
1013 if (r1->start <= r2->finish + 1)
1015 /* Intersected ranges: merge r1 and r2 into r1. */
1016 r1->start = r2->start;
1017 if (r1->finish < r2->finish)
1018 r1->finish = r2->finish;
1019 live_range_t temp = r2;
1020 r2 = r2->next;
1021 ira_finish_live_range (temp);
1022 if (r2 == NULL)
1024 /* To try to merge with subsequent ranges in r1. */
1025 r2 = r1->next;
1026 r1->next = NULL;
1029 else
1031 /* Add r1 to the result. */
1032 if (first == NULL)
1033 first = last = r1;
1034 else
1036 last->next = r1;
1037 last = r1;
1039 r1 = r1->next;
1040 if (r1 == NULL)
1042 /* To try to merge with subsequent ranges in r2. */
1043 r1 = r2->next;
1044 r2->next = NULL;
1048 if (r1 != NULL)
1050 if (first == NULL)
1051 first = r1;
1052 else
1053 last->next = r1;
1054 ira_assert (r1->next == NULL);
1056 else if (r2 != NULL)
1058 if (first == NULL)
1059 first = r2;
1060 else
1061 last->next = r2;
1062 ira_assert (r2->next == NULL);
1064 else
1066 ira_assert (last->next == NULL);
1068 return first;
1071 /* Return TRUE if live ranges R1 and R2 intersect. */
1072 bool
1073 ira_live_ranges_intersect_p (live_range_t r1, live_range_t r2)
1075 /* Remember the live ranges are always kept ordered. */
1076 while (r1 != NULL && r2 != NULL)
1078 if (r1->start > r2->finish)
1079 r1 = r1->next;
1080 else if (r2->start > r1->finish)
1081 r2 = r2->next;
1082 else
1083 return true;
1085 return false;
1088 /* Free allocno live range R. */
1089 void
1090 ira_finish_live_range (live_range_t r)
1092 pool_free (live_range_pool, r);
1095 /* Free list of allocno live ranges starting with R. */
1096 void
1097 ira_finish_live_range_list (live_range_t r)
1099 live_range_t next_r;
1101 for (; r != NULL; r = next_r)
1103 next_r = r->next;
1104 ira_finish_live_range (r);
1108 /* Free updated register costs of allocno A. */
1109 void
1110 ira_free_allocno_updated_costs (ira_allocno_t a)
1112 enum reg_class aclass;
1114 aclass = ALLOCNO_CLASS (a);
1115 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
1116 ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass);
1117 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
1118 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
1119 ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1120 aclass);
1121 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
1124 /* Free and nullify all cost vectors allocated earlier for allocno
1125 A. */
1126 static void
1127 ira_free_allocno_costs (ira_allocno_t a)
1129 enum reg_class aclass = ALLOCNO_CLASS (a);
1130 ira_object_t obj;
1131 ira_allocno_object_iterator oi;
1133 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
1135 ira_finish_live_range_list (OBJECT_LIVE_RANGES (obj));
1136 ira_object_id_map[OBJECT_CONFLICT_ID (obj)] = NULL;
1137 if (OBJECT_CONFLICT_ARRAY (obj) != NULL)
1138 ira_free (OBJECT_CONFLICT_ARRAY (obj));
1139 pool_free (object_pool, obj);
1142 ira_allocnos[ALLOCNO_NUM (a)] = NULL;
1143 if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
1144 ira_free_cost_vector (ALLOCNO_HARD_REG_COSTS (a), aclass);
1145 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL)
1146 ira_free_cost_vector (ALLOCNO_CONFLICT_HARD_REG_COSTS (a), aclass);
1147 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
1148 ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass);
1149 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
1150 ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1151 aclass);
1152 ALLOCNO_HARD_REG_COSTS (a) = NULL;
1153 ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
1154 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
1155 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
1158 /* Free the memory allocated for allocno A. */
1159 static void
1160 finish_allocno (ira_allocno_t a)
1162 ira_free_allocno_costs (a);
1163 pool_free (allocno_pool, a);
1166 /* Free the memory allocated for all allocnos. */
1167 static void
1168 finish_allocnos (void)
1170 ira_allocno_t a;
1171 ira_allocno_iterator ai;
1173 FOR_EACH_ALLOCNO (a, ai)
1174 finish_allocno (a);
1175 ira_free (ira_regno_allocno_map);
1176 ira_object_id_map_vec.release ();
1177 allocno_vec.release ();
1178 free_alloc_pool (allocno_pool);
1179 free_alloc_pool (object_pool);
1180 free_alloc_pool (live_range_pool);
1185 /* Pools for allocno preferences. */
1186 static alloc_pool pref_pool;
1188 /* Vec containing references to all created preferences. It is a
1189 container of array ira_prefs. */
1190 static vec<ira_pref_t> pref_vec;
1192 /* The function initializes data concerning allocno prefs. */
1193 static void
1194 initiate_prefs (void)
1196 pref_pool
1197 = create_alloc_pool ("prefs", sizeof (struct ira_allocno_pref), 100);
1198 pref_vec.create (get_max_uid ());
1199 ira_prefs = NULL;
1200 ira_prefs_num = 0;
1203 /* Return pref for A and HARD_REGNO if any. */
1204 static ira_pref_t
1205 find_allocno_pref (ira_allocno_t a, int hard_regno)
1207 ira_pref_t pref;
1209 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
1210 if (pref->allocno == a && pref->hard_regno == hard_regno)
1211 return pref;
1212 return NULL;
1215 /* Create and return pref with given attributes A, HARD_REGNO, and FREQ. */
1216 ira_pref_t
1217 ira_create_pref (ira_allocno_t a, int hard_regno, int freq)
1219 ira_pref_t pref;
1221 pref = (ira_pref_t) pool_alloc (pref_pool);
1222 pref->num = ira_prefs_num;
1223 pref->allocno = a;
1224 pref->hard_regno = hard_regno;
1225 pref->freq = freq;
1226 pref_vec.safe_push (pref);
1227 ira_prefs = pref_vec.address ();
1228 ira_prefs_num = pref_vec.length ();
1229 return pref;
1232 /* Attach a pref PREF to the corresponding allocno. */
1233 static void
1234 add_allocno_pref_to_list (ira_pref_t pref)
1236 ira_allocno_t a = pref->allocno;
1238 pref->next_pref = ALLOCNO_PREFS (a);
1239 ALLOCNO_PREFS (a) = pref;
1242 /* Create (or update frequency if the pref already exists) the pref of
1243 allocnos A preferring HARD_REGNO with frequency FREQ. */
1244 void
1245 ira_add_allocno_pref (ira_allocno_t a, int hard_regno, int freq)
1247 ira_pref_t pref;
1249 if (freq <= 0)
1250 return;
1251 if ((pref = find_allocno_pref (a, hard_regno)) != NULL)
1253 pref->freq += freq;
1254 return;
1256 pref = ira_create_pref (a, hard_regno, freq);
1257 ira_assert (a != NULL);
1258 add_allocno_pref_to_list (pref);
1261 /* Print info about PREF into file F. */
1262 static void
1263 print_pref (FILE *f, ira_pref_t pref)
1265 fprintf (f, " pref%d:a%d(r%d)<-hr%d@%d\n", pref->num,
1266 ALLOCNO_NUM (pref->allocno), ALLOCNO_REGNO (pref->allocno),
1267 pref->hard_regno, pref->freq);
1270 /* Print info about PREF into stderr. */
1271 void
1272 ira_debug_pref (ira_pref_t pref)
1274 print_pref (stderr, pref);
1277 /* Print info about all prefs into file F. */
1278 static void
1279 print_prefs (FILE *f)
1281 ira_pref_t pref;
1282 ira_pref_iterator pi;
1284 FOR_EACH_PREF (pref, pi)
1285 print_pref (f, pref);
1288 /* Print info about all prefs into stderr. */
1289 void
1290 ira_debug_prefs (void)
1292 print_prefs (stderr);
1295 /* Print info about prefs involving allocno A into file F. */
1296 static void
1297 print_allocno_prefs (FILE *f, ira_allocno_t a)
1299 ira_pref_t pref;
1301 fprintf (f, " a%d(r%d):", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
1302 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
1303 fprintf (f, " pref%d:hr%d@%d", pref->num, pref->hard_regno, pref->freq);
1304 fprintf (f, "\n");
1307 /* Print info about prefs involving allocno A into stderr. */
1308 void
1309 ira_debug_allocno_prefs (ira_allocno_t a)
1311 print_allocno_prefs (stderr, a);
1314 /* The function frees memory allocated for PREF. */
1315 static void
1316 finish_pref (ira_pref_t pref)
1318 ira_prefs[pref->num] = NULL;
1319 pool_free (pref_pool, pref);
1322 /* Remove PREF from the list of allocno prefs and free memory for
1323 it. */
1324 void
1325 ira_remove_pref (ira_pref_t pref)
1327 ira_pref_t cpref, prev;
1329 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1330 fprintf (ira_dump_file, " Removing pref%d:hr%d@%d\n",
1331 pref->num, pref->hard_regno, pref->freq);
1332 for (prev = NULL, cpref = ALLOCNO_PREFS (pref->allocno);
1333 cpref != NULL;
1334 prev = cpref, cpref = cpref->next_pref)
1335 if (cpref == pref)
1336 break;
1337 ira_assert (cpref != NULL);
1338 if (prev == NULL)
1339 ALLOCNO_PREFS (pref->allocno) = pref->next_pref;
1340 else
1341 prev->next_pref = pref->next_pref;
1342 finish_pref (pref);
1345 /* Remove all prefs of allocno A. */
1346 void
1347 ira_remove_allocno_prefs (ira_allocno_t a)
1349 ira_pref_t pref, next_pref;
1351 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = next_pref)
1353 next_pref = pref->next_pref;
1354 finish_pref (pref);
1356 ALLOCNO_PREFS (a) = NULL;
1359 /* Free memory allocated for all prefs. */
1360 static void
1361 finish_prefs (void)
1363 ira_pref_t pref;
1364 ira_pref_iterator pi;
1366 FOR_EACH_PREF (pref, pi)
1367 finish_pref (pref);
1368 pref_vec.release ();
1369 free_alloc_pool (pref_pool);
1374 /* Pools for copies. */
1375 static alloc_pool copy_pool;
1377 /* Vec containing references to all created copies. It is a
1378 container of array ira_copies. */
1379 static vec<ira_copy_t> copy_vec;
1381 /* The function initializes data concerning allocno copies. */
1382 static void
1383 initiate_copies (void)
1385 copy_pool
1386 = create_alloc_pool ("copies", sizeof (struct ira_allocno_copy), 100);
1387 copy_vec.create (get_max_uid ());
1388 ira_copies = NULL;
1389 ira_copies_num = 0;
1392 /* Return copy connecting A1 and A2 and originated from INSN of
1393 LOOP_TREE_NODE if any. */
1394 static ira_copy_t
1395 find_allocno_copy (ira_allocno_t a1, ira_allocno_t a2, rtx_insn *insn,
1396 ira_loop_tree_node_t loop_tree_node)
1398 ira_copy_t cp, next_cp;
1399 ira_allocno_t another_a;
1401 for (cp = ALLOCNO_COPIES (a1); cp != NULL; cp = next_cp)
1403 if (cp->first == a1)
1405 next_cp = cp->next_first_allocno_copy;
1406 another_a = cp->second;
1408 else if (cp->second == a1)
1410 next_cp = cp->next_second_allocno_copy;
1411 another_a = cp->first;
1413 else
1414 gcc_unreachable ();
1415 if (another_a == a2 && cp->insn == insn
1416 && cp->loop_tree_node == loop_tree_node)
1417 return cp;
1419 return NULL;
1422 /* Create and return copy with given attributes LOOP_TREE_NODE, FIRST,
1423 SECOND, FREQ, CONSTRAINT_P, and INSN. */
1424 ira_copy_t
1425 ira_create_copy (ira_allocno_t first, ira_allocno_t second, int freq,
1426 bool constraint_p, rtx_insn *insn,
1427 ira_loop_tree_node_t loop_tree_node)
1429 ira_copy_t cp;
1431 cp = (ira_copy_t) pool_alloc (copy_pool);
1432 cp->num = ira_copies_num;
1433 cp->first = first;
1434 cp->second = second;
1435 cp->freq = freq;
1436 cp->constraint_p = constraint_p;
1437 cp->insn = insn;
1438 cp->loop_tree_node = loop_tree_node;
1439 copy_vec.safe_push (cp);
1440 ira_copies = copy_vec.address ();
1441 ira_copies_num = copy_vec.length ();
1442 return cp;
1445 /* Attach a copy CP to allocnos involved into the copy. */
1446 static void
1447 add_allocno_copy_to_list (ira_copy_t cp)
1449 ira_allocno_t first = cp->first, second = cp->second;
1451 cp->prev_first_allocno_copy = NULL;
1452 cp->prev_second_allocno_copy = NULL;
1453 cp->next_first_allocno_copy = ALLOCNO_COPIES (first);
1454 if (cp->next_first_allocno_copy != NULL)
1456 if (cp->next_first_allocno_copy->first == first)
1457 cp->next_first_allocno_copy->prev_first_allocno_copy = cp;
1458 else
1459 cp->next_first_allocno_copy->prev_second_allocno_copy = cp;
1461 cp->next_second_allocno_copy = ALLOCNO_COPIES (second);
1462 if (cp->next_second_allocno_copy != NULL)
1464 if (cp->next_second_allocno_copy->second == second)
1465 cp->next_second_allocno_copy->prev_second_allocno_copy = cp;
1466 else
1467 cp->next_second_allocno_copy->prev_first_allocno_copy = cp;
1469 ALLOCNO_COPIES (first) = cp;
1470 ALLOCNO_COPIES (second) = cp;
1473 /* Make a copy CP a canonical copy where number of the
1474 first allocno is less than the second one. */
1475 static void
1476 swap_allocno_copy_ends_if_necessary (ira_copy_t cp)
1478 if (ALLOCNO_NUM (cp->first) <= ALLOCNO_NUM (cp->second))
1479 return;
1481 std::swap (cp->first, cp->second);
1482 std::swap (cp->prev_first_allocno_copy, cp->prev_second_allocno_copy);
1483 std::swap (cp->next_first_allocno_copy, cp->next_second_allocno_copy);
1486 /* Create (or update frequency if the copy already exists) and return
1487 the copy of allocnos FIRST and SECOND with frequency FREQ
1488 corresponding to move insn INSN (if any) and originated from
1489 LOOP_TREE_NODE. */
1490 ira_copy_t
1491 ira_add_allocno_copy (ira_allocno_t first, ira_allocno_t second, int freq,
1492 bool constraint_p, rtx_insn *insn,
1493 ira_loop_tree_node_t loop_tree_node)
1495 ira_copy_t cp;
1497 if ((cp = find_allocno_copy (first, second, insn, loop_tree_node)) != NULL)
1499 cp->freq += freq;
1500 return cp;
1502 cp = ira_create_copy (first, second, freq, constraint_p, insn,
1503 loop_tree_node);
1504 ira_assert (first != NULL && second != NULL);
1505 add_allocno_copy_to_list (cp);
1506 swap_allocno_copy_ends_if_necessary (cp);
1507 return cp;
1510 /* Print info about copy CP into file F. */
1511 static void
1512 print_copy (FILE *f, ira_copy_t cp)
1514 fprintf (f, " cp%d:a%d(r%d)<->a%d(r%d)@%d:%s\n", cp->num,
1515 ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
1516 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second), cp->freq,
1517 cp->insn != NULL
1518 ? "move" : cp->constraint_p ? "constraint" : "shuffle");
1521 DEBUG_FUNCTION void
1522 debug (ira_allocno_copy &ref)
1524 print_copy (stderr, &ref);
1527 DEBUG_FUNCTION void
1528 debug (ira_allocno_copy *ptr)
1530 if (ptr)
1531 debug (*ptr);
1532 else
1533 fprintf (stderr, "<nil>\n");
1536 /* Print info about copy CP into stderr. */
1537 void
1538 ira_debug_copy (ira_copy_t cp)
1540 print_copy (stderr, cp);
1543 /* Print info about all copies into file F. */
1544 static void
1545 print_copies (FILE *f)
1547 ira_copy_t cp;
1548 ira_copy_iterator ci;
1550 FOR_EACH_COPY (cp, ci)
1551 print_copy (f, cp);
1554 /* Print info about all copies into stderr. */
1555 void
1556 ira_debug_copies (void)
1558 print_copies (stderr);
1561 /* Print info about copies involving allocno A into file F. */
1562 static void
1563 print_allocno_copies (FILE *f, ira_allocno_t a)
1565 ira_allocno_t another_a;
1566 ira_copy_t cp, next_cp;
1568 fprintf (f, " a%d(r%d):", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
1569 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1571 if (cp->first == a)
1573 next_cp = cp->next_first_allocno_copy;
1574 another_a = cp->second;
1576 else if (cp->second == a)
1578 next_cp = cp->next_second_allocno_copy;
1579 another_a = cp->first;
1581 else
1582 gcc_unreachable ();
1583 fprintf (f, " cp%d:a%d(r%d)@%d", cp->num,
1584 ALLOCNO_NUM (another_a), ALLOCNO_REGNO (another_a), cp->freq);
1586 fprintf (f, "\n");
1589 DEBUG_FUNCTION void
1590 debug (ira_allocno &ref)
1592 print_allocno_copies (stderr, &ref);
1595 DEBUG_FUNCTION void
1596 debug (ira_allocno *ptr)
1598 if (ptr)
1599 debug (*ptr);
1600 else
1601 fprintf (stderr, "<nil>\n");
1605 /* Print info about copies involving allocno A into stderr. */
1606 void
1607 ira_debug_allocno_copies (ira_allocno_t a)
1609 print_allocno_copies (stderr, a);
1612 /* The function frees memory allocated for copy CP. */
1613 static void
1614 finish_copy (ira_copy_t cp)
1616 pool_free (copy_pool, cp);
1620 /* Free memory allocated for all copies. */
1621 static void
1622 finish_copies (void)
1624 ira_copy_t cp;
1625 ira_copy_iterator ci;
1627 FOR_EACH_COPY (cp, ci)
1628 finish_copy (cp);
1629 copy_vec.release ();
1630 free_alloc_pool (copy_pool);
1635 /* Pools for cost vectors. It is defined only for allocno classes. */
1636 static pool_allocator<int> * cost_vector_pool[N_REG_CLASSES];
1638 /* The function initiates work with hard register cost vectors. It
1639 creates allocation pool for each allocno class. */
1640 static void
1641 initiate_cost_vectors (void)
1643 int i;
1644 enum reg_class aclass;
1646 for (i = 0; i < ira_allocno_classes_num; i++)
1648 aclass = ira_allocno_classes[i];
1649 cost_vector_pool[aclass] = new pool_allocator<int>
1650 ("cost vectors", 100,
1651 sizeof (int) * (ira_class_hard_regs_num[aclass] - 1));
1655 /* Allocate and return a cost vector VEC for ACLASS. */
1656 int *
1657 ira_allocate_cost_vector (reg_class_t aclass)
1659 return cost_vector_pool[(int) aclass]->allocate ();
1662 /* Free a cost vector VEC for ACLASS. */
1663 void
1664 ira_free_cost_vector (int *vec, reg_class_t aclass)
1666 ira_assert (vec != NULL);
1667 cost_vector_pool[(int) aclass]->remove (vec);
1670 /* Finish work with hard register cost vectors. Release allocation
1671 pool for each allocno class. */
1672 static void
1673 finish_cost_vectors (void)
1675 int i;
1676 enum reg_class aclass;
1678 for (i = 0; i < ira_allocno_classes_num; i++)
1680 aclass = ira_allocno_classes[i];
1681 delete cost_vector_pool[aclass];
1687 /* Compute a post-ordering of the reverse control flow of the loop body
1688 designated by the children nodes of LOOP_NODE, whose body nodes in
1689 pre-order are input as LOOP_PREORDER. Return a VEC with a post-order
1690 of the reverse loop body.
1692 For the post-order of the reverse CFG, we visit the basic blocks in
1693 LOOP_PREORDER array in the reverse order of where they appear.
1694 This is important: We do not just want to compute a post-order of
1695 the reverse CFG, we want to make a best-guess for a visiting order that
1696 minimizes the number of chain elements per allocno live range. If the
1697 blocks would be visited in a different order, we would still compute a
1698 correct post-ordering but it would be less likely that two nodes
1699 connected by an edge in the CFG are neighbours in the topsort. */
1701 static vec<ira_loop_tree_node_t>
1702 ira_loop_tree_body_rev_postorder (ira_loop_tree_node_t loop_node ATTRIBUTE_UNUSED,
1703 vec<ira_loop_tree_node_t> loop_preorder)
1705 vec<ira_loop_tree_node_t> topsort_nodes = vNULL;
1706 unsigned int n_loop_preorder;
1708 n_loop_preorder = loop_preorder.length ();
1709 if (n_loop_preorder != 0)
1711 ira_loop_tree_node_t subloop_node;
1712 unsigned int i;
1713 auto_vec<ira_loop_tree_node_t> dfs_stack;
1715 /* This is a bit of strange abuse of the BB_VISITED flag: We use
1716 the flag to mark blocks we still have to visit to add them to
1717 our post-order. Define an alias to avoid confusion. */
1718 #define BB_TO_VISIT BB_VISITED
1720 FOR_EACH_VEC_ELT (loop_preorder, i, subloop_node)
1722 gcc_checking_assert (! (subloop_node->bb->flags & BB_TO_VISIT));
1723 subloop_node->bb->flags |= BB_TO_VISIT;
1726 topsort_nodes.create (n_loop_preorder);
1727 dfs_stack.create (n_loop_preorder);
1729 FOR_EACH_VEC_ELT_REVERSE (loop_preorder, i, subloop_node)
1731 if (! (subloop_node->bb->flags & BB_TO_VISIT))
1732 continue;
1734 subloop_node->bb->flags &= ~BB_TO_VISIT;
1735 dfs_stack.quick_push (subloop_node);
1736 while (! dfs_stack.is_empty ())
1738 edge e;
1739 edge_iterator ei;
1741 ira_loop_tree_node_t n = dfs_stack.last ();
1742 FOR_EACH_EDGE (e, ei, n->bb->preds)
1744 ira_loop_tree_node_t pred_node;
1745 basic_block pred_bb = e->src;
1747 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1748 continue;
1750 pred_node = IRA_BB_NODE_BY_INDEX (pred_bb->index);
1751 if (pred_node != n
1752 && (pred_node->bb->flags & BB_TO_VISIT))
1754 pred_node->bb->flags &= ~BB_TO_VISIT;
1755 dfs_stack.quick_push (pred_node);
1758 if (n == dfs_stack.last ())
1760 dfs_stack.pop ();
1761 topsort_nodes.quick_push (n);
1766 #undef BB_TO_VISIT
1769 gcc_assert (topsort_nodes.length () == n_loop_preorder);
1770 return topsort_nodes;
1773 /* The current loop tree node and its regno allocno map. */
1774 ira_loop_tree_node_t ira_curr_loop_tree_node;
1775 ira_allocno_t *ira_curr_regno_allocno_map;
1777 /* This recursive function traverses loop tree with root LOOP_NODE
1778 calling non-null functions PREORDER_FUNC and POSTORDER_FUNC
1779 correspondingly in preorder and postorder. The function sets up
1780 IRA_CURR_LOOP_TREE_NODE and IRA_CURR_REGNO_ALLOCNO_MAP. If BB_P,
1781 basic block nodes of LOOP_NODE is also processed (before its
1782 subloop nodes).
1784 If BB_P is set and POSTORDER_FUNC is given, the basic blocks in
1785 the loop are passed in the *reverse* post-order of the *reverse*
1786 CFG. This is only used by ira_create_allocno_live_ranges, which
1787 wants to visit basic blocks in this order to minimize the number
1788 of elements per live range chain.
1789 Note that the loop tree nodes are still visited in the normal,
1790 forward post-order of the loop tree. */
1792 void
1793 ira_traverse_loop_tree (bool bb_p, ira_loop_tree_node_t loop_node,
1794 void (*preorder_func) (ira_loop_tree_node_t),
1795 void (*postorder_func) (ira_loop_tree_node_t))
1797 ira_loop_tree_node_t subloop_node;
1799 ira_assert (loop_node->bb == NULL);
1800 ira_curr_loop_tree_node = loop_node;
1801 ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
1803 if (preorder_func != NULL)
1804 (*preorder_func) (loop_node);
1806 if (bb_p)
1808 auto_vec<ira_loop_tree_node_t> loop_preorder;
1809 unsigned int i;
1811 /* Add all nodes to the set of nodes to visit. The IRA loop tree
1812 is set up such that nodes in the loop body appear in a pre-order
1813 of their place in the CFG. */
1814 for (subloop_node = loop_node->children;
1815 subloop_node != NULL;
1816 subloop_node = subloop_node->next)
1817 if (subloop_node->bb != NULL)
1818 loop_preorder.safe_push (subloop_node);
1820 if (preorder_func != NULL)
1821 FOR_EACH_VEC_ELT (loop_preorder, i, subloop_node)
1822 (*preorder_func) (subloop_node);
1824 if (postorder_func != NULL)
1826 vec<ira_loop_tree_node_t> loop_rev_postorder =
1827 ira_loop_tree_body_rev_postorder (loop_node, loop_preorder);
1828 FOR_EACH_VEC_ELT_REVERSE (loop_rev_postorder, i, subloop_node)
1829 (*postorder_func) (subloop_node);
1830 loop_rev_postorder.release ();
1834 for (subloop_node = loop_node->subloops;
1835 subloop_node != NULL;
1836 subloop_node = subloop_node->subloop_next)
1838 ira_assert (subloop_node->bb == NULL);
1839 ira_traverse_loop_tree (bb_p, subloop_node,
1840 preorder_func, postorder_func);
1843 ira_curr_loop_tree_node = loop_node;
1844 ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
1846 if (postorder_func != NULL)
1847 (*postorder_func) (loop_node);
1852 /* The basic block currently being processed. */
1853 static basic_block curr_bb;
1855 /* This recursive function creates allocnos corresponding to
1856 pseudo-registers containing in X. True OUTPUT_P means that X is
1857 an lvalue. PARENT corresponds to the parent expression of X. */
1858 static void
1859 create_insn_allocnos (rtx x, rtx outer, bool output_p)
1861 int i, j;
1862 const char *fmt;
1863 enum rtx_code code = GET_CODE (x);
1865 if (code == REG)
1867 int regno;
1869 if ((regno = REGNO (x)) >= FIRST_PSEUDO_REGISTER)
1871 ira_allocno_t a;
1873 if ((a = ira_curr_regno_allocno_map[regno]) == NULL)
1875 a = ira_create_allocno (regno, false, ira_curr_loop_tree_node);
1876 if (outer != NULL && GET_CODE (outer) == SUBREG)
1878 machine_mode wmode = GET_MODE (outer);
1879 if (GET_MODE_SIZE (wmode) > GET_MODE_SIZE (ALLOCNO_WMODE (a)))
1880 ALLOCNO_WMODE (a) = wmode;
1884 ALLOCNO_NREFS (a)++;
1885 ALLOCNO_FREQ (a) += REG_FREQ_FROM_BB (curr_bb);
1886 if (output_p)
1887 bitmap_set_bit (ira_curr_loop_tree_node->modified_regnos, regno);
1889 return;
1891 else if (code == SET)
1893 create_insn_allocnos (SET_DEST (x), NULL, true);
1894 create_insn_allocnos (SET_SRC (x), NULL, false);
1895 return;
1897 else if (code == CLOBBER)
1899 create_insn_allocnos (XEXP (x, 0), NULL, true);
1900 return;
1902 else if (code == MEM)
1904 create_insn_allocnos (XEXP (x, 0), NULL, false);
1905 return;
1907 else if (code == PRE_DEC || code == POST_DEC || code == PRE_INC ||
1908 code == POST_INC || code == POST_MODIFY || code == PRE_MODIFY)
1910 create_insn_allocnos (XEXP (x, 0), NULL, true);
1911 create_insn_allocnos (XEXP (x, 0), NULL, false);
1912 return;
1915 fmt = GET_RTX_FORMAT (code);
1916 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1918 if (fmt[i] == 'e')
1919 create_insn_allocnos (XEXP (x, i), x, output_p);
1920 else if (fmt[i] == 'E')
1921 for (j = 0; j < XVECLEN (x, i); j++)
1922 create_insn_allocnos (XVECEXP (x, i, j), x, output_p);
1926 /* Create allocnos corresponding to pseudo-registers living in the
1927 basic block represented by the corresponding loop tree node
1928 BB_NODE. */
1929 static void
1930 create_bb_allocnos (ira_loop_tree_node_t bb_node)
1932 basic_block bb;
1933 rtx_insn *insn;
1934 unsigned int i;
1935 bitmap_iterator bi;
1937 curr_bb = bb = bb_node->bb;
1938 ira_assert (bb != NULL);
1939 FOR_BB_INSNS_REVERSE (bb, insn)
1940 if (NONDEBUG_INSN_P (insn))
1941 create_insn_allocnos (PATTERN (insn), NULL, false);
1942 /* It might be a allocno living through from one subloop to
1943 another. */
1944 EXECUTE_IF_SET_IN_REG_SET (df_get_live_in (bb), FIRST_PSEUDO_REGISTER, i, bi)
1945 if (ira_curr_regno_allocno_map[i] == NULL)
1946 ira_create_allocno (i, false, ira_curr_loop_tree_node);
1949 /* Create allocnos corresponding to pseudo-registers living on edge E
1950 (a loop entry or exit). Also mark the allocnos as living on the
1951 loop border. */
1952 static void
1953 create_loop_allocnos (edge e)
1955 unsigned int i;
1956 bitmap live_in_regs, border_allocnos;
1957 bitmap_iterator bi;
1958 ira_loop_tree_node_t parent;
1960 live_in_regs = df_get_live_in (e->dest);
1961 border_allocnos = ira_curr_loop_tree_node->border_allocnos;
1962 EXECUTE_IF_SET_IN_REG_SET (df_get_live_out (e->src),
1963 FIRST_PSEUDO_REGISTER, i, bi)
1964 if (bitmap_bit_p (live_in_regs, i))
1966 if (ira_curr_regno_allocno_map[i] == NULL)
1968 /* The order of creations is important for right
1969 ira_regno_allocno_map. */
1970 if ((parent = ira_curr_loop_tree_node->parent) != NULL
1971 && parent->regno_allocno_map[i] == NULL)
1972 ira_create_allocno (i, false, parent);
1973 ira_create_allocno (i, false, ira_curr_loop_tree_node);
1975 bitmap_set_bit (border_allocnos,
1976 ALLOCNO_NUM (ira_curr_regno_allocno_map[i]));
1980 /* Create allocnos corresponding to pseudo-registers living in loop
1981 represented by the corresponding loop tree node LOOP_NODE. This
1982 function is called by ira_traverse_loop_tree. */
1983 static void
1984 create_loop_tree_node_allocnos (ira_loop_tree_node_t loop_node)
1986 if (loop_node->bb != NULL)
1987 create_bb_allocnos (loop_node);
1988 else if (loop_node != ira_loop_tree_root)
1990 int i;
1991 edge_iterator ei;
1992 edge e;
1993 vec<edge> edges;
1995 ira_assert (current_loops != NULL);
1996 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
1997 if (e->src != loop_node->loop->latch)
1998 create_loop_allocnos (e);
2000 edges = get_loop_exit_edges (loop_node->loop);
2001 FOR_EACH_VEC_ELT (edges, i, e)
2002 create_loop_allocnos (e);
2003 edges.release ();
2007 /* Propagate information about allocnos modified inside the loop given
2008 by its LOOP_TREE_NODE to its parent. */
2009 static void
2010 propagate_modified_regnos (ira_loop_tree_node_t loop_tree_node)
2012 if (loop_tree_node == ira_loop_tree_root)
2013 return;
2014 ira_assert (loop_tree_node->bb == NULL);
2015 bitmap_ior_into (loop_tree_node->parent->modified_regnos,
2016 loop_tree_node->modified_regnos);
2019 /* Propagate new info about allocno A (see comments about accumulated
2020 info in allocno definition) to the corresponding allocno on upper
2021 loop tree level. So allocnos on upper levels accumulate
2022 information about the corresponding allocnos in nested regions.
2023 The new info means allocno info finally calculated in this
2024 file. */
2025 static void
2026 propagate_allocno_info (void)
2028 int i;
2029 ira_allocno_t a, parent_a;
2030 ira_loop_tree_node_t parent;
2031 enum reg_class aclass;
2033 if (flag_ira_region != IRA_REGION_ALL
2034 && flag_ira_region != IRA_REGION_MIXED)
2035 return;
2036 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
2037 for (a = ira_regno_allocno_map[i];
2038 a != NULL;
2039 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2040 if ((parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) != NULL
2041 && (parent_a = parent->regno_allocno_map[i]) != NULL
2042 /* There are no caps yet at this point. So use
2043 border_allocnos to find allocnos for the propagation. */
2044 && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE (a)->border_allocnos,
2045 ALLOCNO_NUM (a)))
2047 if (! ALLOCNO_BAD_SPILL_P (a))
2048 ALLOCNO_BAD_SPILL_P (parent_a) = false;
2049 ALLOCNO_NREFS (parent_a) += ALLOCNO_NREFS (a);
2050 ALLOCNO_FREQ (parent_a) += ALLOCNO_FREQ (a);
2051 ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
2052 merge_hard_reg_conflicts (a, parent_a, true);
2053 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
2054 += ALLOCNO_CALLS_CROSSED_NUM (a);
2055 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
2056 += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
2057 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (parent_a),
2058 ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
2059 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
2060 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2061 aclass = ALLOCNO_CLASS (a);
2062 ira_assert (aclass == ALLOCNO_CLASS (parent_a));
2063 ira_allocate_and_accumulate_costs
2064 (&ALLOCNO_HARD_REG_COSTS (parent_a), aclass,
2065 ALLOCNO_HARD_REG_COSTS (a));
2066 ira_allocate_and_accumulate_costs
2067 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a),
2068 aclass,
2069 ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
2070 ALLOCNO_CLASS_COST (parent_a)
2071 += ALLOCNO_CLASS_COST (a);
2072 ALLOCNO_MEMORY_COST (parent_a) += ALLOCNO_MEMORY_COST (a);
2076 /* Create allocnos corresponding to pseudo-registers in the current
2077 function. Traverse the loop tree for this. */
2078 static void
2079 create_allocnos (void)
2081 /* We need to process BB first to correctly link allocnos by member
2082 next_regno_allocno. */
2083 ira_traverse_loop_tree (true, ira_loop_tree_root,
2084 create_loop_tree_node_allocnos, NULL);
2085 if (optimize)
2086 ira_traverse_loop_tree (false, ira_loop_tree_root, NULL,
2087 propagate_modified_regnos);
2092 /* The page contains function to remove some regions from a separate
2093 register allocation. We remove regions whose separate allocation
2094 will hardly improve the result. As a result we speed up regional
2095 register allocation. */
2097 /* The function changes the object in range list given by R to OBJ. */
2098 static void
2099 change_object_in_range_list (live_range_t r, ira_object_t obj)
2101 for (; r != NULL; r = r->next)
2102 r->object = obj;
2105 /* Move all live ranges associated with allocno FROM to allocno TO. */
2106 static void
2107 move_allocno_live_ranges (ira_allocno_t from, ira_allocno_t to)
2109 int i;
2110 int n = ALLOCNO_NUM_OBJECTS (from);
2112 gcc_assert (n == ALLOCNO_NUM_OBJECTS (to));
2114 for (i = 0; i < n; i++)
2116 ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
2117 ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
2118 live_range_t lr = OBJECT_LIVE_RANGES (from_obj);
2120 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2122 fprintf (ira_dump_file,
2123 " Moving ranges of a%dr%d to a%dr%d: ",
2124 ALLOCNO_NUM (from), ALLOCNO_REGNO (from),
2125 ALLOCNO_NUM (to), ALLOCNO_REGNO (to));
2126 ira_print_live_range_list (ira_dump_file, lr);
2128 change_object_in_range_list (lr, to_obj);
2129 OBJECT_LIVE_RANGES (to_obj)
2130 = ira_merge_live_ranges (lr, OBJECT_LIVE_RANGES (to_obj));
2131 OBJECT_LIVE_RANGES (from_obj) = NULL;
2135 static void
2136 copy_allocno_live_ranges (ira_allocno_t from, ira_allocno_t to)
2138 int i;
2139 int n = ALLOCNO_NUM_OBJECTS (from);
2141 gcc_assert (n == ALLOCNO_NUM_OBJECTS (to));
2143 for (i = 0; i < n; i++)
2145 ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
2146 ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
2147 live_range_t lr = OBJECT_LIVE_RANGES (from_obj);
2149 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2151 fprintf (ira_dump_file, " Copying ranges of a%dr%d to a%dr%d: ",
2152 ALLOCNO_NUM (from), ALLOCNO_REGNO (from),
2153 ALLOCNO_NUM (to), ALLOCNO_REGNO (to));
2154 ira_print_live_range_list (ira_dump_file, lr);
2156 lr = ira_copy_live_range_list (lr);
2157 change_object_in_range_list (lr, to_obj);
2158 OBJECT_LIVE_RANGES (to_obj)
2159 = ira_merge_live_ranges (lr, OBJECT_LIVE_RANGES (to_obj));
2163 /* Return TRUE if NODE represents a loop with low register
2164 pressure. */
2165 static bool
2166 low_pressure_loop_node_p (ira_loop_tree_node_t node)
2168 int i;
2169 enum reg_class pclass;
2171 if (node->bb != NULL)
2172 return false;
2174 for (i = 0; i < ira_pressure_classes_num; i++)
2176 pclass = ira_pressure_classes[i];
2177 if (node->reg_pressure[pclass] > ira_class_hard_regs_num[pclass]
2178 && ira_class_hard_regs_num[pclass] > 1)
2179 return false;
2181 return true;
2184 #ifdef STACK_REGS
2185 /* Return TRUE if LOOP has a complex enter or exit edge. We don't
2186 form a region from such loop if the target use stack register
2187 because reg-stack.c can not deal with such edges. */
2188 static bool
2189 loop_with_complex_edge_p (struct loop *loop)
2191 int i;
2192 edge_iterator ei;
2193 edge e;
2194 vec<edge> edges;
2195 bool res;
2197 FOR_EACH_EDGE (e, ei, loop->header->preds)
2198 if (e->flags & EDGE_EH)
2199 return true;
2200 edges = get_loop_exit_edges (loop);
2201 res = false;
2202 FOR_EACH_VEC_ELT (edges, i, e)
2203 if (e->flags & EDGE_COMPLEX)
2205 res = true;
2206 break;
2208 edges.release ();
2209 return res;
2211 #endif
2213 /* Sort loops for marking them for removal. We put already marked
2214 loops first, then less frequent loops next, and then outer loops
2215 next. */
2216 static int
2217 loop_compare_func (const void *v1p, const void *v2p)
2219 int diff;
2220 ira_loop_tree_node_t l1 = *(const ira_loop_tree_node_t *) v1p;
2221 ira_loop_tree_node_t l2 = *(const ira_loop_tree_node_t *) v2p;
2223 ira_assert (l1->parent != NULL && l2->parent != NULL);
2224 if (l1->to_remove_p && ! l2->to_remove_p)
2225 return -1;
2226 if (! l1->to_remove_p && l2->to_remove_p)
2227 return 1;
2228 if ((diff = l1->loop->header->frequency - l2->loop->header->frequency) != 0)
2229 return diff;
2230 if ((diff = (int) loop_depth (l1->loop) - (int) loop_depth (l2->loop)) != 0)
2231 return diff;
2232 /* Make sorting stable. */
2233 return l1->loop_num - l2->loop_num;
2236 /* Mark loops which should be removed from regional allocation. We
2237 remove a loop with low register pressure inside another loop with
2238 register pressure. In this case a separate allocation of the loop
2239 hardly helps (for irregular register file architecture it could
2240 help by choosing a better hard register in the loop but we prefer
2241 faster allocation even in this case). We also remove cheap loops
2242 if there are more than IRA_MAX_LOOPS_NUM of them. Loop with EH
2243 exit or enter edges are removed too because the allocation might
2244 require put pseudo moves on the EH edges (we could still do this
2245 for pseudos with caller saved hard registers in some cases but it
2246 is impossible to say here or during top-down allocation pass what
2247 hard register the pseudos get finally). */
2248 static void
2249 mark_loops_for_removal (void)
2251 int i, n;
2252 ira_loop_tree_node_t *sorted_loops;
2253 loop_p loop;
2255 ira_assert (current_loops != NULL);
2256 sorted_loops
2257 = (ira_loop_tree_node_t *) ira_allocate (sizeof (ira_loop_tree_node_t)
2258 * number_of_loops (cfun));
2259 for (n = i = 0; vec_safe_iterate (get_loops (cfun), i, &loop); i++)
2260 if (ira_loop_nodes[i].regno_allocno_map != NULL)
2262 if (ira_loop_nodes[i].parent == NULL)
2264 /* Don't remove the root. */
2265 ira_loop_nodes[i].to_remove_p = false;
2266 continue;
2268 sorted_loops[n++] = &ira_loop_nodes[i];
2269 ira_loop_nodes[i].to_remove_p
2270 = ((low_pressure_loop_node_p (ira_loop_nodes[i].parent)
2271 && low_pressure_loop_node_p (&ira_loop_nodes[i]))
2272 #ifdef STACK_REGS
2273 || loop_with_complex_edge_p (ira_loop_nodes[i].loop)
2274 #endif
2277 qsort (sorted_loops, n, sizeof (ira_loop_tree_node_t), loop_compare_func);
2278 for (i = 0; n - i + 1 > IRA_MAX_LOOPS_NUM; i++)
2280 sorted_loops[i]->to_remove_p = true;
2281 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2282 fprintf
2283 (ira_dump_file,
2284 " Mark loop %d (header %d, freq %d, depth %d) for removal (%s)\n",
2285 sorted_loops[i]->loop_num, sorted_loops[i]->loop->header->index,
2286 sorted_loops[i]->loop->header->frequency,
2287 loop_depth (sorted_loops[i]->loop),
2288 low_pressure_loop_node_p (sorted_loops[i]->parent)
2289 && low_pressure_loop_node_p (sorted_loops[i])
2290 ? "low pressure" : "cheap loop");
2292 ira_free (sorted_loops);
2295 /* Mark all loops but root for removing. */
2296 static void
2297 mark_all_loops_for_removal (void)
2299 int i;
2300 loop_p loop;
2302 ira_assert (current_loops != NULL);
2303 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
2304 if (ira_loop_nodes[i].regno_allocno_map != NULL)
2306 if (ira_loop_nodes[i].parent == NULL)
2308 /* Don't remove the root. */
2309 ira_loop_nodes[i].to_remove_p = false;
2310 continue;
2312 ira_loop_nodes[i].to_remove_p = true;
2313 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2314 fprintf
2315 (ira_dump_file,
2316 " Mark loop %d (header %d, freq %d, depth %d) for removal\n",
2317 ira_loop_nodes[i].loop_num,
2318 ira_loop_nodes[i].loop->header->index,
2319 ira_loop_nodes[i].loop->header->frequency,
2320 loop_depth (ira_loop_nodes[i].loop));
2324 /* Definition of vector of loop tree nodes. */
2326 /* Vec containing references to all removed loop tree nodes. */
2327 static vec<ira_loop_tree_node_t> removed_loop_vec;
2329 /* Vec containing references to all children of loop tree nodes. */
2330 static vec<ira_loop_tree_node_t> children_vec;
2332 /* Remove subregions of NODE if their separate allocation will not
2333 improve the result. */
2334 static void
2335 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_node_t node)
2337 unsigned int start;
2338 bool remove_p;
2339 ira_loop_tree_node_t subnode;
2341 remove_p = node->to_remove_p;
2342 if (! remove_p)
2343 children_vec.safe_push (node);
2344 start = children_vec.length ();
2345 for (subnode = node->children; subnode != NULL; subnode = subnode->next)
2346 if (subnode->bb == NULL)
2347 remove_uneccesary_loop_nodes_from_loop_tree (subnode);
2348 else
2349 children_vec.safe_push (subnode);
2350 node->children = node->subloops = NULL;
2351 if (remove_p)
2353 removed_loop_vec.safe_push (node);
2354 return;
2356 while (children_vec.length () > start)
2358 subnode = children_vec.pop ();
2359 subnode->parent = node;
2360 subnode->next = node->children;
2361 node->children = subnode;
2362 if (subnode->bb == NULL)
2364 subnode->subloop_next = node->subloops;
2365 node->subloops = subnode;
2370 /* Return TRUE if NODE is inside PARENT. */
2371 static bool
2372 loop_is_inside_p (ira_loop_tree_node_t node, ira_loop_tree_node_t parent)
2374 for (node = node->parent; node != NULL; node = node->parent)
2375 if (node == parent)
2376 return true;
2377 return false;
2380 /* Sort allocnos according to their order in regno allocno list. */
2381 static int
2382 regno_allocno_order_compare_func (const void *v1p, const void *v2p)
2384 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2385 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2386 ira_loop_tree_node_t n1 = ALLOCNO_LOOP_TREE_NODE (a1);
2387 ira_loop_tree_node_t n2 = ALLOCNO_LOOP_TREE_NODE (a2);
2389 if (loop_is_inside_p (n1, n2))
2390 return -1;
2391 else if (loop_is_inside_p (n2, n1))
2392 return 1;
2393 /* If allocnos are equally good, sort by allocno numbers, so that
2394 the results of qsort leave nothing to chance. We put allocnos
2395 with higher number first in the list because it is the original
2396 order for allocnos from loops on the same levels. */
2397 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
2400 /* This array is used to sort allocnos to restore allocno order in
2401 the regno allocno list. */
2402 static ira_allocno_t *regno_allocnos;
2404 /* Restore allocno order for REGNO in the regno allocno list. */
2405 static void
2406 ira_rebuild_regno_allocno_list (int regno)
2408 int i, n;
2409 ira_allocno_t a;
2411 for (n = 0, a = ira_regno_allocno_map[regno];
2412 a != NULL;
2413 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2414 regno_allocnos[n++] = a;
2415 ira_assert (n > 0);
2416 qsort (regno_allocnos, n, sizeof (ira_allocno_t),
2417 regno_allocno_order_compare_func);
2418 for (i = 1; i < n; i++)
2419 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[i - 1]) = regno_allocnos[i];
2420 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[n - 1]) = NULL;
2421 ira_regno_allocno_map[regno] = regno_allocnos[0];
2422 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2423 fprintf (ira_dump_file, " Rebuilding regno allocno list for %d\n", regno);
2426 /* Propagate info from allocno FROM_A to allocno A. */
2427 static void
2428 propagate_some_info_from_allocno (ira_allocno_t a, ira_allocno_t from_a)
2430 enum reg_class aclass;
2432 merge_hard_reg_conflicts (from_a, a, false);
2433 ALLOCNO_NREFS (a) += ALLOCNO_NREFS (from_a);
2434 ALLOCNO_FREQ (a) += ALLOCNO_FREQ (from_a);
2435 ALLOCNO_CALL_FREQ (a) += ALLOCNO_CALL_FREQ (from_a);
2436 ALLOCNO_CALLS_CROSSED_NUM (a) += ALLOCNO_CALLS_CROSSED_NUM (from_a);
2437 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a)
2438 += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (from_a);
2439 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a),
2440 ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (from_a));
2442 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
2443 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (from_a);
2444 if (! ALLOCNO_BAD_SPILL_P (from_a))
2445 ALLOCNO_BAD_SPILL_P (a) = false;
2446 aclass = ALLOCNO_CLASS (from_a);
2447 ira_assert (aclass == ALLOCNO_CLASS (a));
2448 ira_allocate_and_accumulate_costs (&ALLOCNO_HARD_REG_COSTS (a), aclass,
2449 ALLOCNO_HARD_REG_COSTS (from_a));
2450 ira_allocate_and_accumulate_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
2451 aclass,
2452 ALLOCNO_CONFLICT_HARD_REG_COSTS (from_a));
2453 ALLOCNO_CLASS_COST (a) += ALLOCNO_CLASS_COST (from_a);
2454 ALLOCNO_MEMORY_COST (a) += ALLOCNO_MEMORY_COST (from_a);
2457 /* Remove allocnos from loops removed from the allocation
2458 consideration. */
2459 static void
2460 remove_unnecessary_allocnos (void)
2462 int regno;
2463 bool merged_p, rebuild_p;
2464 ira_allocno_t a, prev_a, next_a, parent_a;
2465 ira_loop_tree_node_t a_node, parent;
2467 merged_p = false;
2468 regno_allocnos = NULL;
2469 for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
2471 rebuild_p = false;
2472 for (prev_a = NULL, a = ira_regno_allocno_map[regno];
2473 a != NULL;
2474 a = next_a)
2476 next_a = ALLOCNO_NEXT_REGNO_ALLOCNO (a);
2477 a_node = ALLOCNO_LOOP_TREE_NODE (a);
2478 if (! a_node->to_remove_p)
2479 prev_a = a;
2480 else
2482 for (parent = a_node->parent;
2483 (parent_a = parent->regno_allocno_map[regno]) == NULL
2484 && parent->to_remove_p;
2485 parent = parent->parent)
2487 if (parent_a == NULL)
2489 /* There are no allocnos with the same regno in
2490 upper region -- just move the allocno to the
2491 upper region. */
2492 prev_a = a;
2493 ALLOCNO_LOOP_TREE_NODE (a) = parent;
2494 parent->regno_allocno_map[regno] = a;
2495 bitmap_set_bit (parent->all_allocnos, ALLOCNO_NUM (a));
2496 rebuild_p = true;
2498 else
2500 /* Remove the allocno and update info of allocno in
2501 the upper region. */
2502 if (prev_a == NULL)
2503 ira_regno_allocno_map[regno] = next_a;
2504 else
2505 ALLOCNO_NEXT_REGNO_ALLOCNO (prev_a) = next_a;
2506 move_allocno_live_ranges (a, parent_a);
2507 merged_p = true;
2508 propagate_some_info_from_allocno (parent_a, a);
2509 /* Remove it from the corresponding regno allocno
2510 map to avoid info propagation of subsequent
2511 allocno into this already removed allocno. */
2512 a_node->regno_allocno_map[regno] = NULL;
2513 ira_remove_allocno_prefs (a);
2514 finish_allocno (a);
2518 if (rebuild_p)
2519 /* We need to restore the order in regno allocno list. */
2521 if (regno_allocnos == NULL)
2522 regno_allocnos
2523 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
2524 * ira_allocnos_num);
2525 ira_rebuild_regno_allocno_list (regno);
2528 if (merged_p)
2529 ira_rebuild_start_finish_chains ();
2530 if (regno_allocnos != NULL)
2531 ira_free (regno_allocnos);
2534 /* Remove allocnos from all loops but the root. */
2535 static void
2536 remove_low_level_allocnos (void)
2538 int regno;
2539 bool merged_p, propagate_p;
2540 ira_allocno_t a, top_a;
2541 ira_loop_tree_node_t a_node, parent;
2542 ira_allocno_iterator ai;
2544 merged_p = false;
2545 FOR_EACH_ALLOCNO (a, ai)
2547 a_node = ALLOCNO_LOOP_TREE_NODE (a);
2548 if (a_node == ira_loop_tree_root || ALLOCNO_CAP_MEMBER (a) != NULL)
2549 continue;
2550 regno = ALLOCNO_REGNO (a);
2551 if ((top_a = ira_loop_tree_root->regno_allocno_map[regno]) == NULL)
2553 ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
2554 ira_loop_tree_root->regno_allocno_map[regno] = a;
2555 continue;
2557 propagate_p = a_node->parent->regno_allocno_map[regno] == NULL;
2558 /* Remove the allocno and update info of allocno in the upper
2559 region. */
2560 move_allocno_live_ranges (a, top_a);
2561 merged_p = true;
2562 if (propagate_p)
2563 propagate_some_info_from_allocno (top_a, a);
2565 FOR_EACH_ALLOCNO (a, ai)
2567 a_node = ALLOCNO_LOOP_TREE_NODE (a);
2568 if (a_node == ira_loop_tree_root)
2569 continue;
2570 parent = a_node->parent;
2571 regno = ALLOCNO_REGNO (a);
2572 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2573 ira_assert (ALLOCNO_CAP (a) != NULL);
2574 else if (ALLOCNO_CAP (a) == NULL)
2575 ira_assert (parent->regno_allocno_map[regno] != NULL);
2577 FOR_EACH_ALLOCNO (a, ai)
2579 regno = ALLOCNO_REGNO (a);
2580 if (ira_loop_tree_root->regno_allocno_map[regno] == a)
2582 ira_object_t obj;
2583 ira_allocno_object_iterator oi;
2585 ira_regno_allocno_map[regno] = a;
2586 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = NULL;
2587 ALLOCNO_CAP_MEMBER (a) = NULL;
2588 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
2589 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
2590 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
2591 #ifdef STACK_REGS
2592 if (ALLOCNO_TOTAL_NO_STACK_REG_P (a))
2593 ALLOCNO_NO_STACK_REG_P (a) = true;
2594 #endif
2596 else
2598 ira_remove_allocno_prefs (a);
2599 finish_allocno (a);
2602 if (merged_p)
2603 ira_rebuild_start_finish_chains ();
2606 /* Remove loops from consideration. We remove all loops except for
2607 root if ALL_P or loops for which a separate allocation will not
2608 improve the result. We have to do this after allocno creation and
2609 their costs and allocno class evaluation because only after that
2610 the register pressure can be known and is calculated. */
2611 static void
2612 remove_unnecessary_regions (bool all_p)
2614 if (current_loops == NULL)
2615 return;
2616 if (all_p)
2617 mark_all_loops_for_removal ();
2618 else
2619 mark_loops_for_removal ();
2620 children_vec.create (last_basic_block_for_fn (cfun)
2621 + number_of_loops (cfun));
2622 removed_loop_vec.create (last_basic_block_for_fn (cfun)
2623 + number_of_loops (cfun));
2624 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_root);
2625 children_vec.release ();
2626 if (all_p)
2627 remove_low_level_allocnos ();
2628 else
2629 remove_unnecessary_allocnos ();
2630 while (removed_loop_vec.length () > 0)
2631 finish_loop_tree_node (removed_loop_vec.pop ());
2632 removed_loop_vec.release ();
2637 /* At this point true value of allocno attribute bad_spill_p means
2638 that there is an insn where allocno occurs and where the allocno
2639 can not be used as memory. The function updates the attribute, now
2640 it can be true only for allocnos which can not be used as memory in
2641 an insn and in whose live ranges there is other allocno deaths.
2642 Spilling allocnos with true value will not improve the code because
2643 it will not make other allocnos colorable and additional reloads
2644 for the corresponding pseudo will be generated in reload pass for
2645 each insn it occurs.
2647 This is a trick mentioned in one classic article of Chaitin etc
2648 which is frequently omitted in other implementations of RA based on
2649 graph coloring. */
2650 static void
2651 update_bad_spill_attribute (void)
2653 int i;
2654 ira_allocno_t a;
2655 ira_allocno_iterator ai;
2656 ira_allocno_object_iterator aoi;
2657 ira_object_t obj;
2658 live_range_t r;
2659 enum reg_class aclass;
2660 bitmap_head dead_points[N_REG_CLASSES];
2662 for (i = 0; i < ira_allocno_classes_num; i++)
2664 aclass = ira_allocno_classes[i];
2665 bitmap_initialize (&dead_points[aclass], &reg_obstack);
2667 FOR_EACH_ALLOCNO (a, ai)
2669 aclass = ALLOCNO_CLASS (a);
2670 if (aclass == NO_REGS)
2671 continue;
2672 FOR_EACH_ALLOCNO_OBJECT (a, obj, aoi)
2673 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
2674 bitmap_set_bit (&dead_points[aclass], r->finish);
2676 FOR_EACH_ALLOCNO (a, ai)
2678 aclass = ALLOCNO_CLASS (a);
2679 if (aclass == NO_REGS)
2680 continue;
2681 if (! ALLOCNO_BAD_SPILL_P (a))
2682 continue;
2683 FOR_EACH_ALLOCNO_OBJECT (a, obj, aoi)
2685 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
2687 for (i = r->start + 1; i < r->finish; i++)
2688 if (bitmap_bit_p (&dead_points[aclass], i))
2689 break;
2690 if (i < r->finish)
2691 break;
2693 if (r != NULL)
2695 ALLOCNO_BAD_SPILL_P (a) = false;
2696 break;
2700 for (i = 0; i < ira_allocno_classes_num; i++)
2702 aclass = ira_allocno_classes[i];
2703 bitmap_clear (&dead_points[aclass]);
2709 /* Set up minimal and maximal live range points for allocnos. */
2710 static void
2711 setup_min_max_allocno_live_range_point (void)
2713 int i;
2714 ira_allocno_t a, parent_a, cap;
2715 ira_allocno_iterator ai;
2716 #ifdef ENABLE_IRA_CHECKING
2717 ira_object_iterator oi;
2718 ira_object_t obj;
2719 #endif
2720 live_range_t r;
2721 ira_loop_tree_node_t parent;
2723 FOR_EACH_ALLOCNO (a, ai)
2725 int n = ALLOCNO_NUM_OBJECTS (a);
2727 for (i = 0; i < n; i++)
2729 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2730 r = OBJECT_LIVE_RANGES (obj);
2731 if (r == NULL)
2732 continue;
2733 OBJECT_MAX (obj) = r->finish;
2734 for (; r->next != NULL; r = r->next)
2736 OBJECT_MIN (obj) = r->start;
2739 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
2740 for (a = ira_regno_allocno_map[i];
2741 a != NULL;
2742 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2744 int j;
2745 int n = ALLOCNO_NUM_OBJECTS (a);
2747 for (j = 0; j < n; j++)
2749 ira_object_t obj = ALLOCNO_OBJECT (a, j);
2750 ira_object_t parent_obj;
2752 if (OBJECT_MAX (obj) < 0)
2753 continue;
2754 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2755 /* Accumulation of range info. */
2756 if (ALLOCNO_CAP (a) != NULL)
2758 for (cap = ALLOCNO_CAP (a); cap != NULL; cap = ALLOCNO_CAP (cap))
2760 ira_object_t cap_obj = ALLOCNO_OBJECT (cap, j);
2761 if (OBJECT_MAX (cap_obj) < OBJECT_MAX (obj))
2762 OBJECT_MAX (cap_obj) = OBJECT_MAX (obj);
2763 if (OBJECT_MIN (cap_obj) > OBJECT_MIN (obj))
2764 OBJECT_MIN (cap_obj) = OBJECT_MIN (obj);
2766 continue;
2768 if ((parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) == NULL)
2769 continue;
2770 parent_a = parent->regno_allocno_map[i];
2771 parent_obj = ALLOCNO_OBJECT (parent_a, j);
2772 if (OBJECT_MAX (parent_obj) < OBJECT_MAX (obj))
2773 OBJECT_MAX (parent_obj) = OBJECT_MAX (obj);
2774 if (OBJECT_MIN (parent_obj) > OBJECT_MIN (obj))
2775 OBJECT_MIN (parent_obj) = OBJECT_MIN (obj);
2778 #ifdef ENABLE_IRA_CHECKING
2779 FOR_EACH_OBJECT (obj, oi)
2781 if ((0 <= OBJECT_MIN (obj) && OBJECT_MIN (obj) <= ira_max_point)
2782 && (0 <= OBJECT_MAX (obj) && OBJECT_MAX (obj) <= ira_max_point))
2783 continue;
2784 gcc_unreachable ();
2786 #endif
2789 /* Sort allocnos according to their live ranges. Allocnos with
2790 smaller allocno class are put first unless we use priority
2791 coloring. Allocnos with the same class are ordered according
2792 their start (min). Allocnos with the same start are ordered
2793 according their finish (max). */
2794 static int
2795 object_range_compare_func (const void *v1p, const void *v2p)
2797 int diff;
2798 ira_object_t obj1 = *(const ira_object_t *) v1p;
2799 ira_object_t obj2 = *(const ira_object_t *) v2p;
2800 ira_allocno_t a1 = OBJECT_ALLOCNO (obj1);
2801 ira_allocno_t a2 = OBJECT_ALLOCNO (obj2);
2803 if ((diff = OBJECT_MIN (obj1) - OBJECT_MIN (obj2)) != 0)
2804 return diff;
2805 if ((diff = OBJECT_MAX (obj1) - OBJECT_MAX (obj2)) != 0)
2806 return diff;
2807 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2810 /* Sort ira_object_id_map and set up conflict id of allocnos. */
2811 static void
2812 sort_conflict_id_map (void)
2814 int i, num;
2815 ira_allocno_t a;
2816 ira_allocno_iterator ai;
2818 num = 0;
2819 FOR_EACH_ALLOCNO (a, ai)
2821 ira_allocno_object_iterator oi;
2822 ira_object_t obj;
2824 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
2825 ira_object_id_map[num++] = obj;
2827 if (num > 1)
2828 qsort (ira_object_id_map, num, sizeof (ira_object_t),
2829 object_range_compare_func);
2830 for (i = 0; i < num; i++)
2832 ira_object_t obj = ira_object_id_map[i];
2834 gcc_assert (obj != NULL);
2835 OBJECT_CONFLICT_ID (obj) = i;
2837 for (i = num; i < ira_objects_num; i++)
2838 ira_object_id_map[i] = NULL;
2841 /* Set up minimal and maximal conflict ids of allocnos with which
2842 given allocno can conflict. */
2843 static void
2844 setup_min_max_conflict_allocno_ids (void)
2846 int aclass;
2847 int i, j, min, max, start, finish, first_not_finished, filled_area_start;
2848 int *live_range_min, *last_lived;
2849 int word0_min, word0_max;
2850 ira_allocno_t a;
2851 ira_allocno_iterator ai;
2853 live_range_min = (int *) ira_allocate (sizeof (int) * ira_objects_num);
2854 aclass = -1;
2855 first_not_finished = -1;
2856 for (i = 0; i < ira_objects_num; i++)
2858 ira_object_t obj = ira_object_id_map[i];
2860 if (obj == NULL)
2861 continue;
2863 a = OBJECT_ALLOCNO (obj);
2865 if (aclass < 0)
2867 aclass = ALLOCNO_CLASS (a);
2868 min = i;
2869 first_not_finished = i;
2871 else
2873 start = OBJECT_MIN (obj);
2874 /* If we skip an allocno, the allocno with smaller ids will
2875 be also skipped because of the secondary sorting the
2876 range finishes (see function
2877 object_range_compare_func). */
2878 while (first_not_finished < i
2879 && start > OBJECT_MAX (ira_object_id_map
2880 [first_not_finished]))
2881 first_not_finished++;
2882 min = first_not_finished;
2884 if (min == i)
2885 /* We could increase min further in this case but it is good
2886 enough. */
2887 min++;
2888 live_range_min[i] = OBJECT_MIN (obj);
2889 OBJECT_MIN (obj) = min;
2891 last_lived = (int *) ira_allocate (sizeof (int) * ira_max_point);
2892 aclass = -1;
2893 filled_area_start = -1;
2894 for (i = ira_objects_num - 1; i >= 0; i--)
2896 ira_object_t obj = ira_object_id_map[i];
2898 if (obj == NULL)
2899 continue;
2901 a = OBJECT_ALLOCNO (obj);
2902 if (aclass < 0)
2904 aclass = ALLOCNO_CLASS (a);
2905 for (j = 0; j < ira_max_point; j++)
2906 last_lived[j] = -1;
2907 filled_area_start = ira_max_point;
2909 min = live_range_min[i];
2910 finish = OBJECT_MAX (obj);
2911 max = last_lived[finish];
2912 if (max < 0)
2913 /* We could decrease max further in this case but it is good
2914 enough. */
2915 max = OBJECT_CONFLICT_ID (obj) - 1;
2916 OBJECT_MAX (obj) = max;
2917 /* In filling, we can go further A range finish to recognize
2918 intersection quickly because if the finish of subsequently
2919 processed allocno (it has smaller conflict id) range is
2920 further A range finish than they are definitely intersected
2921 (the reason for this is the allocnos with bigger conflict id
2922 have their range starts not smaller than allocnos with
2923 smaller ids. */
2924 for (j = min; j < filled_area_start; j++)
2925 last_lived[j] = i;
2926 filled_area_start = min;
2928 ira_free (last_lived);
2929 ira_free (live_range_min);
2931 /* For allocnos with more than one object, we may later record extra conflicts in
2932 subobject 0 that we cannot really know about here.
2933 For now, simply widen the min/max range of these subobjects. */
2935 word0_min = INT_MAX;
2936 word0_max = INT_MIN;
2938 FOR_EACH_ALLOCNO (a, ai)
2940 int n = ALLOCNO_NUM_OBJECTS (a);
2941 ira_object_t obj0;
2943 if (n < 2)
2944 continue;
2945 obj0 = ALLOCNO_OBJECT (a, 0);
2946 if (OBJECT_CONFLICT_ID (obj0) < word0_min)
2947 word0_min = OBJECT_CONFLICT_ID (obj0);
2948 if (OBJECT_CONFLICT_ID (obj0) > word0_max)
2949 word0_max = OBJECT_CONFLICT_ID (obj0);
2951 FOR_EACH_ALLOCNO (a, ai)
2953 int n = ALLOCNO_NUM_OBJECTS (a);
2954 ira_object_t obj0;
2956 if (n < 2)
2957 continue;
2958 obj0 = ALLOCNO_OBJECT (a, 0);
2959 if (OBJECT_MIN (obj0) > word0_min)
2960 OBJECT_MIN (obj0) = word0_min;
2961 if (OBJECT_MAX (obj0) < word0_max)
2962 OBJECT_MAX (obj0) = word0_max;
2968 static void
2969 create_caps (void)
2971 ira_allocno_t a;
2972 ira_allocno_iterator ai;
2973 ira_loop_tree_node_t loop_tree_node;
2975 FOR_EACH_ALLOCNO (a, ai)
2977 if (ALLOCNO_LOOP_TREE_NODE (a) == ira_loop_tree_root)
2978 continue;
2979 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2980 create_cap_allocno (a);
2981 else if (ALLOCNO_CAP (a) == NULL)
2983 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
2984 if (!bitmap_bit_p (loop_tree_node->border_allocnos, ALLOCNO_NUM (a)))
2985 create_cap_allocno (a);
2992 /* The page contains code transforming more one region internal
2993 representation (IR) to one region IR which is necessary for reload.
2994 This transformation is called IR flattening. We might just rebuild
2995 the IR for one region but we don't do it because it takes a lot of
2996 time. */
2998 /* Map: regno -> allocnos which will finally represent the regno for
2999 IR with one region. */
3000 static ira_allocno_t *regno_top_level_allocno_map;
3002 /* Find the allocno that corresponds to A at a level one higher up in the
3003 loop tree. Returns NULL if A is a cap, or if it has no parent. */
3004 ira_allocno_t
3005 ira_parent_allocno (ira_allocno_t a)
3007 ira_loop_tree_node_t parent;
3009 if (ALLOCNO_CAP (a) != NULL)
3010 return NULL;
3012 parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
3013 if (parent == NULL)
3014 return NULL;
3016 return parent->regno_allocno_map[ALLOCNO_REGNO (a)];
3019 /* Find the allocno that corresponds to A at a level one higher up in the
3020 loop tree. If ALLOCNO_CAP is set for A, return that. */
3021 ira_allocno_t
3022 ira_parent_or_cap_allocno (ira_allocno_t a)
3024 if (ALLOCNO_CAP (a) != NULL)
3025 return ALLOCNO_CAP (a);
3027 return ira_parent_allocno (a);
3030 /* Process all allocnos originated from pseudo REGNO and copy live
3031 ranges, hard reg conflicts, and allocno stack reg attributes from
3032 low level allocnos to final allocnos which are destinations of
3033 removed stores at a loop exit. Return true if we copied live
3034 ranges. */
3035 static bool
3036 copy_info_to_removed_store_destinations (int regno)
3038 ira_allocno_t a;
3039 ira_allocno_t parent_a = NULL;
3040 ira_loop_tree_node_t parent;
3041 bool merged_p;
3043 merged_p = false;
3044 for (a = ira_regno_allocno_map[regno];
3045 a != NULL;
3046 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
3048 if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))])
3049 /* This allocno will be removed. */
3050 continue;
3052 /* Caps will be removed. */
3053 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3054 for (parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
3055 parent != NULL;
3056 parent = parent->parent)
3057 if ((parent_a = parent->regno_allocno_map[regno]) == NULL
3058 || (parent_a
3059 == regno_top_level_allocno_map[REGNO
3060 (allocno_emit_reg (parent_a))]
3061 && ALLOCNO_EMIT_DATA (parent_a)->mem_optimized_dest_p))
3062 break;
3063 if (parent == NULL || parent_a == NULL)
3064 continue;
3066 copy_allocno_live_ranges (a, parent_a);
3067 merge_hard_reg_conflicts (a, parent_a, true);
3069 ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
3070 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
3071 += ALLOCNO_CALLS_CROSSED_NUM (a);
3072 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
3073 += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
3074 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (parent_a),
3075 ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
3076 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
3077 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
3078 merged_p = true;
3080 return merged_p;
3083 /* Flatten the IR. In other words, this function transforms IR as if
3084 it were built with one region (without loops). We could make it
3085 much simpler by rebuilding IR with one region, but unfortunately it
3086 takes a lot of time. MAX_REGNO_BEFORE_EMIT and
3087 IRA_MAX_POINT_BEFORE_EMIT are correspondingly MAX_REG_NUM () and
3088 IRA_MAX_POINT before emitting insns on the loop borders. */
3089 void
3090 ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
3092 int i, j;
3093 bool keep_p;
3094 int hard_regs_num;
3095 bool new_pseudos_p, merged_p, mem_dest_p;
3096 unsigned int n;
3097 enum reg_class aclass;
3098 ira_allocno_t a, parent_a, first, second, node_first, node_second;
3099 ira_copy_t cp;
3100 ira_loop_tree_node_t node;
3101 live_range_t r;
3102 ira_allocno_iterator ai;
3103 ira_copy_iterator ci;
3105 regno_top_level_allocno_map
3106 = (ira_allocno_t *) ira_allocate (max_reg_num ()
3107 * sizeof (ira_allocno_t));
3108 memset (regno_top_level_allocno_map, 0,
3109 max_reg_num () * sizeof (ira_allocno_t));
3110 new_pseudos_p = merged_p = false;
3111 FOR_EACH_ALLOCNO (a, ai)
3113 ira_allocno_object_iterator oi;
3114 ira_object_t obj;
3116 if (ALLOCNO_CAP_MEMBER (a) != NULL)
3117 /* Caps are not in the regno allocno maps and they are never
3118 will be transformed into allocnos existing after IR
3119 flattening. */
3120 continue;
3121 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
3122 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
3123 OBJECT_CONFLICT_HARD_REGS (obj));
3124 #ifdef STACK_REGS
3125 ALLOCNO_TOTAL_NO_STACK_REG_P (a) = ALLOCNO_NO_STACK_REG_P (a);
3126 #endif
3128 /* Fix final allocno attributes. */
3129 for (i = max_regno_before_emit - 1; i >= FIRST_PSEUDO_REGISTER; i--)
3131 mem_dest_p = false;
3132 for (a = ira_regno_allocno_map[i];
3133 a != NULL;
3134 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
3136 ira_emit_data_t parent_data, data = ALLOCNO_EMIT_DATA (a);
3138 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3139 if (data->somewhere_renamed_p)
3140 new_pseudos_p = true;
3141 parent_a = ira_parent_allocno (a);
3142 if (parent_a == NULL)
3144 ALLOCNO_COPIES (a) = NULL;
3145 regno_top_level_allocno_map[REGNO (data->reg)] = a;
3146 continue;
3148 ira_assert (ALLOCNO_CAP_MEMBER (parent_a) == NULL);
3150 if (data->mem_optimized_dest != NULL)
3151 mem_dest_p = true;
3152 parent_data = ALLOCNO_EMIT_DATA (parent_a);
3153 if (REGNO (data->reg) == REGNO (parent_data->reg))
3155 merge_hard_reg_conflicts (a, parent_a, true);
3156 move_allocno_live_ranges (a, parent_a);
3157 merged_p = true;
3158 parent_data->mem_optimized_dest_p
3159 = (parent_data->mem_optimized_dest_p
3160 || data->mem_optimized_dest_p);
3161 continue;
3163 new_pseudos_p = true;
3164 for (;;)
3166 ALLOCNO_NREFS (parent_a) -= ALLOCNO_NREFS (a);
3167 ALLOCNO_FREQ (parent_a) -= ALLOCNO_FREQ (a);
3168 ALLOCNO_CALL_FREQ (parent_a) -= ALLOCNO_CALL_FREQ (a);
3169 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
3170 -= ALLOCNO_CALLS_CROSSED_NUM (a);
3171 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
3172 -= ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
3173 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
3174 -= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
3175 ira_assert (ALLOCNO_CALLS_CROSSED_NUM (parent_a) >= 0
3176 && ALLOCNO_NREFS (parent_a) >= 0
3177 && ALLOCNO_FREQ (parent_a) >= 0);
3178 aclass = ALLOCNO_CLASS (parent_a);
3179 hard_regs_num = ira_class_hard_regs_num[aclass];
3180 if (ALLOCNO_HARD_REG_COSTS (a) != NULL
3181 && ALLOCNO_HARD_REG_COSTS (parent_a) != NULL)
3182 for (j = 0; j < hard_regs_num; j++)
3183 ALLOCNO_HARD_REG_COSTS (parent_a)[j]
3184 -= ALLOCNO_HARD_REG_COSTS (a)[j];
3185 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL
3186 && ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a) != NULL)
3187 for (j = 0; j < hard_regs_num; j++)
3188 ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a)[j]
3189 -= ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[j];
3190 ALLOCNO_CLASS_COST (parent_a)
3191 -= ALLOCNO_CLASS_COST (a);
3192 ALLOCNO_MEMORY_COST (parent_a) -= ALLOCNO_MEMORY_COST (a);
3193 parent_a = ira_parent_allocno (parent_a);
3194 if (parent_a == NULL)
3195 break;
3197 ALLOCNO_COPIES (a) = NULL;
3198 regno_top_level_allocno_map[REGNO (data->reg)] = a;
3200 if (mem_dest_p && copy_info_to_removed_store_destinations (i))
3201 merged_p = true;
3203 ira_assert (new_pseudos_p || ira_max_point_before_emit == ira_max_point);
3204 if (merged_p || ira_max_point_before_emit != ira_max_point)
3205 ira_rebuild_start_finish_chains ();
3206 if (new_pseudos_p)
3208 sparseset objects_live;
3210 /* Rebuild conflicts. */
3211 FOR_EACH_ALLOCNO (a, ai)
3213 ira_allocno_object_iterator oi;
3214 ira_object_t obj;
3216 if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
3217 || ALLOCNO_CAP_MEMBER (a) != NULL)
3218 continue;
3219 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
3221 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
3222 ira_assert (r->object == obj);
3223 clear_conflicts (obj);
3226 objects_live = sparseset_alloc (ira_objects_num);
3227 for (i = 0; i < ira_max_point; i++)
3229 for (r = ira_start_point_ranges[i]; r != NULL; r = r->start_next)
3231 ira_object_t obj = r->object;
3233 a = OBJECT_ALLOCNO (obj);
3234 if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
3235 || ALLOCNO_CAP_MEMBER (a) != NULL)
3236 continue;
3238 aclass = ALLOCNO_CLASS (a);
3239 EXECUTE_IF_SET_IN_SPARSESET (objects_live, n)
3241 ira_object_t live_obj = ira_object_id_map[n];
3242 ira_allocno_t live_a = OBJECT_ALLOCNO (live_obj);
3243 enum reg_class live_aclass = ALLOCNO_CLASS (live_a);
3245 if (ira_reg_classes_intersect_p[aclass][live_aclass]
3246 /* Don't set up conflict for the allocno with itself. */
3247 && live_a != a)
3248 ira_add_conflict (obj, live_obj);
3250 sparseset_set_bit (objects_live, OBJECT_CONFLICT_ID (obj));
3253 for (r = ira_finish_point_ranges[i]; r != NULL; r = r->finish_next)
3254 sparseset_clear_bit (objects_live, OBJECT_CONFLICT_ID (r->object));
3256 sparseset_free (objects_live);
3257 compress_conflict_vecs ();
3259 /* Mark some copies for removing and change allocnos in the rest
3260 copies. */
3261 FOR_EACH_COPY (cp, ci)
3263 if (ALLOCNO_CAP_MEMBER (cp->first) != NULL
3264 || ALLOCNO_CAP_MEMBER (cp->second) != NULL)
3266 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
3267 fprintf
3268 (ira_dump_file, " Remove cp%d:%c%dr%d-%c%dr%d\n",
3269 cp->num, ALLOCNO_CAP_MEMBER (cp->first) != NULL ? 'c' : 'a',
3270 ALLOCNO_NUM (cp->first),
3271 REGNO (allocno_emit_reg (cp->first)),
3272 ALLOCNO_CAP_MEMBER (cp->second) != NULL ? 'c' : 'a',
3273 ALLOCNO_NUM (cp->second),
3274 REGNO (allocno_emit_reg (cp->second)));
3275 cp->loop_tree_node = NULL;
3276 continue;
3278 first
3279 = regno_top_level_allocno_map[REGNO (allocno_emit_reg (cp->first))];
3280 second
3281 = regno_top_level_allocno_map[REGNO (allocno_emit_reg (cp->second))];
3282 node = cp->loop_tree_node;
3283 if (node == NULL)
3284 keep_p = true; /* It copy generated in ira-emit.c. */
3285 else
3287 /* Check that the copy was not propagated from level on
3288 which we will have different pseudos. */
3289 node_first = node->regno_allocno_map[ALLOCNO_REGNO (cp->first)];
3290 node_second = node->regno_allocno_map[ALLOCNO_REGNO (cp->second)];
3291 keep_p = ((REGNO (allocno_emit_reg (first))
3292 == REGNO (allocno_emit_reg (node_first)))
3293 && (REGNO (allocno_emit_reg (second))
3294 == REGNO (allocno_emit_reg (node_second))));
3296 if (keep_p)
3298 cp->loop_tree_node = ira_loop_tree_root;
3299 cp->first = first;
3300 cp->second = second;
3302 else
3304 cp->loop_tree_node = NULL;
3305 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
3306 fprintf (ira_dump_file, " Remove cp%d:a%dr%d-a%dr%d\n",
3307 cp->num, ALLOCNO_NUM (cp->first),
3308 REGNO (allocno_emit_reg (cp->first)),
3309 ALLOCNO_NUM (cp->second),
3310 REGNO (allocno_emit_reg (cp->second)));
3313 /* Remove unnecessary allocnos on lower levels of the loop tree. */
3314 FOR_EACH_ALLOCNO (a, ai)
3316 if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
3317 || ALLOCNO_CAP_MEMBER (a) != NULL)
3319 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
3320 fprintf (ira_dump_file, " Remove a%dr%d\n",
3321 ALLOCNO_NUM (a), REGNO (allocno_emit_reg (a)));
3322 ira_remove_allocno_prefs (a);
3323 finish_allocno (a);
3324 continue;
3326 ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
3327 ALLOCNO_REGNO (a) = REGNO (allocno_emit_reg (a));
3328 ALLOCNO_CAP (a) = NULL;
3329 /* Restore updated costs for assignments from reload. */
3330 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
3331 ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
3332 if (! ALLOCNO_ASSIGNED_P (a))
3333 ira_free_allocno_updated_costs (a);
3334 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3335 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3337 /* Remove unnecessary copies. */
3338 FOR_EACH_COPY (cp, ci)
3340 if (cp->loop_tree_node == NULL)
3342 ira_copies[cp->num] = NULL;
3343 finish_copy (cp);
3344 continue;
3346 ira_assert
3347 (ALLOCNO_LOOP_TREE_NODE (cp->first) == ira_loop_tree_root
3348 && ALLOCNO_LOOP_TREE_NODE (cp->second) == ira_loop_tree_root);
3349 add_allocno_copy_to_list (cp);
3350 swap_allocno_copy_ends_if_necessary (cp);
3352 rebuild_regno_allocno_maps ();
3353 if (ira_max_point != ira_max_point_before_emit)
3354 ira_compress_allocno_live_ranges ();
3355 ira_free (regno_top_level_allocno_map);
3360 #ifdef ENABLE_IRA_CHECKING
3361 /* Check creation of all allocnos. Allocnos on lower levels should
3362 have allocnos or caps on all upper levels. */
3363 static void
3364 check_allocno_creation (void)
3366 ira_allocno_t a;
3367 ira_allocno_iterator ai;
3368 ira_loop_tree_node_t loop_tree_node;
3370 FOR_EACH_ALLOCNO (a, ai)
3372 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
3373 ira_assert (bitmap_bit_p (loop_tree_node->all_allocnos,
3374 ALLOCNO_NUM (a)));
3375 if (loop_tree_node == ira_loop_tree_root)
3376 continue;
3377 if (ALLOCNO_CAP_MEMBER (a) != NULL)
3378 ira_assert (ALLOCNO_CAP (a) != NULL);
3379 else if (ALLOCNO_CAP (a) == NULL)
3380 ira_assert (loop_tree_node->parent
3381 ->regno_allocno_map[ALLOCNO_REGNO (a)] != NULL
3382 && bitmap_bit_p (loop_tree_node->border_allocnos,
3383 ALLOCNO_NUM (a)));
3386 #endif
3388 /* Identify allocnos which prefer a register class with a single hard register.
3389 Adjust ALLOCNO_CONFLICT_HARD_REG_COSTS so that conflicting allocnos are
3390 less likely to use the preferred singleton register. */
3391 static void
3392 update_conflict_hard_reg_costs (void)
3394 ira_allocno_t a;
3395 ira_allocno_iterator ai;
3396 int i, index, min;
3398 FOR_EACH_ALLOCNO (a, ai)
3400 reg_class_t aclass = ALLOCNO_CLASS (a);
3401 reg_class_t pref = reg_preferred_class (ALLOCNO_REGNO (a));
3402 int singleton = ira_class_singleton[pref][ALLOCNO_MODE (a)];
3403 if (singleton < 0)
3404 continue;
3405 index = ira_class_hard_reg_index[(int) aclass][singleton];
3406 if (index < 0)
3407 continue;
3408 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) == NULL
3409 || ALLOCNO_HARD_REG_COSTS (a) == NULL)
3410 continue;
3411 min = INT_MAX;
3412 for (i = ira_class_hard_regs_num[(int) aclass] - 1; i >= 0; i--)
3413 if (ALLOCNO_HARD_REG_COSTS (a)[i] > ALLOCNO_CLASS_COST (a)
3414 && min > ALLOCNO_HARD_REG_COSTS (a)[i])
3415 min = ALLOCNO_HARD_REG_COSTS (a)[i];
3416 if (min == INT_MAX)
3417 continue;
3418 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
3419 aclass, 0);
3420 ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[index]
3421 -= min - ALLOCNO_CLASS_COST (a);
3425 /* Create a internal representation (IR) for IRA (allocnos, copies,
3426 loop tree nodes). The function returns TRUE if we generate loop
3427 structure (besides nodes representing all function and the basic
3428 blocks) for regional allocation. A true return means that we
3429 really need to flatten IR before the reload. */
3430 bool
3431 ira_build (void)
3433 bool loops_p;
3435 df_analyze ();
3436 initiate_cost_vectors ();
3437 initiate_allocnos ();
3438 initiate_prefs ();
3439 initiate_copies ();
3440 create_loop_tree_nodes ();
3441 form_loop_tree ();
3442 create_allocnos ();
3443 ira_costs ();
3444 create_allocno_objects ();
3445 ira_create_allocno_live_ranges ();
3446 remove_unnecessary_regions (false);
3447 ira_compress_allocno_live_ranges ();
3448 update_bad_spill_attribute ();
3449 loops_p = more_one_region_p ();
3450 if (loops_p)
3452 propagate_allocno_info ();
3453 create_caps ();
3455 ira_tune_allocno_costs ();
3456 #ifdef ENABLE_IRA_CHECKING
3457 check_allocno_creation ();
3458 #endif
3459 setup_min_max_allocno_live_range_point ();
3460 sort_conflict_id_map ();
3461 setup_min_max_conflict_allocno_ids ();
3462 ira_build_conflicts ();
3463 update_conflict_hard_reg_costs ();
3464 if (! ira_conflicts_p)
3466 ira_allocno_t a;
3467 ira_allocno_iterator ai;
3469 /* Remove all regions but root one. */
3470 if (loops_p)
3472 remove_unnecessary_regions (true);
3473 loops_p = false;
3475 /* We don't save hard registers around calls for fast allocation
3476 -- add caller clobbered registers as conflicting ones to
3477 allocno crossing calls. */
3478 FOR_EACH_ALLOCNO (a, ai)
3479 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
3480 ior_hard_reg_conflicts (a, &call_used_reg_set);
3482 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3483 print_copies (ira_dump_file);
3484 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3485 print_prefs (ira_dump_file);
3486 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3488 int n, nr, nr_big;
3489 ira_allocno_t a;
3490 live_range_t r;
3491 ira_allocno_iterator ai;
3493 n = 0;
3494 nr = 0;
3495 nr_big = 0;
3496 FOR_EACH_ALLOCNO (a, ai)
3498 int j, nobj = ALLOCNO_NUM_OBJECTS (a);
3500 if (nobj > 1)
3501 nr_big++;
3502 for (j = 0; j < nobj; j++)
3504 ira_object_t obj = ALLOCNO_OBJECT (a, j);
3505 n += OBJECT_NUM_CONFLICTS (obj);
3506 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
3507 nr++;
3510 fprintf (ira_dump_file, " regions=%d, blocks=%d, points=%d\n",
3511 current_loops == NULL ? 1 : number_of_loops (cfun),
3512 n_basic_blocks_for_fn (cfun), ira_max_point);
3513 fprintf (ira_dump_file,
3514 " allocnos=%d (big %d), copies=%d, conflicts=%d, ranges=%d\n",
3515 ira_allocnos_num, nr_big, ira_copies_num, n, nr);
3517 return loops_p;
3520 /* Release the data created by function ira_build. */
3521 void
3522 ira_destroy (void)
3524 finish_loop_tree_nodes ();
3525 finish_prefs ();
3526 finish_copies ();
3527 finish_allocnos ();
3528 finish_cost_vectors ();
3529 ira_finish_allocno_live_ranges ();