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