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