New syntax for -fsanitize-recover.
[gcc.git] / gcc / ira-build.c
1 /* Building internal representation for IRA.
2 Copyright (C) 2006-2014 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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 "basic-block.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "diagnostic-core.h"
35 #include "params.h"
36 #include "df.h"
37 #include "reload.h"
38 #include "sparseset.h"
39 #include "ira-int.h"
40 #include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
41
42 static ira_copy_t find_allocno_copy (ira_allocno_t, ira_allocno_t, rtx_insn *,
43 ira_loop_tree_node_t);
44
45 /* The root of the loop tree corresponding to the all function. */
46 ira_loop_tree_node_t ira_loop_tree_root;
47
48 /* Height of the loop tree. */
49 int ira_loop_tree_height;
50
51 /* All nodes representing basic blocks are referred through the
52 following array. We can not use basic block member `aux' for this
53 because it is used for insertion of insns on edges. */
54 ira_loop_tree_node_t ira_bb_nodes;
55
56 /* All nodes representing loops are referred through the following
57 array. */
58 ira_loop_tree_node_t ira_loop_nodes;
59
60 /* And size of the ira_loop_nodes array. */
61 unsigned int ira_loop_nodes_count;
62
63 /* Map regno -> allocnos with given regno (see comments for
64 allocno member `next_regno_allocno'). */
65 ira_allocno_t *ira_regno_allocno_map;
66
67 /* Array of references to all allocnos. The order number of the
68 allocno corresponds to the index in the array. Removed allocnos
69 have NULL element value. */
70 ira_allocno_t *ira_allocnos;
71
72 /* Sizes of the previous array. */
73 int ira_allocnos_num;
74
75 /* Count of conflict record structures we've created, used when creating
76 a new conflict id. */
77 int ira_objects_num;
78
79 /* Map a conflict id to its conflict record. */
80 ira_object_t *ira_object_id_map;
81
82 /* Array of references to all allocno preferences. The order number
83 of the preference corresponds to the index in the array. */
84 ira_pref_t *ira_prefs;
85
86 /* Size of the previous array. */
87 int ira_prefs_num;
88
89 /* Array of references to all copies. The order number of the copy
90 corresponds to the index in the array. Removed copies have NULL
91 element value. */
92 ira_copy_t *ira_copies;
93
94 /* Size of the previous array. */
95 int ira_copies_num;
96
97 \f
98
99 /* LAST_BASIC_BLOCK before generating additional insns because of live
100 range splitting. Emitting insns on a critical edge creates a new
101 basic block. */
102 static int last_basic_block_before_change;
103
104 /* Initialize some members in loop tree node NODE. Use LOOP_NUM for
105 the member loop_num. */
106 static void
107 init_loop_tree_node (struct ira_loop_tree_node *node, int loop_num)
108 {
109 int max_regno = max_reg_num ();
110
111 node->regno_allocno_map
112 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t) * max_regno);
113 memset (node->regno_allocno_map, 0, sizeof (ira_allocno_t) * max_regno);
114 memset (node->reg_pressure, 0, sizeof (node->reg_pressure));
115 node->all_allocnos = ira_allocate_bitmap ();
116 node->modified_regnos = ira_allocate_bitmap ();
117 node->border_allocnos = ira_allocate_bitmap ();
118 node->local_copies = ira_allocate_bitmap ();
119 node->loop_num = loop_num;
120 node->children = NULL;
121 node->subloops = NULL;
122 }
123
124
125 /* The following function allocates the loop tree nodes. If
126 CURRENT_LOOPS is NULL, the nodes corresponding to the loops (except
127 the root which corresponds the all function) will be not allocated
128 but nodes will still be allocated for basic blocks. */
129 static void
130 create_loop_tree_nodes (void)
131 {
132 unsigned int i, j;
133 bool skip_p;
134 edge_iterator ei;
135 edge e;
136 vec<edge> edges;
137 loop_p loop;
138
139 ira_bb_nodes
140 = ((struct ira_loop_tree_node *)
141 ira_allocate (sizeof (struct ira_loop_tree_node)
142 * last_basic_block_for_fn (cfun)));
143 last_basic_block_before_change = last_basic_block_for_fn (cfun);
144 for (i = 0; i < (unsigned int) last_basic_block_for_fn (cfun); i++)
145 {
146 ira_bb_nodes[i].regno_allocno_map = NULL;
147 memset (ira_bb_nodes[i].reg_pressure, 0,
148 sizeof (ira_bb_nodes[i].reg_pressure));
149 ira_bb_nodes[i].all_allocnos = NULL;
150 ira_bb_nodes[i].modified_regnos = NULL;
151 ira_bb_nodes[i].border_allocnos = NULL;
152 ira_bb_nodes[i].local_copies = NULL;
153 }
154 if (current_loops == NULL)
155 {
156 ira_loop_nodes_count = 1;
157 ira_loop_nodes = ((struct ira_loop_tree_node *)
158 ira_allocate (sizeof (struct ira_loop_tree_node)));
159 init_loop_tree_node (ira_loop_nodes, 0);
160 return;
161 }
162 ira_loop_nodes_count = number_of_loops (cfun);
163 ira_loop_nodes = ((struct ira_loop_tree_node *)
164 ira_allocate (sizeof (struct ira_loop_tree_node)
165 * ira_loop_nodes_count));
166 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
167 {
168 if (loop_outer (loop) != NULL)
169 {
170 ira_loop_nodes[i].regno_allocno_map = NULL;
171 skip_p = false;
172 FOR_EACH_EDGE (e, ei, loop->header->preds)
173 if (e->src != loop->latch
174 && (e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
175 {
176 skip_p = true;
177 break;
178 }
179 if (skip_p)
180 continue;
181 edges = get_loop_exit_edges (loop);
182 FOR_EACH_VEC_ELT (edges, j, e)
183 if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
184 {
185 skip_p = true;
186 break;
187 }
188 edges.release ();
189 if (skip_p)
190 continue;
191 }
192 init_loop_tree_node (&ira_loop_nodes[i], loop->num);
193 }
194 }
195
196 /* The function returns TRUE if there are more one allocation
197 region. */
198 static bool
199 more_one_region_p (void)
200 {
201 unsigned int i;
202 loop_p loop;
203
204 if (current_loops != NULL)
205 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
206 if (ira_loop_nodes[i].regno_allocno_map != NULL
207 && ira_loop_tree_root != &ira_loop_nodes[i])
208 return true;
209 return false;
210 }
211
212 /* Free the loop tree node of a loop. */
213 static void
214 finish_loop_tree_node (ira_loop_tree_node_t loop)
215 {
216 if (loop->regno_allocno_map != NULL)
217 {
218 ira_assert (loop->bb == NULL);
219 ira_free_bitmap (loop->local_copies);
220 ira_free_bitmap (loop->border_allocnos);
221 ira_free_bitmap (loop->modified_regnos);
222 ira_free_bitmap (loop->all_allocnos);
223 ira_free (loop->regno_allocno_map);
224 loop->regno_allocno_map = NULL;
225 }
226 }
227
228 /* Free the loop tree nodes. */
229 static void
230 finish_loop_tree_nodes (void)
231 {
232 unsigned int i;
233
234 for (i = 0; i < ira_loop_nodes_count; i++)
235 finish_loop_tree_node (&ira_loop_nodes[i]);
236 ira_free (ira_loop_nodes);
237 for (i = 0; i < (unsigned int) last_basic_block_before_change; i++)
238 {
239 if (ira_bb_nodes[i].local_copies != NULL)
240 ira_free_bitmap (ira_bb_nodes[i].local_copies);
241 if (ira_bb_nodes[i].border_allocnos != NULL)
242 ira_free_bitmap (ira_bb_nodes[i].border_allocnos);
243 if (ira_bb_nodes[i].modified_regnos != NULL)
244 ira_free_bitmap (ira_bb_nodes[i].modified_regnos);
245 if (ira_bb_nodes[i].all_allocnos != NULL)
246 ira_free_bitmap (ira_bb_nodes[i].all_allocnos);
247 if (ira_bb_nodes[i].regno_allocno_map != NULL)
248 ira_free (ira_bb_nodes[i].regno_allocno_map);
249 }
250 ira_free (ira_bb_nodes);
251 }
252
253 \f
254
255 /* The following recursive function adds LOOP to the loop tree
256 hierarchy. LOOP is added only once. If LOOP is NULL we adding
257 loop designating the whole function when CFG loops are not
258 built. */
259 static void
260 add_loop_to_tree (struct loop *loop)
261 {
262 int loop_num;
263 struct loop *parent;
264 ira_loop_tree_node_t loop_node, parent_node;
265
266 /* We can not use loop node access macros here because of potential
267 checking and because the nodes are not initialized enough
268 yet. */
269 if (loop != NULL && loop_outer (loop) != NULL)
270 add_loop_to_tree (loop_outer (loop));
271 loop_num = loop != NULL ? loop->num : 0;
272 if (ira_loop_nodes[loop_num].regno_allocno_map != NULL
273 && ira_loop_nodes[loop_num].children == NULL)
274 {
275 /* We have not added loop node to the tree yet. */
276 loop_node = &ira_loop_nodes[loop_num];
277 loop_node->loop = loop;
278 loop_node->bb = NULL;
279 if (loop == NULL)
280 parent = NULL;
281 else
282 {
283 for (parent = loop_outer (loop);
284 parent != NULL;
285 parent = loop_outer (parent))
286 if (ira_loop_nodes[parent->num].regno_allocno_map != NULL)
287 break;
288 }
289 if (parent == NULL)
290 {
291 loop_node->next = NULL;
292 loop_node->subloop_next = NULL;
293 loop_node->parent = NULL;
294 }
295 else
296 {
297 parent_node = &ira_loop_nodes[parent->num];
298 loop_node->next = parent_node->children;
299 parent_node->children = loop_node;
300 loop_node->subloop_next = parent_node->subloops;
301 parent_node->subloops = loop_node;
302 loop_node->parent = parent_node;
303 }
304 }
305 }
306
307 /* The following recursive function sets up levels of nodes of the
308 tree given its root LOOP_NODE. The enumeration starts with LEVEL.
309 The function returns maximal value of level in the tree + 1. */
310 static int
311 setup_loop_tree_level (ira_loop_tree_node_t loop_node, int level)
312 {
313 int height, max_height;
314 ira_loop_tree_node_t subloop_node;
315
316 ira_assert (loop_node->bb == NULL);
317 loop_node->level = level;
318 max_height = level + 1;
319 for (subloop_node = loop_node->subloops;
320 subloop_node != NULL;
321 subloop_node = subloop_node->subloop_next)
322 {
323 ira_assert (subloop_node->bb == NULL);
324 height = setup_loop_tree_level (subloop_node, level + 1);
325 if (height > max_height)
326 max_height = height;
327 }
328 return max_height;
329 }
330
331 /* Create the loop tree. The algorithm is designed to provide correct
332 order of loops (they are ordered by their last loop BB) and basic
333 blocks in the chain formed by member next. */
334 static void
335 form_loop_tree (void)
336 {
337 basic_block bb;
338 struct loop *parent;
339 ira_loop_tree_node_t bb_node, loop_node;
340
341 /* We can not use loop/bb node access macros because of potential
342 checking and because the nodes are not initialized enough
343 yet. */
344 FOR_EACH_BB_FN (bb, cfun)
345 {
346 bb_node = &ira_bb_nodes[bb->index];
347 bb_node->bb = bb;
348 bb_node->loop = NULL;
349 bb_node->subloops = NULL;
350 bb_node->children = NULL;
351 bb_node->subloop_next = NULL;
352 bb_node->next = NULL;
353 if (current_loops == NULL)
354 parent = NULL;
355 else
356 {
357 for (parent = bb->loop_father;
358 parent != NULL;
359 parent = loop_outer (parent))
360 if (ira_loop_nodes[parent->num].regno_allocno_map != NULL)
361 break;
362 }
363 add_loop_to_tree (parent);
364 loop_node = &ira_loop_nodes[parent == NULL ? 0 : parent->num];
365 bb_node->next = loop_node->children;
366 bb_node->parent = loop_node;
367 loop_node->children = bb_node;
368 }
369 ira_loop_tree_root = IRA_LOOP_NODE_BY_INDEX (0);
370 ira_loop_tree_height = setup_loop_tree_level (ira_loop_tree_root, 0);
371 ira_assert (ira_loop_tree_root->regno_allocno_map != NULL);
372 }
373
374 \f
375
376 /* Rebuild IRA_REGNO_ALLOCNO_MAP and REGNO_ALLOCNO_MAPs of the loop
377 tree nodes. */
378 static void
379 rebuild_regno_allocno_maps (void)
380 {
381 unsigned int l;
382 int max_regno, regno;
383 ira_allocno_t a;
384 ira_loop_tree_node_t loop_tree_node;
385 loop_p loop;
386 ira_allocno_iterator ai;
387
388 ira_assert (current_loops != NULL);
389 max_regno = max_reg_num ();
390 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), l, loop)
391 if (ira_loop_nodes[l].regno_allocno_map != NULL)
392 {
393 ira_free (ira_loop_nodes[l].regno_allocno_map);
394 ira_loop_nodes[l].regno_allocno_map
395 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
396 * max_regno);
397 memset (ira_loop_nodes[l].regno_allocno_map, 0,
398 sizeof (ira_allocno_t) * max_regno);
399 }
400 ira_free (ira_regno_allocno_map);
401 ira_regno_allocno_map
402 = (ira_allocno_t *) ira_allocate (max_regno * sizeof (ira_allocno_t));
403 memset (ira_regno_allocno_map, 0, max_regno * sizeof (ira_allocno_t));
404 FOR_EACH_ALLOCNO (a, ai)
405 {
406 if (ALLOCNO_CAP_MEMBER (a) != NULL)
407 /* Caps are not in the regno allocno maps. */
408 continue;
409 regno = ALLOCNO_REGNO (a);
410 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
411 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = ira_regno_allocno_map[regno];
412 ira_regno_allocno_map[regno] = a;
413 if (loop_tree_node->regno_allocno_map[regno] == NULL)
414 /* Remember that we can create temporary allocnos to break
415 cycles in register shuffle. */
416 loop_tree_node->regno_allocno_map[regno] = a;
417 }
418 }
419 \f
420
421 /* Pools for allocnos, allocno live ranges and objects. */
422 static alloc_pool allocno_pool, live_range_pool, object_pool;
423
424 /* Vec containing references to all created allocnos. It is a
425 container of array allocnos. */
426 static vec<ira_allocno_t> allocno_vec;
427
428 /* Vec containing references to all created ira_objects. It is a
429 container of ira_object_id_map. */
430 static vec<ira_object_t> ira_object_id_map_vec;
431
432 /* Initialize data concerning allocnos. */
433 static void
434 initiate_allocnos (void)
435 {
436 live_range_pool
437 = create_alloc_pool ("live ranges",
438 sizeof (struct live_range), 100);
439 allocno_pool
440 = create_alloc_pool ("allocnos", sizeof (struct ira_allocno), 100);
441 object_pool
442 = create_alloc_pool ("objects", sizeof (struct ira_object), 100);
443 allocno_vec.create (max_reg_num () * 2);
444 ira_allocnos = NULL;
445 ira_allocnos_num = 0;
446 ira_objects_num = 0;
447 ira_object_id_map_vec.create (max_reg_num () * 2);
448 ira_object_id_map = NULL;
449 ira_regno_allocno_map
450 = (ira_allocno_t *) ira_allocate (max_reg_num ()
451 * sizeof (ira_allocno_t));
452 memset (ira_regno_allocno_map, 0, max_reg_num () * sizeof (ira_allocno_t));
453 }
454
455 /* Create and return an object corresponding to a new allocno A. */
456 static ira_object_t
457 ira_create_object (ira_allocno_t a, int subword)
458 {
459 enum reg_class aclass = ALLOCNO_CLASS (a);
460 ira_object_t obj = (ira_object_t) pool_alloc (object_pool);
461
462 OBJECT_ALLOCNO (obj) = a;
463 OBJECT_SUBWORD (obj) = subword;
464 OBJECT_CONFLICT_ID (obj) = ira_objects_num;
465 OBJECT_CONFLICT_VEC_P (obj) = false;
466 OBJECT_CONFLICT_ARRAY (obj) = NULL;
467 OBJECT_NUM_CONFLICTS (obj) = 0;
468 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj), ira_no_alloc_regs);
469 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), ira_no_alloc_regs);
470 IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
471 reg_class_contents[aclass]);
472 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
473 reg_class_contents[aclass]);
474 OBJECT_MIN (obj) = INT_MAX;
475 OBJECT_MAX (obj) = -1;
476 OBJECT_LIVE_RANGES (obj) = NULL;
477
478 ira_object_id_map_vec.safe_push (obj);
479 ira_object_id_map
480 = ira_object_id_map_vec.address ();
481 ira_objects_num = ira_object_id_map_vec.length ();
482
483 return obj;
484 }
485
486 /* Create and return the allocno corresponding to REGNO in
487 LOOP_TREE_NODE. Add the allocno to the list of allocnos with the
488 same regno if CAP_P is FALSE. */
489 ira_allocno_t
490 ira_create_allocno (int regno, bool cap_p,
491 ira_loop_tree_node_t loop_tree_node)
492 {
493 ira_allocno_t a;
494
495 a = (ira_allocno_t) pool_alloc (allocno_pool);
496 ALLOCNO_REGNO (a) = regno;
497 ALLOCNO_LOOP_TREE_NODE (a) = loop_tree_node;
498 if (! cap_p)
499 {
500 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = ira_regno_allocno_map[regno];
501 ira_regno_allocno_map[regno] = a;
502 if (loop_tree_node->regno_allocno_map[regno] == NULL)
503 /* Remember that we can create temporary allocnos to break
504 cycles in register shuffle on region borders (see
505 ira-emit.c). */
506 loop_tree_node->regno_allocno_map[regno] = a;
507 }
508 ALLOCNO_CAP (a) = NULL;
509 ALLOCNO_CAP_MEMBER (a) = NULL;
510 ALLOCNO_NUM (a) = ira_allocnos_num;
511 bitmap_set_bit (loop_tree_node->all_allocnos, ALLOCNO_NUM (a));
512 ALLOCNO_NREFS (a) = 0;
513 ALLOCNO_FREQ (a) = 0;
514 ALLOCNO_HARD_REGNO (a) = -1;
515 ALLOCNO_CALL_FREQ (a) = 0;
516 ALLOCNO_CALLS_CROSSED_NUM (a) = 0;
517 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a) = 0;
518 CLEAR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
519 #ifdef STACK_REGS
520 ALLOCNO_NO_STACK_REG_P (a) = false;
521 ALLOCNO_TOTAL_NO_STACK_REG_P (a) = false;
522 #endif
523 ALLOCNO_DONT_REASSIGN_P (a) = false;
524 ALLOCNO_BAD_SPILL_P (a) = false;
525 ALLOCNO_ASSIGNED_P (a) = false;
526 ALLOCNO_MODE (a) = (regno < 0 ? VOIDmode : PSEUDO_REGNO_MODE (regno));
527 ALLOCNO_WMODE (a) = ALLOCNO_MODE (a);
528 ALLOCNO_PREFS (a) = NULL;
529 ALLOCNO_COPIES (a) = NULL;
530 ALLOCNO_HARD_REG_COSTS (a) = NULL;
531 ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
532 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
533 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
534 ALLOCNO_CLASS (a) = NO_REGS;
535 ALLOCNO_UPDATED_CLASS_COST (a) = 0;
536 ALLOCNO_CLASS_COST (a) = 0;
537 ALLOCNO_MEMORY_COST (a) = 0;
538 ALLOCNO_UPDATED_MEMORY_COST (a) = 0;
539 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) = 0;
540 ALLOCNO_NUM_OBJECTS (a) = 0;
541
542 ALLOCNO_ADD_DATA (a) = NULL;
543 allocno_vec.safe_push (a);
544 ira_allocnos = allocno_vec.address ();
545 ira_allocnos_num = allocno_vec.length ();
546
547 return a;
548 }
549
550 /* Set up register class for A and update its conflict hard
551 registers. */
552 void
553 ira_set_allocno_class (ira_allocno_t a, enum reg_class aclass)
554 {
555 ira_allocno_object_iterator oi;
556 ira_object_t obj;
557
558 ALLOCNO_CLASS (a) = aclass;
559 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
560 {
561 IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
562 reg_class_contents[aclass]);
563 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
564 reg_class_contents[aclass]);
565 }
566 }
567
568 /* Determine the number of objects we should associate with allocno A
569 and allocate them. */
570 void
571 ira_create_allocno_objects (ira_allocno_t a)
572 {
573 enum machine_mode mode = ALLOCNO_MODE (a);
574 enum reg_class aclass = ALLOCNO_CLASS (a);
575 int n = ira_reg_class_max_nregs[aclass][mode];
576 int i;
577
578 if (GET_MODE_SIZE (mode) != 2 * UNITS_PER_WORD || n != 2)
579 n = 1;
580
581 ALLOCNO_NUM_OBJECTS (a) = n;
582 for (i = 0; i < n; i++)
583 ALLOCNO_OBJECT (a, i) = ira_create_object (a, i);
584 }
585
586 /* For each allocno, set ALLOCNO_NUM_OBJECTS and create the
587 ALLOCNO_OBJECT structures. This must be called after the allocno
588 classes are known. */
589 static void
590 create_allocno_objects (void)
591 {
592 ira_allocno_t a;
593 ira_allocno_iterator ai;
594
595 FOR_EACH_ALLOCNO (a, ai)
596 ira_create_allocno_objects (a);
597 }
598
599 /* Merge hard register conflict information for all objects associated with
600 allocno TO into the corresponding objects associated with FROM.
601 If TOTAL_ONLY is true, we only merge OBJECT_TOTAL_CONFLICT_HARD_REGS. */
602 static void
603 merge_hard_reg_conflicts (ira_allocno_t from, ira_allocno_t to,
604 bool total_only)
605 {
606 int i;
607 gcc_assert (ALLOCNO_NUM_OBJECTS (to) == ALLOCNO_NUM_OBJECTS (from));
608 for (i = 0; i < ALLOCNO_NUM_OBJECTS (to); i++)
609 {
610 ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
611 ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
612
613 if (!total_only)
614 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (to_obj),
615 OBJECT_CONFLICT_HARD_REGS (from_obj));
616 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (to_obj),
617 OBJECT_TOTAL_CONFLICT_HARD_REGS (from_obj));
618 }
619 #ifdef STACK_REGS
620 if (!total_only && ALLOCNO_NO_STACK_REG_P (from))
621 ALLOCNO_NO_STACK_REG_P (to) = true;
622 if (ALLOCNO_TOTAL_NO_STACK_REG_P (from))
623 ALLOCNO_TOTAL_NO_STACK_REG_P (to) = true;
624 #endif
625 }
626
627 /* Update hard register conflict information for all objects associated with
628 A to include the regs in SET. */
629 void
630 ior_hard_reg_conflicts (ira_allocno_t a, HARD_REG_SET *set)
631 {
632 ira_allocno_object_iterator i;
633 ira_object_t obj;
634
635 FOR_EACH_ALLOCNO_OBJECT (a, obj, i)
636 {
637 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj), *set);
638 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), *set);
639 }
640 }
641
642 /* Return TRUE if a conflict vector with NUM elements is more
643 profitable than a conflict bit vector for OBJ. */
644 bool
645 ira_conflict_vector_profitable_p (ira_object_t obj, int num)
646 {
647 int nw;
648 int max = OBJECT_MAX (obj);
649 int min = OBJECT_MIN (obj);
650
651 if (max < min)
652 /* We prefer a bit vector in such case because it does not result
653 in allocation. */
654 return false;
655
656 nw = (max - min + IRA_INT_BITS) / IRA_INT_BITS;
657 return (2 * sizeof (ira_object_t) * (num + 1)
658 < 3 * nw * sizeof (IRA_INT_TYPE));
659 }
660
661 /* Allocates and initialize the conflict vector of OBJ for NUM
662 conflicting objects. */
663 void
664 ira_allocate_conflict_vec (ira_object_t obj, int num)
665 {
666 int size;
667 ira_object_t *vec;
668
669 ira_assert (OBJECT_CONFLICT_ARRAY (obj) == NULL);
670 num++; /* for NULL end marker */
671 size = sizeof (ira_object_t) * num;
672 OBJECT_CONFLICT_ARRAY (obj) = ira_allocate (size);
673 vec = (ira_object_t *) OBJECT_CONFLICT_ARRAY (obj);
674 vec[0] = NULL;
675 OBJECT_NUM_CONFLICTS (obj) = 0;
676 OBJECT_CONFLICT_ARRAY_SIZE (obj) = size;
677 OBJECT_CONFLICT_VEC_P (obj) = true;
678 }
679
680 /* Allocate and initialize the conflict bit vector of OBJ. */
681 static void
682 allocate_conflict_bit_vec (ira_object_t obj)
683 {
684 unsigned int size;
685
686 ira_assert (OBJECT_CONFLICT_ARRAY (obj) == NULL);
687 size = ((OBJECT_MAX (obj) - OBJECT_MIN (obj) + IRA_INT_BITS)
688 / IRA_INT_BITS * sizeof (IRA_INT_TYPE));
689 OBJECT_CONFLICT_ARRAY (obj) = ira_allocate (size);
690 memset (OBJECT_CONFLICT_ARRAY (obj), 0, size);
691 OBJECT_CONFLICT_ARRAY_SIZE (obj) = size;
692 OBJECT_CONFLICT_VEC_P (obj) = false;
693 }
694
695 /* Allocate and initialize the conflict vector or conflict bit vector
696 of OBJ for NUM conflicting allocnos whatever is more profitable. */
697 void
698 ira_allocate_object_conflicts (ira_object_t obj, int num)
699 {
700 if (ira_conflict_vector_profitable_p (obj, num))
701 ira_allocate_conflict_vec (obj, num);
702 else
703 allocate_conflict_bit_vec (obj);
704 }
705
706 /* Add OBJ2 to the conflicts of OBJ1. */
707 static void
708 add_to_conflicts (ira_object_t obj1, ira_object_t obj2)
709 {
710 int num;
711 unsigned int size;
712
713 if (OBJECT_CONFLICT_VEC_P (obj1))
714 {
715 ira_object_t *vec = OBJECT_CONFLICT_VEC (obj1);
716 int curr_num = OBJECT_NUM_CONFLICTS (obj1);
717 num = curr_num + 2;
718 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) < num * sizeof (ira_object_t))
719 {
720 ira_object_t *newvec;
721 size = (3 * num / 2 + 1) * sizeof (ira_allocno_t);
722 newvec = (ira_object_t *) ira_allocate (size);
723 memcpy (newvec, vec, curr_num * sizeof (ira_object_t));
724 ira_free (vec);
725 vec = newvec;
726 OBJECT_CONFLICT_ARRAY (obj1) = vec;
727 OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
728 }
729 vec[num - 2] = obj2;
730 vec[num - 1] = NULL;
731 OBJECT_NUM_CONFLICTS (obj1)++;
732 }
733 else
734 {
735 int nw, added_head_nw, id;
736 IRA_INT_TYPE *vec = OBJECT_CONFLICT_BITVEC (obj1);
737
738 id = OBJECT_CONFLICT_ID (obj2);
739 if (OBJECT_MIN (obj1) > id)
740 {
741 /* Expand head of the bit vector. */
742 added_head_nw = (OBJECT_MIN (obj1) - id - 1) / IRA_INT_BITS + 1;
743 nw = (OBJECT_MAX (obj1) - OBJECT_MIN (obj1)) / IRA_INT_BITS + 1;
744 size = (nw + added_head_nw) * sizeof (IRA_INT_TYPE);
745 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) >= size)
746 {
747 memmove ((char *) vec + added_head_nw * sizeof (IRA_INT_TYPE),
748 vec, nw * sizeof (IRA_INT_TYPE));
749 memset (vec, 0, added_head_nw * sizeof (IRA_INT_TYPE));
750 }
751 else
752 {
753 size
754 = (3 * (nw + added_head_nw) / 2 + 1) * sizeof (IRA_INT_TYPE);
755 vec = (IRA_INT_TYPE *) ira_allocate (size);
756 memcpy ((char *) vec + added_head_nw * sizeof (IRA_INT_TYPE),
757 OBJECT_CONFLICT_ARRAY (obj1), nw * sizeof (IRA_INT_TYPE));
758 memset (vec, 0, added_head_nw * sizeof (IRA_INT_TYPE));
759 memset ((char *) vec
760 + (nw + added_head_nw) * sizeof (IRA_INT_TYPE),
761 0, size - (nw + added_head_nw) * sizeof (IRA_INT_TYPE));
762 ira_free (OBJECT_CONFLICT_ARRAY (obj1));
763 OBJECT_CONFLICT_ARRAY (obj1) = vec;
764 OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
765 }
766 OBJECT_MIN (obj1) -= added_head_nw * IRA_INT_BITS;
767 }
768 else if (OBJECT_MAX (obj1) < id)
769 {
770 nw = (id - OBJECT_MIN (obj1)) / IRA_INT_BITS + 1;
771 size = nw * sizeof (IRA_INT_TYPE);
772 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) < size)
773 {
774 /* Expand tail of the bit vector. */
775 size = (3 * nw / 2 + 1) * sizeof (IRA_INT_TYPE);
776 vec = (IRA_INT_TYPE *) ira_allocate (size);
777 memcpy (vec, OBJECT_CONFLICT_ARRAY (obj1), OBJECT_CONFLICT_ARRAY_SIZE (obj1));
778 memset ((char *) vec + OBJECT_CONFLICT_ARRAY_SIZE (obj1),
779 0, size - OBJECT_CONFLICT_ARRAY_SIZE (obj1));
780 ira_free (OBJECT_CONFLICT_ARRAY (obj1));
781 OBJECT_CONFLICT_ARRAY (obj1) = vec;
782 OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
783 }
784 OBJECT_MAX (obj1) = id;
785 }
786 SET_MINMAX_SET_BIT (vec, id, OBJECT_MIN (obj1), OBJECT_MAX (obj1));
787 }
788 }
789
790 /* Add OBJ1 to the conflicts of OBJ2 and vice versa. */
791 static void
792 ira_add_conflict (ira_object_t obj1, ira_object_t obj2)
793 {
794 add_to_conflicts (obj1, obj2);
795 add_to_conflicts (obj2, obj1);
796 }
797
798 /* Clear all conflicts of OBJ. */
799 static void
800 clear_conflicts (ira_object_t obj)
801 {
802 if (OBJECT_CONFLICT_VEC_P (obj))
803 {
804 OBJECT_NUM_CONFLICTS (obj) = 0;
805 OBJECT_CONFLICT_VEC (obj)[0] = NULL;
806 }
807 else if (OBJECT_CONFLICT_ARRAY_SIZE (obj) != 0)
808 {
809 int nw;
810
811 nw = (OBJECT_MAX (obj) - OBJECT_MIN (obj)) / IRA_INT_BITS + 1;
812 memset (OBJECT_CONFLICT_BITVEC (obj), 0, nw * sizeof (IRA_INT_TYPE));
813 }
814 }
815
816 /* The array used to find duplications in conflict vectors of
817 allocnos. */
818 static int *conflict_check;
819
820 /* The value used to mark allocation presence in conflict vector of
821 the current allocno. */
822 static int curr_conflict_check_tick;
823
824 /* Remove duplications in conflict vector of OBJ. */
825 static void
826 compress_conflict_vec (ira_object_t obj)
827 {
828 ira_object_t *vec, conflict_obj;
829 int i, j;
830
831 ira_assert (OBJECT_CONFLICT_VEC_P (obj));
832 vec = OBJECT_CONFLICT_VEC (obj);
833 curr_conflict_check_tick++;
834 for (i = j = 0; (conflict_obj = vec[i]) != NULL; i++)
835 {
836 int id = OBJECT_CONFLICT_ID (conflict_obj);
837 if (conflict_check[id] != curr_conflict_check_tick)
838 {
839 conflict_check[id] = curr_conflict_check_tick;
840 vec[j++] = conflict_obj;
841 }
842 }
843 OBJECT_NUM_CONFLICTS (obj) = j;
844 vec[j] = NULL;
845 }
846
847 /* Remove duplications in conflict vectors of all allocnos. */
848 static void
849 compress_conflict_vecs (void)
850 {
851 ira_object_t obj;
852 ira_object_iterator oi;
853
854 conflict_check = (int *) ira_allocate (sizeof (int) * ira_objects_num);
855 memset (conflict_check, 0, sizeof (int) * ira_objects_num);
856 curr_conflict_check_tick = 0;
857 FOR_EACH_OBJECT (obj, oi)
858 {
859 if (OBJECT_CONFLICT_VEC_P (obj))
860 compress_conflict_vec (obj);
861 }
862 ira_free (conflict_check);
863 }
864
865 /* This recursive function outputs allocno A and if it is a cap the
866 function outputs its members. */
867 void
868 ira_print_expanded_allocno (ira_allocno_t a)
869 {
870 basic_block bb;
871
872 fprintf (ira_dump_file, " a%d(r%d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
873 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
874 fprintf (ira_dump_file, ",b%d", bb->index);
875 else
876 fprintf (ira_dump_file, ",l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
877 if (ALLOCNO_CAP_MEMBER (a) != NULL)
878 {
879 fprintf (ira_dump_file, ":");
880 ira_print_expanded_allocno (ALLOCNO_CAP_MEMBER (a));
881 }
882 fprintf (ira_dump_file, ")");
883 }
884
885 /* Create and return the cap representing allocno A in the
886 parent loop. */
887 static ira_allocno_t
888 create_cap_allocno (ira_allocno_t a)
889 {
890 ira_allocno_t cap;
891 ira_loop_tree_node_t parent;
892 enum reg_class aclass;
893
894 parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
895 cap = ira_create_allocno (ALLOCNO_REGNO (a), true, parent);
896 ALLOCNO_MODE (cap) = ALLOCNO_MODE (a);
897 ALLOCNO_WMODE (cap) = ALLOCNO_WMODE (a);
898 aclass = ALLOCNO_CLASS (a);
899 ira_set_allocno_class (cap, aclass);
900 ira_create_allocno_objects (cap);
901 ALLOCNO_CAP_MEMBER (cap) = a;
902 ALLOCNO_CAP (a) = cap;
903 ALLOCNO_CLASS_COST (cap) = ALLOCNO_CLASS_COST (a);
904 ALLOCNO_MEMORY_COST (cap) = ALLOCNO_MEMORY_COST (a);
905 ira_allocate_and_copy_costs
906 (&ALLOCNO_HARD_REG_COSTS (cap), aclass, ALLOCNO_HARD_REG_COSTS (a));
907 ira_allocate_and_copy_costs
908 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (cap), aclass,
909 ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
910 ALLOCNO_BAD_SPILL_P (cap) = ALLOCNO_BAD_SPILL_P (a);
911 ALLOCNO_NREFS (cap) = ALLOCNO_NREFS (a);
912 ALLOCNO_FREQ (cap) = ALLOCNO_FREQ (a);
913 ALLOCNO_CALL_FREQ (cap) = ALLOCNO_CALL_FREQ (a);
914
915 merge_hard_reg_conflicts (a, cap, false);
916
917 ALLOCNO_CALLS_CROSSED_NUM (cap) = ALLOCNO_CALLS_CROSSED_NUM (a);
918 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (cap) = ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
919 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (cap),
920 ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
921 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
922 {
923 fprintf (ira_dump_file, " Creating cap ");
924 ira_print_expanded_allocno (cap);
925 fprintf (ira_dump_file, "\n");
926 }
927 return cap;
928 }
929
930 /* Create and return a live range for OBJECT with given attributes. */
931 live_range_t
932 ira_create_live_range (ira_object_t obj, int start, int finish,
933 live_range_t next)
934 {
935 live_range_t p;
936
937 p = (live_range_t) pool_alloc (live_range_pool);
938 p->object = obj;
939 p->start = start;
940 p->finish = finish;
941 p->next = next;
942 return p;
943 }
944
945 /* Create a new live range for OBJECT and queue it at the head of its
946 live range list. */
947 void
948 ira_add_live_range_to_object (ira_object_t object, int start, int finish)
949 {
950 live_range_t p;
951 p = ira_create_live_range (object, start, finish,
952 OBJECT_LIVE_RANGES (object));
953 OBJECT_LIVE_RANGES (object) = p;
954 }
955
956 /* Copy allocno live range R and return the result. */
957 static live_range_t
958 copy_live_range (live_range_t r)
959 {
960 live_range_t p;
961
962 p = (live_range_t) pool_alloc (live_range_pool);
963 *p = *r;
964 return p;
965 }
966
967 /* Copy allocno live range list given by its head R and return the
968 result. */
969 live_range_t
970 ira_copy_live_range_list (live_range_t r)
971 {
972 live_range_t p, first, last;
973
974 if (r == NULL)
975 return NULL;
976 for (first = last = NULL; r != NULL; r = r->next)
977 {
978 p = copy_live_range (r);
979 if (first == NULL)
980 first = p;
981 else
982 last->next = p;
983 last = p;
984 }
985 return first;
986 }
987
988 /* Merge ranges R1 and R2 and returns the result. The function
989 maintains the order of ranges and tries to minimize number of the
990 result ranges. */
991 live_range_t
992 ira_merge_live_ranges (live_range_t r1, live_range_t r2)
993 {
994 live_range_t first, last, temp;
995
996 if (r1 == NULL)
997 return r2;
998 if (r2 == NULL)
999 return r1;
1000 for (first = last = NULL; r1 != NULL && r2 != NULL;)
1001 {
1002 if (r1->start < r2->start)
1003 {
1004 temp = r1;
1005 r1 = r2;
1006 r2 = temp;
1007 }
1008 if (r1->start <= r2->finish + 1)
1009 {
1010 /* Intersected ranges: merge r1 and r2 into r1. */
1011 r1->start = r2->start;
1012 if (r1->finish < r2->finish)
1013 r1->finish = r2->finish;
1014 temp = r2;
1015 r2 = r2->next;
1016 ira_finish_live_range (temp);
1017 if (r2 == NULL)
1018 {
1019 /* To try to merge with subsequent ranges in r1. */
1020 r2 = r1->next;
1021 r1->next = NULL;
1022 }
1023 }
1024 else
1025 {
1026 /* Add r1 to the result. */
1027 if (first == NULL)
1028 first = last = r1;
1029 else
1030 {
1031 last->next = r1;
1032 last = r1;
1033 }
1034 r1 = r1->next;
1035 if (r1 == NULL)
1036 {
1037 /* To try to merge with subsequent ranges in r2. */
1038 r1 = r2->next;
1039 r2->next = NULL;
1040 }
1041 }
1042 }
1043 if (r1 != NULL)
1044 {
1045 if (first == NULL)
1046 first = r1;
1047 else
1048 last->next = r1;
1049 ira_assert (r1->next == NULL);
1050 }
1051 else if (r2 != NULL)
1052 {
1053 if (first == NULL)
1054 first = r2;
1055 else
1056 last->next = r2;
1057 ira_assert (r2->next == NULL);
1058 }
1059 else
1060 {
1061 ira_assert (last->next == NULL);
1062 }
1063 return first;
1064 }
1065
1066 /* Return TRUE if live ranges R1 and R2 intersect. */
1067 bool
1068 ira_live_ranges_intersect_p (live_range_t r1, live_range_t r2)
1069 {
1070 /* Remember the live ranges are always kept ordered. */
1071 while (r1 != NULL && r2 != NULL)
1072 {
1073 if (r1->start > r2->finish)
1074 r1 = r1->next;
1075 else if (r2->start > r1->finish)
1076 r2 = r2->next;
1077 else
1078 return true;
1079 }
1080 return false;
1081 }
1082
1083 /* Free allocno live range R. */
1084 void
1085 ira_finish_live_range (live_range_t r)
1086 {
1087 pool_free (live_range_pool, r);
1088 }
1089
1090 /* Free list of allocno live ranges starting with R. */
1091 void
1092 ira_finish_live_range_list (live_range_t r)
1093 {
1094 live_range_t next_r;
1095
1096 for (; r != NULL; r = next_r)
1097 {
1098 next_r = r->next;
1099 ira_finish_live_range (r);
1100 }
1101 }
1102
1103 /* Free updated register costs of allocno A. */
1104 void
1105 ira_free_allocno_updated_costs (ira_allocno_t a)
1106 {
1107 enum reg_class aclass;
1108
1109 aclass = ALLOCNO_CLASS (a);
1110 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
1111 ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass);
1112 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
1113 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
1114 ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1115 aclass);
1116 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
1117 }
1118
1119 /* Free and nullify all cost vectors allocated earlier for allocno
1120 A. */
1121 static void
1122 ira_free_allocno_costs (ira_allocno_t a)
1123 {
1124 enum reg_class aclass = ALLOCNO_CLASS (a);
1125 ira_object_t obj;
1126 ira_allocno_object_iterator oi;
1127
1128 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
1129 {
1130 ira_finish_live_range_list (OBJECT_LIVE_RANGES (obj));
1131 ira_object_id_map[OBJECT_CONFLICT_ID (obj)] = NULL;
1132 if (OBJECT_CONFLICT_ARRAY (obj) != NULL)
1133 ira_free (OBJECT_CONFLICT_ARRAY (obj));
1134 pool_free (object_pool, obj);
1135 }
1136
1137 ira_allocnos[ALLOCNO_NUM (a)] = NULL;
1138 if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
1139 ira_free_cost_vector (ALLOCNO_HARD_REG_COSTS (a), aclass);
1140 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL)
1141 ira_free_cost_vector (ALLOCNO_CONFLICT_HARD_REG_COSTS (a), aclass);
1142 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
1143 ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass);
1144 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
1145 ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1146 aclass);
1147 ALLOCNO_HARD_REG_COSTS (a) = NULL;
1148 ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
1149 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
1150 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
1151 }
1152
1153 /* Free the memory allocated for allocno A. */
1154 static void
1155 finish_allocno (ira_allocno_t a)
1156 {
1157 ira_free_allocno_costs (a);
1158 pool_free (allocno_pool, a);
1159 }
1160
1161 /* Free the memory allocated for all allocnos. */
1162 static void
1163 finish_allocnos (void)
1164 {
1165 ira_allocno_t a;
1166 ira_allocno_iterator ai;
1167
1168 FOR_EACH_ALLOCNO (a, ai)
1169 finish_allocno (a);
1170 ira_free (ira_regno_allocno_map);
1171 ira_object_id_map_vec.release ();
1172 allocno_vec.release ();
1173 free_alloc_pool (allocno_pool);
1174 free_alloc_pool (object_pool);
1175 free_alloc_pool (live_range_pool);
1176 }
1177
1178 \f
1179
1180 /* Pools for allocno preferences. */
1181 static alloc_pool pref_pool;
1182
1183 /* Vec containing references to all created preferences. It is a
1184 container of array ira_prefs. */
1185 static vec<ira_pref_t> pref_vec;
1186
1187 /* The function initializes data concerning allocno prefs. */
1188 static void
1189 initiate_prefs (void)
1190 {
1191 pref_pool
1192 = create_alloc_pool ("prefs", sizeof (struct ira_allocno_pref), 100);
1193 pref_vec.create (get_max_uid ());
1194 ira_prefs = NULL;
1195 ira_prefs_num = 0;
1196 }
1197
1198 /* Return pref for A and HARD_REGNO if any. */
1199 static ira_pref_t
1200 find_allocno_pref (ira_allocno_t a, int hard_regno)
1201 {
1202 ira_pref_t pref;
1203
1204 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
1205 if (pref->allocno == a && pref->hard_regno == hard_regno)
1206 return pref;
1207 return NULL;
1208 }
1209
1210 /* Create and return pref with given attributes A, HARD_REGNO, and FREQ. */
1211 ira_pref_t
1212 ira_create_pref (ira_allocno_t a, int hard_regno, int freq)
1213 {
1214 ira_pref_t pref;
1215
1216 pref = (ira_pref_t) pool_alloc (pref_pool);
1217 pref->num = ira_prefs_num;
1218 pref->allocno = a;
1219 pref->hard_regno = hard_regno;
1220 pref->freq = freq;
1221 pref_vec.safe_push (pref);
1222 ira_prefs = pref_vec.address ();
1223 ira_prefs_num = pref_vec.length ();
1224 return pref;
1225 }
1226
1227 /* Attach a pref PREF to the corresponding allocno. */
1228 static void
1229 add_allocno_pref_to_list (ira_pref_t pref)
1230 {
1231 ira_allocno_t a = pref->allocno;
1232
1233 pref->next_pref = ALLOCNO_PREFS (a);
1234 ALLOCNO_PREFS (a) = pref;
1235 }
1236
1237 /* Create (or update frequency if the pref already exists) the pref of
1238 allocnos A preferring HARD_REGNO with frequency FREQ. */
1239 void
1240 ira_add_allocno_pref (ira_allocno_t a, int hard_regno, int freq)
1241 {
1242 ira_pref_t pref;
1243
1244 if (freq <= 0)
1245 return;
1246 if ((pref = find_allocno_pref (a, hard_regno)) != NULL)
1247 {
1248 pref->freq += freq;
1249 return;
1250 }
1251 pref = ira_create_pref (a, hard_regno, freq);
1252 ira_assert (a != NULL);
1253 add_allocno_pref_to_list (pref);
1254 }
1255
1256 /* Print info about PREF into file F. */
1257 static void
1258 print_pref (FILE *f, ira_pref_t pref)
1259 {
1260 fprintf (f, " pref%d:a%d(r%d)<-hr%d@%d\n", pref->num,
1261 ALLOCNO_NUM (pref->allocno), ALLOCNO_REGNO (pref->allocno),
1262 pref->hard_regno, pref->freq);
1263 }
1264
1265 /* Print info about PREF into stderr. */
1266 void
1267 ira_debug_pref (ira_pref_t pref)
1268 {
1269 print_pref (stderr, pref);
1270 }
1271
1272 /* Print info about all prefs into file F. */
1273 static void
1274 print_prefs (FILE *f)
1275 {
1276 ira_pref_t pref;
1277 ira_pref_iterator pi;
1278
1279 FOR_EACH_PREF (pref, pi)
1280 print_pref (f, pref);
1281 }
1282
1283 /* Print info about all prefs into stderr. */
1284 void
1285 ira_debug_prefs (void)
1286 {
1287 print_prefs (stderr);
1288 }
1289
1290 /* Print info about prefs involving allocno A into file F. */
1291 static void
1292 print_allocno_prefs (FILE *f, ira_allocno_t a)
1293 {
1294 ira_pref_t pref;
1295
1296 fprintf (f, " a%d(r%d):", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
1297 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
1298 fprintf (f, " pref%d:hr%d@%d", pref->num, pref->hard_regno, pref->freq);
1299 fprintf (f, "\n");
1300 }
1301
1302 /* Print info about prefs involving allocno A into stderr. */
1303 void
1304 ira_debug_allocno_prefs (ira_allocno_t a)
1305 {
1306 print_allocno_prefs (stderr, a);
1307 }
1308
1309 /* The function frees memory allocated for PREF. */
1310 static void
1311 finish_pref (ira_pref_t pref)
1312 {
1313 ira_prefs[pref->num] = NULL;
1314 pool_free (pref_pool, pref);
1315 }
1316
1317 /* Remove PREF from the list of allocno prefs and free memory for
1318 it. */
1319 void
1320 ira_remove_pref (ira_pref_t pref)
1321 {
1322 ira_pref_t cpref, prev;
1323
1324 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1325 fprintf (ira_dump_file, " Removing pref%d:hr%d@%d\n",
1326 pref->num, pref->hard_regno, pref->freq);
1327 for (prev = NULL, cpref = ALLOCNO_PREFS (pref->allocno);
1328 cpref != NULL;
1329 prev = cpref, cpref = cpref->next_pref)
1330 if (cpref == pref)
1331 break;
1332 ira_assert (cpref != NULL);
1333 if (prev == NULL)
1334 ALLOCNO_PREFS (pref->allocno) = pref->next_pref;
1335 else
1336 prev->next_pref = pref->next_pref;
1337 finish_pref (pref);
1338 }
1339
1340 /* Remove all prefs of allocno A. */
1341 void
1342 ira_remove_allocno_prefs (ira_allocno_t a)
1343 {
1344 ira_pref_t pref, next_pref;
1345
1346 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = next_pref)
1347 {
1348 next_pref = pref->next_pref;
1349 finish_pref (pref);
1350 }
1351 ALLOCNO_PREFS (a) = NULL;
1352 }
1353
1354 /* Free memory allocated for all prefs. */
1355 static void
1356 finish_prefs (void)
1357 {
1358 ira_pref_t pref;
1359 ira_pref_iterator pi;
1360
1361 FOR_EACH_PREF (pref, pi)
1362 finish_pref (pref);
1363 pref_vec.release ();
1364 free_alloc_pool (pref_pool);
1365 }
1366
1367 \f
1368
1369 /* Pools for copies. */
1370 static alloc_pool copy_pool;
1371
1372 /* Vec containing references to all created copies. It is a
1373 container of array ira_copies. */
1374 static vec<ira_copy_t> copy_vec;
1375
1376 /* The function initializes data concerning allocno copies. */
1377 static void
1378 initiate_copies (void)
1379 {
1380 copy_pool
1381 = create_alloc_pool ("copies", sizeof (struct ira_allocno_copy), 100);
1382 copy_vec.create (get_max_uid ());
1383 ira_copies = NULL;
1384 ira_copies_num = 0;
1385 }
1386
1387 /* Return copy connecting A1 and A2 and originated from INSN of
1388 LOOP_TREE_NODE if any. */
1389 static ira_copy_t
1390 find_allocno_copy (ira_allocno_t a1, ira_allocno_t a2, rtx_insn *insn,
1391 ira_loop_tree_node_t loop_tree_node)
1392 {
1393 ira_copy_t cp, next_cp;
1394 ira_allocno_t another_a;
1395
1396 for (cp = ALLOCNO_COPIES (a1); cp != NULL; cp = next_cp)
1397 {
1398 if (cp->first == a1)
1399 {
1400 next_cp = cp->next_first_allocno_copy;
1401 another_a = cp->second;
1402 }
1403 else if (cp->second == a1)
1404 {
1405 next_cp = cp->next_second_allocno_copy;
1406 another_a = cp->first;
1407 }
1408 else
1409 gcc_unreachable ();
1410 if (another_a == a2 && cp->insn == insn
1411 && cp->loop_tree_node == loop_tree_node)
1412 return cp;
1413 }
1414 return NULL;
1415 }
1416
1417 /* Create and return copy with given attributes LOOP_TREE_NODE, FIRST,
1418 SECOND, FREQ, CONSTRAINT_P, and INSN. */
1419 ira_copy_t
1420 ira_create_copy (ira_allocno_t first, ira_allocno_t second, int freq,
1421 bool constraint_p, rtx_insn *insn,
1422 ira_loop_tree_node_t loop_tree_node)
1423 {
1424 ira_copy_t cp;
1425
1426 cp = (ira_copy_t) pool_alloc (copy_pool);
1427 cp->num = ira_copies_num;
1428 cp->first = first;
1429 cp->second = second;
1430 cp->freq = freq;
1431 cp->constraint_p = constraint_p;
1432 cp->insn = insn;
1433 cp->loop_tree_node = loop_tree_node;
1434 copy_vec.safe_push (cp);
1435 ira_copies = copy_vec.address ();
1436 ira_copies_num = copy_vec.length ();
1437 return cp;
1438 }
1439
1440 /* Attach a copy CP to allocnos involved into the copy. */
1441 static void
1442 add_allocno_copy_to_list (ira_copy_t cp)
1443 {
1444 ira_allocno_t first = cp->first, second = cp->second;
1445
1446 cp->prev_first_allocno_copy = NULL;
1447 cp->prev_second_allocno_copy = NULL;
1448 cp->next_first_allocno_copy = ALLOCNO_COPIES (first);
1449 if (cp->next_first_allocno_copy != NULL)
1450 {
1451 if (cp->next_first_allocno_copy->first == first)
1452 cp->next_first_allocno_copy->prev_first_allocno_copy = cp;
1453 else
1454 cp->next_first_allocno_copy->prev_second_allocno_copy = cp;
1455 }
1456 cp->next_second_allocno_copy = ALLOCNO_COPIES (second);
1457 if (cp->next_second_allocno_copy != NULL)
1458 {
1459 if (cp->next_second_allocno_copy->second == second)
1460 cp->next_second_allocno_copy->prev_second_allocno_copy = cp;
1461 else
1462 cp->next_second_allocno_copy->prev_first_allocno_copy = cp;
1463 }
1464 ALLOCNO_COPIES (first) = cp;
1465 ALLOCNO_COPIES (second) = cp;
1466 }
1467
1468 /* Make a copy CP a canonical copy where number of the
1469 first allocno is less than the second one. */
1470 static void
1471 swap_allocno_copy_ends_if_necessary (ira_copy_t cp)
1472 {
1473 ira_allocno_t temp;
1474 ira_copy_t temp_cp;
1475
1476 if (ALLOCNO_NUM (cp->first) <= ALLOCNO_NUM (cp->second))
1477 return;
1478
1479 temp = cp->first;
1480 cp->first = cp->second;
1481 cp->second = temp;
1482
1483 temp_cp = cp->prev_first_allocno_copy;
1484 cp->prev_first_allocno_copy = cp->prev_second_allocno_copy;
1485 cp->prev_second_allocno_copy = temp_cp;
1486
1487 temp_cp = cp->next_first_allocno_copy;
1488 cp->next_first_allocno_copy = cp->next_second_allocno_copy;
1489 cp->next_second_allocno_copy = temp_cp;
1490 }
1491
1492 /* Create (or update frequency if the copy already exists) and return
1493 the copy of allocnos FIRST and SECOND with frequency FREQ
1494 corresponding to move insn INSN (if any) and originated from
1495 LOOP_TREE_NODE. */
1496 ira_copy_t
1497 ira_add_allocno_copy (ira_allocno_t first, ira_allocno_t second, int freq,
1498 bool constraint_p, rtx_insn *insn,
1499 ira_loop_tree_node_t loop_tree_node)
1500 {
1501 ira_copy_t cp;
1502
1503 if ((cp = find_allocno_copy (first, second, insn, loop_tree_node)) != NULL)
1504 {
1505 cp->freq += freq;
1506 return cp;
1507 }
1508 cp = ira_create_copy (first, second, freq, constraint_p, insn,
1509 loop_tree_node);
1510 ira_assert (first != NULL && second != NULL);
1511 add_allocno_copy_to_list (cp);
1512 swap_allocno_copy_ends_if_necessary (cp);
1513 return cp;
1514 }
1515
1516 /* Print info about copy CP into file F. */
1517 static void
1518 print_copy (FILE *f, ira_copy_t cp)
1519 {
1520 fprintf (f, " cp%d:a%d(r%d)<->a%d(r%d)@%d:%s\n", cp->num,
1521 ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
1522 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second), cp->freq,
1523 cp->insn != NULL
1524 ? "move" : cp->constraint_p ? "constraint" : "shuffle");
1525 }
1526
1527 DEBUG_FUNCTION void
1528 debug (ira_allocno_copy &ref)
1529 {
1530 print_copy (stderr, &ref);
1531 }
1532
1533 DEBUG_FUNCTION void
1534 debug (ira_allocno_copy *ptr)
1535 {
1536 if (ptr)
1537 debug (*ptr);
1538 else
1539 fprintf (stderr, "<nil>\n");
1540 }
1541
1542 /* Print info about copy CP into stderr. */
1543 void
1544 ira_debug_copy (ira_copy_t cp)
1545 {
1546 print_copy (stderr, cp);
1547 }
1548
1549 /* Print info about all copies into file F. */
1550 static void
1551 print_copies (FILE *f)
1552 {
1553 ira_copy_t cp;
1554 ira_copy_iterator ci;
1555
1556 FOR_EACH_COPY (cp, ci)
1557 print_copy (f, cp);
1558 }
1559
1560 /* Print info about all copies into stderr. */
1561 void
1562 ira_debug_copies (void)
1563 {
1564 print_copies (stderr);
1565 }
1566
1567 /* Print info about copies involving allocno A into file F. */
1568 static void
1569 print_allocno_copies (FILE *f, ira_allocno_t a)
1570 {
1571 ira_allocno_t another_a;
1572 ira_copy_t cp, next_cp;
1573
1574 fprintf (f, " a%d(r%d):", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
1575 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1576 {
1577 if (cp->first == a)
1578 {
1579 next_cp = cp->next_first_allocno_copy;
1580 another_a = cp->second;
1581 }
1582 else if (cp->second == a)
1583 {
1584 next_cp = cp->next_second_allocno_copy;
1585 another_a = cp->first;
1586 }
1587 else
1588 gcc_unreachable ();
1589 fprintf (f, " cp%d:a%d(r%d)@%d", cp->num,
1590 ALLOCNO_NUM (another_a), ALLOCNO_REGNO (another_a), cp->freq);
1591 }
1592 fprintf (f, "\n");
1593 }
1594
1595 DEBUG_FUNCTION void
1596 debug (ira_allocno &ref)
1597 {
1598 print_allocno_copies (stderr, &ref);
1599 }
1600
1601 DEBUG_FUNCTION void
1602 debug (ira_allocno *ptr)
1603 {
1604 if (ptr)
1605 debug (*ptr);
1606 else
1607 fprintf (stderr, "<nil>\n");
1608 }
1609
1610
1611 /* Print info about copies involving allocno A into stderr. */
1612 void
1613 ira_debug_allocno_copies (ira_allocno_t a)
1614 {
1615 print_allocno_copies (stderr, a);
1616 }
1617
1618 /* The function frees memory allocated for copy CP. */
1619 static void
1620 finish_copy (ira_copy_t cp)
1621 {
1622 pool_free (copy_pool, cp);
1623 }
1624
1625
1626 /* Free memory allocated for all copies. */
1627 static void
1628 finish_copies (void)
1629 {
1630 ira_copy_t cp;
1631 ira_copy_iterator ci;
1632
1633 FOR_EACH_COPY (cp, ci)
1634 finish_copy (cp);
1635 copy_vec.release ();
1636 free_alloc_pool (copy_pool);
1637 }
1638
1639 \f
1640
1641 /* Pools for cost vectors. It is defined only for allocno classes. */
1642 static alloc_pool cost_vector_pool[N_REG_CLASSES];
1643
1644 /* The function initiates work with hard register cost vectors. It
1645 creates allocation pool for each allocno class. */
1646 static void
1647 initiate_cost_vectors (void)
1648 {
1649 int i;
1650 enum reg_class aclass;
1651
1652 for (i = 0; i < ira_allocno_classes_num; i++)
1653 {
1654 aclass = ira_allocno_classes[i];
1655 cost_vector_pool[aclass]
1656 = create_alloc_pool ("cost vectors",
1657 sizeof (int) * ira_class_hard_regs_num[aclass],
1658 100);
1659 }
1660 }
1661
1662 /* Allocate and return a cost vector VEC for ACLASS. */
1663 int *
1664 ira_allocate_cost_vector (reg_class_t aclass)
1665 {
1666 return (int *) pool_alloc (cost_vector_pool[(int) aclass]);
1667 }
1668
1669 /* Free a cost vector VEC for ACLASS. */
1670 void
1671 ira_free_cost_vector (int *vec, reg_class_t aclass)
1672 {
1673 ira_assert (vec != NULL);
1674 pool_free (cost_vector_pool[(int) aclass], vec);
1675 }
1676
1677 /* Finish work with hard register cost vectors. Release allocation
1678 pool for each allocno class. */
1679 static void
1680 finish_cost_vectors (void)
1681 {
1682 int i;
1683 enum reg_class aclass;
1684
1685 for (i = 0; i < ira_allocno_classes_num; i++)
1686 {
1687 aclass = ira_allocno_classes[i];
1688 free_alloc_pool (cost_vector_pool[aclass]);
1689 }
1690 }
1691
1692 \f
1693
1694 /* Compute a post-ordering of the reverse control flow of the loop body
1695 designated by the children nodes of LOOP_NODE, whose body nodes in
1696 pre-order are input as LOOP_PREORDER. Return a VEC with a post-order
1697 of the reverse loop body.
1698
1699 For the post-order of the reverse CFG, we visit the basic blocks in
1700 LOOP_PREORDER array in the reverse order of where they appear.
1701 This is important: We do not just want to compute a post-order of
1702 the reverse CFG, we want to make a best-guess for a visiting order that
1703 minimizes the number of chain elements per allocno live range. If the
1704 blocks would be visited in a different order, we would still compute a
1705 correct post-ordering but it would be less likely that two nodes
1706 connected by an edge in the CFG are neighbours in the topsort. */
1707
1708 static vec<ira_loop_tree_node_t>
1709 ira_loop_tree_body_rev_postorder (ira_loop_tree_node_t loop_node ATTRIBUTE_UNUSED,
1710 vec<ira_loop_tree_node_t> loop_preorder)
1711 {
1712 vec<ira_loop_tree_node_t> topsort_nodes = vNULL;
1713 unsigned int n_loop_preorder;
1714
1715 n_loop_preorder = loop_preorder.length ();
1716 if (n_loop_preorder != 0)
1717 {
1718 ira_loop_tree_node_t subloop_node;
1719 unsigned int i;
1720 auto_vec<ira_loop_tree_node_t> dfs_stack;
1721
1722 /* This is a bit of strange abuse of the BB_VISITED flag: We use
1723 the flag to mark blocks we still have to visit to add them to
1724 our post-order. Define an alias to avoid confusion. */
1725 #define BB_TO_VISIT BB_VISITED
1726
1727 FOR_EACH_VEC_ELT (loop_preorder, i, subloop_node)
1728 {
1729 gcc_checking_assert (! (subloop_node->bb->flags & BB_TO_VISIT));
1730 subloop_node->bb->flags |= BB_TO_VISIT;
1731 }
1732
1733 topsort_nodes.create (n_loop_preorder);
1734 dfs_stack.create (n_loop_preorder);
1735
1736 FOR_EACH_VEC_ELT_REVERSE (loop_preorder, i, subloop_node)
1737 {
1738 if (! (subloop_node->bb->flags & BB_TO_VISIT))
1739 continue;
1740
1741 subloop_node->bb->flags &= ~BB_TO_VISIT;
1742 dfs_stack.quick_push (subloop_node);
1743 while (! dfs_stack.is_empty ())
1744 {
1745 edge e;
1746 edge_iterator ei;
1747
1748 ira_loop_tree_node_t n = dfs_stack.last ();
1749 FOR_EACH_EDGE (e, ei, n->bb->preds)
1750 {
1751 ira_loop_tree_node_t pred_node;
1752 basic_block pred_bb = e->src;
1753
1754 if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1755 continue;
1756
1757 pred_node = IRA_BB_NODE_BY_INDEX (pred_bb->index);
1758 if (pred_node != n
1759 && (pred_node->bb->flags & BB_TO_VISIT))
1760 {
1761 pred_node->bb->flags &= ~BB_TO_VISIT;
1762 dfs_stack.quick_push (pred_node);
1763 }
1764 }
1765 if (n == dfs_stack.last ())
1766 {
1767 dfs_stack.pop ();
1768 topsort_nodes.quick_push (n);
1769 }
1770 }
1771 }
1772
1773 #undef BB_TO_VISIT
1774 }
1775
1776 gcc_assert (topsort_nodes.length () == n_loop_preorder);
1777 return topsort_nodes;
1778 }
1779
1780 /* The current loop tree node and its regno allocno map. */
1781 ira_loop_tree_node_t ira_curr_loop_tree_node;
1782 ira_allocno_t *ira_curr_regno_allocno_map;
1783
1784 /* This recursive function traverses loop tree with root LOOP_NODE
1785 calling non-null functions PREORDER_FUNC and POSTORDER_FUNC
1786 correspondingly in preorder and postorder. The function sets up
1787 IRA_CURR_LOOP_TREE_NODE and IRA_CURR_REGNO_ALLOCNO_MAP. If BB_P,
1788 basic block nodes of LOOP_NODE is also processed (before its
1789 subloop nodes).
1790
1791 If BB_P is set and POSTORDER_FUNC is given, the basic blocks in
1792 the loop are passed in the *reverse* post-order of the *reverse*
1793 CFG. This is only used by ira_create_allocno_live_ranges, which
1794 wants to visit basic blocks in this order to minimize the number
1795 of elements per live range chain.
1796 Note that the loop tree nodes are still visited in the normal,
1797 forward post-order of the loop tree. */
1798
1799 void
1800 ira_traverse_loop_tree (bool bb_p, ira_loop_tree_node_t loop_node,
1801 void (*preorder_func) (ira_loop_tree_node_t),
1802 void (*postorder_func) (ira_loop_tree_node_t))
1803 {
1804 ira_loop_tree_node_t subloop_node;
1805
1806 ira_assert (loop_node->bb == NULL);
1807 ira_curr_loop_tree_node = loop_node;
1808 ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
1809
1810 if (preorder_func != NULL)
1811 (*preorder_func) (loop_node);
1812
1813 if (bb_p)
1814 {
1815 auto_vec<ira_loop_tree_node_t> loop_preorder;
1816 unsigned int i;
1817
1818 /* Add all nodes to the set of nodes to visit. The IRA loop tree
1819 is set up such that nodes in the loop body appear in a pre-order
1820 of their place in the CFG. */
1821 for (subloop_node = loop_node->children;
1822 subloop_node != NULL;
1823 subloop_node = subloop_node->next)
1824 if (subloop_node->bb != NULL)
1825 loop_preorder.safe_push (subloop_node);
1826
1827 if (preorder_func != NULL)
1828 FOR_EACH_VEC_ELT (loop_preorder, i, subloop_node)
1829 (*preorder_func) (subloop_node);
1830
1831 if (postorder_func != NULL)
1832 {
1833 vec<ira_loop_tree_node_t> loop_rev_postorder =
1834 ira_loop_tree_body_rev_postorder (loop_node, loop_preorder);
1835 FOR_EACH_VEC_ELT_REVERSE (loop_rev_postorder, i, subloop_node)
1836 (*postorder_func) (subloop_node);
1837 loop_rev_postorder.release ();
1838 }
1839 }
1840
1841 for (subloop_node = loop_node->subloops;
1842 subloop_node != NULL;
1843 subloop_node = subloop_node->subloop_next)
1844 {
1845 ira_assert (subloop_node->bb == NULL);
1846 ira_traverse_loop_tree (bb_p, subloop_node,
1847 preorder_func, postorder_func);
1848 }
1849
1850 ira_curr_loop_tree_node = loop_node;
1851 ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
1852
1853 if (postorder_func != NULL)
1854 (*postorder_func) (loop_node);
1855 }
1856
1857 \f
1858
1859 /* The basic block currently being processed. */
1860 static basic_block curr_bb;
1861
1862 /* This recursive function creates allocnos corresponding to
1863 pseudo-registers containing in X. True OUTPUT_P means that X is
1864 an lvalue. PARENT corresponds to the parent expression of X. */
1865 static void
1866 create_insn_allocnos (rtx x, rtx outer, bool output_p)
1867 {
1868 int i, j;
1869 const char *fmt;
1870 enum rtx_code code = GET_CODE (x);
1871
1872 if (code == REG)
1873 {
1874 int regno;
1875
1876 if ((regno = REGNO (x)) >= FIRST_PSEUDO_REGISTER)
1877 {
1878 ira_allocno_t a;
1879
1880 if ((a = ira_curr_regno_allocno_map[regno]) == NULL)
1881 {
1882 a = ira_create_allocno (regno, false, ira_curr_loop_tree_node);
1883 if (outer != NULL && GET_CODE (outer) == SUBREG)
1884 {
1885 enum machine_mode wmode = GET_MODE (outer);
1886 if (GET_MODE_SIZE (wmode) > GET_MODE_SIZE (ALLOCNO_WMODE (a)))
1887 ALLOCNO_WMODE (a) = wmode;
1888 }
1889 }
1890
1891 ALLOCNO_NREFS (a)++;
1892 ALLOCNO_FREQ (a) += REG_FREQ_FROM_BB (curr_bb);
1893 if (output_p)
1894 bitmap_set_bit (ira_curr_loop_tree_node->modified_regnos, regno);
1895 }
1896 return;
1897 }
1898 else if (code == SET)
1899 {
1900 create_insn_allocnos (SET_DEST (x), NULL, true);
1901 create_insn_allocnos (SET_SRC (x), NULL, false);
1902 return;
1903 }
1904 else if (code == CLOBBER)
1905 {
1906 create_insn_allocnos (XEXP (x, 0), NULL, true);
1907 return;
1908 }
1909 else if (code == MEM)
1910 {
1911 create_insn_allocnos (XEXP (x, 0), NULL, false);
1912 return;
1913 }
1914 else if (code == PRE_DEC || code == POST_DEC || code == PRE_INC ||
1915 code == POST_INC || code == POST_MODIFY || code == PRE_MODIFY)
1916 {
1917 create_insn_allocnos (XEXP (x, 0), NULL, true);
1918 create_insn_allocnos (XEXP (x, 0), NULL, false);
1919 return;
1920 }
1921
1922 fmt = GET_RTX_FORMAT (code);
1923 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1924 {
1925 if (fmt[i] == 'e')
1926 create_insn_allocnos (XEXP (x, i), x, output_p);
1927 else if (fmt[i] == 'E')
1928 for (j = 0; j < XVECLEN (x, i); j++)
1929 create_insn_allocnos (XVECEXP (x, i, j), x, output_p);
1930 }
1931 }
1932
1933 /* Create allocnos corresponding to pseudo-registers living in the
1934 basic block represented by the corresponding loop tree node
1935 BB_NODE. */
1936 static void
1937 create_bb_allocnos (ira_loop_tree_node_t bb_node)
1938 {
1939 basic_block bb;
1940 rtx_insn *insn;
1941 unsigned int i;
1942 bitmap_iterator bi;
1943
1944 curr_bb = bb = bb_node->bb;
1945 ira_assert (bb != NULL);
1946 FOR_BB_INSNS_REVERSE (bb, insn)
1947 if (NONDEBUG_INSN_P (insn))
1948 create_insn_allocnos (PATTERN (insn), NULL, false);
1949 /* It might be a allocno living through from one subloop to
1950 another. */
1951 EXECUTE_IF_SET_IN_REG_SET (df_get_live_in (bb), FIRST_PSEUDO_REGISTER, i, bi)
1952 if (ira_curr_regno_allocno_map[i] == NULL)
1953 ira_create_allocno (i, false, ira_curr_loop_tree_node);
1954 }
1955
1956 /* Create allocnos corresponding to pseudo-registers living on edge E
1957 (a loop entry or exit). Also mark the allocnos as living on the
1958 loop border. */
1959 static void
1960 create_loop_allocnos (edge e)
1961 {
1962 unsigned int i;
1963 bitmap live_in_regs, border_allocnos;
1964 bitmap_iterator bi;
1965 ira_loop_tree_node_t parent;
1966
1967 live_in_regs = df_get_live_in (e->dest);
1968 border_allocnos = ira_curr_loop_tree_node->border_allocnos;
1969 EXECUTE_IF_SET_IN_REG_SET (df_get_live_out (e->src),
1970 FIRST_PSEUDO_REGISTER, i, bi)
1971 if (bitmap_bit_p (live_in_regs, i))
1972 {
1973 if (ira_curr_regno_allocno_map[i] == NULL)
1974 {
1975 /* The order of creations is important for right
1976 ira_regno_allocno_map. */
1977 if ((parent = ira_curr_loop_tree_node->parent) != NULL
1978 && parent->regno_allocno_map[i] == NULL)
1979 ira_create_allocno (i, false, parent);
1980 ira_create_allocno (i, false, ira_curr_loop_tree_node);
1981 }
1982 bitmap_set_bit (border_allocnos,
1983 ALLOCNO_NUM (ira_curr_regno_allocno_map[i]));
1984 }
1985 }
1986
1987 /* Create allocnos corresponding to pseudo-registers living in loop
1988 represented by the corresponding loop tree node LOOP_NODE. This
1989 function is called by ira_traverse_loop_tree. */
1990 static void
1991 create_loop_tree_node_allocnos (ira_loop_tree_node_t loop_node)
1992 {
1993 if (loop_node->bb != NULL)
1994 create_bb_allocnos (loop_node);
1995 else if (loop_node != ira_loop_tree_root)
1996 {
1997 int i;
1998 edge_iterator ei;
1999 edge e;
2000 vec<edge> edges;
2001
2002 ira_assert (current_loops != NULL);
2003 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
2004 if (e->src != loop_node->loop->latch)
2005 create_loop_allocnos (e);
2006
2007 edges = get_loop_exit_edges (loop_node->loop);
2008 FOR_EACH_VEC_ELT (edges, i, e)
2009 create_loop_allocnos (e);
2010 edges.release ();
2011 }
2012 }
2013
2014 /* Propagate information about allocnos modified inside the loop given
2015 by its LOOP_TREE_NODE to its parent. */
2016 static void
2017 propagate_modified_regnos (ira_loop_tree_node_t loop_tree_node)
2018 {
2019 if (loop_tree_node == ira_loop_tree_root)
2020 return;
2021 ira_assert (loop_tree_node->bb == NULL);
2022 bitmap_ior_into (loop_tree_node->parent->modified_regnos,
2023 loop_tree_node->modified_regnos);
2024 }
2025
2026 /* Propagate new info about allocno A (see comments about accumulated
2027 info in allocno definition) to the corresponding allocno on upper
2028 loop tree level. So allocnos on upper levels accumulate
2029 information about the corresponding allocnos in nested regions.
2030 The new info means allocno info finally calculated in this
2031 file. */
2032 static void
2033 propagate_allocno_info (void)
2034 {
2035 int i;
2036 ira_allocno_t a, parent_a;
2037 ira_loop_tree_node_t parent;
2038 enum reg_class aclass;
2039
2040 if (flag_ira_region != IRA_REGION_ALL
2041 && flag_ira_region != IRA_REGION_MIXED)
2042 return;
2043 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
2044 for (a = ira_regno_allocno_map[i];
2045 a != NULL;
2046 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2047 if ((parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) != NULL
2048 && (parent_a = parent->regno_allocno_map[i]) != NULL
2049 /* There are no caps yet at this point. So use
2050 border_allocnos to find allocnos for the propagation. */
2051 && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE (a)->border_allocnos,
2052 ALLOCNO_NUM (a)))
2053 {
2054 if (! ALLOCNO_BAD_SPILL_P (a))
2055 ALLOCNO_BAD_SPILL_P (parent_a) = false;
2056 ALLOCNO_NREFS (parent_a) += ALLOCNO_NREFS (a);
2057 ALLOCNO_FREQ (parent_a) += ALLOCNO_FREQ (a);
2058 ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
2059 merge_hard_reg_conflicts (a, parent_a, true);
2060 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
2061 += ALLOCNO_CALLS_CROSSED_NUM (a);
2062 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
2063 += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
2064 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (parent_a),
2065 ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
2066 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
2067 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2068 aclass = ALLOCNO_CLASS (a);
2069 ira_assert (aclass == ALLOCNO_CLASS (parent_a));
2070 ira_allocate_and_accumulate_costs
2071 (&ALLOCNO_HARD_REG_COSTS (parent_a), aclass,
2072 ALLOCNO_HARD_REG_COSTS (a));
2073 ira_allocate_and_accumulate_costs
2074 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a),
2075 aclass,
2076 ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
2077 ALLOCNO_CLASS_COST (parent_a)
2078 += ALLOCNO_CLASS_COST (a);
2079 ALLOCNO_MEMORY_COST (parent_a) += ALLOCNO_MEMORY_COST (a);
2080 }
2081 }
2082
2083 /* Create allocnos corresponding to pseudo-registers in the current
2084 function. Traverse the loop tree for this. */
2085 static void
2086 create_allocnos (void)
2087 {
2088 /* We need to process BB first to correctly link allocnos by member
2089 next_regno_allocno. */
2090 ira_traverse_loop_tree (true, ira_loop_tree_root,
2091 create_loop_tree_node_allocnos, NULL);
2092 if (optimize)
2093 ira_traverse_loop_tree (false, ira_loop_tree_root, NULL,
2094 propagate_modified_regnos);
2095 }
2096
2097 \f
2098
2099 /* The page contains function to remove some regions from a separate
2100 register allocation. We remove regions whose separate allocation
2101 will hardly improve the result. As a result we speed up regional
2102 register allocation. */
2103
2104 /* The function changes the object in range list given by R to OBJ. */
2105 static void
2106 change_object_in_range_list (live_range_t r, ira_object_t obj)
2107 {
2108 for (; r != NULL; r = r->next)
2109 r->object = obj;
2110 }
2111
2112 /* Move all live ranges associated with allocno FROM to allocno TO. */
2113 static void
2114 move_allocno_live_ranges (ira_allocno_t from, ira_allocno_t to)
2115 {
2116 int i;
2117 int n = ALLOCNO_NUM_OBJECTS (from);
2118
2119 gcc_assert (n == ALLOCNO_NUM_OBJECTS (to));
2120
2121 for (i = 0; i < n; i++)
2122 {
2123 ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
2124 ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
2125 live_range_t lr = OBJECT_LIVE_RANGES (from_obj);
2126
2127 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2128 {
2129 fprintf (ira_dump_file,
2130 " Moving ranges of a%dr%d to a%dr%d: ",
2131 ALLOCNO_NUM (from), ALLOCNO_REGNO (from),
2132 ALLOCNO_NUM (to), ALLOCNO_REGNO (to));
2133 ira_print_live_range_list (ira_dump_file, lr);
2134 }
2135 change_object_in_range_list (lr, to_obj);
2136 OBJECT_LIVE_RANGES (to_obj)
2137 = ira_merge_live_ranges (lr, OBJECT_LIVE_RANGES (to_obj));
2138 OBJECT_LIVE_RANGES (from_obj) = NULL;
2139 }
2140 }
2141
2142 static void
2143 copy_allocno_live_ranges (ira_allocno_t from, ira_allocno_t to)
2144 {
2145 int i;
2146 int n = ALLOCNO_NUM_OBJECTS (from);
2147
2148 gcc_assert (n == ALLOCNO_NUM_OBJECTS (to));
2149
2150 for (i = 0; i < n; i++)
2151 {
2152 ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
2153 ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
2154 live_range_t lr = OBJECT_LIVE_RANGES (from_obj);
2155
2156 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2157 {
2158 fprintf (ira_dump_file, " Copying ranges of a%dr%d to a%dr%d: ",
2159 ALLOCNO_NUM (from), ALLOCNO_REGNO (from),
2160 ALLOCNO_NUM (to), ALLOCNO_REGNO (to));
2161 ira_print_live_range_list (ira_dump_file, lr);
2162 }
2163 lr = ira_copy_live_range_list (lr);
2164 change_object_in_range_list (lr, to_obj);
2165 OBJECT_LIVE_RANGES (to_obj)
2166 = ira_merge_live_ranges (lr, OBJECT_LIVE_RANGES (to_obj));
2167 }
2168 }
2169
2170 /* Return TRUE if NODE represents a loop with low register
2171 pressure. */
2172 static bool
2173 low_pressure_loop_node_p (ira_loop_tree_node_t node)
2174 {
2175 int i;
2176 enum reg_class pclass;
2177
2178 if (node->bb != NULL)
2179 return false;
2180
2181 for (i = 0; i < ira_pressure_classes_num; i++)
2182 {
2183 pclass = ira_pressure_classes[i];
2184 if (node->reg_pressure[pclass] > ira_class_hard_regs_num[pclass]
2185 && ira_class_hard_regs_num[pclass] > 1)
2186 return false;
2187 }
2188 return true;
2189 }
2190
2191 #ifdef STACK_REGS
2192 /* Return TRUE if LOOP has a complex enter or exit edge. We don't
2193 form a region from such loop if the target use stack register
2194 because reg-stack.c can not deal with such edges. */
2195 static bool
2196 loop_with_complex_edge_p (struct loop *loop)
2197 {
2198 int i;
2199 edge_iterator ei;
2200 edge e;
2201 vec<edge> edges;
2202 bool res;
2203
2204 FOR_EACH_EDGE (e, ei, loop->header->preds)
2205 if (e->flags & EDGE_EH)
2206 return true;
2207 edges = get_loop_exit_edges (loop);
2208 res = false;
2209 FOR_EACH_VEC_ELT (edges, i, e)
2210 if (e->flags & EDGE_COMPLEX)
2211 {
2212 res = true;
2213 break;
2214 }
2215 edges.release ();
2216 return res;
2217 }
2218 #endif
2219
2220 /* Sort loops for marking them for removal. We put already marked
2221 loops first, then less frequent loops next, and then outer loops
2222 next. */
2223 static int
2224 loop_compare_func (const void *v1p, const void *v2p)
2225 {
2226 int diff;
2227 ira_loop_tree_node_t l1 = *(const ira_loop_tree_node_t *) v1p;
2228 ira_loop_tree_node_t l2 = *(const ira_loop_tree_node_t *) v2p;
2229
2230 ira_assert (l1->parent != NULL && l2->parent != NULL);
2231 if (l1->to_remove_p && ! l2->to_remove_p)
2232 return -1;
2233 if (! l1->to_remove_p && l2->to_remove_p)
2234 return 1;
2235 if ((diff = l1->loop->header->frequency - l2->loop->header->frequency) != 0)
2236 return diff;
2237 if ((diff = (int) loop_depth (l1->loop) - (int) loop_depth (l2->loop)) != 0)
2238 return diff;
2239 /* Make sorting stable. */
2240 return l1->loop_num - l2->loop_num;
2241 }
2242
2243 /* Mark loops which should be removed from regional allocation. We
2244 remove a loop with low register pressure inside another loop with
2245 register pressure. In this case a separate allocation of the loop
2246 hardly helps (for irregular register file architecture it could
2247 help by choosing a better hard register in the loop but we prefer
2248 faster allocation even in this case). We also remove cheap loops
2249 if there are more than IRA_MAX_LOOPS_NUM of them. Loop with EH
2250 exit or enter edges are removed too because the allocation might
2251 require put pseudo moves on the EH edges (we could still do this
2252 for pseudos with caller saved hard registers in some cases but it
2253 is impossible to say here or during top-down allocation pass what
2254 hard register the pseudos get finally). */
2255 static void
2256 mark_loops_for_removal (void)
2257 {
2258 int i, n;
2259 ira_loop_tree_node_t *sorted_loops;
2260 loop_p loop;
2261
2262 ira_assert (current_loops != NULL);
2263 sorted_loops
2264 = (ira_loop_tree_node_t *) ira_allocate (sizeof (ira_loop_tree_node_t)
2265 * number_of_loops (cfun));
2266 for (n = i = 0; vec_safe_iterate (get_loops (cfun), i, &loop); i++)
2267 if (ira_loop_nodes[i].regno_allocno_map != NULL)
2268 {
2269 if (ira_loop_nodes[i].parent == NULL)
2270 {
2271 /* Don't remove the root. */
2272 ira_loop_nodes[i].to_remove_p = false;
2273 continue;
2274 }
2275 sorted_loops[n++] = &ira_loop_nodes[i];
2276 ira_loop_nodes[i].to_remove_p
2277 = ((low_pressure_loop_node_p (ira_loop_nodes[i].parent)
2278 && low_pressure_loop_node_p (&ira_loop_nodes[i]))
2279 #ifdef STACK_REGS
2280 || loop_with_complex_edge_p (ira_loop_nodes[i].loop)
2281 #endif
2282 );
2283 }
2284 qsort (sorted_loops, n, sizeof (ira_loop_tree_node_t), loop_compare_func);
2285 for (i = 0; n - i + 1 > IRA_MAX_LOOPS_NUM; i++)
2286 {
2287 sorted_loops[i]->to_remove_p = true;
2288 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2289 fprintf
2290 (ira_dump_file,
2291 " Mark loop %d (header %d, freq %d, depth %d) for removal (%s)\n",
2292 sorted_loops[i]->loop_num, sorted_loops[i]->loop->header->index,
2293 sorted_loops[i]->loop->header->frequency,
2294 loop_depth (sorted_loops[i]->loop),
2295 low_pressure_loop_node_p (sorted_loops[i]->parent)
2296 && low_pressure_loop_node_p (sorted_loops[i])
2297 ? "low pressure" : "cheap loop");
2298 }
2299 ira_free (sorted_loops);
2300 }
2301
2302 /* Mark all loops but root for removing. */
2303 static void
2304 mark_all_loops_for_removal (void)
2305 {
2306 int i;
2307 loop_p loop;
2308
2309 ira_assert (current_loops != NULL);
2310 FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
2311 if (ira_loop_nodes[i].regno_allocno_map != NULL)
2312 {
2313 if (ira_loop_nodes[i].parent == NULL)
2314 {
2315 /* Don't remove the root. */
2316 ira_loop_nodes[i].to_remove_p = false;
2317 continue;
2318 }
2319 ira_loop_nodes[i].to_remove_p = true;
2320 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2321 fprintf
2322 (ira_dump_file,
2323 " Mark loop %d (header %d, freq %d, depth %d) for removal\n",
2324 ira_loop_nodes[i].loop_num,
2325 ira_loop_nodes[i].loop->header->index,
2326 ira_loop_nodes[i].loop->header->frequency,
2327 loop_depth (ira_loop_nodes[i].loop));
2328 }
2329 }
2330
2331 /* Definition of vector of loop tree nodes. */
2332
2333 /* Vec containing references to all removed loop tree nodes. */
2334 static vec<ira_loop_tree_node_t> removed_loop_vec;
2335
2336 /* Vec containing references to all children of loop tree nodes. */
2337 static vec<ira_loop_tree_node_t> children_vec;
2338
2339 /* Remove subregions of NODE if their separate allocation will not
2340 improve the result. */
2341 static void
2342 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_node_t node)
2343 {
2344 unsigned int start;
2345 bool remove_p;
2346 ira_loop_tree_node_t subnode;
2347
2348 remove_p = node->to_remove_p;
2349 if (! remove_p)
2350 children_vec.safe_push (node);
2351 start = children_vec.length ();
2352 for (subnode = node->children; subnode != NULL; subnode = subnode->next)
2353 if (subnode->bb == NULL)
2354 remove_uneccesary_loop_nodes_from_loop_tree (subnode);
2355 else
2356 children_vec.safe_push (subnode);
2357 node->children = node->subloops = NULL;
2358 if (remove_p)
2359 {
2360 removed_loop_vec.safe_push (node);
2361 return;
2362 }
2363 while (children_vec.length () > start)
2364 {
2365 subnode = children_vec.pop ();
2366 subnode->parent = node;
2367 subnode->next = node->children;
2368 node->children = subnode;
2369 if (subnode->bb == NULL)
2370 {
2371 subnode->subloop_next = node->subloops;
2372 node->subloops = subnode;
2373 }
2374 }
2375 }
2376
2377 /* Return TRUE if NODE is inside PARENT. */
2378 static bool
2379 loop_is_inside_p (ira_loop_tree_node_t node, ira_loop_tree_node_t parent)
2380 {
2381 for (node = node->parent; node != NULL; node = node->parent)
2382 if (node == parent)
2383 return true;
2384 return false;
2385 }
2386
2387 /* Sort allocnos according to their order in regno allocno list. */
2388 static int
2389 regno_allocno_order_compare_func (const void *v1p, const void *v2p)
2390 {
2391 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2392 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2393 ira_loop_tree_node_t n1 = ALLOCNO_LOOP_TREE_NODE (a1);
2394 ira_loop_tree_node_t n2 = ALLOCNO_LOOP_TREE_NODE (a2);
2395
2396 if (loop_is_inside_p (n1, n2))
2397 return -1;
2398 else if (loop_is_inside_p (n2, n1))
2399 return 1;
2400 /* If allocnos are equally good, sort by allocno numbers, so that
2401 the results of qsort leave nothing to chance. We put allocnos
2402 with higher number first in the list because it is the original
2403 order for allocnos from loops on the same levels. */
2404 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
2405 }
2406
2407 /* This array is used to sort allocnos to restore allocno order in
2408 the regno allocno list. */
2409 static ira_allocno_t *regno_allocnos;
2410
2411 /* Restore allocno order for REGNO in the regno allocno list. */
2412 static void
2413 ira_rebuild_regno_allocno_list (int regno)
2414 {
2415 int i, n;
2416 ira_allocno_t a;
2417
2418 for (n = 0, a = ira_regno_allocno_map[regno];
2419 a != NULL;
2420 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2421 regno_allocnos[n++] = a;
2422 ira_assert (n > 0);
2423 qsort (regno_allocnos, n, sizeof (ira_allocno_t),
2424 regno_allocno_order_compare_func);
2425 for (i = 1; i < n; i++)
2426 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[i - 1]) = regno_allocnos[i];
2427 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[n - 1]) = NULL;
2428 ira_regno_allocno_map[regno] = regno_allocnos[0];
2429 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2430 fprintf (ira_dump_file, " Rebuilding regno allocno list for %d\n", regno);
2431 }
2432
2433 /* Propagate info from allocno FROM_A to allocno A. */
2434 static void
2435 propagate_some_info_from_allocno (ira_allocno_t a, ira_allocno_t from_a)
2436 {
2437 enum reg_class aclass;
2438
2439 merge_hard_reg_conflicts (from_a, a, false);
2440 ALLOCNO_NREFS (a) += ALLOCNO_NREFS (from_a);
2441 ALLOCNO_FREQ (a) += ALLOCNO_FREQ (from_a);
2442 ALLOCNO_CALL_FREQ (a) += ALLOCNO_CALL_FREQ (from_a);
2443 ALLOCNO_CALLS_CROSSED_NUM (a) += ALLOCNO_CALLS_CROSSED_NUM (from_a);
2444 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a)
2445 += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (from_a);
2446 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a),
2447 ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (from_a));
2448
2449 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
2450 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (from_a);
2451 if (! ALLOCNO_BAD_SPILL_P (from_a))
2452 ALLOCNO_BAD_SPILL_P (a) = false;
2453 aclass = ALLOCNO_CLASS (from_a);
2454 ira_assert (aclass == ALLOCNO_CLASS (a));
2455 ira_allocate_and_accumulate_costs (&ALLOCNO_HARD_REG_COSTS (a), aclass,
2456 ALLOCNO_HARD_REG_COSTS (from_a));
2457 ira_allocate_and_accumulate_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
2458 aclass,
2459 ALLOCNO_CONFLICT_HARD_REG_COSTS (from_a));
2460 ALLOCNO_CLASS_COST (a) += ALLOCNO_CLASS_COST (from_a);
2461 ALLOCNO_MEMORY_COST (a) += ALLOCNO_MEMORY_COST (from_a);
2462 }
2463
2464 /* Remove allocnos from loops removed from the allocation
2465 consideration. */
2466 static void
2467 remove_unnecessary_allocnos (void)
2468 {
2469 int regno;
2470 bool merged_p, rebuild_p;
2471 ira_allocno_t a, prev_a, next_a, parent_a;
2472 ira_loop_tree_node_t a_node, parent;
2473
2474 merged_p = false;
2475 regno_allocnos = NULL;
2476 for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
2477 {
2478 rebuild_p = false;
2479 for (prev_a = NULL, a = ira_regno_allocno_map[regno];
2480 a != NULL;
2481 a = next_a)
2482 {
2483 next_a = ALLOCNO_NEXT_REGNO_ALLOCNO (a);
2484 a_node = ALLOCNO_LOOP_TREE_NODE (a);
2485 if (! a_node->to_remove_p)
2486 prev_a = a;
2487 else
2488 {
2489 for (parent = a_node->parent;
2490 (parent_a = parent->regno_allocno_map[regno]) == NULL
2491 && parent->to_remove_p;
2492 parent = parent->parent)
2493 ;
2494 if (parent_a == NULL)
2495 {
2496 /* There are no allocnos with the same regno in
2497 upper region -- just move the allocno to the
2498 upper region. */
2499 prev_a = a;
2500 ALLOCNO_LOOP_TREE_NODE (a) = parent;
2501 parent->regno_allocno_map[regno] = a;
2502 bitmap_set_bit (parent->all_allocnos, ALLOCNO_NUM (a));
2503 rebuild_p = true;
2504 }
2505 else
2506 {
2507 /* Remove the allocno and update info of allocno in
2508 the upper region. */
2509 if (prev_a == NULL)
2510 ira_regno_allocno_map[regno] = next_a;
2511 else
2512 ALLOCNO_NEXT_REGNO_ALLOCNO (prev_a) = next_a;
2513 move_allocno_live_ranges (a, parent_a);
2514 merged_p = true;
2515 propagate_some_info_from_allocno (parent_a, a);
2516 /* Remove it from the corresponding regno allocno
2517 map to avoid info propagation of subsequent
2518 allocno into this already removed allocno. */
2519 a_node->regno_allocno_map[regno] = NULL;
2520 ira_remove_allocno_prefs (a);
2521 finish_allocno (a);
2522 }
2523 }
2524 }
2525 if (rebuild_p)
2526 /* We need to restore the order in regno allocno list. */
2527 {
2528 if (regno_allocnos == NULL)
2529 regno_allocnos
2530 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
2531 * ira_allocnos_num);
2532 ira_rebuild_regno_allocno_list (regno);
2533 }
2534 }
2535 if (merged_p)
2536 ira_rebuild_start_finish_chains ();
2537 if (regno_allocnos != NULL)
2538 ira_free (regno_allocnos);
2539 }
2540
2541 /* Remove allocnos from all loops but the root. */
2542 static void
2543 remove_low_level_allocnos (void)
2544 {
2545 int regno;
2546 bool merged_p, propagate_p;
2547 ira_allocno_t a, top_a;
2548 ira_loop_tree_node_t a_node, parent;
2549 ira_allocno_iterator ai;
2550
2551 merged_p = false;
2552 FOR_EACH_ALLOCNO (a, ai)
2553 {
2554 a_node = ALLOCNO_LOOP_TREE_NODE (a);
2555 if (a_node == ira_loop_tree_root || ALLOCNO_CAP_MEMBER (a) != NULL)
2556 continue;
2557 regno = ALLOCNO_REGNO (a);
2558 if ((top_a = ira_loop_tree_root->regno_allocno_map[regno]) == NULL)
2559 {
2560 ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
2561 ira_loop_tree_root->regno_allocno_map[regno] = a;
2562 continue;
2563 }
2564 propagate_p = a_node->parent->regno_allocno_map[regno] == NULL;
2565 /* Remove the allocno and update info of allocno in the upper
2566 region. */
2567 move_allocno_live_ranges (a, top_a);
2568 merged_p = true;
2569 if (propagate_p)
2570 propagate_some_info_from_allocno (top_a, a);
2571 }
2572 FOR_EACH_ALLOCNO (a, ai)
2573 {
2574 a_node = ALLOCNO_LOOP_TREE_NODE (a);
2575 if (a_node == ira_loop_tree_root)
2576 continue;
2577 parent = a_node->parent;
2578 regno = ALLOCNO_REGNO (a);
2579 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2580 ira_assert (ALLOCNO_CAP (a) != NULL);
2581 else if (ALLOCNO_CAP (a) == NULL)
2582 ira_assert (parent->regno_allocno_map[regno] != NULL);
2583 }
2584 FOR_EACH_ALLOCNO (a, ai)
2585 {
2586 regno = ALLOCNO_REGNO (a);
2587 if (ira_loop_tree_root->regno_allocno_map[regno] == a)
2588 {
2589 ira_object_t obj;
2590 ira_allocno_object_iterator oi;
2591
2592 ira_regno_allocno_map[regno] = a;
2593 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = NULL;
2594 ALLOCNO_CAP_MEMBER (a) = NULL;
2595 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
2596 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
2597 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
2598 #ifdef STACK_REGS
2599 if (ALLOCNO_TOTAL_NO_STACK_REG_P (a))
2600 ALLOCNO_NO_STACK_REG_P (a) = true;
2601 #endif
2602 }
2603 else
2604 {
2605 ira_remove_allocno_prefs (a);
2606 finish_allocno (a);
2607 }
2608 }
2609 if (merged_p)
2610 ira_rebuild_start_finish_chains ();
2611 }
2612
2613 /* Remove loops from consideration. We remove all loops except for
2614 root if ALL_P or loops for which a separate allocation will not
2615 improve the result. We have to do this after allocno creation and
2616 their costs and allocno class evaluation because only after that
2617 the register pressure can be known and is calculated. */
2618 static void
2619 remove_unnecessary_regions (bool all_p)
2620 {
2621 if (current_loops == NULL)
2622 return;
2623 if (all_p)
2624 mark_all_loops_for_removal ();
2625 else
2626 mark_loops_for_removal ();
2627 children_vec.create (last_basic_block_for_fn (cfun)
2628 + number_of_loops (cfun));
2629 removed_loop_vec.create (last_basic_block_for_fn (cfun)
2630 + number_of_loops (cfun));
2631 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_root);
2632 children_vec.release ();
2633 if (all_p)
2634 remove_low_level_allocnos ();
2635 else
2636 remove_unnecessary_allocnos ();
2637 while (removed_loop_vec.length () > 0)
2638 finish_loop_tree_node (removed_loop_vec.pop ());
2639 removed_loop_vec.release ();
2640 }
2641
2642 \f
2643
2644 /* At this point true value of allocno attribute bad_spill_p means
2645 that there is an insn where allocno occurs and where the allocno
2646 can not be used as memory. The function updates the attribute, now
2647 it can be true only for allocnos which can not be used as memory in
2648 an insn and in whose live ranges there is other allocno deaths.
2649 Spilling allocnos with true value will not improve the code because
2650 it will not make other allocnos colorable and additional reloads
2651 for the corresponding pseudo will be generated in reload pass for
2652 each insn it occurs.
2653
2654 This is a trick mentioned in one classic article of Chaitin etc
2655 which is frequently omitted in other implementations of RA based on
2656 graph coloring. */
2657 static void
2658 update_bad_spill_attribute (void)
2659 {
2660 int i;
2661 ira_allocno_t a;
2662 ira_allocno_iterator ai;
2663 ira_allocno_object_iterator aoi;
2664 ira_object_t obj;
2665 live_range_t r;
2666 enum reg_class aclass;
2667 bitmap_head dead_points[N_REG_CLASSES];
2668
2669 for (i = 0; i < ira_allocno_classes_num; i++)
2670 {
2671 aclass = ira_allocno_classes[i];
2672 bitmap_initialize (&dead_points[aclass], &reg_obstack);
2673 }
2674 FOR_EACH_ALLOCNO (a, ai)
2675 {
2676 aclass = ALLOCNO_CLASS (a);
2677 if (aclass == NO_REGS)
2678 continue;
2679 FOR_EACH_ALLOCNO_OBJECT (a, obj, aoi)
2680 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
2681 bitmap_set_bit (&dead_points[aclass], r->finish);
2682 }
2683 FOR_EACH_ALLOCNO (a, ai)
2684 {
2685 aclass = ALLOCNO_CLASS (a);
2686 if (aclass == NO_REGS)
2687 continue;
2688 if (! ALLOCNO_BAD_SPILL_P (a))
2689 continue;
2690 FOR_EACH_ALLOCNO_OBJECT (a, obj, aoi)
2691 {
2692 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
2693 {
2694 for (i = r->start + 1; i < r->finish; i++)
2695 if (bitmap_bit_p (&dead_points[aclass], i))
2696 break;
2697 if (i < r->finish)
2698 break;
2699 }
2700 if (r != NULL)
2701 {
2702 ALLOCNO_BAD_SPILL_P (a) = false;
2703 break;
2704 }
2705 }
2706 }
2707 for (i = 0; i < ira_allocno_classes_num; i++)
2708 {
2709 aclass = ira_allocno_classes[i];
2710 bitmap_clear (&dead_points[aclass]);
2711 }
2712 }
2713
2714 \f
2715
2716 /* Set up minimal and maximal live range points for allocnos. */
2717 static void
2718 setup_min_max_allocno_live_range_point (void)
2719 {
2720 int i;
2721 ira_allocno_t a, parent_a, cap;
2722 ira_allocno_iterator ai;
2723 #ifdef ENABLE_IRA_CHECKING
2724 ira_object_iterator oi;
2725 ira_object_t obj;
2726 #endif
2727 live_range_t r;
2728 ira_loop_tree_node_t parent;
2729
2730 FOR_EACH_ALLOCNO (a, ai)
2731 {
2732 int n = ALLOCNO_NUM_OBJECTS (a);
2733
2734 for (i = 0; i < n; i++)
2735 {
2736 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2737 r = OBJECT_LIVE_RANGES (obj);
2738 if (r == NULL)
2739 continue;
2740 OBJECT_MAX (obj) = r->finish;
2741 for (; r->next != NULL; r = r->next)
2742 ;
2743 OBJECT_MIN (obj) = r->start;
2744 }
2745 }
2746 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
2747 for (a = ira_regno_allocno_map[i];
2748 a != NULL;
2749 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2750 {
2751 int j;
2752 int n = ALLOCNO_NUM_OBJECTS (a);
2753
2754 for (j = 0; j < n; j++)
2755 {
2756 ira_object_t obj = ALLOCNO_OBJECT (a, j);
2757 ira_object_t parent_obj;
2758
2759 if (OBJECT_MAX (obj) < 0)
2760 continue;
2761 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2762 /* Accumulation of range info. */
2763 if (ALLOCNO_CAP (a) != NULL)
2764 {
2765 for (cap = ALLOCNO_CAP (a); cap != NULL; cap = ALLOCNO_CAP (cap))
2766 {
2767 ira_object_t cap_obj = ALLOCNO_OBJECT (cap, j);
2768 if (OBJECT_MAX (cap_obj) < OBJECT_MAX (obj))
2769 OBJECT_MAX (cap_obj) = OBJECT_MAX (obj);
2770 if (OBJECT_MIN (cap_obj) > OBJECT_MIN (obj))
2771 OBJECT_MIN (cap_obj) = OBJECT_MIN (obj);
2772 }
2773 continue;
2774 }
2775 if ((parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) == NULL)
2776 continue;
2777 parent_a = parent->regno_allocno_map[i];
2778 parent_obj = ALLOCNO_OBJECT (parent_a, j);
2779 if (OBJECT_MAX (parent_obj) < OBJECT_MAX (obj))
2780 OBJECT_MAX (parent_obj) = OBJECT_MAX (obj);
2781 if (OBJECT_MIN (parent_obj) > OBJECT_MIN (obj))
2782 OBJECT_MIN (parent_obj) = OBJECT_MIN (obj);
2783 }
2784 }
2785 #ifdef ENABLE_IRA_CHECKING
2786 FOR_EACH_OBJECT (obj, oi)
2787 {
2788 if ((0 <= OBJECT_MIN (obj) && OBJECT_MIN (obj) <= ira_max_point)
2789 && (0 <= OBJECT_MAX (obj) && OBJECT_MAX (obj) <= ira_max_point))
2790 continue;
2791 gcc_unreachable ();
2792 }
2793 #endif
2794 }
2795
2796 /* Sort allocnos according to their live ranges. Allocnos with
2797 smaller allocno class are put first unless we use priority
2798 coloring. Allocnos with the same class are ordered according
2799 their start (min). Allocnos with the same start are ordered
2800 according their finish (max). */
2801 static int
2802 object_range_compare_func (const void *v1p, const void *v2p)
2803 {
2804 int diff;
2805 ira_object_t obj1 = *(const ira_object_t *) v1p;
2806 ira_object_t obj2 = *(const ira_object_t *) v2p;
2807 ira_allocno_t a1 = OBJECT_ALLOCNO (obj1);
2808 ira_allocno_t a2 = OBJECT_ALLOCNO (obj2);
2809
2810 if ((diff = OBJECT_MIN (obj1) - OBJECT_MIN (obj2)) != 0)
2811 return diff;
2812 if ((diff = OBJECT_MAX (obj1) - OBJECT_MAX (obj2)) != 0)
2813 return diff;
2814 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2815 }
2816
2817 /* Sort ira_object_id_map and set up conflict id of allocnos. */
2818 static void
2819 sort_conflict_id_map (void)
2820 {
2821 int i, num;
2822 ira_allocno_t a;
2823 ira_allocno_iterator ai;
2824
2825 num = 0;
2826 FOR_EACH_ALLOCNO (a, ai)
2827 {
2828 ira_allocno_object_iterator oi;
2829 ira_object_t obj;
2830
2831 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
2832 ira_object_id_map[num++] = obj;
2833 }
2834 if (num > 1)
2835 qsort (ira_object_id_map, num, sizeof (ira_object_t),
2836 object_range_compare_func);
2837 for (i = 0; i < num; i++)
2838 {
2839 ira_object_t obj = ira_object_id_map[i];
2840
2841 gcc_assert (obj != NULL);
2842 OBJECT_CONFLICT_ID (obj) = i;
2843 }
2844 for (i = num; i < ira_objects_num; i++)
2845 ira_object_id_map[i] = NULL;
2846 }
2847
2848 /* Set up minimal and maximal conflict ids of allocnos with which
2849 given allocno can conflict. */
2850 static void
2851 setup_min_max_conflict_allocno_ids (void)
2852 {
2853 int aclass;
2854 int i, j, min, max, start, finish, first_not_finished, filled_area_start;
2855 int *live_range_min, *last_lived;
2856 int word0_min, word0_max;
2857 ira_allocno_t a;
2858 ira_allocno_iterator ai;
2859
2860 live_range_min = (int *) ira_allocate (sizeof (int) * ira_objects_num);
2861 aclass = -1;
2862 first_not_finished = -1;
2863 for (i = 0; i < ira_objects_num; i++)
2864 {
2865 ira_object_t obj = ira_object_id_map[i];
2866
2867 if (obj == NULL)
2868 continue;
2869
2870 a = OBJECT_ALLOCNO (obj);
2871
2872 if (aclass < 0)
2873 {
2874 aclass = ALLOCNO_CLASS (a);
2875 min = i;
2876 first_not_finished = i;
2877 }
2878 else
2879 {
2880 start = OBJECT_MIN (obj);
2881 /* If we skip an allocno, the allocno with smaller ids will
2882 be also skipped because of the secondary sorting the
2883 range finishes (see function
2884 object_range_compare_func). */
2885 while (first_not_finished < i
2886 && start > OBJECT_MAX (ira_object_id_map
2887 [first_not_finished]))
2888 first_not_finished++;
2889 min = first_not_finished;
2890 }
2891 if (min == i)
2892 /* We could increase min further in this case but it is good
2893 enough. */
2894 min++;
2895 live_range_min[i] = OBJECT_MIN (obj);
2896 OBJECT_MIN (obj) = min;
2897 }
2898 last_lived = (int *) ira_allocate (sizeof (int) * ira_max_point);
2899 aclass = -1;
2900 filled_area_start = -1;
2901 for (i = ira_objects_num - 1; i >= 0; i--)
2902 {
2903 ira_object_t obj = ira_object_id_map[i];
2904
2905 if (obj == NULL)
2906 continue;
2907
2908 a = OBJECT_ALLOCNO (obj);
2909 if (aclass < 0)
2910 {
2911 aclass = ALLOCNO_CLASS (a);
2912 for (j = 0; j < ira_max_point; j++)
2913 last_lived[j] = -1;
2914 filled_area_start = ira_max_point;
2915 }
2916 min = live_range_min[i];
2917 finish = OBJECT_MAX (obj);
2918 max = last_lived[finish];
2919 if (max < 0)
2920 /* We could decrease max further in this case but it is good
2921 enough. */
2922 max = OBJECT_CONFLICT_ID (obj) - 1;
2923 OBJECT_MAX (obj) = max;
2924 /* In filling, we can go further A range finish to recognize
2925 intersection quickly because if the finish of subsequently
2926 processed allocno (it has smaller conflict id) range is
2927 further A range finish than they are definitely intersected
2928 (the reason for this is the allocnos with bigger conflict id
2929 have their range starts not smaller than allocnos with
2930 smaller ids. */
2931 for (j = min; j < filled_area_start; j++)
2932 last_lived[j] = i;
2933 filled_area_start = min;
2934 }
2935 ira_free (last_lived);
2936 ira_free (live_range_min);
2937
2938 /* For allocnos with more than one object, we may later record extra conflicts in
2939 subobject 0 that we cannot really know about here.
2940 For now, simply widen the min/max range of these subobjects. */
2941
2942 word0_min = INT_MAX;
2943 word0_max = INT_MIN;
2944
2945 FOR_EACH_ALLOCNO (a, ai)
2946 {
2947 int n = ALLOCNO_NUM_OBJECTS (a);
2948 ira_object_t obj0;
2949
2950 if (n < 2)
2951 continue;
2952 obj0 = ALLOCNO_OBJECT (a, 0);
2953 if (OBJECT_CONFLICT_ID (obj0) < word0_min)
2954 word0_min = OBJECT_CONFLICT_ID (obj0);
2955 if (OBJECT_CONFLICT_ID (obj0) > word0_max)
2956 word0_max = OBJECT_CONFLICT_ID (obj0);
2957 }
2958 FOR_EACH_ALLOCNO (a, ai)
2959 {
2960 int n = ALLOCNO_NUM_OBJECTS (a);
2961 ira_object_t obj0;
2962
2963 if (n < 2)
2964 continue;
2965 obj0 = ALLOCNO_OBJECT (a, 0);
2966 if (OBJECT_MIN (obj0) > word0_min)
2967 OBJECT_MIN (obj0) = word0_min;
2968 if (OBJECT_MAX (obj0) < word0_max)
2969 OBJECT_MAX (obj0) = word0_max;
2970 }
2971 }
2972
2973 \f
2974
2975 static void
2976 create_caps (void)
2977 {
2978 ira_allocno_t a;
2979 ira_allocno_iterator ai;
2980 ira_loop_tree_node_t loop_tree_node;
2981
2982 FOR_EACH_ALLOCNO (a, ai)
2983 {
2984 if (ALLOCNO_LOOP_TREE_NODE (a) == ira_loop_tree_root)
2985 continue;
2986 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2987 create_cap_allocno (a);
2988 else if (ALLOCNO_CAP (a) == NULL)
2989 {
2990 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
2991 if (!bitmap_bit_p (loop_tree_node->border_allocnos, ALLOCNO_NUM (a)))
2992 create_cap_allocno (a);
2993 }
2994 }
2995 }
2996
2997 \f
2998
2999 /* The page contains code transforming more one region internal
3000 representation (IR) to one region IR which is necessary for reload.
3001 This transformation is called IR flattening. We might just rebuild
3002 the IR for one region but we don't do it because it takes a lot of
3003 time. */
3004
3005 /* Map: regno -> allocnos which will finally represent the regno for
3006 IR with one region. */
3007 static ira_allocno_t *regno_top_level_allocno_map;
3008
3009 /* Find the allocno that corresponds to A at a level one higher up in the
3010 loop tree. Returns NULL if A is a cap, or if it has no parent. */
3011 ira_allocno_t
3012 ira_parent_allocno (ira_allocno_t a)
3013 {
3014 ira_loop_tree_node_t parent;
3015
3016 if (ALLOCNO_CAP (a) != NULL)
3017 return NULL;
3018
3019 parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
3020 if (parent == NULL)
3021 return NULL;
3022
3023 return parent->regno_allocno_map[ALLOCNO_REGNO (a)];
3024 }
3025
3026 /* Find the allocno that corresponds to A at a level one higher up in the
3027 loop tree. If ALLOCNO_CAP is set for A, return that. */
3028 ira_allocno_t
3029 ira_parent_or_cap_allocno (ira_allocno_t a)
3030 {
3031 if (ALLOCNO_CAP (a) != NULL)
3032 return ALLOCNO_CAP (a);
3033
3034 return ira_parent_allocno (a);
3035 }
3036
3037 /* Process all allocnos originated from pseudo REGNO and copy live
3038 ranges, hard reg conflicts, and allocno stack reg attributes from
3039 low level allocnos to final allocnos which are destinations of
3040 removed stores at a loop exit. Return true if we copied live
3041 ranges. */
3042 static bool
3043 copy_info_to_removed_store_destinations (int regno)
3044 {
3045 ira_allocno_t a;
3046 ira_allocno_t parent_a = NULL;
3047 ira_loop_tree_node_t parent;
3048 bool merged_p;
3049
3050 merged_p = false;
3051 for (a = ira_regno_allocno_map[regno];
3052 a != NULL;
3053 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
3054 {
3055 if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))])
3056 /* This allocno will be removed. */
3057 continue;
3058
3059 /* Caps will be removed. */
3060 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3061 for (parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
3062 parent != NULL;
3063 parent = parent->parent)
3064 if ((parent_a = parent->regno_allocno_map[regno]) == NULL
3065 || (parent_a
3066 == regno_top_level_allocno_map[REGNO
3067 (allocno_emit_reg (parent_a))]
3068 && ALLOCNO_EMIT_DATA (parent_a)->mem_optimized_dest_p))
3069 break;
3070 if (parent == NULL || parent_a == NULL)
3071 continue;
3072
3073 copy_allocno_live_ranges (a, parent_a);
3074 merge_hard_reg_conflicts (a, parent_a, true);
3075
3076 ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
3077 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
3078 += ALLOCNO_CALLS_CROSSED_NUM (a);
3079 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
3080 += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
3081 IOR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (parent_a),
3082 ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
3083 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
3084 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
3085 merged_p = true;
3086 }
3087 return merged_p;
3088 }
3089
3090 /* Flatten the IR. In other words, this function transforms IR as if
3091 it were built with one region (without loops). We could make it
3092 much simpler by rebuilding IR with one region, but unfortunately it
3093 takes a lot of time. MAX_REGNO_BEFORE_EMIT and
3094 IRA_MAX_POINT_BEFORE_EMIT are correspondingly MAX_REG_NUM () and
3095 IRA_MAX_POINT before emitting insns on the loop borders. */
3096 void
3097 ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
3098 {
3099 int i, j;
3100 bool keep_p;
3101 int hard_regs_num;
3102 bool new_pseudos_p, merged_p, mem_dest_p;
3103 unsigned int n;
3104 enum reg_class aclass;
3105 ira_allocno_t a, parent_a, first, second, node_first, node_second;
3106 ira_copy_t cp;
3107 ira_loop_tree_node_t node;
3108 live_range_t r;
3109 ira_allocno_iterator ai;
3110 ira_copy_iterator ci;
3111
3112 regno_top_level_allocno_map
3113 = (ira_allocno_t *) ira_allocate (max_reg_num ()
3114 * sizeof (ira_allocno_t));
3115 memset (regno_top_level_allocno_map, 0,
3116 max_reg_num () * sizeof (ira_allocno_t));
3117 new_pseudos_p = merged_p = false;
3118 FOR_EACH_ALLOCNO (a, ai)
3119 {
3120 ira_allocno_object_iterator oi;
3121 ira_object_t obj;
3122
3123 if (ALLOCNO_CAP_MEMBER (a) != NULL)
3124 /* Caps are not in the regno allocno maps and they are never
3125 will be transformed into allocnos existing after IR
3126 flattening. */
3127 continue;
3128 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
3129 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
3130 OBJECT_CONFLICT_HARD_REGS (obj));
3131 #ifdef STACK_REGS
3132 ALLOCNO_TOTAL_NO_STACK_REG_P (a) = ALLOCNO_NO_STACK_REG_P (a);
3133 #endif
3134 }
3135 /* Fix final allocno attributes. */
3136 for (i = max_regno_before_emit - 1; i >= FIRST_PSEUDO_REGISTER; i--)
3137 {
3138 mem_dest_p = false;
3139 for (a = ira_regno_allocno_map[i];
3140 a != NULL;
3141 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
3142 {
3143 ira_emit_data_t parent_data, data = ALLOCNO_EMIT_DATA (a);
3144
3145 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3146 if (data->somewhere_renamed_p)
3147 new_pseudos_p = true;
3148 parent_a = ira_parent_allocno (a);
3149 if (parent_a == NULL)
3150 {
3151 ALLOCNO_COPIES (a) = NULL;
3152 regno_top_level_allocno_map[REGNO (data->reg)] = a;
3153 continue;
3154 }
3155 ira_assert (ALLOCNO_CAP_MEMBER (parent_a) == NULL);
3156
3157 if (data->mem_optimized_dest != NULL)
3158 mem_dest_p = true;
3159 parent_data = ALLOCNO_EMIT_DATA (parent_a);
3160 if (REGNO (data->reg) == REGNO (parent_data->reg))
3161 {
3162 merge_hard_reg_conflicts (a, parent_a, true);
3163 move_allocno_live_ranges (a, parent_a);
3164 merged_p = true;
3165 parent_data->mem_optimized_dest_p
3166 = (parent_data->mem_optimized_dest_p
3167 || data->mem_optimized_dest_p);
3168 continue;
3169 }
3170 new_pseudos_p = true;
3171 for (;;)
3172 {
3173 ALLOCNO_NREFS (parent_a) -= ALLOCNO_NREFS (a);
3174 ALLOCNO_FREQ (parent_a) -= ALLOCNO_FREQ (a);
3175 ALLOCNO_CALL_FREQ (parent_a) -= ALLOCNO_CALL_FREQ (a);
3176 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
3177 -= ALLOCNO_CALLS_CROSSED_NUM (a);
3178 ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
3179 -= ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
3180 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
3181 -= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
3182 ira_assert (ALLOCNO_CALLS_CROSSED_NUM (parent_a) >= 0
3183 && ALLOCNO_NREFS (parent_a) >= 0
3184 && ALLOCNO_FREQ (parent_a) >= 0);
3185 aclass = ALLOCNO_CLASS (parent_a);
3186 hard_regs_num = ira_class_hard_regs_num[aclass];
3187 if (ALLOCNO_HARD_REG_COSTS (a) != NULL
3188 && ALLOCNO_HARD_REG_COSTS (parent_a) != NULL)
3189 for (j = 0; j < hard_regs_num; j++)
3190 ALLOCNO_HARD_REG_COSTS (parent_a)[j]
3191 -= ALLOCNO_HARD_REG_COSTS (a)[j];
3192 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL
3193 && ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a) != NULL)
3194 for (j = 0; j < hard_regs_num; j++)
3195 ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a)[j]
3196 -= ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[j];
3197 ALLOCNO_CLASS_COST (parent_a)
3198 -= ALLOCNO_CLASS_COST (a);
3199 ALLOCNO_MEMORY_COST (parent_a) -= ALLOCNO_MEMORY_COST (a);
3200 parent_a = ira_parent_allocno (parent_a);
3201 if (parent_a == NULL)
3202 break;
3203 }
3204 ALLOCNO_COPIES (a) = NULL;
3205 regno_top_level_allocno_map[REGNO (data->reg)] = a;
3206 }
3207 if (mem_dest_p && copy_info_to_removed_store_destinations (i))
3208 merged_p = true;
3209 }
3210 ira_assert (new_pseudos_p || ira_max_point_before_emit == ira_max_point);
3211 if (merged_p || ira_max_point_before_emit != ira_max_point)
3212 ira_rebuild_start_finish_chains ();
3213 if (new_pseudos_p)
3214 {
3215 sparseset objects_live;
3216
3217 /* Rebuild conflicts. */
3218 FOR_EACH_ALLOCNO (a, ai)
3219 {
3220 ira_allocno_object_iterator oi;
3221 ira_object_t obj;
3222
3223 if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
3224 || ALLOCNO_CAP_MEMBER (a) != NULL)
3225 continue;
3226 FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
3227 {
3228 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
3229 ira_assert (r->object == obj);
3230 clear_conflicts (obj);
3231 }
3232 }
3233 objects_live = sparseset_alloc (ira_objects_num);
3234 for (i = 0; i < ira_max_point; i++)
3235 {
3236 for (r = ira_start_point_ranges[i]; r != NULL; r = r->start_next)
3237 {
3238 ira_object_t obj = r->object;
3239
3240 a = OBJECT_ALLOCNO (obj);
3241 if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
3242 || ALLOCNO_CAP_MEMBER (a) != NULL)
3243 continue;
3244
3245 aclass = ALLOCNO_CLASS (a);
3246 sparseset_set_bit (objects_live, OBJECT_CONFLICT_ID (obj));
3247 EXECUTE_IF_SET_IN_SPARSESET (objects_live, n)
3248 {
3249 ira_object_t live_obj = ira_object_id_map[n];
3250 ira_allocno_t live_a = OBJECT_ALLOCNO (live_obj);
3251 enum reg_class live_aclass = ALLOCNO_CLASS (live_a);
3252
3253 if (ira_reg_classes_intersect_p[aclass][live_aclass]
3254 /* Don't set up conflict for the allocno with itself. */
3255 && live_a != a)
3256 ira_add_conflict (obj, live_obj);
3257 }
3258 }
3259
3260 for (r = ira_finish_point_ranges[i]; r != NULL; r = r->finish_next)
3261 sparseset_clear_bit (objects_live, OBJECT_CONFLICT_ID (r->object));
3262 }
3263 sparseset_free (objects_live);
3264 compress_conflict_vecs ();
3265 }
3266 /* Mark some copies for removing and change allocnos in the rest
3267 copies. */
3268 FOR_EACH_COPY (cp, ci)
3269 {
3270 if (ALLOCNO_CAP_MEMBER (cp->first) != NULL
3271 || ALLOCNO_CAP_MEMBER (cp->second) != NULL)
3272 {
3273 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
3274 fprintf
3275 (ira_dump_file, " Remove cp%d:%c%dr%d-%c%dr%d\n",
3276 cp->num, ALLOCNO_CAP_MEMBER (cp->first) != NULL ? 'c' : 'a',
3277 ALLOCNO_NUM (cp->first),
3278 REGNO (allocno_emit_reg (cp->first)),
3279 ALLOCNO_CAP_MEMBER (cp->second) != NULL ? 'c' : 'a',
3280 ALLOCNO_NUM (cp->second),
3281 REGNO (allocno_emit_reg (cp->second)));
3282 cp->loop_tree_node = NULL;
3283 continue;
3284 }
3285 first
3286 = regno_top_level_allocno_map[REGNO (allocno_emit_reg (cp->first))];
3287 second
3288 = regno_top_level_allocno_map[REGNO (allocno_emit_reg (cp->second))];
3289 node = cp->loop_tree_node;
3290 if (node == NULL)
3291 keep_p = true; /* It copy generated in ira-emit.c. */
3292 else
3293 {
3294 /* Check that the copy was not propagated from level on
3295 which we will have different pseudos. */
3296 node_first = node->regno_allocno_map[ALLOCNO_REGNO (cp->first)];
3297 node_second = node->regno_allocno_map[ALLOCNO_REGNO (cp->second)];
3298 keep_p = ((REGNO (allocno_emit_reg (first))
3299 == REGNO (allocno_emit_reg (node_first)))
3300 && (REGNO (allocno_emit_reg (second))
3301 == REGNO (allocno_emit_reg (node_second))));
3302 }
3303 if (keep_p)
3304 {
3305 cp->loop_tree_node = ira_loop_tree_root;
3306 cp->first = first;
3307 cp->second = second;
3308 }
3309 else
3310 {
3311 cp->loop_tree_node = NULL;
3312 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
3313 fprintf (ira_dump_file, " Remove cp%d:a%dr%d-a%dr%d\n",
3314 cp->num, ALLOCNO_NUM (cp->first),
3315 REGNO (allocno_emit_reg (cp->first)),
3316 ALLOCNO_NUM (cp->second),
3317 REGNO (allocno_emit_reg (cp->second)));
3318 }
3319 }
3320 /* Remove unnecessary allocnos on lower levels of the loop tree. */
3321 FOR_EACH_ALLOCNO (a, ai)
3322 {
3323 if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
3324 || ALLOCNO_CAP_MEMBER (a) != NULL)
3325 {
3326 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
3327 fprintf (ira_dump_file, " Remove a%dr%d\n",
3328 ALLOCNO_NUM (a), REGNO (allocno_emit_reg (a)));
3329 ira_remove_allocno_prefs (a);
3330 finish_allocno (a);
3331 continue;
3332 }
3333 ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
3334 ALLOCNO_REGNO (a) = REGNO (allocno_emit_reg (a));
3335 ALLOCNO_CAP (a) = NULL;
3336 /* Restore updated costs for assignments from reload. */
3337 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
3338 ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
3339 if (! ALLOCNO_ASSIGNED_P (a))
3340 ira_free_allocno_updated_costs (a);
3341 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3342 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3343 }
3344 /* Remove unnecessary copies. */
3345 FOR_EACH_COPY (cp, ci)
3346 {
3347 if (cp->loop_tree_node == NULL)
3348 {
3349 ira_copies[cp->num] = NULL;
3350 finish_copy (cp);
3351 continue;
3352 }
3353 ira_assert
3354 (ALLOCNO_LOOP_TREE_NODE (cp->first) == ira_loop_tree_root
3355 && ALLOCNO_LOOP_TREE_NODE (cp->second) == ira_loop_tree_root);
3356 add_allocno_copy_to_list (cp);
3357 swap_allocno_copy_ends_if_necessary (cp);
3358 }
3359 rebuild_regno_allocno_maps ();
3360 if (ira_max_point != ira_max_point_before_emit)
3361 ira_compress_allocno_live_ranges ();
3362 ira_free (regno_top_level_allocno_map);
3363 }
3364
3365 \f
3366
3367 #ifdef ENABLE_IRA_CHECKING
3368 /* Check creation of all allocnos. Allocnos on lower levels should
3369 have allocnos or caps on all upper levels. */
3370 static void
3371 check_allocno_creation (void)
3372 {
3373 ira_allocno_t a;
3374 ira_allocno_iterator ai;
3375 ira_loop_tree_node_t loop_tree_node;
3376
3377 FOR_EACH_ALLOCNO (a, ai)
3378 {
3379 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
3380 ira_assert (bitmap_bit_p (loop_tree_node->all_allocnos,
3381 ALLOCNO_NUM (a)));
3382 if (loop_tree_node == ira_loop_tree_root)
3383 continue;
3384 if (ALLOCNO_CAP_MEMBER (a) != NULL)
3385 ira_assert (ALLOCNO_CAP (a) != NULL);
3386 else if (ALLOCNO_CAP (a) == NULL)
3387 ira_assert (loop_tree_node->parent
3388 ->regno_allocno_map[ALLOCNO_REGNO (a)] != NULL
3389 && bitmap_bit_p (loop_tree_node->border_allocnos,
3390 ALLOCNO_NUM (a)));
3391 }
3392 }
3393 #endif
3394
3395 /* Identify allocnos which prefer a register class with a single hard register.
3396 Adjust ALLOCNO_CONFLICT_HARD_REG_COSTS so that conflicting allocnos are
3397 less likely to use the preferred singleton register. */
3398 static void
3399 update_conflict_hard_reg_costs (void)
3400 {
3401 ira_allocno_t a;
3402 ira_allocno_iterator ai;
3403 int i, index, min;
3404
3405 FOR_EACH_ALLOCNO (a, ai)
3406 {
3407 reg_class_t aclass = ALLOCNO_CLASS (a);
3408 reg_class_t pref = reg_preferred_class (ALLOCNO_REGNO (a));
3409 int singleton = ira_class_singleton[pref][ALLOCNO_MODE (a)];
3410 if (singleton < 0)
3411 continue;
3412 index = ira_class_hard_reg_index[(int) aclass][singleton];
3413 if (index < 0)
3414 continue;
3415 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) == NULL
3416 || ALLOCNO_HARD_REG_COSTS (a) == NULL)
3417 continue;
3418 min = INT_MAX;
3419 for (i = ira_class_hard_regs_num[(int) aclass] - 1; i >= 0; i--)
3420 if (ALLOCNO_HARD_REG_COSTS (a)[i] > ALLOCNO_CLASS_COST (a)
3421 && min > ALLOCNO_HARD_REG_COSTS (a)[i])
3422 min = ALLOCNO_HARD_REG_COSTS (a)[i];
3423 if (min == INT_MAX)
3424 continue;
3425 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
3426 aclass, 0);
3427 ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[index]
3428 -= min - ALLOCNO_CLASS_COST (a);
3429 }
3430 }
3431
3432 /* Create a internal representation (IR) for IRA (allocnos, copies,
3433 loop tree nodes). The function returns TRUE if we generate loop
3434 structure (besides nodes representing all function and the basic
3435 blocks) for regional allocation. A true return means that we
3436 really need to flatten IR before the reload. */
3437 bool
3438 ira_build (void)
3439 {
3440 bool loops_p;
3441
3442 df_analyze ();
3443 initiate_cost_vectors ();
3444 initiate_allocnos ();
3445 initiate_prefs ();
3446 initiate_copies ();
3447 create_loop_tree_nodes ();
3448 form_loop_tree ();
3449 create_allocnos ();
3450 ira_costs ();
3451 create_allocno_objects ();
3452 ira_create_allocno_live_ranges ();
3453 remove_unnecessary_regions (false);
3454 ira_compress_allocno_live_ranges ();
3455 update_bad_spill_attribute ();
3456 loops_p = more_one_region_p ();
3457 if (loops_p)
3458 {
3459 propagate_allocno_info ();
3460 create_caps ();
3461 }
3462 ira_tune_allocno_costs ();
3463 #ifdef ENABLE_IRA_CHECKING
3464 check_allocno_creation ();
3465 #endif
3466 setup_min_max_allocno_live_range_point ();
3467 sort_conflict_id_map ();
3468 setup_min_max_conflict_allocno_ids ();
3469 ira_build_conflicts ();
3470 update_conflict_hard_reg_costs ();
3471 if (! ira_conflicts_p)
3472 {
3473 ira_allocno_t a;
3474 ira_allocno_iterator ai;
3475
3476 /* Remove all regions but root one. */
3477 if (loops_p)
3478 {
3479 remove_unnecessary_regions (true);
3480 loops_p = false;
3481 }
3482 /* We don't save hard registers around calls for fast allocation
3483 -- add caller clobbered registers as conflicting ones to
3484 allocno crossing calls. */
3485 FOR_EACH_ALLOCNO (a, ai)
3486 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
3487 ior_hard_reg_conflicts (a, &call_used_reg_set);
3488 }
3489 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3490 print_copies (ira_dump_file);
3491 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3492 print_prefs (ira_dump_file);
3493 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3494 {
3495 int n, nr, nr_big;
3496 ira_allocno_t a;
3497 live_range_t r;
3498 ira_allocno_iterator ai;
3499
3500 n = 0;
3501 nr = 0;
3502 nr_big = 0;
3503 FOR_EACH_ALLOCNO (a, ai)
3504 {
3505 int j, nobj = ALLOCNO_NUM_OBJECTS (a);
3506
3507 if (nobj > 1)
3508 nr_big++;
3509 for (j = 0; j < nobj; j++)
3510 {
3511 ira_object_t obj = ALLOCNO_OBJECT (a, j);
3512 n += OBJECT_NUM_CONFLICTS (obj);
3513 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
3514 nr++;
3515 }
3516 }
3517 fprintf (ira_dump_file, " regions=%d, blocks=%d, points=%d\n",
3518 current_loops == NULL ? 1 : number_of_loops (cfun),
3519 n_basic_blocks_for_fn (cfun), ira_max_point);
3520 fprintf (ira_dump_file,
3521 " allocnos=%d (big %d), copies=%d, conflicts=%d, ranges=%d\n",
3522 ira_allocnos_num, nr_big, ira_copies_num, n, nr);
3523 }
3524 return loops_p;
3525 }
3526
3527 /* Release the data created by function ira_build. */
3528 void
3529 ira_destroy (void)
3530 {
3531 finish_loop_tree_nodes ();
3532 finish_prefs ();
3533 finish_copies ();
3534 finish_allocnos ();
3535 finish_cost_vectors ();
3536 ira_finish_allocno_live_ranges ();
3537 }