2015-07-06 Marc Glisse <marc.glisse@inria.fr>
[official-gcc.git] / gcc / ira-build.c
blob9fcbcb0aa2b32cce5d428d97137946693c2ec879
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 "function.h"
33 #include "dominance.h"
34 #include "cfg.h"
35 #include "basic-block.h"
36 #include "insn-config.h"
37 #include "recog.h"
38 #include "diagnostic-core.h"
39 #include "params.h"
40 #include "df.h"
41 #include "reload.h"
42 #include "sparseset.h"
43 #include "ira-int.h"
44 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
46 static ira_copy_t find_allocno_copy (ira_allocno_t, ira_allocno_t, rtx_insn *,
47 ira_loop_tree_node_t);
49 /* The root of the loop tree corresponding to the all function. */
50 ira_loop_tree_node_t ira_loop_tree_root;
52 /* Height of the loop tree. */
53 int ira_loop_tree_height;
55 /* All nodes representing basic blocks are referred through the
56 following array. We can not use basic block member `aux' for this
57 because it is used for insertion of insns on edges. */
58 ira_loop_tree_node_t ira_bb_nodes;
60 /* All nodes representing loops are referred through the following
61 array. */
62 ira_loop_tree_node_t ira_loop_nodes;
64 /* And size of the ira_loop_nodes array. */
65 unsigned int ira_loop_nodes_count;
67 /* Map regno -> allocnos with given regno (see comments for
68 allocno member `next_regno_allocno'). */
69 ira_allocno_t *ira_regno_allocno_map;
71 /* Array of references to all allocnos. The order number of the
72 allocno corresponds to the index in the array. Removed allocnos
73 have NULL element value. */
74 ira_allocno_t *ira_allocnos;
76 /* Sizes of the previous array. */
77 int ira_allocnos_num;
79 /* Count of conflict record structures we've created, used when creating
80 a new conflict id. */
81 int ira_objects_num;
83 /* Map a conflict id to its conflict record. */
84 ira_object_t *ira_object_id_map;
86 /* Array of references to all allocno preferences. The order number
87 of the preference corresponds to the index in the array. */
88 ira_pref_t *ira_prefs;
90 /* Size of the previous array. */
91 int ira_prefs_num;
93 /* Array of references to all copies. The order number of the copy
94 corresponds to the index in the array. Removed copies have NULL
95 element value. */
96 ira_copy_t *ira_copies;
98 /* Size of the previous array. */
99 int ira_copies_num;
103 /* LAST_BASIC_BLOCK before generating additional insns because of live
104 range splitting. Emitting insns on a critical edge creates a new
105 basic block. */
106 static int last_basic_block_before_change;
108 /* Initialize some members in loop tree node NODE. Use LOOP_NUM for
109 the member loop_num. */
110 static void
111 init_loop_tree_node (struct ira_loop_tree_node *node, int loop_num)
113 int max_regno = max_reg_num ();
115 node->regno_allocno_map
116 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t) * max_regno);
117 memset (node->regno_allocno_map, 0, sizeof (ira_allocno_t) * max_regno);
118 memset (node->reg_pressure, 0, sizeof (node->reg_pressure));
119 node->all_allocnos = ira_allocate_bitmap ();
120 node->modified_regnos = ira_allocate_bitmap ();
121 node->border_allocnos = ira_allocate_bitmap ();
122 node->local_copies = ira_allocate_bitmap ();
123 node->loop_num = loop_num;
124 node->children = NULL;
125 node->subloops = NULL;
129 /* The following function allocates the loop tree nodes. If
130 CURRENT_LOOPS is NULL, the nodes corresponding to the loops (except
131 the root which corresponds the all function) will be not allocated
132 but nodes will still be allocated for basic blocks. */
133 static void
134 create_loop_tree_nodes (void)
136 unsigned int i, j;
137 bool skip_p;
138 edge_iterator ei;
139 edge e;
140 vec<edge> edges;
141 loop_p loop;
143 ira_bb_nodes
144 = ((struct ira_loop_tree_node *)
145 ira_allocate (sizeof (struct ira_loop_tree_node)
146 * last_basic_block_for_fn (cfun)));
147 last_basic_block_before_change = last_basic_block_for_fn (cfun);
148 for (i = 0; i < (unsigned int) last_basic_block_for_fn (cfun); i++)
150 ira_bb_nodes[i].regno_allocno_map = NULL;
151 memset (ira_bb_nodes[i].reg_pressure, 0,
152 sizeof (ira_bb_nodes[i].reg_pressure));
153 ira_bb_nodes[i].all_allocnos = NULL;
154 ira_bb_nodes[i].modified_regnos = NULL;
155 ira_bb_nodes[i].border_allocnos = NULL;
156 ira_bb_nodes[i].local_copies = NULL;
158 if (current_loops == NULL)
160 ira_loop_nodes_count = 1;
161 ira_loop_nodes = ((struct ira_loop_tree_node *)
162 ira_allocate (sizeof (struct ira_loop_tree_node)));
163 init_loop_tree_node (ira_loop_nodes, 0);
164 return;
166 ira_loop_nodes_count = number_of_loops (cfun);
167 ira_loop_nodes = ((struct ira_loop_tree_node *)
168 ira_allocate (sizeof (struct ira_loop_tree_node)
169 * ira_loop_nodes_count));
170 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
172 if (loop_outer (loop) != NULL)
174 ira_loop_nodes[i].regno_allocno_map = NULL;
175 skip_p = false;
176 FOR_EACH_EDGE (e, ei, loop->header->preds)
177 if (e->src != loop->latch
178 && (e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
180 skip_p = true;
181 break;
183 if (skip_p)
184 continue;
185 edges = get_loop_exit_edges (loop);
186 FOR_EACH_VEC_ELT (edges, j, e)
187 if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
189 skip_p = true;
190 break;
192 edges.release ();
193 if (skip_p)
194 continue;
196 init_loop_tree_node (&ira_loop_nodes[i], loop->num);
200 /* The function returns TRUE if there are more one allocation
201 region. */
202 static bool
203 more_one_region_p (void)
205 unsigned int i;
206 loop_p loop;
208 if (current_loops != NULL)
209 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
210 if (ira_loop_nodes[i].regno_allocno_map != NULL
211 && ira_loop_tree_root != &ira_loop_nodes[i])
212 return true;
213 return false;
216 /* Free the loop tree node of a loop. */
217 static void
218 finish_loop_tree_node (ira_loop_tree_node_t loop)
220 if (loop->regno_allocno_map != NULL)
222 ira_assert (loop->bb == NULL);
223 ira_free_bitmap (loop->local_copies);
224 ira_free_bitmap (loop->border_allocnos);
225 ira_free_bitmap (loop->modified_regnos);
226 ira_free_bitmap (loop->all_allocnos);
227 ira_free (loop->regno_allocno_map);
228 loop->regno_allocno_map = NULL;
232 /* Free the loop tree nodes. */
233 static void
234 finish_loop_tree_nodes (void)
236 unsigned int i;
238 for (i = 0; i < ira_loop_nodes_count; i++)
239 finish_loop_tree_node (&ira_loop_nodes[i]);
240 ira_free (ira_loop_nodes);
241 for (i = 0; i < (unsigned int) last_basic_block_before_change; i++)
243 if (ira_bb_nodes[i].local_copies != NULL)
244 ira_free_bitmap (ira_bb_nodes[i].local_copies);
245 if (ira_bb_nodes[i].border_allocnos != NULL)
246 ira_free_bitmap (ira_bb_nodes[i].border_allocnos);
247 if (ira_bb_nodes[i].modified_regnos != NULL)
248 ira_free_bitmap (ira_bb_nodes[i].modified_regnos);
249 if (ira_bb_nodes[i].all_allocnos != NULL)
250 ira_free_bitmap (ira_bb_nodes[i].all_allocnos);
251 if (ira_bb_nodes[i].regno_allocno_map != NULL)
252 ira_free (ira_bb_nodes[i].regno_allocno_map);
254 ira_free (ira_bb_nodes);
259 /* The following recursive function adds LOOP to the loop tree
260 hierarchy. LOOP is added only once. If LOOP is NULL we adding
261 loop designating the whole function when CFG loops are not
262 built. */
263 static void
264 add_loop_to_tree (struct loop *loop)
266 int loop_num;
267 struct loop *parent;
268 ira_loop_tree_node_t loop_node, parent_node;
270 /* We can not use loop node access macros here because of potential
271 checking and because the nodes are not initialized enough
272 yet. */
273 if (loop != NULL && loop_outer (loop) != NULL)
274 add_loop_to_tree (loop_outer (loop));
275 loop_num = loop != NULL ? loop->num : 0;
276 if (ira_loop_nodes[loop_num].regno_allocno_map != NULL
277 && ira_loop_nodes[loop_num].children == NULL)
279 /* We have not added loop node to the tree yet. */
280 loop_node = &ira_loop_nodes[loop_num];
281 loop_node->loop = loop;
282 loop_node->bb = NULL;
283 if (loop == NULL)
284 parent = NULL;
285 else
287 for (parent = loop_outer (loop);
288 parent != NULL;
289 parent = loop_outer (parent))
290 if (ira_loop_nodes[parent->num].regno_allocno_map != NULL)
291 break;
293 if (parent == NULL)
295 loop_node->next = NULL;
296 loop_node->subloop_next = NULL;
297 loop_node->parent = NULL;
299 else
301 parent_node = &ira_loop_nodes[parent->num];
302 loop_node->next = parent_node->children;
303 parent_node->children = loop_node;
304 loop_node->subloop_next = parent_node->subloops;
305 parent_node->subloops = loop_node;
306 loop_node->parent = parent_node;
311 /* The following recursive function sets up levels of nodes of the
312 tree given its root LOOP_NODE. The enumeration starts with LEVEL.
313 The function returns maximal value of level in the tree + 1. */
314 static int
315 setup_loop_tree_level (ira_loop_tree_node_t loop_node, int level)
317 int height, max_height;
318 ira_loop_tree_node_t subloop_node;
320 ira_assert (loop_node->bb == NULL);
321 loop_node->level = level;
322 max_height = level + 1;
323 for (subloop_node = loop_node->subloops;
324 subloop_node != NULL;
325 subloop_node = subloop_node->subloop_next)
327 ira_assert (subloop_node->bb == NULL);
328 height = setup_loop_tree_level (subloop_node, level + 1);
329 if (height > max_height)
330 max_height = height;
332 return max_height;
335 /* Create the loop tree. The algorithm is designed to provide correct
336 order of loops (they are ordered by their last loop BB) and basic
337 blocks in the chain formed by member next. */
338 static void
339 form_loop_tree (void)
341 basic_block bb;
342 struct loop *parent;
343 ira_loop_tree_node_t bb_node, loop_node;
345 /* We can not use loop/bb node access macros because of potential
346 checking and because the nodes are not initialized enough
347 yet. */
348 FOR_EACH_BB_FN (bb, cfun)
350 bb_node = &ira_bb_nodes[bb->index];
351 bb_node->bb = bb;
352 bb_node->loop = NULL;
353 bb_node->subloops = NULL;
354 bb_node->children = NULL;
355 bb_node->subloop_next = NULL;
356 bb_node->next = NULL;
357 if (current_loops == NULL)
358 parent = NULL;
359 else
361 for (parent = bb->loop_father;
362 parent != NULL;
363 parent = loop_outer (parent))
364 if (ira_loop_nodes[parent->num].regno_allocno_map != NULL)
365 break;
367 add_loop_to_tree (parent);
368 loop_node = &ira_loop_nodes[parent == NULL ? 0 : parent->num];
369 bb_node->next = loop_node->children;
370 bb_node->parent = loop_node;
371 loop_node->children = bb_node;
373 ira_loop_tree_root = IRA_LOOP_NODE_BY_INDEX (0);
374 ira_loop_tree_height = setup_loop_tree_level (ira_loop_tree_root, 0);
375 ira_assert (ira_loop_tree_root->regno_allocno_map != NULL);
380 /* Rebuild IRA_REGNO_ALLOCNO_MAP and REGNO_ALLOCNO_MAPs of the loop
381 tree nodes. */
382 static void
383 rebuild_regno_allocno_maps (void)
385 unsigned int l;
386 int max_regno, regno;
387 ira_allocno_t a;
388 ira_loop_tree_node_t loop_tree_node;
389 loop_p loop;
390 ira_allocno_iterator ai;
392 ira_assert (current_loops != NULL);
393 max_regno = max_reg_num ();
394 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), l, loop)
395 if (ira_loop_nodes[l].regno_allocno_map != NULL)
397 ira_free (ira_loop_nodes[l].regno_allocno_map);
398 ira_loop_nodes[l].regno_allocno_map
399 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
400 * max_regno);
401 memset (ira_loop_nodes[l].regno_allocno_map, 0,
402 sizeof (ira_allocno_t) * max_regno);
404 ira_free (ira_regno_allocno_map);
405 ira_regno_allocno_map
406 = (ira_allocno_t *) ira_allocate (max_regno * sizeof (ira_allocno_t));
407 memset (ira_regno_allocno_map, 0, max_regno * sizeof (ira_allocno_t));
408 FOR_EACH_ALLOCNO (a, ai)
410 if (ALLOCNO_CAP_MEMBER (a) != NULL)
411 /* Caps are not in the regno allocno maps. */
412 continue;
413 regno = ALLOCNO_REGNO (a);
414 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
415 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = ira_regno_allocno_map[regno];
416 ira_regno_allocno_map[regno] = a;
417 if (loop_tree_node->regno_allocno_map[regno] == NULL)
418 /* Remember that we can create temporary allocnos to break
419 cycles in register shuffle. */
420 loop_tree_node->regno_allocno_map[regno] = a;
425 /* Pools for allocnos, allocno live ranges and objects. */
426 static pool_allocator<live_range> live_range_pool ("live ranges", 100);
427 static pool_allocator<ira_allocno> allocno_pool ("allocnos", 100);
428 static pool_allocator<ira_object> object_pool ("objects", 100);
430 /* Vec containing references to all created allocnos. It is a
431 container of array allocnos. */
432 static vec<ira_allocno_t> allocno_vec;
434 /* Vec containing references to all created ira_objects. It is a
435 container of ira_object_id_map. */
436 static vec<ira_object_t> ira_object_id_map_vec;
438 /* Initialize data concerning allocnos. */
439 static void
440 initiate_allocnos (void)
442 allocno_vec.create (max_reg_num () * 2);
443 ira_allocnos = NULL;
444 ira_allocnos_num = 0;
445 ira_objects_num = 0;
446 ira_object_id_map_vec.create (max_reg_num () * 2);
447 ira_object_id_map = NULL;
448 ira_regno_allocno_map
449 = (ira_allocno_t *) ira_allocate (max_reg_num ()
450 * sizeof (ira_allocno_t));
451 memset (ira_regno_allocno_map, 0, max_reg_num () * sizeof (ira_allocno_t));
454 /* Create and return an object corresponding to a new allocno A. */
455 static ira_object_t
456 ira_create_object (ira_allocno_t a, int subword)
458 enum reg_class aclass = ALLOCNO_CLASS (a);
459 ira_object_t obj = object_pool.allocate ();
461 OBJECT_ALLOCNO (obj) = a;
462 OBJECT_SUBWORD (obj) = subword;
463 OBJECT_CONFLICT_ID (obj) = ira_objects_num;
464 OBJECT_CONFLICT_VEC_P (obj) = false;
465 OBJECT_CONFLICT_ARRAY (obj) = NULL;
466 OBJECT_NUM_CONFLICTS (obj) = 0;
467 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj), ira_no_alloc_regs);
468 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), ira_no_alloc_regs);
469 IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
470 reg_class_contents[aclass]);
471 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
472 reg_class_contents[aclass]);
473 OBJECT_MIN (obj) = INT_MAX;
474 OBJECT_MAX (obj) = -1;
475 OBJECT_LIVE_RANGES (obj) = NULL;
477 ira_object_id_map_vec.safe_push (obj);
478 ira_object_id_map
479 = ira_object_id_map_vec.address ();
480 ira_objects_num = ira_object_id_map_vec.length ();
482 return obj;
485 /* Create and return the allocno corresponding to REGNO in
486 LOOP_TREE_NODE. Add the allocno to the list of allocnos with the
487 same regno if CAP_P is FALSE. */
488 ira_allocno_t
489 ira_create_allocno (int regno, bool cap_p,
490 ira_loop_tree_node_t loop_tree_node)
492 ira_allocno_t a;
494 a = allocno_pool.allocate ();
495 ALLOCNO_REGNO (a) = regno;
496 ALLOCNO_LOOP_TREE_NODE (a) = loop_tree_node;
497 if (! cap_p)
499 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = ira_regno_allocno_map[regno];
500 ira_regno_allocno_map[regno] = a;
501 if (loop_tree_node->regno_allocno_map[regno] == NULL)
502 /* Remember that we can create temporary allocnos to break
503 cycles in register shuffle on region borders (see
504 ira-emit.c). */
505 loop_tree_node->regno_allocno_map[regno] = a;
507 ALLOCNO_CAP (a) = NULL;
508 ALLOCNO_CAP_MEMBER (a) = NULL;
509 ALLOCNO_NUM (a) = ira_allocnos_num;
510 bitmap_set_bit (loop_tree_node->all_allocnos, ALLOCNO_NUM (a));
511 ALLOCNO_NREFS (a) = 0;
512 ALLOCNO_FREQ (a) = 0;
513 ALLOCNO_HARD_REGNO (a) = -1;
514 ALLOCNO_CALL_FREQ (a) = 0;
515 ALLOCNO_CALLS_CROSSED_NUM (a) = 0;
516 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a) = 0;
517 CLEAR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
518 #ifdef STACK_REGS
519 ALLOCNO_NO_STACK_REG_P (a) = false;
520 ALLOCNO_TOTAL_NO_STACK_REG_P (a) = false;
521 #endif
522 ALLOCNO_DONT_REASSIGN_P (a) = false;
523 ALLOCNO_BAD_SPILL_P (a) = false;
524 ALLOCNO_ASSIGNED_P (a) = false;
525 ALLOCNO_MODE (a) = (regno < 0 ? VOIDmode : PSEUDO_REGNO_MODE (regno));
526 ALLOCNO_WMODE (a) = ALLOCNO_MODE (a);
527 ALLOCNO_PREFS (a) = NULL;
528 ALLOCNO_COPIES (a) = NULL;
529 ALLOCNO_HARD_REG_COSTS (a) = NULL;
530 ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
531 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
532 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
533 ALLOCNO_CLASS (a) = NO_REGS;
534 ALLOCNO_UPDATED_CLASS_COST (a) = 0;
535 ALLOCNO_CLASS_COST (a) = 0;
536 ALLOCNO_MEMORY_COST (a) = 0;
537 ALLOCNO_UPDATED_MEMORY_COST (a) = 0;
538 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) = 0;
539 ALLOCNO_NUM_OBJECTS (a) = 0;
541 ALLOCNO_ADD_DATA (a) = NULL;
542 allocno_vec.safe_push (a);
543 ira_allocnos = allocno_vec.address ();
544 ira_allocnos_num = allocno_vec.length ();
546 return a;
549 /* Set up register class for A and update its conflict hard
550 registers. */
551 void
552 ira_set_allocno_class (ira_allocno_t a, enum reg_class aclass)
554 ira_allocno_object_iterator oi;
555 ira_object_t obj;
557 ALLOCNO_CLASS (a) = aclass;
558 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
560 IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
561 reg_class_contents[aclass]);
562 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
563 reg_class_contents[aclass]);
567 /* Determine the number of objects we should associate with allocno A
568 and allocate them. */
569 void
570 ira_create_allocno_objects (ira_allocno_t a)
572 machine_mode mode = ALLOCNO_MODE (a);
573 enum reg_class aclass = ALLOCNO_CLASS (a);
574 int n = ira_reg_class_max_nregs[aclass][mode];
575 int i;
577 if (GET_MODE_SIZE (mode) != 2 * UNITS_PER_WORD || n != 2)
578 n = 1;
580 ALLOCNO_NUM_OBJECTS (a) = n;
581 for (i = 0; i < n; i++)
582 ALLOCNO_OBJECT (a, i) = ira_create_object (a, i);
585 /* For each allocno, set ALLOCNO_NUM_OBJECTS and create the
586 ALLOCNO_OBJECT structures. This must be called after the allocno
587 classes are known. */
588 static void
589 create_allocno_objects (void)
591 ira_allocno_t a;
592 ira_allocno_iterator ai;
594 FOR_EACH_ALLOCNO (a, ai)
595 ira_create_allocno_objects (a);
598 /* Merge hard register conflict information for all objects associated with
599 allocno TO into the corresponding objects associated with FROM.
600 If TOTAL_ONLY is true, we only merge OBJECT_TOTAL_CONFLICT_HARD_REGS. */
601 static void
602 merge_hard_reg_conflicts (ira_allocno_t from, ira_allocno_t to,
603 bool total_only)
605 int i;
606 gcc_assert (ALLOCNO_NUM_OBJECTS (to) == ALLOCNO_NUM_OBJECTS (from));
607 for (i = 0; i < ALLOCNO_NUM_OBJECTS (to); i++)
609 ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
610 ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
612 if (!total_only)
613 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (to_obj),
614 OBJECT_CONFLICT_HARD_REGS (from_obj));
615 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (to_obj),
616 OBJECT_TOTAL_CONFLICT_HARD_REGS (from_obj));
618 #ifdef STACK_REGS
619 if (!total_only && ALLOCNO_NO_STACK_REG_P (from))
620 ALLOCNO_NO_STACK_REG_P (to) = true;
621 if (ALLOCNO_TOTAL_NO_STACK_REG_P (from))
622 ALLOCNO_TOTAL_NO_STACK_REG_P (to) = true;
623 #endif
626 /* Update hard register conflict information for all objects associated with
627 A to include the regs in SET. */
628 void
629 ior_hard_reg_conflicts (ira_allocno_t a, HARD_REG_SET *set)
631 ira_allocno_object_iterator i;
632 ira_object_t obj;
634 FOR_EACH_ALLOCNO_OBJECT (a, obj, i)
636 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj), *set);
637 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), *set);
641 /* Return TRUE if a conflict vector with NUM elements is more
642 profitable than a conflict bit vector for OBJ. */
643 bool
644 ira_conflict_vector_profitable_p (ira_object_t obj, int num)
646 int nw;
647 int max = OBJECT_MAX (obj);
648 int min = OBJECT_MIN (obj);
650 if (max < min)
651 /* We prefer a bit vector in such case because it does not result
652 in allocation. */
653 return false;
655 nw = (max - min + IRA_INT_BITS) / IRA_INT_BITS;
656 return (2 * sizeof (ira_object_t) * (num + 1)
657 < 3 * nw * sizeof (IRA_INT_TYPE));
660 /* Allocates and initialize the conflict vector of OBJ for NUM
661 conflicting objects. */
662 void
663 ira_allocate_conflict_vec (ira_object_t obj, int num)
665 int size;
666 ira_object_t *vec;
668 ira_assert (OBJECT_CONFLICT_ARRAY (obj) == NULL);
669 num++; /* for NULL end marker */
670 size = sizeof (ira_object_t) * num;
671 OBJECT_CONFLICT_ARRAY (obj) = ira_allocate (size);
672 vec = (ira_object_t *) OBJECT_CONFLICT_ARRAY (obj);
673 vec[0] = NULL;
674 OBJECT_NUM_CONFLICTS (obj) = 0;
675 OBJECT_CONFLICT_ARRAY_SIZE (obj) = size;
676 OBJECT_CONFLICT_VEC_P (obj) = true;
679 /* Allocate and initialize the conflict bit vector of OBJ. */
680 static void
681 allocate_conflict_bit_vec (ira_object_t obj)
683 unsigned int size;
685 ira_assert (OBJECT_CONFLICT_ARRAY (obj) == NULL);
686 size = ((OBJECT_MAX (obj) - OBJECT_MIN (obj) + IRA_INT_BITS)
687 / IRA_INT_BITS * sizeof (IRA_INT_TYPE));
688 OBJECT_CONFLICT_ARRAY (obj) = ira_allocate (size);
689 memset (OBJECT_CONFLICT_ARRAY (obj), 0, size);
690 OBJECT_CONFLICT_ARRAY_SIZE (obj) = size;
691 OBJECT_CONFLICT_VEC_P (obj) = false;
694 /* Allocate and initialize the conflict vector or conflict bit vector
695 of OBJ for NUM conflicting allocnos whatever is more profitable. */
696 void
697 ira_allocate_object_conflicts (ira_object_t obj, int num)
699 if (ira_conflict_vector_profitable_p (obj, num))
700 ira_allocate_conflict_vec (obj, num);
701 else
702 allocate_conflict_bit_vec (obj);
705 /* Add OBJ2 to the conflicts of OBJ1. */
706 static void
707 add_to_conflicts (ira_object_t obj1, ira_object_t obj2)
709 int num;
710 unsigned int size;
712 if (OBJECT_CONFLICT_VEC_P (obj1))
714 ira_object_t *vec = OBJECT_CONFLICT_VEC (obj1);
715 int curr_num = OBJECT_NUM_CONFLICTS (obj1);
716 num = curr_num + 2;
717 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) < num * sizeof (ira_object_t))
719 ira_object_t *newvec;
720 size = (3 * num / 2 + 1) * sizeof (ira_allocno_t);
721 newvec = (ira_object_t *) ira_allocate (size);
722 memcpy (newvec, vec, curr_num * sizeof (ira_object_t));
723 ira_free (vec);
724 vec = newvec;
725 OBJECT_CONFLICT_ARRAY (obj1) = vec;
726 OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
728 vec[num - 2] = obj2;
729 vec[num - 1] = NULL;
730 OBJECT_NUM_CONFLICTS (obj1)++;
732 else
734 int nw, added_head_nw, id;
735 IRA_INT_TYPE *vec = OBJECT_CONFLICT_BITVEC (obj1);
737 id = OBJECT_CONFLICT_ID (obj2);
738 if (OBJECT_MIN (obj1) > id)
740 /* Expand head of the bit vector. */
741 added_head_nw = (OBJECT_MIN (obj1) - id - 1) / IRA_INT_BITS + 1;
742 nw = (OBJECT_MAX (obj1) - OBJECT_MIN (obj1)) / IRA_INT_BITS + 1;
743 size = (nw + added_head_nw) * sizeof (IRA_INT_TYPE);
744 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) >= size)
746 memmove ((char *) vec + added_head_nw * sizeof (IRA_INT_TYPE),
747 vec, nw * sizeof (IRA_INT_TYPE));
748 memset (vec, 0, added_head_nw * sizeof (IRA_INT_TYPE));
750 else
752 size
753 = (3 * (nw + added_head_nw) / 2 + 1) * sizeof (IRA_INT_TYPE);
754 vec = (IRA_INT_TYPE *) ira_allocate (size);
755 memcpy ((char *) vec + added_head_nw * sizeof (IRA_INT_TYPE),
756 OBJECT_CONFLICT_ARRAY (obj1), nw * sizeof (IRA_INT_TYPE));
757 memset (vec, 0, added_head_nw * sizeof (IRA_INT_TYPE));
758 memset ((char *) vec
759 + (nw + added_head_nw) * sizeof (IRA_INT_TYPE),
760 0, size - (nw + added_head_nw) * sizeof (IRA_INT_TYPE));
761 ira_free (OBJECT_CONFLICT_ARRAY (obj1));
762 OBJECT_CONFLICT_ARRAY (obj1) = vec;
763 OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
765 OBJECT_MIN (obj1) -= added_head_nw * IRA_INT_BITS;
767 else if (OBJECT_MAX (obj1) < id)
769 nw = (id - OBJECT_MIN (obj1)) / IRA_INT_BITS + 1;
770 size = nw * sizeof (IRA_INT_TYPE);
771 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) < size)
773 /* Expand tail of the bit vector. */
774 size = (3 * nw / 2 + 1) * sizeof (IRA_INT_TYPE);
775 vec = (IRA_INT_TYPE *) ira_allocate (size);
776 memcpy (vec, OBJECT_CONFLICT_ARRAY (obj1), OBJECT_CONFLICT_ARRAY_SIZE (obj1));
777 memset ((char *) vec + OBJECT_CONFLICT_ARRAY_SIZE (obj1),
778 0, size - OBJECT_CONFLICT_ARRAY_SIZE (obj1));
779 ira_free (OBJECT_CONFLICT_ARRAY (obj1));
780 OBJECT_CONFLICT_ARRAY (obj1) = vec;
781 OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
783 OBJECT_MAX (obj1) = id;
785 SET_MINMAX_SET_BIT (vec, id, OBJECT_MIN (obj1), OBJECT_MAX (obj1));
789 /* Add OBJ1 to the conflicts of OBJ2 and vice versa. */
790 static void
791 ira_add_conflict (ira_object_t obj1, ira_object_t obj2)
793 add_to_conflicts (obj1, obj2);
794 add_to_conflicts (obj2, obj1);
797 /* Clear all conflicts of OBJ. */
798 static void
799 clear_conflicts (ira_object_t obj)
801 if (OBJECT_CONFLICT_VEC_P (obj))
803 OBJECT_NUM_CONFLICTS (obj) = 0;
804 OBJECT_CONFLICT_VEC (obj)[0] = NULL;
806 else if (OBJECT_CONFLICT_ARRAY_SIZE (obj) != 0)
808 int nw;
810 nw = (OBJECT_MAX (obj) - OBJECT_MIN (obj)) / IRA_INT_BITS + 1;
811 memset (OBJECT_CONFLICT_BITVEC (obj), 0, nw * sizeof (IRA_INT_TYPE));
815 /* The array used to find duplications in conflict vectors of
816 allocnos. */
817 static int *conflict_check;
819 /* The value used to mark allocation presence in conflict vector of
820 the current allocno. */
821 static int curr_conflict_check_tick;
823 /* Remove duplications in conflict vector of OBJ. */
824 static void
825 compress_conflict_vec (ira_object_t obj)
827 ira_object_t *vec, conflict_obj;
828 int i, j;
830 ira_assert (OBJECT_CONFLICT_VEC_P (obj));
831 vec = OBJECT_CONFLICT_VEC (obj);
832 curr_conflict_check_tick++;
833 for (i = j = 0; (conflict_obj = vec[i]) != NULL; i++)
835 int id = OBJECT_CONFLICT_ID (conflict_obj);
836 if (conflict_check[id] != curr_conflict_check_tick)
838 conflict_check[id] = curr_conflict_check_tick;
839 vec[j++] = conflict_obj;
842 OBJECT_NUM_CONFLICTS (obj) = j;
843 vec[j] = NULL;
846 /* Remove duplications in conflict vectors of all allocnos. */
847 static void
848 compress_conflict_vecs (void)
850 ira_object_t obj;
851 ira_object_iterator oi;
853 conflict_check = (int *) ira_allocate (sizeof (int) * ira_objects_num);
854 memset (conflict_check, 0, sizeof (int) * ira_objects_num);
855 curr_conflict_check_tick = 0;
856 FOR_EACH_OBJECT (obj, oi)
858 if (OBJECT_CONFLICT_VEC_P (obj))
859 compress_conflict_vec (obj);
861 ira_free (conflict_check);
864 /* This recursive function outputs allocno A and if it is a cap the
865 function outputs its members. */
866 void
867 ira_print_expanded_allocno (ira_allocno_t a)
869 basic_block bb;
871 fprintf (ira_dump_file, " a%d(r%d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
872 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
873 fprintf (ira_dump_file, ",b%d", bb->index);
874 else
875 fprintf (ira_dump_file, ",l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
876 if (ALLOCNO_CAP_MEMBER (a) != NULL)
878 fprintf (ira_dump_file, ":");
879 ira_print_expanded_allocno (ALLOCNO_CAP_MEMBER (a));
881 fprintf (ira_dump_file, ")");
884 /* Create and return the cap representing allocno A in the
885 parent loop. */
886 static ira_allocno_t
887 create_cap_allocno (ira_allocno_t a)
889 ira_allocno_t cap;
890 ira_loop_tree_node_t parent;
891 enum reg_class aclass;
893 parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
894 cap = ira_create_allocno (ALLOCNO_REGNO (a), true, parent);
895 ALLOCNO_MODE (cap) = ALLOCNO_MODE (a);
896 ALLOCNO_WMODE (cap) = ALLOCNO_WMODE (a);
897 aclass = ALLOCNO_CLASS (a);
898 ira_set_allocno_class (cap, aclass);
899 ira_create_allocno_objects (cap);
900 ALLOCNO_CAP_MEMBER (cap) = a;
901 ALLOCNO_CAP (a) = cap;
902 ALLOCNO_CLASS_COST (cap) = ALLOCNO_CLASS_COST (a);
903 ALLOCNO_MEMORY_COST (cap) = ALLOCNO_MEMORY_COST (a);
904 ira_allocate_and_copy_costs
905 (&ALLOCNO_HARD_REG_COSTS (cap), aclass, ALLOCNO_HARD_REG_COSTS (a));
906 ira_allocate_and_copy_costs
907 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (cap), aclass,
908 ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
909 ALLOCNO_BAD_SPILL_P (cap) = ALLOCNO_BAD_SPILL_P (a);
910 ALLOCNO_NREFS (cap) = ALLOCNO_NREFS (a);
911 ALLOCNO_FREQ (cap) = ALLOCNO_FREQ (a);
912 ALLOCNO_CALL_FREQ (cap) = ALLOCNO_CALL_FREQ (a);
914 merge_hard_reg_conflicts (a, cap, false);
916 ALLOCNO_CALLS_CROSSED_NUM (cap) = ALLOCNO_CALLS_CROSSED_NUM (a);
917 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (cap) = ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
918 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (cap),
919 ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
920 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
922 fprintf (ira_dump_file, " Creating cap ");
923 ira_print_expanded_allocno (cap);
924 fprintf (ira_dump_file, "\n");
926 return cap;
929 /* Create and return a live range for OBJECT with given attributes. */
930 live_range_t
931 ira_create_live_range (ira_object_t obj, int start, int finish,
932 live_range_t next)
934 live_range_t p;
936 p = live_range_pool.allocate ();
937 p->object = obj;
938 p->start = start;
939 p->finish = finish;
940 p->next = next;
941 return p;
944 /* Create a new live range for OBJECT and queue it at the head of its
945 live range list. */
946 void
947 ira_add_live_range_to_object (ira_object_t object, int start, int finish)
949 live_range_t p;
950 p = ira_create_live_range (object, start, finish,
951 OBJECT_LIVE_RANGES (object));
952 OBJECT_LIVE_RANGES (object) = p;
955 /* Copy allocno live range R and return the result. */
956 static live_range_t
957 copy_live_range (live_range_t r)
959 live_range_t p;
961 p = live_range_pool.allocate ();
962 *p = *r;
963 return p;
966 /* Copy allocno live range list given by its head R and return the
967 result. */
968 live_range_t
969 ira_copy_live_range_list (live_range_t r)
971 live_range_t p, first, last;
973 if (r == NULL)
974 return NULL;
975 for (first = last = NULL; r != NULL; r = r->next)
977 p = copy_live_range (r);
978 if (first == NULL)
979 first = p;
980 else
981 last->next = p;
982 last = p;
984 return first;
987 /* Merge ranges R1 and R2 and returns the result. The function
988 maintains the order of ranges and tries to minimize number of the
989 result ranges. */
990 live_range_t
991 ira_merge_live_ranges (live_range_t r1, live_range_t r2)
993 live_range_t first, last;
995 if (r1 == NULL)
996 return r2;
997 if (r2 == NULL)
998 return r1;
999 for (first = last = NULL; r1 != NULL && r2 != NULL;)
1001 if (r1->start < r2->start)
1002 std::swap (r1, r2);
1003 if (r1->start <= r2->finish + 1)
1005 /* Intersected ranges: merge r1 and r2 into r1. */
1006 r1->start = r2->start;
1007 if (r1->finish < r2->finish)
1008 r1->finish = r2->finish;
1009 live_range_t temp = r2;
1010 r2 = r2->next;
1011 ira_finish_live_range (temp);
1012 if (r2 == NULL)
1014 /* To try to merge with subsequent ranges in r1. */
1015 r2 = r1->next;
1016 r1->next = NULL;
1019 else
1021 /* Add r1 to the result. */
1022 if (first == NULL)
1023 first = last = r1;
1024 else
1026 last->next = r1;
1027 last = r1;
1029 r1 = r1->next;
1030 if (r1 == NULL)
1032 /* To try to merge with subsequent ranges in r2. */
1033 r1 = r2->next;
1034 r2->next = NULL;
1038 if (r1 != NULL)
1040 if (first == NULL)
1041 first = r1;
1042 else
1043 last->next = r1;
1044 ira_assert (r1->next == NULL);
1046 else if (r2 != NULL)
1048 if (first == NULL)
1049 first = r2;
1050 else
1051 last->next = r2;
1052 ira_assert (r2->next == NULL);
1054 else
1056 ira_assert (last->next == NULL);
1058 return first;
1061 /* Return TRUE if live ranges R1 and R2 intersect. */
1062 bool
1063 ira_live_ranges_intersect_p (live_range_t r1, live_range_t r2)
1065 /* Remember the live ranges are always kept ordered. */
1066 while (r1 != NULL && r2 != NULL)
1068 if (r1->start > r2->finish)
1069 r1 = r1->next;
1070 else if (r2->start > r1->finish)
1071 r2 = r2->next;
1072 else
1073 return true;
1075 return false;
1078 /* Free allocno live range R. */
1079 void
1080 ira_finish_live_range (live_range_t r)
1082 live_range_pool.remove (r);
1085 /* Free list of allocno live ranges starting with R. */
1086 void
1087 ira_finish_live_range_list (live_range_t r)
1089 live_range_t next_r;
1091 for (; r != NULL; r = next_r)
1093 next_r = r->next;
1094 ira_finish_live_range (r);
1098 /* Free updated register costs of allocno A. */
1099 void
1100 ira_free_allocno_updated_costs (ira_allocno_t a)
1102 enum reg_class aclass;
1104 aclass = ALLOCNO_CLASS (a);
1105 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
1106 ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass);
1107 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
1108 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
1109 ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1110 aclass);
1111 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
1114 /* Free and nullify all cost vectors allocated earlier for allocno
1115 A. */
1116 static void
1117 ira_free_allocno_costs (ira_allocno_t a)
1119 enum reg_class aclass = ALLOCNO_CLASS (a);
1120 ira_object_t obj;
1121 ira_allocno_object_iterator oi;
1123 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
1125 ira_finish_live_range_list (OBJECT_LIVE_RANGES (obj));
1126 ira_object_id_map[OBJECT_CONFLICT_ID (obj)] = NULL;
1127 if (OBJECT_CONFLICT_ARRAY (obj) != NULL)
1128 ira_free (OBJECT_CONFLICT_ARRAY (obj));
1129 object_pool.remove (obj);
1132 ira_allocnos[ALLOCNO_NUM (a)] = NULL;
1133 if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
1134 ira_free_cost_vector (ALLOCNO_HARD_REG_COSTS (a), aclass);
1135 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL)
1136 ira_free_cost_vector (ALLOCNO_CONFLICT_HARD_REG_COSTS (a), aclass);
1137 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
1138 ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass);
1139 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
1140 ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1141 aclass);
1142 ALLOCNO_HARD_REG_COSTS (a) = NULL;
1143 ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
1144 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
1145 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
1148 /* Free the memory allocated for allocno A. */
1149 static void
1150 finish_allocno (ira_allocno_t a)
1152 ira_free_allocno_costs (a);
1153 allocno_pool.remove (a);
1156 /* Free the memory allocated for all allocnos. */
1157 static void
1158 finish_allocnos (void)
1160 ira_allocno_t a;
1161 ira_allocno_iterator ai;
1163 FOR_EACH_ALLOCNO (a, ai)
1164 finish_allocno (a);
1165 ira_free (ira_regno_allocno_map);
1166 ira_object_id_map_vec.release ();
1167 allocno_vec.release ();
1168 allocno_pool.release ();
1169 object_pool.release ();
1170 live_range_pool.release ();
1175 /* Pools for allocno preferences. */
1176 static pool_allocator <ira_allocno_pref> pref_pool ("prefs", 100);
1178 /* Vec containing references to all created preferences. It is a
1179 container of array ira_prefs. */
1180 static vec<ira_pref_t> pref_vec;
1182 /* The function initializes data concerning allocno prefs. */
1183 static void
1184 initiate_prefs (void)
1186 pref_vec.create (get_max_uid ());
1187 ira_prefs = NULL;
1188 ira_prefs_num = 0;
1191 /* Return pref for A and HARD_REGNO if any. */
1192 static ira_pref_t
1193 find_allocno_pref (ira_allocno_t a, int hard_regno)
1195 ira_pref_t pref;
1197 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
1198 if (pref->allocno == a && pref->hard_regno == hard_regno)
1199 return pref;
1200 return NULL;
1203 /* Create and return pref with given attributes A, HARD_REGNO, and FREQ. */
1204 ira_pref_t
1205 ira_create_pref (ira_allocno_t a, int hard_regno, int freq)
1207 ira_pref_t pref;
1209 pref = pref_pool.allocate ();
1210 pref->num = ira_prefs_num;
1211 pref->allocno = a;
1212 pref->hard_regno = hard_regno;
1213 pref->freq = freq;
1214 pref_vec.safe_push (pref);
1215 ira_prefs = pref_vec.address ();
1216 ira_prefs_num = pref_vec.length ();
1217 return pref;
1220 /* Attach a pref PREF to the corresponding allocno. */
1221 static void
1222 add_allocno_pref_to_list (ira_pref_t pref)
1224 ira_allocno_t a = pref->allocno;
1226 pref->next_pref = ALLOCNO_PREFS (a);
1227 ALLOCNO_PREFS (a) = pref;
1230 /* Create (or update frequency if the pref already exists) the pref of
1231 allocnos A preferring HARD_REGNO with frequency FREQ. */
1232 void
1233 ira_add_allocno_pref (ira_allocno_t a, int hard_regno, int freq)
1235 ira_pref_t pref;
1237 if (freq <= 0)
1238 return;
1239 if ((pref = find_allocno_pref (a, hard_regno)) != NULL)
1241 pref->freq += freq;
1242 return;
1244 pref = ira_create_pref (a, hard_regno, freq);
1245 ira_assert (a != NULL);
1246 add_allocno_pref_to_list (pref);
1249 /* Print info about PREF into file F. */
1250 static void
1251 print_pref (FILE *f, ira_pref_t pref)
1253 fprintf (f, " pref%d:a%d(r%d)<-hr%d@%d\n", pref->num,
1254 ALLOCNO_NUM (pref->allocno), ALLOCNO_REGNO (pref->allocno),
1255 pref->hard_regno, pref->freq);
1258 /* Print info about PREF into stderr. */
1259 void
1260 ira_debug_pref (ira_pref_t pref)
1262 print_pref (stderr, pref);
1265 /* Print info about all prefs into file F. */
1266 static void
1267 print_prefs (FILE *f)
1269 ira_pref_t pref;
1270 ira_pref_iterator pi;
1272 FOR_EACH_PREF (pref, pi)
1273 print_pref (f, pref);
1276 /* Print info about all prefs into stderr. */
1277 void
1278 ira_debug_prefs (void)
1280 print_prefs (stderr);
1283 /* Print info about prefs involving allocno A into file F. */
1284 static void
1285 print_allocno_prefs (FILE *f, ira_allocno_t a)
1287 ira_pref_t pref;
1289 fprintf (f, " a%d(r%d):", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
1290 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
1291 fprintf (f, " pref%d:hr%d@%d", pref->num, pref->hard_regno, pref->freq);
1292 fprintf (f, "\n");
1295 /* Print info about prefs involving allocno A into stderr. */
1296 void
1297 ira_debug_allocno_prefs (ira_allocno_t a)
1299 print_allocno_prefs (stderr, a);
1302 /* The function frees memory allocated for PREF. */
1303 static void
1304 finish_pref (ira_pref_t pref)
1306 ira_prefs[pref->num] = NULL;
1307 pref_pool.remove (pref);
1310 /* Remove PREF from the list of allocno prefs and free memory for
1311 it. */
1312 void
1313 ira_remove_pref (ira_pref_t pref)
1315 ira_pref_t cpref, prev;
1317 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1318 fprintf (ira_dump_file, " Removing pref%d:hr%d@%d\n",
1319 pref->num, pref->hard_regno, pref->freq);
1320 for (prev = NULL, cpref = ALLOCNO_PREFS (pref->allocno);
1321 cpref != NULL;
1322 prev = cpref, cpref = cpref->next_pref)
1323 if (cpref == pref)
1324 break;
1325 ira_assert (cpref != NULL);
1326 if (prev == NULL)
1327 ALLOCNO_PREFS (pref->allocno) = pref->next_pref;
1328 else
1329 prev->next_pref = pref->next_pref;
1330 finish_pref (pref);
1333 /* Remove all prefs of allocno A. */
1334 void
1335 ira_remove_allocno_prefs (ira_allocno_t a)
1337 ira_pref_t pref, next_pref;
1339 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = next_pref)
1341 next_pref = pref->next_pref;
1342 finish_pref (pref);
1344 ALLOCNO_PREFS (a) = NULL;
1347 /* Free memory allocated for all prefs. */
1348 static void
1349 finish_prefs (void)
1351 ira_pref_t pref;
1352 ira_pref_iterator pi;
1354 FOR_EACH_PREF (pref, pi)
1355 finish_pref (pref);
1356 pref_vec.release ();
1357 pref_pool.release ();
1362 /* Pools for copies. */
1363 static pool_allocator<ira_allocno_copy> copy_pool ("copies", 100);
1365 /* Vec containing references to all created copies. It is a
1366 container of array ira_copies. */
1367 static vec<ira_copy_t> copy_vec;
1369 /* The function initializes data concerning allocno copies. */
1370 static void
1371 initiate_copies (void)
1373 copy_vec.create (get_max_uid ());
1374 ira_copies = NULL;
1375 ira_copies_num = 0;
1378 /* Return copy connecting A1 and A2 and originated from INSN of
1379 LOOP_TREE_NODE if any. */
1380 static ira_copy_t
1381 find_allocno_copy (ira_allocno_t a1, ira_allocno_t a2, rtx_insn *insn,
1382 ira_loop_tree_node_t loop_tree_node)
1384 ira_copy_t cp, next_cp;
1385 ira_allocno_t another_a;
1387 for (cp = ALLOCNO_COPIES (a1); cp != NULL; cp = next_cp)
1389 if (cp->first == a1)
1391 next_cp = cp->next_first_allocno_copy;
1392 another_a = cp->second;
1394 else if (cp->second == a1)
1396 next_cp = cp->next_second_allocno_copy;
1397 another_a = cp->first;
1399 else
1400 gcc_unreachable ();
1401 if (another_a == a2 && cp->insn == insn
1402 && cp->loop_tree_node == loop_tree_node)
1403 return cp;
1405 return NULL;
1408 /* Create and return copy with given attributes LOOP_TREE_NODE, FIRST,
1409 SECOND, FREQ, CONSTRAINT_P, and INSN. */
1410 ira_copy_t
1411 ira_create_copy (ira_allocno_t first, ira_allocno_t second, int freq,
1412 bool constraint_p, rtx_insn *insn,
1413 ira_loop_tree_node_t loop_tree_node)
1415 ira_copy_t cp;
1417 cp = copy_pool.allocate ();
1418 cp->num = ira_copies_num;
1419 cp->first = first;
1420 cp->second = second;
1421 cp->freq = freq;
1422 cp->constraint_p = constraint_p;
1423 cp->insn = insn;
1424 cp->loop_tree_node = loop_tree_node;
1425 copy_vec.safe_push (cp);
1426 ira_copies = copy_vec.address ();
1427 ira_copies_num = copy_vec.length ();
1428 return cp;
1431 /* Attach a copy CP to allocnos involved into the copy. */
1432 static void
1433 add_allocno_copy_to_list (ira_copy_t cp)
1435 ira_allocno_t first = cp->first, second = cp->second;
1437 cp->prev_first_allocno_copy = NULL;
1438 cp->prev_second_allocno_copy = NULL;
1439 cp->next_first_allocno_copy = ALLOCNO_COPIES (first);
1440 if (cp->next_first_allocno_copy != NULL)
1442 if (cp->next_first_allocno_copy->first == first)
1443 cp->next_first_allocno_copy->prev_first_allocno_copy = cp;
1444 else
1445 cp->next_first_allocno_copy->prev_second_allocno_copy = cp;
1447 cp->next_second_allocno_copy = ALLOCNO_COPIES (second);
1448 if (cp->next_second_allocno_copy != NULL)
1450 if (cp->next_second_allocno_copy->second == second)
1451 cp->next_second_allocno_copy->prev_second_allocno_copy = cp;
1452 else
1453 cp->next_second_allocno_copy->prev_first_allocno_copy = cp;
1455 ALLOCNO_COPIES (first) = cp;
1456 ALLOCNO_COPIES (second) = cp;
1459 /* Make a copy CP a canonical copy where number of the
1460 first allocno is less than the second one. */
1461 static void
1462 swap_allocno_copy_ends_if_necessary (ira_copy_t cp)
1464 if (ALLOCNO_NUM (cp->first) <= ALLOCNO_NUM (cp->second))
1465 return;
1467 std::swap (cp->first, cp->second);
1468 std::swap (cp->prev_first_allocno_copy, cp->prev_second_allocno_copy);
1469 std::swap (cp->next_first_allocno_copy, cp->next_second_allocno_copy);
1472 /* Create (or update frequency if the copy already exists) and return
1473 the copy of allocnos FIRST and SECOND with frequency FREQ
1474 corresponding to move insn INSN (if any) and originated from
1475 LOOP_TREE_NODE. */
1476 ira_copy_t
1477 ira_add_allocno_copy (ira_allocno_t first, ira_allocno_t second, int freq,
1478 bool constraint_p, rtx_insn *insn,
1479 ira_loop_tree_node_t loop_tree_node)
1481 ira_copy_t cp;
1483 if ((cp = find_allocno_copy (first, second, insn, loop_tree_node)) != NULL)
1485 cp->freq += freq;
1486 return cp;
1488 cp = ira_create_copy (first, second, freq, constraint_p, insn,
1489 loop_tree_node);
1490 ira_assert (first != NULL && second != NULL);
1491 add_allocno_copy_to_list (cp);
1492 swap_allocno_copy_ends_if_necessary (cp);
1493 return cp;
1496 /* Print info about copy CP into file F. */
1497 static void
1498 print_copy (FILE *f, ira_copy_t cp)
1500 fprintf (f, " cp%d:a%d(r%d)<->a%d(r%d)@%d:%s\n", cp->num,
1501 ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
1502 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second), cp->freq,
1503 cp->insn != NULL
1504 ? "move" : cp->constraint_p ? "constraint" : "shuffle");
1507 DEBUG_FUNCTION void
1508 debug (ira_allocno_copy &ref)
1510 print_copy (stderr, &ref);
1513 DEBUG_FUNCTION void
1514 debug (ira_allocno_copy *ptr)
1516 if (ptr)
1517 debug (*ptr);
1518 else
1519 fprintf (stderr, "<nil>\n");
1522 /* Print info about copy CP into stderr. */
1523 void
1524 ira_debug_copy (ira_copy_t cp)
1526 print_copy (stderr, cp);
1529 /* Print info about all copies into file F. */
1530 static void
1531 print_copies (FILE *f)
1533 ira_copy_t cp;
1534 ira_copy_iterator ci;
1536 FOR_EACH_COPY (cp, ci)
1537 print_copy (f, cp);
1540 /* Print info about all copies into stderr. */
1541 void
1542 ira_debug_copies (void)
1544 print_copies (stderr);
1547 /* Print info about copies involving allocno A into file F. */
1548 static void
1549 print_allocno_copies (FILE *f, ira_allocno_t a)
1551 ira_allocno_t another_a;
1552 ira_copy_t cp, next_cp;
1554 fprintf (f, " a%d(r%d):", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
1555 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1557 if (cp->first == a)
1559 next_cp = cp->next_first_allocno_copy;
1560 another_a = cp->second;
1562 else if (cp->second == a)
1564 next_cp = cp->next_second_allocno_copy;
1565 another_a = cp->first;
1567 else
1568 gcc_unreachable ();
1569 fprintf (f, " cp%d:a%d(r%d)@%d", cp->num,
1570 ALLOCNO_NUM (another_a), ALLOCNO_REGNO (another_a), cp->freq);
1572 fprintf (f, "\n");
1575 DEBUG_FUNCTION void
1576 debug (ira_allocno &ref)
1578 print_allocno_copies (stderr, &ref);
1581 DEBUG_FUNCTION void
1582 debug (ira_allocno *ptr)
1584 if (ptr)
1585 debug (*ptr);
1586 else
1587 fprintf (stderr, "<nil>\n");
1591 /* Print info about copies involving allocno A into stderr. */
1592 void
1593 ira_debug_allocno_copies (ira_allocno_t a)
1595 print_allocno_copies (stderr, a);
1598 /* The function frees memory allocated for copy CP. */
1599 static void
1600 finish_copy (ira_copy_t cp)
1602 copy_pool.remove (cp);
1606 /* Free memory allocated for all copies. */
1607 static void
1608 finish_copies (void)
1610 ira_copy_t cp;
1611 ira_copy_iterator ci;
1613 FOR_EACH_COPY (cp, ci)
1614 finish_copy (cp);
1615 copy_vec.release ();
1616 copy_pool.release ();
1621 /* Pools for cost vectors. It is defined only for allocno classes. */
1622 static pool_allocator<int> * cost_vector_pool[N_REG_CLASSES];
1624 /* The function initiates work with hard register cost vectors. It
1625 creates allocation pool for each allocno class. */
1626 static void
1627 initiate_cost_vectors (void)
1629 int i;
1630 enum reg_class aclass;
1632 for (i = 0; i < ira_allocno_classes_num; i++)
1634 aclass = ira_allocno_classes[i];
1635 cost_vector_pool[aclass] = new pool_allocator<int>
1636 ("cost vectors", 100,
1637 sizeof (int) * (ira_class_hard_regs_num[aclass] - 1));
1641 /* Allocate and return a cost vector VEC for ACLASS. */
1642 int *
1643 ira_allocate_cost_vector (reg_class_t aclass)
1645 return cost_vector_pool[(int) aclass]->allocate ();
1648 /* Free a cost vector VEC for ACLASS. */
1649 void
1650 ira_free_cost_vector (int *vec, reg_class_t aclass)
1652 ira_assert (vec != NULL);
1653 cost_vector_pool[(int) aclass]->remove (vec);
1656 /* Finish work with hard register cost vectors. Release allocation
1657 pool for each allocno class. */
1658 static void
1659 finish_cost_vectors (void)
1661 int i;
1662 enum reg_class aclass;
1664 for (i = 0; i < ira_allocno_classes_num; i++)
1666 aclass = ira_allocno_classes[i];
1667 delete cost_vector_pool[aclass];
1673 /* Compute a post-ordering of the reverse control flow of the loop body
1674 designated by the children nodes of LOOP_NODE, whose body nodes in
1675 pre-order are input as LOOP_PREORDER. Return a VEC with a post-order
1676 of the reverse loop body.
1678 For the post-order of the reverse CFG, we visit the basic blocks in
1679 LOOP_PREORDER array in the reverse order of where they appear.
1680 This is important: We do not just want to compute a post-order of
1681 the reverse CFG, we want to make a best-guess for a visiting order that
1682 minimizes the number of chain elements per allocno live range. If the
1683 blocks would be visited in a different order, we would still compute a
1684 correct post-ordering but it would be less likely that two nodes
1685 connected by an edge in the CFG are neighbours in the topsort. */
1687 static vec<ira_loop_tree_node_t>
1688 ira_loop_tree_body_rev_postorder (ira_loop_tree_node_t loop_node ATTRIBUTE_UNUSED,
1689 vec<ira_loop_tree_node_t> loop_preorder)
1691 vec<ira_loop_tree_node_t> topsort_nodes = vNULL;
1692 unsigned int n_loop_preorder;
1694 n_loop_preorder = loop_preorder.length ();
1695 if (n_loop_preorder != 0)
1697 ira_loop_tree_node_t subloop_node;
1698 unsigned int i;
1699 auto_vec<ira_loop_tree_node_t> dfs_stack;
1701 /* This is a bit of strange abuse of the BB_VISITED flag: We use
1702 the flag to mark blocks we still have to visit to add them to
1703 our post-order. Define an alias to avoid confusion. */
1704 #define BB_TO_VISIT BB_VISITED
1706 FOR_EACH_VEC_ELT (loop_preorder, i, subloop_node)
1708 gcc_checking_assert (! (subloop_node->bb->flags & BB_TO_VISIT));
1709 subloop_node->bb->flags |= BB_TO_VISIT;
1712 topsort_nodes.create (n_loop_preorder);
1713 dfs_stack.create (n_loop_preorder);
1715 FOR_EACH_VEC_ELT_REVERSE (loop_preorder, i, subloop_node)
1717 if (! (subloop_node->bb->flags & BB_TO_VISIT))
1718 continue;
1720 subloop_node->bb->flags &= ~BB_TO_VISIT;
1721 dfs_stack.quick_push (subloop_node);
1722 while (! dfs_stack.is_empty ())
1724 edge e;
1725 edge_iterator ei;
1727 ira_loop_tree_node_t n = dfs_stack.last ();
1728 FOR_EACH_EDGE (e, ei, n->bb->preds)
1730 ira_loop_tree_node_t pred_node;
1731 basic_block pred_bb = e->src;
1733 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1734 continue;
1736 pred_node = IRA_BB_NODE_BY_INDEX (pred_bb->index);
1737 if (pred_node != n
1738 && (pred_node->bb->flags & BB_TO_VISIT))
1740 pred_node->bb->flags &= ~BB_TO_VISIT;
1741 dfs_stack.quick_push (pred_node);
1744 if (n == dfs_stack.last ())
1746 dfs_stack.pop ();
1747 topsort_nodes.quick_push (n);
1752 #undef BB_TO_VISIT
1755 gcc_assert (topsort_nodes.length () == n_loop_preorder);
1756 return topsort_nodes;
1759 /* The current loop tree node and its regno allocno map. */
1760 ira_loop_tree_node_t ira_curr_loop_tree_node;
1761 ira_allocno_t *ira_curr_regno_allocno_map;
1763 /* This recursive function traverses loop tree with root LOOP_NODE
1764 calling non-null functions PREORDER_FUNC and POSTORDER_FUNC
1765 correspondingly in preorder and postorder. The function sets up
1766 IRA_CURR_LOOP_TREE_NODE and IRA_CURR_REGNO_ALLOCNO_MAP. If BB_P,
1767 basic block nodes of LOOP_NODE is also processed (before its
1768 subloop nodes).
1770 If BB_P is set and POSTORDER_FUNC is given, the basic blocks in
1771 the loop are passed in the *reverse* post-order of the *reverse*
1772 CFG. This is only used by ira_create_allocno_live_ranges, which
1773 wants to visit basic blocks in this order to minimize the number
1774 of elements per live range chain.
1775 Note that the loop tree nodes are still visited in the normal,
1776 forward post-order of the loop tree. */
1778 void
1779 ira_traverse_loop_tree (bool bb_p, ira_loop_tree_node_t loop_node,
1780 void (*preorder_func) (ira_loop_tree_node_t),
1781 void (*postorder_func) (ira_loop_tree_node_t))
1783 ira_loop_tree_node_t subloop_node;
1785 ira_assert (loop_node->bb == NULL);
1786 ira_curr_loop_tree_node = loop_node;
1787 ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
1789 if (preorder_func != NULL)
1790 (*preorder_func) (loop_node);
1792 if (bb_p)
1794 auto_vec<ira_loop_tree_node_t> loop_preorder;
1795 unsigned int i;
1797 /* Add all nodes to the set of nodes to visit. The IRA loop tree
1798 is set up such that nodes in the loop body appear in a pre-order
1799 of their place in the CFG. */
1800 for (subloop_node = loop_node->children;
1801 subloop_node != NULL;
1802 subloop_node = subloop_node->next)
1803 if (subloop_node->bb != NULL)
1804 loop_preorder.safe_push (subloop_node);
1806 if (preorder_func != NULL)
1807 FOR_EACH_VEC_ELT (loop_preorder, i, subloop_node)
1808 (*preorder_func) (subloop_node);
1810 if (postorder_func != NULL)
1812 vec<ira_loop_tree_node_t> loop_rev_postorder =
1813 ira_loop_tree_body_rev_postorder (loop_node, loop_preorder);
1814 FOR_EACH_VEC_ELT_REVERSE (loop_rev_postorder, i, subloop_node)
1815 (*postorder_func) (subloop_node);
1816 loop_rev_postorder.release ();
1820 for (subloop_node = loop_node->subloops;
1821 subloop_node != NULL;
1822 subloop_node = subloop_node->subloop_next)
1824 ira_assert (subloop_node->bb == NULL);
1825 ira_traverse_loop_tree (bb_p, subloop_node,
1826 preorder_func, postorder_func);
1829 ira_curr_loop_tree_node = loop_node;
1830 ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
1832 if (postorder_func != NULL)
1833 (*postorder_func) (loop_node);
1838 /* The basic block currently being processed. */
1839 static basic_block curr_bb;
1841 /* This recursive function creates allocnos corresponding to
1842 pseudo-registers containing in X. True OUTPUT_P means that X is
1843 an lvalue. PARENT corresponds to the parent expression of X. */
1844 static void
1845 create_insn_allocnos (rtx x, rtx outer, bool output_p)
1847 int i, j;
1848 const char *fmt;
1849 enum rtx_code code = GET_CODE (x);
1851 if (code == REG)
1853 int regno;
1855 if ((regno = REGNO (x)) >= FIRST_PSEUDO_REGISTER)
1857 ira_allocno_t a;
1859 if ((a = ira_curr_regno_allocno_map[regno]) == NULL)
1861 a = ira_create_allocno (regno, false, ira_curr_loop_tree_node);
1862 if (outer != NULL && GET_CODE (outer) == SUBREG)
1864 machine_mode wmode = GET_MODE (outer);
1865 if (GET_MODE_SIZE (wmode) > GET_MODE_SIZE (ALLOCNO_WMODE (a)))
1866 ALLOCNO_WMODE (a) = wmode;
1870 ALLOCNO_NREFS (a)++;
1871 ALLOCNO_FREQ (a) += REG_FREQ_FROM_BB (curr_bb);
1872 if (output_p)
1873 bitmap_set_bit (ira_curr_loop_tree_node->modified_regnos, regno);
1875 return;
1877 else if (code == SET)
1879 create_insn_allocnos (SET_DEST (x), NULL, true);
1880 create_insn_allocnos (SET_SRC (x), NULL, false);
1881 return;
1883 else if (code == CLOBBER)
1885 create_insn_allocnos (XEXP (x, 0), NULL, true);
1886 return;
1888 else if (code == MEM)
1890 create_insn_allocnos (XEXP (x, 0), NULL, false);
1891 return;
1893 else if (code == PRE_DEC || code == POST_DEC || code == PRE_INC ||
1894 code == POST_INC || code == POST_MODIFY || code == PRE_MODIFY)
1896 create_insn_allocnos (XEXP (x, 0), NULL, true);
1897 create_insn_allocnos (XEXP (x, 0), NULL, false);
1898 return;
1901 fmt = GET_RTX_FORMAT (code);
1902 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1904 if (fmt[i] == 'e')
1905 create_insn_allocnos (XEXP (x, i), x, output_p);
1906 else if (fmt[i] == 'E')
1907 for (j = 0; j < XVECLEN (x, i); j++)
1908 create_insn_allocnos (XVECEXP (x, i, j), x, output_p);
1912 /* Create allocnos corresponding to pseudo-registers living in the
1913 basic block represented by the corresponding loop tree node
1914 BB_NODE. */
1915 static void
1916 create_bb_allocnos (ira_loop_tree_node_t bb_node)
1918 basic_block bb;
1919 rtx_insn *insn;
1920 unsigned int i;
1921 bitmap_iterator bi;
1923 curr_bb = bb = bb_node->bb;
1924 ira_assert (bb != NULL);
1925 FOR_BB_INSNS_REVERSE (bb, insn)
1926 if (NONDEBUG_INSN_P (insn))
1927 create_insn_allocnos (PATTERN (insn), NULL, false);
1928 /* It might be a allocno living through from one subloop to
1929 another. */
1930 EXECUTE_IF_SET_IN_REG_SET (df_get_live_in (bb), FIRST_PSEUDO_REGISTER, i, bi)
1931 if (ira_curr_regno_allocno_map[i] == NULL)
1932 ira_create_allocno (i, false, ira_curr_loop_tree_node);
1935 /* Create allocnos corresponding to pseudo-registers living on edge E
1936 (a loop entry or exit). Also mark the allocnos as living on the
1937 loop border. */
1938 static void
1939 create_loop_allocnos (edge e)
1941 unsigned int i;
1942 bitmap live_in_regs, border_allocnos;
1943 bitmap_iterator bi;
1944 ira_loop_tree_node_t parent;
1946 live_in_regs = df_get_live_in (e->dest);
1947 border_allocnos = ira_curr_loop_tree_node->border_allocnos;
1948 EXECUTE_IF_SET_IN_REG_SET (df_get_live_out (e->src),
1949 FIRST_PSEUDO_REGISTER, i, bi)
1950 if (bitmap_bit_p (live_in_regs, i))
1952 if (ira_curr_regno_allocno_map[i] == NULL)
1954 /* The order of creations is important for right
1955 ira_regno_allocno_map. */
1956 if ((parent = ira_curr_loop_tree_node->parent) != NULL
1957 && parent->regno_allocno_map[i] == NULL)
1958 ira_create_allocno (i, false, parent);
1959 ira_create_allocno (i, false, ira_curr_loop_tree_node);
1961 bitmap_set_bit (border_allocnos,
1962 ALLOCNO_NUM (ira_curr_regno_allocno_map[i]));
1966 /* Create allocnos corresponding to pseudo-registers living in loop
1967 represented by the corresponding loop tree node LOOP_NODE. This
1968 function is called by ira_traverse_loop_tree. */
1969 static void
1970 create_loop_tree_node_allocnos (ira_loop_tree_node_t loop_node)
1972 if (loop_node->bb != NULL)
1973 create_bb_allocnos (loop_node);
1974 else if (loop_node != ira_loop_tree_root)
1976 int i;
1977 edge_iterator ei;
1978 edge e;
1979 vec<edge> edges;
1981 ira_assert (current_loops != NULL);
1982 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
1983 if (e->src != loop_node->loop->latch)
1984 create_loop_allocnos (e);
1986 edges = get_loop_exit_edges (loop_node->loop);
1987 FOR_EACH_VEC_ELT (edges, i, e)
1988 create_loop_allocnos (e);
1989 edges.release ();
1993 /* Propagate information about allocnos modified inside the loop given
1994 by its LOOP_TREE_NODE to its parent. */
1995 static void
1996 propagate_modified_regnos (ira_loop_tree_node_t loop_tree_node)
1998 if (loop_tree_node == ira_loop_tree_root)
1999 return;
2000 ira_assert (loop_tree_node->bb == NULL);
2001 bitmap_ior_into (loop_tree_node->parent->modified_regnos,
2002 loop_tree_node->modified_regnos);
2005 /* Propagate new info about allocno A (see comments about accumulated
2006 info in allocno definition) to the corresponding allocno on upper
2007 loop tree level. So allocnos on upper levels accumulate
2008 information about the corresponding allocnos in nested regions.
2009 The new info means allocno info finally calculated in this
2010 file. */
2011 static void
2012 propagate_allocno_info (void)
2014 int i;
2015 ira_allocno_t a, parent_a;
2016 ira_loop_tree_node_t parent;
2017 enum reg_class aclass;
2019 if (flag_ira_region != IRA_REGION_ALL
2020 && flag_ira_region != IRA_REGION_MIXED)
2021 return;
2022 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
2023 for (a = ira_regno_allocno_map[i];
2024 a != NULL;
2025 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2026 if ((parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) != NULL
2027 && (parent_a = parent->regno_allocno_map[i]) != NULL
2028 /* There are no caps yet at this point. So use
2029 border_allocnos to find allocnos for the propagation. */
2030 && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE (a)->border_allocnos,
2031 ALLOCNO_NUM (a)))
2033 if (! ALLOCNO_BAD_SPILL_P (a))
2034 ALLOCNO_BAD_SPILL_P (parent_a) = false;
2035 ALLOCNO_NREFS (parent_a) += ALLOCNO_NREFS (a);
2036 ALLOCNO_FREQ (parent_a) += ALLOCNO_FREQ (a);
2037 ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
2038 merge_hard_reg_conflicts (a, parent_a, true);
2039 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
2040 += ALLOCNO_CALLS_CROSSED_NUM (a);
2041 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
2042 += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
2043 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (parent_a),
2044 ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
2045 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
2046 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2047 aclass = ALLOCNO_CLASS (a);
2048 ira_assert (aclass == ALLOCNO_CLASS (parent_a));
2049 ira_allocate_and_accumulate_costs
2050 (&ALLOCNO_HARD_REG_COSTS (parent_a), aclass,
2051 ALLOCNO_HARD_REG_COSTS (a));
2052 ira_allocate_and_accumulate_costs
2053 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a),
2054 aclass,
2055 ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
2056 ALLOCNO_CLASS_COST (parent_a)
2057 += ALLOCNO_CLASS_COST (a);
2058 ALLOCNO_MEMORY_COST (parent_a) += ALLOCNO_MEMORY_COST (a);
2062 /* Create allocnos corresponding to pseudo-registers in the current
2063 function. Traverse the loop tree for this. */
2064 static void
2065 create_allocnos (void)
2067 /* We need to process BB first to correctly link allocnos by member
2068 next_regno_allocno. */
2069 ira_traverse_loop_tree (true, ira_loop_tree_root,
2070 create_loop_tree_node_allocnos, NULL);
2071 if (optimize)
2072 ira_traverse_loop_tree (false, ira_loop_tree_root, NULL,
2073 propagate_modified_regnos);
2078 /* The page contains function to remove some regions from a separate
2079 register allocation. We remove regions whose separate allocation
2080 will hardly improve the result. As a result we speed up regional
2081 register allocation. */
2083 /* The function changes the object in range list given by R to OBJ. */
2084 static void
2085 change_object_in_range_list (live_range_t r, ira_object_t obj)
2087 for (; r != NULL; r = r->next)
2088 r->object = obj;
2091 /* Move all live ranges associated with allocno FROM to allocno TO. */
2092 static void
2093 move_allocno_live_ranges (ira_allocno_t from, ira_allocno_t to)
2095 int i;
2096 int n = ALLOCNO_NUM_OBJECTS (from);
2098 gcc_assert (n == ALLOCNO_NUM_OBJECTS (to));
2100 for (i = 0; i < n; i++)
2102 ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
2103 ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
2104 live_range_t lr = OBJECT_LIVE_RANGES (from_obj);
2106 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2108 fprintf (ira_dump_file,
2109 " Moving ranges of a%dr%d to a%dr%d: ",
2110 ALLOCNO_NUM (from), ALLOCNO_REGNO (from),
2111 ALLOCNO_NUM (to), ALLOCNO_REGNO (to));
2112 ira_print_live_range_list (ira_dump_file, lr);
2114 change_object_in_range_list (lr, to_obj);
2115 OBJECT_LIVE_RANGES (to_obj)
2116 = ira_merge_live_ranges (lr, OBJECT_LIVE_RANGES (to_obj));
2117 OBJECT_LIVE_RANGES (from_obj) = NULL;
2121 static void
2122 copy_allocno_live_ranges (ira_allocno_t from, ira_allocno_t to)
2124 int i;
2125 int n = ALLOCNO_NUM_OBJECTS (from);
2127 gcc_assert (n == ALLOCNO_NUM_OBJECTS (to));
2129 for (i = 0; i < n; i++)
2131 ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
2132 ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
2133 live_range_t lr = OBJECT_LIVE_RANGES (from_obj);
2135 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2137 fprintf (ira_dump_file, " Copying ranges of a%dr%d to a%dr%d: ",
2138 ALLOCNO_NUM (from), ALLOCNO_REGNO (from),
2139 ALLOCNO_NUM (to), ALLOCNO_REGNO (to));
2140 ira_print_live_range_list (ira_dump_file, lr);
2142 lr = ira_copy_live_range_list (lr);
2143 change_object_in_range_list (lr, to_obj);
2144 OBJECT_LIVE_RANGES (to_obj)
2145 = ira_merge_live_ranges (lr, OBJECT_LIVE_RANGES (to_obj));
2149 /* Return TRUE if NODE represents a loop with low register
2150 pressure. */
2151 static bool
2152 low_pressure_loop_node_p (ira_loop_tree_node_t node)
2154 int i;
2155 enum reg_class pclass;
2157 if (node->bb != NULL)
2158 return false;
2160 for (i = 0; i < ira_pressure_classes_num; i++)
2162 pclass = ira_pressure_classes[i];
2163 if (node->reg_pressure[pclass] > ira_class_hard_regs_num[pclass]
2164 && ira_class_hard_regs_num[pclass] > 1)
2165 return false;
2167 return true;
2170 #ifdef STACK_REGS
2171 /* Return TRUE if LOOP has a complex enter or exit edge. We don't
2172 form a region from such loop if the target use stack register
2173 because reg-stack.c can not deal with such edges. */
2174 static bool
2175 loop_with_complex_edge_p (struct loop *loop)
2177 int i;
2178 edge_iterator ei;
2179 edge e;
2180 vec<edge> edges;
2181 bool res;
2183 FOR_EACH_EDGE (e, ei, loop->header->preds)
2184 if (e->flags & EDGE_EH)
2185 return true;
2186 edges = get_loop_exit_edges (loop);
2187 res = false;
2188 FOR_EACH_VEC_ELT (edges, i, e)
2189 if (e->flags & EDGE_COMPLEX)
2191 res = true;
2192 break;
2194 edges.release ();
2195 return res;
2197 #endif
2199 /* Sort loops for marking them for removal. We put already marked
2200 loops first, then less frequent loops next, and then outer loops
2201 next. */
2202 static int
2203 loop_compare_func (const void *v1p, const void *v2p)
2205 int diff;
2206 ira_loop_tree_node_t l1 = *(const ira_loop_tree_node_t *) v1p;
2207 ira_loop_tree_node_t l2 = *(const ira_loop_tree_node_t *) v2p;
2209 ira_assert (l1->parent != NULL && l2->parent != NULL);
2210 if (l1->to_remove_p && ! l2->to_remove_p)
2211 return -1;
2212 if (! l1->to_remove_p && l2->to_remove_p)
2213 return 1;
2214 if ((diff = l1->loop->header->frequency - l2->loop->header->frequency) != 0)
2215 return diff;
2216 if ((diff = (int) loop_depth (l1->loop) - (int) loop_depth (l2->loop)) != 0)
2217 return diff;
2218 /* Make sorting stable. */
2219 return l1->loop_num - l2->loop_num;
2222 /* Mark loops which should be removed from regional allocation. We
2223 remove a loop with low register pressure inside another loop with
2224 register pressure. In this case a separate allocation of the loop
2225 hardly helps (for irregular register file architecture it could
2226 help by choosing a better hard register in the loop but we prefer
2227 faster allocation even in this case). We also remove cheap loops
2228 if there are more than IRA_MAX_LOOPS_NUM of them. Loop with EH
2229 exit or enter edges are removed too because the allocation might
2230 require put pseudo moves on the EH edges (we could still do this
2231 for pseudos with caller saved hard registers in some cases but it
2232 is impossible to say here or during top-down allocation pass what
2233 hard register the pseudos get finally). */
2234 static void
2235 mark_loops_for_removal (void)
2237 int i, n;
2238 ira_loop_tree_node_t *sorted_loops;
2239 loop_p loop;
2241 ira_assert (current_loops != NULL);
2242 sorted_loops
2243 = (ira_loop_tree_node_t *) ira_allocate (sizeof (ira_loop_tree_node_t)
2244 * number_of_loops (cfun));
2245 for (n = i = 0; vec_safe_iterate (get_loops (cfun), i, &loop); i++)
2246 if (ira_loop_nodes[i].regno_allocno_map != NULL)
2248 if (ira_loop_nodes[i].parent == NULL)
2250 /* Don't remove the root. */
2251 ira_loop_nodes[i].to_remove_p = false;
2252 continue;
2254 sorted_loops[n++] = &ira_loop_nodes[i];
2255 ira_loop_nodes[i].to_remove_p
2256 = ((low_pressure_loop_node_p (ira_loop_nodes[i].parent)
2257 && low_pressure_loop_node_p (&ira_loop_nodes[i]))
2258 #ifdef STACK_REGS
2259 || loop_with_complex_edge_p (ira_loop_nodes[i].loop)
2260 #endif
2263 qsort (sorted_loops, n, sizeof (ira_loop_tree_node_t), loop_compare_func);
2264 for (i = 0; n - i + 1 > IRA_MAX_LOOPS_NUM; i++)
2266 sorted_loops[i]->to_remove_p = true;
2267 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2268 fprintf
2269 (ira_dump_file,
2270 " Mark loop %d (header %d, freq %d, depth %d) for removal (%s)\n",
2271 sorted_loops[i]->loop_num, sorted_loops[i]->loop->header->index,
2272 sorted_loops[i]->loop->header->frequency,
2273 loop_depth (sorted_loops[i]->loop),
2274 low_pressure_loop_node_p (sorted_loops[i]->parent)
2275 && low_pressure_loop_node_p (sorted_loops[i])
2276 ? "low pressure" : "cheap loop");
2278 ira_free (sorted_loops);
2281 /* Mark all loops but root for removing. */
2282 static void
2283 mark_all_loops_for_removal (void)
2285 int i;
2286 loop_p loop;
2288 ira_assert (current_loops != NULL);
2289 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
2290 if (ira_loop_nodes[i].regno_allocno_map != NULL)
2292 if (ira_loop_nodes[i].parent == NULL)
2294 /* Don't remove the root. */
2295 ira_loop_nodes[i].to_remove_p = false;
2296 continue;
2298 ira_loop_nodes[i].to_remove_p = true;
2299 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2300 fprintf
2301 (ira_dump_file,
2302 " Mark loop %d (header %d, freq %d, depth %d) for removal\n",
2303 ira_loop_nodes[i].loop_num,
2304 ira_loop_nodes[i].loop->header->index,
2305 ira_loop_nodes[i].loop->header->frequency,
2306 loop_depth (ira_loop_nodes[i].loop));
2310 /* Definition of vector of loop tree nodes. */
2312 /* Vec containing references to all removed loop tree nodes. */
2313 static vec<ira_loop_tree_node_t> removed_loop_vec;
2315 /* Vec containing references to all children of loop tree nodes. */
2316 static vec<ira_loop_tree_node_t> children_vec;
2318 /* Remove subregions of NODE if their separate allocation will not
2319 improve the result. */
2320 static void
2321 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_node_t node)
2323 unsigned int start;
2324 bool remove_p;
2325 ira_loop_tree_node_t subnode;
2327 remove_p = node->to_remove_p;
2328 if (! remove_p)
2329 children_vec.safe_push (node);
2330 start = children_vec.length ();
2331 for (subnode = node->children; subnode != NULL; subnode = subnode->next)
2332 if (subnode->bb == NULL)
2333 remove_uneccesary_loop_nodes_from_loop_tree (subnode);
2334 else
2335 children_vec.safe_push (subnode);
2336 node->children = node->subloops = NULL;
2337 if (remove_p)
2339 removed_loop_vec.safe_push (node);
2340 return;
2342 while (children_vec.length () > start)
2344 subnode = children_vec.pop ();
2345 subnode->parent = node;
2346 subnode->next = node->children;
2347 node->children = subnode;
2348 if (subnode->bb == NULL)
2350 subnode->subloop_next = node->subloops;
2351 node->subloops = subnode;
2356 /* Return TRUE if NODE is inside PARENT. */
2357 static bool
2358 loop_is_inside_p (ira_loop_tree_node_t node, ira_loop_tree_node_t parent)
2360 for (node = node->parent; node != NULL; node = node->parent)
2361 if (node == parent)
2362 return true;
2363 return false;
2366 /* Sort allocnos according to their order in regno allocno list. */
2367 static int
2368 regno_allocno_order_compare_func (const void *v1p, const void *v2p)
2370 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2371 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2372 ira_loop_tree_node_t n1 = ALLOCNO_LOOP_TREE_NODE (a1);
2373 ira_loop_tree_node_t n2 = ALLOCNO_LOOP_TREE_NODE (a2);
2375 if (loop_is_inside_p (n1, n2))
2376 return -1;
2377 else if (loop_is_inside_p (n2, n1))
2378 return 1;
2379 /* If allocnos are equally good, sort by allocno numbers, so that
2380 the results of qsort leave nothing to chance. We put allocnos
2381 with higher number first in the list because it is the original
2382 order for allocnos from loops on the same levels. */
2383 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
2386 /* This array is used to sort allocnos to restore allocno order in
2387 the regno allocno list. */
2388 static ira_allocno_t *regno_allocnos;
2390 /* Restore allocno order for REGNO in the regno allocno list. */
2391 static void
2392 ira_rebuild_regno_allocno_list (int regno)
2394 int i, n;
2395 ira_allocno_t a;
2397 for (n = 0, a = ira_regno_allocno_map[regno];
2398 a != NULL;
2399 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2400 regno_allocnos[n++] = a;
2401 ira_assert (n > 0);
2402 qsort (regno_allocnos, n, sizeof (ira_allocno_t),
2403 regno_allocno_order_compare_func);
2404 for (i = 1; i < n; i++)
2405 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[i - 1]) = regno_allocnos[i];
2406 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[n - 1]) = NULL;
2407 ira_regno_allocno_map[regno] = regno_allocnos[0];
2408 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2409 fprintf (ira_dump_file, " Rebuilding regno allocno list for %d\n", regno);
2412 /* Propagate info from allocno FROM_A to allocno A. */
2413 static void
2414 propagate_some_info_from_allocno (ira_allocno_t a, ira_allocno_t from_a)
2416 enum reg_class aclass;
2418 merge_hard_reg_conflicts (from_a, a, false);
2419 ALLOCNO_NREFS (a) += ALLOCNO_NREFS (from_a);
2420 ALLOCNO_FREQ (a) += ALLOCNO_FREQ (from_a);
2421 ALLOCNO_CALL_FREQ (a) += ALLOCNO_CALL_FREQ (from_a);
2422 ALLOCNO_CALLS_CROSSED_NUM (a) += ALLOCNO_CALLS_CROSSED_NUM (from_a);
2423 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a)
2424 += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (from_a);
2425 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a),
2426 ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (from_a));
2428 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
2429 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (from_a);
2430 if (! ALLOCNO_BAD_SPILL_P (from_a))
2431 ALLOCNO_BAD_SPILL_P (a) = false;
2432 aclass = ALLOCNO_CLASS (from_a);
2433 ira_assert (aclass == ALLOCNO_CLASS (a));
2434 ira_allocate_and_accumulate_costs (&ALLOCNO_HARD_REG_COSTS (a), aclass,
2435 ALLOCNO_HARD_REG_COSTS (from_a));
2436 ira_allocate_and_accumulate_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
2437 aclass,
2438 ALLOCNO_CONFLICT_HARD_REG_COSTS (from_a));
2439 ALLOCNO_CLASS_COST (a) += ALLOCNO_CLASS_COST (from_a);
2440 ALLOCNO_MEMORY_COST (a) += ALLOCNO_MEMORY_COST (from_a);
2443 /* Remove allocnos from loops removed from the allocation
2444 consideration. */
2445 static void
2446 remove_unnecessary_allocnos (void)
2448 int regno;
2449 bool merged_p, rebuild_p;
2450 ira_allocno_t a, prev_a, next_a, parent_a;
2451 ira_loop_tree_node_t a_node, parent;
2453 merged_p = false;
2454 regno_allocnos = NULL;
2455 for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
2457 rebuild_p = false;
2458 for (prev_a = NULL, a = ira_regno_allocno_map[regno];
2459 a != NULL;
2460 a = next_a)
2462 next_a = ALLOCNO_NEXT_REGNO_ALLOCNO (a);
2463 a_node = ALLOCNO_LOOP_TREE_NODE (a);
2464 if (! a_node->to_remove_p)
2465 prev_a = a;
2466 else
2468 for (parent = a_node->parent;
2469 (parent_a = parent->regno_allocno_map[regno]) == NULL
2470 && parent->to_remove_p;
2471 parent = parent->parent)
2473 if (parent_a == NULL)
2475 /* There are no allocnos with the same regno in
2476 upper region -- just move the allocno to the
2477 upper region. */
2478 prev_a = a;
2479 ALLOCNO_LOOP_TREE_NODE (a) = parent;
2480 parent->regno_allocno_map[regno] = a;
2481 bitmap_set_bit (parent->all_allocnos, ALLOCNO_NUM (a));
2482 rebuild_p = true;
2484 else
2486 /* Remove the allocno and update info of allocno in
2487 the upper region. */
2488 if (prev_a == NULL)
2489 ira_regno_allocno_map[regno] = next_a;
2490 else
2491 ALLOCNO_NEXT_REGNO_ALLOCNO (prev_a) = next_a;
2492 move_allocno_live_ranges (a, parent_a);
2493 merged_p = true;
2494 propagate_some_info_from_allocno (parent_a, a);
2495 /* Remove it from the corresponding regno allocno
2496 map to avoid info propagation of subsequent
2497 allocno into this already removed allocno. */
2498 a_node->regno_allocno_map[regno] = NULL;
2499 ira_remove_allocno_prefs (a);
2500 finish_allocno (a);
2504 if (rebuild_p)
2505 /* We need to restore the order in regno allocno list. */
2507 if (regno_allocnos == NULL)
2508 regno_allocnos
2509 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
2510 * ira_allocnos_num);
2511 ira_rebuild_regno_allocno_list (regno);
2514 if (merged_p)
2515 ira_rebuild_start_finish_chains ();
2516 if (regno_allocnos != NULL)
2517 ira_free (regno_allocnos);
2520 /* Remove allocnos from all loops but the root. */
2521 static void
2522 remove_low_level_allocnos (void)
2524 int regno;
2525 bool merged_p, propagate_p;
2526 ira_allocno_t a, top_a;
2527 ira_loop_tree_node_t a_node, parent;
2528 ira_allocno_iterator ai;
2530 merged_p = false;
2531 FOR_EACH_ALLOCNO (a, ai)
2533 a_node = ALLOCNO_LOOP_TREE_NODE (a);
2534 if (a_node == ira_loop_tree_root || ALLOCNO_CAP_MEMBER (a) != NULL)
2535 continue;
2536 regno = ALLOCNO_REGNO (a);
2537 if ((top_a = ira_loop_tree_root->regno_allocno_map[regno]) == NULL)
2539 ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
2540 ira_loop_tree_root->regno_allocno_map[regno] = a;
2541 continue;
2543 propagate_p = a_node->parent->regno_allocno_map[regno] == NULL;
2544 /* Remove the allocno and update info of allocno in the upper
2545 region. */
2546 move_allocno_live_ranges (a, top_a);
2547 merged_p = true;
2548 if (propagate_p)
2549 propagate_some_info_from_allocno (top_a, a);
2551 FOR_EACH_ALLOCNO (a, ai)
2553 a_node = ALLOCNO_LOOP_TREE_NODE (a);
2554 if (a_node == ira_loop_tree_root)
2555 continue;
2556 parent = a_node->parent;
2557 regno = ALLOCNO_REGNO (a);
2558 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2559 ira_assert (ALLOCNO_CAP (a) != NULL);
2560 else if (ALLOCNO_CAP (a) == NULL)
2561 ira_assert (parent->regno_allocno_map[regno] != NULL);
2563 FOR_EACH_ALLOCNO (a, ai)
2565 regno = ALLOCNO_REGNO (a);
2566 if (ira_loop_tree_root->regno_allocno_map[regno] == a)
2568 ira_object_t obj;
2569 ira_allocno_object_iterator oi;
2571 ira_regno_allocno_map[regno] = a;
2572 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = NULL;
2573 ALLOCNO_CAP_MEMBER (a) = NULL;
2574 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
2575 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
2576 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
2577 #ifdef STACK_REGS
2578 if (ALLOCNO_TOTAL_NO_STACK_REG_P (a))
2579 ALLOCNO_NO_STACK_REG_P (a) = true;
2580 #endif
2582 else
2584 ira_remove_allocno_prefs (a);
2585 finish_allocno (a);
2588 if (merged_p)
2589 ira_rebuild_start_finish_chains ();
2592 /* Remove loops from consideration. We remove all loops except for
2593 root if ALL_P or loops for which a separate allocation will not
2594 improve the result. We have to do this after allocno creation and
2595 their costs and allocno class evaluation because only after that
2596 the register pressure can be known and is calculated. */
2597 static void
2598 remove_unnecessary_regions (bool all_p)
2600 if (current_loops == NULL)
2601 return;
2602 if (all_p)
2603 mark_all_loops_for_removal ();
2604 else
2605 mark_loops_for_removal ();
2606 children_vec.create (last_basic_block_for_fn (cfun)
2607 + number_of_loops (cfun));
2608 removed_loop_vec.create (last_basic_block_for_fn (cfun)
2609 + number_of_loops (cfun));
2610 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_root);
2611 children_vec.release ();
2612 if (all_p)
2613 remove_low_level_allocnos ();
2614 else
2615 remove_unnecessary_allocnos ();
2616 while (removed_loop_vec.length () > 0)
2617 finish_loop_tree_node (removed_loop_vec.pop ());
2618 removed_loop_vec.release ();
2623 /* At this point true value of allocno attribute bad_spill_p means
2624 that there is an insn where allocno occurs and where the allocno
2625 can not be used as memory. The function updates the attribute, now
2626 it can be true only for allocnos which can not be used as memory in
2627 an insn and in whose live ranges there is other allocno deaths.
2628 Spilling allocnos with true value will not improve the code because
2629 it will not make other allocnos colorable and additional reloads
2630 for the corresponding pseudo will be generated in reload pass for
2631 each insn it occurs.
2633 This is a trick mentioned in one classic article of Chaitin etc
2634 which is frequently omitted in other implementations of RA based on
2635 graph coloring. */
2636 static void
2637 update_bad_spill_attribute (void)
2639 int i;
2640 ira_allocno_t a;
2641 ira_allocno_iterator ai;
2642 ira_allocno_object_iterator aoi;
2643 ira_object_t obj;
2644 live_range_t r;
2645 enum reg_class aclass;
2646 bitmap_head dead_points[N_REG_CLASSES];
2648 for (i = 0; i < ira_allocno_classes_num; i++)
2650 aclass = ira_allocno_classes[i];
2651 bitmap_initialize (&dead_points[aclass], &reg_obstack);
2653 FOR_EACH_ALLOCNO (a, ai)
2655 aclass = ALLOCNO_CLASS (a);
2656 if (aclass == NO_REGS)
2657 continue;
2658 FOR_EACH_ALLOCNO_OBJECT (a, obj, aoi)
2659 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
2660 bitmap_set_bit (&dead_points[aclass], r->finish);
2662 FOR_EACH_ALLOCNO (a, ai)
2664 aclass = ALLOCNO_CLASS (a);
2665 if (aclass == NO_REGS)
2666 continue;
2667 if (! ALLOCNO_BAD_SPILL_P (a))
2668 continue;
2669 FOR_EACH_ALLOCNO_OBJECT (a, obj, aoi)
2671 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
2673 for (i = r->start + 1; i < r->finish; i++)
2674 if (bitmap_bit_p (&dead_points[aclass], i))
2675 break;
2676 if (i < r->finish)
2677 break;
2679 if (r != NULL)
2681 ALLOCNO_BAD_SPILL_P (a) = false;
2682 break;
2686 for (i = 0; i < ira_allocno_classes_num; i++)
2688 aclass = ira_allocno_classes[i];
2689 bitmap_clear (&dead_points[aclass]);
2695 /* Set up minimal and maximal live range points for allocnos. */
2696 static void
2697 setup_min_max_allocno_live_range_point (void)
2699 int i;
2700 ira_allocno_t a, parent_a, cap;
2701 ira_allocno_iterator ai;
2702 #ifdef ENABLE_IRA_CHECKING
2703 ira_object_iterator oi;
2704 ira_object_t obj;
2705 #endif
2706 live_range_t r;
2707 ira_loop_tree_node_t parent;
2709 FOR_EACH_ALLOCNO (a, ai)
2711 int n = ALLOCNO_NUM_OBJECTS (a);
2713 for (i = 0; i < n; i++)
2715 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2716 r = OBJECT_LIVE_RANGES (obj);
2717 if (r == NULL)
2718 continue;
2719 OBJECT_MAX (obj) = r->finish;
2720 for (; r->next != NULL; r = r->next)
2722 OBJECT_MIN (obj) = r->start;
2725 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
2726 for (a = ira_regno_allocno_map[i];
2727 a != NULL;
2728 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2730 int j;
2731 int n = ALLOCNO_NUM_OBJECTS (a);
2733 for (j = 0; j < n; j++)
2735 ira_object_t obj = ALLOCNO_OBJECT (a, j);
2736 ira_object_t parent_obj;
2738 if (OBJECT_MAX (obj) < 0)
2739 continue;
2740 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2741 /* Accumulation of range info. */
2742 if (ALLOCNO_CAP (a) != NULL)
2744 for (cap = ALLOCNO_CAP (a); cap != NULL; cap = ALLOCNO_CAP (cap))
2746 ira_object_t cap_obj = ALLOCNO_OBJECT (cap, j);
2747 if (OBJECT_MAX (cap_obj) < OBJECT_MAX (obj))
2748 OBJECT_MAX (cap_obj) = OBJECT_MAX (obj);
2749 if (OBJECT_MIN (cap_obj) > OBJECT_MIN (obj))
2750 OBJECT_MIN (cap_obj) = OBJECT_MIN (obj);
2752 continue;
2754 if ((parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) == NULL)
2755 continue;
2756 parent_a = parent->regno_allocno_map[i];
2757 parent_obj = ALLOCNO_OBJECT (parent_a, j);
2758 if (OBJECT_MAX (parent_obj) < OBJECT_MAX (obj))
2759 OBJECT_MAX (parent_obj) = OBJECT_MAX (obj);
2760 if (OBJECT_MIN (parent_obj) > OBJECT_MIN (obj))
2761 OBJECT_MIN (parent_obj) = OBJECT_MIN (obj);
2764 #ifdef ENABLE_IRA_CHECKING
2765 FOR_EACH_OBJECT (obj, oi)
2767 if ((0 <= OBJECT_MIN (obj) && OBJECT_MIN (obj) <= ira_max_point)
2768 && (0 <= OBJECT_MAX (obj) && OBJECT_MAX (obj) <= ira_max_point))
2769 continue;
2770 gcc_unreachable ();
2772 #endif
2775 /* Sort allocnos according to their live ranges. Allocnos with
2776 smaller allocno class are put first unless we use priority
2777 coloring. Allocnos with the same class are ordered according
2778 their start (min). Allocnos with the same start are ordered
2779 according their finish (max). */
2780 static int
2781 object_range_compare_func (const void *v1p, const void *v2p)
2783 int diff;
2784 ira_object_t obj1 = *(const ira_object_t *) v1p;
2785 ira_object_t obj2 = *(const ira_object_t *) v2p;
2786 ira_allocno_t a1 = OBJECT_ALLOCNO (obj1);
2787 ira_allocno_t a2 = OBJECT_ALLOCNO (obj2);
2789 if ((diff = OBJECT_MIN (obj1) - OBJECT_MIN (obj2)) != 0)
2790 return diff;
2791 if ((diff = OBJECT_MAX (obj1) - OBJECT_MAX (obj2)) != 0)
2792 return diff;
2793 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2796 /* Sort ira_object_id_map and set up conflict id of allocnos. */
2797 static void
2798 sort_conflict_id_map (void)
2800 int i, num;
2801 ira_allocno_t a;
2802 ira_allocno_iterator ai;
2804 num = 0;
2805 FOR_EACH_ALLOCNO (a, ai)
2807 ira_allocno_object_iterator oi;
2808 ira_object_t obj;
2810 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
2811 ira_object_id_map[num++] = obj;
2813 if (num > 1)
2814 qsort (ira_object_id_map, num, sizeof (ira_object_t),
2815 object_range_compare_func);
2816 for (i = 0; i < num; i++)
2818 ira_object_t obj = ira_object_id_map[i];
2820 gcc_assert (obj != NULL);
2821 OBJECT_CONFLICT_ID (obj) = i;
2823 for (i = num; i < ira_objects_num; i++)
2824 ira_object_id_map[i] = NULL;
2827 /* Set up minimal and maximal conflict ids of allocnos with which
2828 given allocno can conflict. */
2829 static void
2830 setup_min_max_conflict_allocno_ids (void)
2832 int aclass;
2833 int i, j, min, max, start, finish, first_not_finished, filled_area_start;
2834 int *live_range_min, *last_lived;
2835 int word0_min, word0_max;
2836 ira_allocno_t a;
2837 ira_allocno_iterator ai;
2839 live_range_min = (int *) ira_allocate (sizeof (int) * ira_objects_num);
2840 aclass = -1;
2841 first_not_finished = -1;
2842 for (i = 0; i < ira_objects_num; i++)
2844 ira_object_t obj = ira_object_id_map[i];
2846 if (obj == NULL)
2847 continue;
2849 a = OBJECT_ALLOCNO (obj);
2851 if (aclass < 0)
2853 aclass = ALLOCNO_CLASS (a);
2854 min = i;
2855 first_not_finished = i;
2857 else
2859 start = OBJECT_MIN (obj);
2860 /* If we skip an allocno, the allocno with smaller ids will
2861 be also skipped because of the secondary sorting the
2862 range finishes (see function
2863 object_range_compare_func). */
2864 while (first_not_finished < i
2865 && start > OBJECT_MAX (ira_object_id_map
2866 [first_not_finished]))
2867 first_not_finished++;
2868 min = first_not_finished;
2870 if (min == i)
2871 /* We could increase min further in this case but it is good
2872 enough. */
2873 min++;
2874 live_range_min[i] = OBJECT_MIN (obj);
2875 OBJECT_MIN (obj) = min;
2877 last_lived = (int *) ira_allocate (sizeof (int) * ira_max_point);
2878 aclass = -1;
2879 filled_area_start = -1;
2880 for (i = ira_objects_num - 1; i >= 0; i--)
2882 ira_object_t obj = ira_object_id_map[i];
2884 if (obj == NULL)
2885 continue;
2887 a = OBJECT_ALLOCNO (obj);
2888 if (aclass < 0)
2890 aclass = ALLOCNO_CLASS (a);
2891 for (j = 0; j < ira_max_point; j++)
2892 last_lived[j] = -1;
2893 filled_area_start = ira_max_point;
2895 min = live_range_min[i];
2896 finish = OBJECT_MAX (obj);
2897 max = last_lived[finish];
2898 if (max < 0)
2899 /* We could decrease max further in this case but it is good
2900 enough. */
2901 max = OBJECT_CONFLICT_ID (obj) - 1;
2902 OBJECT_MAX (obj) = max;
2903 /* In filling, we can go further A range finish to recognize
2904 intersection quickly because if the finish of subsequently
2905 processed allocno (it has smaller conflict id) range is
2906 further A range finish than they are definitely intersected
2907 (the reason for this is the allocnos with bigger conflict id
2908 have their range starts not smaller than allocnos with
2909 smaller ids. */
2910 for (j = min; j < filled_area_start; j++)
2911 last_lived[j] = i;
2912 filled_area_start = min;
2914 ira_free (last_lived);
2915 ira_free (live_range_min);
2917 /* For allocnos with more than one object, we may later record extra conflicts in
2918 subobject 0 that we cannot really know about here.
2919 For now, simply widen the min/max range of these subobjects. */
2921 word0_min = INT_MAX;
2922 word0_max = INT_MIN;
2924 FOR_EACH_ALLOCNO (a, ai)
2926 int n = ALLOCNO_NUM_OBJECTS (a);
2927 ira_object_t obj0;
2929 if (n < 2)
2930 continue;
2931 obj0 = ALLOCNO_OBJECT (a, 0);
2932 if (OBJECT_CONFLICT_ID (obj0) < word0_min)
2933 word0_min = OBJECT_CONFLICT_ID (obj0);
2934 if (OBJECT_CONFLICT_ID (obj0) > word0_max)
2935 word0_max = OBJECT_CONFLICT_ID (obj0);
2937 FOR_EACH_ALLOCNO (a, ai)
2939 int n = ALLOCNO_NUM_OBJECTS (a);
2940 ira_object_t obj0;
2942 if (n < 2)
2943 continue;
2944 obj0 = ALLOCNO_OBJECT (a, 0);
2945 if (OBJECT_MIN (obj0) > word0_min)
2946 OBJECT_MIN (obj0) = word0_min;
2947 if (OBJECT_MAX (obj0) < word0_max)
2948 OBJECT_MAX (obj0) = word0_max;
2954 static void
2955 create_caps (void)
2957 ira_allocno_t a;
2958 ira_allocno_iterator ai;
2959 ira_loop_tree_node_t loop_tree_node;
2961 FOR_EACH_ALLOCNO (a, ai)
2963 if (ALLOCNO_LOOP_TREE_NODE (a) == ira_loop_tree_root)
2964 continue;
2965 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2966 create_cap_allocno (a);
2967 else if (ALLOCNO_CAP (a) == NULL)
2969 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
2970 if (!bitmap_bit_p (loop_tree_node->border_allocnos, ALLOCNO_NUM (a)))
2971 create_cap_allocno (a);
2978 /* The page contains code transforming more one region internal
2979 representation (IR) to one region IR which is necessary for reload.
2980 This transformation is called IR flattening. We might just rebuild
2981 the IR for one region but we don't do it because it takes a lot of
2982 time. */
2984 /* Map: regno -> allocnos which will finally represent the regno for
2985 IR with one region. */
2986 static ira_allocno_t *regno_top_level_allocno_map;
2988 /* Find the allocno that corresponds to A at a level one higher up in the
2989 loop tree. Returns NULL if A is a cap, or if it has no parent. */
2990 ira_allocno_t
2991 ira_parent_allocno (ira_allocno_t a)
2993 ira_loop_tree_node_t parent;
2995 if (ALLOCNO_CAP (a) != NULL)
2996 return NULL;
2998 parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
2999 if (parent == NULL)
3000 return NULL;
3002 return parent->regno_allocno_map[ALLOCNO_REGNO (a)];
3005 /* Find the allocno that corresponds to A at a level one higher up in the
3006 loop tree. If ALLOCNO_CAP is set for A, return that. */
3007 ira_allocno_t
3008 ira_parent_or_cap_allocno (ira_allocno_t a)
3010 if (ALLOCNO_CAP (a) != NULL)
3011 return ALLOCNO_CAP (a);
3013 return ira_parent_allocno (a);
3016 /* Process all allocnos originated from pseudo REGNO and copy live
3017 ranges, hard reg conflicts, and allocno stack reg attributes from
3018 low level allocnos to final allocnos which are destinations of
3019 removed stores at a loop exit. Return true if we copied live
3020 ranges. */
3021 static bool
3022 copy_info_to_removed_store_destinations (int regno)
3024 ira_allocno_t a;
3025 ira_allocno_t parent_a = NULL;
3026 ira_loop_tree_node_t parent;
3027 bool merged_p;
3029 merged_p = false;
3030 for (a = ira_regno_allocno_map[regno];
3031 a != NULL;
3032 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
3034 if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))])
3035 /* This allocno will be removed. */
3036 continue;
3038 /* Caps will be removed. */
3039 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3040 for (parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
3041 parent != NULL;
3042 parent = parent->parent)
3043 if ((parent_a = parent->regno_allocno_map[regno]) == NULL
3044 || (parent_a
3045 == regno_top_level_allocno_map[REGNO
3046 (allocno_emit_reg (parent_a))]
3047 && ALLOCNO_EMIT_DATA (parent_a)->mem_optimized_dest_p))
3048 break;
3049 if (parent == NULL || parent_a == NULL)
3050 continue;
3052 copy_allocno_live_ranges (a, parent_a);
3053 merge_hard_reg_conflicts (a, parent_a, true);
3055 ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
3056 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
3057 += ALLOCNO_CALLS_CROSSED_NUM (a);
3058 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
3059 += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
3060 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (parent_a),
3061 ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
3062 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
3063 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
3064 merged_p = true;
3066 return merged_p;
3069 /* Flatten the IR. In other words, this function transforms IR as if
3070 it were built with one region (without loops). We could make it
3071 much simpler by rebuilding IR with one region, but unfortunately it
3072 takes a lot of time. MAX_REGNO_BEFORE_EMIT and
3073 IRA_MAX_POINT_BEFORE_EMIT are correspondingly MAX_REG_NUM () and
3074 IRA_MAX_POINT before emitting insns on the loop borders. */
3075 void
3076 ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
3078 int i, j;
3079 bool keep_p;
3080 int hard_regs_num;
3081 bool new_pseudos_p, merged_p, mem_dest_p;
3082 unsigned int n;
3083 enum reg_class aclass;
3084 ira_allocno_t a, parent_a, first, second, node_first, node_second;
3085 ira_copy_t cp;
3086 ira_loop_tree_node_t node;
3087 live_range_t r;
3088 ira_allocno_iterator ai;
3089 ira_copy_iterator ci;
3091 regno_top_level_allocno_map
3092 = (ira_allocno_t *) ira_allocate (max_reg_num ()
3093 * sizeof (ira_allocno_t));
3094 memset (regno_top_level_allocno_map, 0,
3095 max_reg_num () * sizeof (ira_allocno_t));
3096 new_pseudos_p = merged_p = false;
3097 FOR_EACH_ALLOCNO (a, ai)
3099 ira_allocno_object_iterator oi;
3100 ira_object_t obj;
3102 if (ALLOCNO_CAP_MEMBER (a) != NULL)
3103 /* Caps are not in the regno allocno maps and they are never
3104 will be transformed into allocnos existing after IR
3105 flattening. */
3106 continue;
3107 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
3108 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
3109 OBJECT_CONFLICT_HARD_REGS (obj));
3110 #ifdef STACK_REGS
3111 ALLOCNO_TOTAL_NO_STACK_REG_P (a) = ALLOCNO_NO_STACK_REG_P (a);
3112 #endif
3114 /* Fix final allocno attributes. */
3115 for (i = max_regno_before_emit - 1; i >= FIRST_PSEUDO_REGISTER; i--)
3117 mem_dest_p = false;
3118 for (a = ira_regno_allocno_map[i];
3119 a != NULL;
3120 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
3122 ira_emit_data_t parent_data, data = ALLOCNO_EMIT_DATA (a);
3124 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3125 if (data->somewhere_renamed_p)
3126 new_pseudos_p = true;
3127 parent_a = ira_parent_allocno (a);
3128 if (parent_a == NULL)
3130 ALLOCNO_COPIES (a) = NULL;
3131 regno_top_level_allocno_map[REGNO (data->reg)] = a;
3132 continue;
3134 ira_assert (ALLOCNO_CAP_MEMBER (parent_a) == NULL);
3136 if (data->mem_optimized_dest != NULL)
3137 mem_dest_p = true;
3138 parent_data = ALLOCNO_EMIT_DATA (parent_a);
3139 if (REGNO (data->reg) == REGNO (parent_data->reg))
3141 merge_hard_reg_conflicts (a, parent_a, true);
3142 move_allocno_live_ranges (a, parent_a);
3143 merged_p = true;
3144 parent_data->mem_optimized_dest_p
3145 = (parent_data->mem_optimized_dest_p
3146 || data->mem_optimized_dest_p);
3147 continue;
3149 new_pseudos_p = true;
3150 for (;;)
3152 ALLOCNO_NREFS (parent_a) -= ALLOCNO_NREFS (a);
3153 ALLOCNO_FREQ (parent_a) -= ALLOCNO_FREQ (a);
3154 ALLOCNO_CALL_FREQ (parent_a) -= ALLOCNO_CALL_FREQ (a);
3155 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
3156 -= ALLOCNO_CALLS_CROSSED_NUM (a);
3157 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
3158 -= ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
3159 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
3160 -= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
3161 ira_assert (ALLOCNO_CALLS_CROSSED_NUM (parent_a) >= 0
3162 && ALLOCNO_NREFS (parent_a) >= 0
3163 && ALLOCNO_FREQ (parent_a) >= 0);
3164 aclass = ALLOCNO_CLASS (parent_a);
3165 hard_regs_num = ira_class_hard_regs_num[aclass];
3166 if (ALLOCNO_HARD_REG_COSTS (a) != NULL
3167 && ALLOCNO_HARD_REG_COSTS (parent_a) != NULL)
3168 for (j = 0; j < hard_regs_num; j++)
3169 ALLOCNO_HARD_REG_COSTS (parent_a)[j]
3170 -= ALLOCNO_HARD_REG_COSTS (a)[j];
3171 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL
3172 && ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a) != NULL)
3173 for (j = 0; j < hard_regs_num; j++)
3174 ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a)[j]
3175 -= ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[j];
3176 ALLOCNO_CLASS_COST (parent_a)
3177 -= ALLOCNO_CLASS_COST (a);
3178 ALLOCNO_MEMORY_COST (parent_a) -= ALLOCNO_MEMORY_COST (a);
3179 parent_a = ira_parent_allocno (parent_a);
3180 if (parent_a == NULL)
3181 break;
3183 ALLOCNO_COPIES (a) = NULL;
3184 regno_top_level_allocno_map[REGNO (data->reg)] = a;
3186 if (mem_dest_p && copy_info_to_removed_store_destinations (i))
3187 merged_p = true;
3189 ira_assert (new_pseudos_p || ira_max_point_before_emit == ira_max_point);
3190 if (merged_p || ira_max_point_before_emit != ira_max_point)
3191 ira_rebuild_start_finish_chains ();
3192 if (new_pseudos_p)
3194 sparseset objects_live;
3196 /* Rebuild conflicts. */
3197 FOR_EACH_ALLOCNO (a, ai)
3199 ira_allocno_object_iterator oi;
3200 ira_object_t obj;
3202 if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
3203 || ALLOCNO_CAP_MEMBER (a) != NULL)
3204 continue;
3205 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
3207 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
3208 ira_assert (r->object == obj);
3209 clear_conflicts (obj);
3212 objects_live = sparseset_alloc (ira_objects_num);
3213 for (i = 0; i < ira_max_point; i++)
3215 for (r = ira_start_point_ranges[i]; r != NULL; r = r->start_next)
3217 ira_object_t obj = r->object;
3219 a = OBJECT_ALLOCNO (obj);
3220 if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
3221 || ALLOCNO_CAP_MEMBER (a) != NULL)
3222 continue;
3224 aclass = ALLOCNO_CLASS (a);
3225 EXECUTE_IF_SET_IN_SPARSESET (objects_live, n)
3227 ira_object_t live_obj = ira_object_id_map[n];
3228 ira_allocno_t live_a = OBJECT_ALLOCNO (live_obj);
3229 enum reg_class live_aclass = ALLOCNO_CLASS (live_a);
3231 if (ira_reg_classes_intersect_p[aclass][live_aclass]
3232 /* Don't set up conflict for the allocno with itself. */
3233 && live_a != a)
3234 ira_add_conflict (obj, live_obj);
3236 sparseset_set_bit (objects_live, OBJECT_CONFLICT_ID (obj));
3239 for (r = ira_finish_point_ranges[i]; r != NULL; r = r->finish_next)
3240 sparseset_clear_bit (objects_live, OBJECT_CONFLICT_ID (r->object));
3242 sparseset_free (objects_live);
3243 compress_conflict_vecs ();
3245 /* Mark some copies for removing and change allocnos in the rest
3246 copies. */
3247 FOR_EACH_COPY (cp, ci)
3249 if (ALLOCNO_CAP_MEMBER (cp->first) != NULL
3250 || ALLOCNO_CAP_MEMBER (cp->second) != NULL)
3252 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
3253 fprintf
3254 (ira_dump_file, " Remove cp%d:%c%dr%d-%c%dr%d\n",
3255 cp->num, ALLOCNO_CAP_MEMBER (cp->first) != NULL ? 'c' : 'a',
3256 ALLOCNO_NUM (cp->first),
3257 REGNO (allocno_emit_reg (cp->first)),
3258 ALLOCNO_CAP_MEMBER (cp->second) != NULL ? 'c' : 'a',
3259 ALLOCNO_NUM (cp->second),
3260 REGNO (allocno_emit_reg (cp->second)));
3261 cp->loop_tree_node = NULL;
3262 continue;
3264 first
3265 = regno_top_level_allocno_map[REGNO (allocno_emit_reg (cp->first))];
3266 second
3267 = regno_top_level_allocno_map[REGNO (allocno_emit_reg (cp->second))];
3268 node = cp->loop_tree_node;
3269 if (node == NULL)
3270 keep_p = true; /* It copy generated in ira-emit.c. */
3271 else
3273 /* Check that the copy was not propagated from level on
3274 which we will have different pseudos. */
3275 node_first = node->regno_allocno_map[ALLOCNO_REGNO (cp->first)];
3276 node_second = node->regno_allocno_map[ALLOCNO_REGNO (cp->second)];
3277 keep_p = ((REGNO (allocno_emit_reg (first))
3278 == REGNO (allocno_emit_reg (node_first)))
3279 && (REGNO (allocno_emit_reg (second))
3280 == REGNO (allocno_emit_reg (node_second))));
3282 if (keep_p)
3284 cp->loop_tree_node = ira_loop_tree_root;
3285 cp->first = first;
3286 cp->second = second;
3288 else
3290 cp->loop_tree_node = NULL;
3291 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
3292 fprintf (ira_dump_file, " Remove cp%d:a%dr%d-a%dr%d\n",
3293 cp->num, ALLOCNO_NUM (cp->first),
3294 REGNO (allocno_emit_reg (cp->first)),
3295 ALLOCNO_NUM (cp->second),
3296 REGNO (allocno_emit_reg (cp->second)));
3299 /* Remove unnecessary allocnos on lower levels of the loop tree. */
3300 FOR_EACH_ALLOCNO (a, ai)
3302 if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
3303 || ALLOCNO_CAP_MEMBER (a) != NULL)
3305 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
3306 fprintf (ira_dump_file, " Remove a%dr%d\n",
3307 ALLOCNO_NUM (a), REGNO (allocno_emit_reg (a)));
3308 ira_remove_allocno_prefs (a);
3309 finish_allocno (a);
3310 continue;
3312 ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
3313 ALLOCNO_REGNO (a) = REGNO (allocno_emit_reg (a));
3314 ALLOCNO_CAP (a) = NULL;
3315 /* Restore updated costs for assignments from reload. */
3316 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
3317 ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
3318 if (! ALLOCNO_ASSIGNED_P (a))
3319 ira_free_allocno_updated_costs (a);
3320 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3321 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3323 /* Remove unnecessary copies. */
3324 FOR_EACH_COPY (cp, ci)
3326 if (cp->loop_tree_node == NULL)
3328 ira_copies[cp->num] = NULL;
3329 finish_copy (cp);
3330 continue;
3332 ira_assert
3333 (ALLOCNO_LOOP_TREE_NODE (cp->first) == ira_loop_tree_root
3334 && ALLOCNO_LOOP_TREE_NODE (cp->second) == ira_loop_tree_root);
3335 add_allocno_copy_to_list (cp);
3336 swap_allocno_copy_ends_if_necessary (cp);
3338 rebuild_regno_allocno_maps ();
3339 if (ira_max_point != ira_max_point_before_emit)
3340 ira_compress_allocno_live_ranges ();
3341 ira_free (regno_top_level_allocno_map);
3346 #ifdef ENABLE_IRA_CHECKING
3347 /* Check creation of all allocnos. Allocnos on lower levels should
3348 have allocnos or caps on all upper levels. */
3349 static void
3350 check_allocno_creation (void)
3352 ira_allocno_t a;
3353 ira_allocno_iterator ai;
3354 ira_loop_tree_node_t loop_tree_node;
3356 FOR_EACH_ALLOCNO (a, ai)
3358 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
3359 ira_assert (bitmap_bit_p (loop_tree_node->all_allocnos,
3360 ALLOCNO_NUM (a)));
3361 if (loop_tree_node == ira_loop_tree_root)
3362 continue;
3363 if (ALLOCNO_CAP_MEMBER (a) != NULL)
3364 ira_assert (ALLOCNO_CAP (a) != NULL);
3365 else if (ALLOCNO_CAP (a) == NULL)
3366 ira_assert (loop_tree_node->parent
3367 ->regno_allocno_map[ALLOCNO_REGNO (a)] != NULL
3368 && bitmap_bit_p (loop_tree_node->border_allocnos,
3369 ALLOCNO_NUM (a)));
3372 #endif
3374 /* Identify allocnos which prefer a register class with a single hard register.
3375 Adjust ALLOCNO_CONFLICT_HARD_REG_COSTS so that conflicting allocnos are
3376 less likely to use the preferred singleton register. */
3377 static void
3378 update_conflict_hard_reg_costs (void)
3380 ira_allocno_t a;
3381 ira_allocno_iterator ai;
3382 int i, index, min;
3384 FOR_EACH_ALLOCNO (a, ai)
3386 reg_class_t aclass = ALLOCNO_CLASS (a);
3387 reg_class_t pref = reg_preferred_class (ALLOCNO_REGNO (a));
3388 int singleton = ira_class_singleton[pref][ALLOCNO_MODE (a)];
3389 if (singleton < 0)
3390 continue;
3391 index = ira_class_hard_reg_index[(int) aclass][singleton];
3392 if (index < 0)
3393 continue;
3394 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) == NULL
3395 || ALLOCNO_HARD_REG_COSTS (a) == NULL)
3396 continue;
3397 min = INT_MAX;
3398 for (i = ira_class_hard_regs_num[(int) aclass] - 1; i >= 0; i--)
3399 if (ALLOCNO_HARD_REG_COSTS (a)[i] > ALLOCNO_CLASS_COST (a)
3400 && min > ALLOCNO_HARD_REG_COSTS (a)[i])
3401 min = ALLOCNO_HARD_REG_COSTS (a)[i];
3402 if (min == INT_MAX)
3403 continue;
3404 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
3405 aclass, 0);
3406 ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[index]
3407 -= min - ALLOCNO_CLASS_COST (a);
3411 /* Create a internal representation (IR) for IRA (allocnos, copies,
3412 loop tree nodes). The function returns TRUE if we generate loop
3413 structure (besides nodes representing all function and the basic
3414 blocks) for regional allocation. A true return means that we
3415 really need to flatten IR before the reload. */
3416 bool
3417 ira_build (void)
3419 bool loops_p;
3421 df_analyze ();
3422 initiate_cost_vectors ();
3423 initiate_allocnos ();
3424 initiate_prefs ();
3425 initiate_copies ();
3426 create_loop_tree_nodes ();
3427 form_loop_tree ();
3428 create_allocnos ();
3429 ira_costs ();
3430 create_allocno_objects ();
3431 ira_create_allocno_live_ranges ();
3432 remove_unnecessary_regions (false);
3433 ira_compress_allocno_live_ranges ();
3434 update_bad_spill_attribute ();
3435 loops_p = more_one_region_p ();
3436 if (loops_p)
3438 propagate_allocno_info ();
3439 create_caps ();
3441 ira_tune_allocno_costs ();
3442 #ifdef ENABLE_IRA_CHECKING
3443 check_allocno_creation ();
3444 #endif
3445 setup_min_max_allocno_live_range_point ();
3446 sort_conflict_id_map ();
3447 setup_min_max_conflict_allocno_ids ();
3448 ira_build_conflicts ();
3449 update_conflict_hard_reg_costs ();
3450 if (! ira_conflicts_p)
3452 ira_allocno_t a;
3453 ira_allocno_iterator ai;
3455 /* Remove all regions but root one. */
3456 if (loops_p)
3458 remove_unnecessary_regions (true);
3459 loops_p = false;
3461 /* We don't save hard registers around calls for fast allocation
3462 -- add caller clobbered registers as conflicting ones to
3463 allocno crossing calls. */
3464 FOR_EACH_ALLOCNO (a, ai)
3465 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
3466 ior_hard_reg_conflicts (a, &call_used_reg_set);
3468 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3469 print_copies (ira_dump_file);
3470 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3471 print_prefs (ira_dump_file);
3472 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3474 int n, nr, nr_big;
3475 ira_allocno_t a;
3476 live_range_t r;
3477 ira_allocno_iterator ai;
3479 n = 0;
3480 nr = 0;
3481 nr_big = 0;
3482 FOR_EACH_ALLOCNO (a, ai)
3484 int j, nobj = ALLOCNO_NUM_OBJECTS (a);
3486 if (nobj > 1)
3487 nr_big++;
3488 for (j = 0; j < nobj; j++)
3490 ira_object_t obj = ALLOCNO_OBJECT (a, j);
3491 n += OBJECT_NUM_CONFLICTS (obj);
3492 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
3493 nr++;
3496 fprintf (ira_dump_file, " regions=%d, blocks=%d, points=%d\n",
3497 current_loops == NULL ? 1 : number_of_loops (cfun),
3498 n_basic_blocks_for_fn (cfun), ira_max_point);
3499 fprintf (ira_dump_file,
3500 " allocnos=%d (big %d), copies=%d, conflicts=%d, ranges=%d\n",
3501 ira_allocnos_num, nr_big, ira_copies_num, n, nr);
3503 return loops_p;
3506 /* Release the data created by function ira_build. */
3507 void
3508 ira_destroy (void)
3510 finish_loop_tree_nodes ();
3511 finish_prefs ();
3512 finish_copies ();
3513 finish_allocnos ();
3514 finish_cost_vectors ();
3515 ira_finish_allocno_live_ranges ();