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