Line data Source code
1 : /* IRA allocation based on graph coloring.
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 "tree.h"
28 : #include "predict.h"
29 : #include "df.h"
30 : #include "memmodel.h"
31 : #include "tm_p.h"
32 : #include "insn-config.h"
33 : #include "regs.h"
34 : #include "ira.h"
35 : #include "ira-int.h"
36 : #include "reload.h"
37 : #include "cfgloop.h"
38 : #include "lra.h"
39 :
40 : /* To prevent soft conflict detection becoming quadratic in the
41 : loop depth. Only for very pathological cases, so it hardly
42 : seems worth a --param. */
43 : const int max_soft_conflict_loop_depth = 64;
44 :
45 : /* Return the regset for allocno A that represents all registers
46 : allowed by A's dependent filters. */
47 :
48 : static HARD_REG_SET
49 0 : ira_dependent_filter (ira_allocno_t a)
50 : {
51 0 : HARD_REG_SET allowed;
52 0 : SET_HARD_REG_SET (allowed);
53 :
54 0 : for (auto *dep_filter = ALLOCNO_DEPENDENT_FILTERS (a);
55 0 : dep_filter;
56 0 : dep_filter = dep_filter->next)
57 : {
58 0 : unsigned int ref_regno;
59 : /* Nothing to filter if the referenced allocno didn't get a
60 : hardreg. */
61 0 : if (dep_filter->ref_allocno)
62 : {
63 0 : int hr = ALLOCNO_HARD_REGNO (dep_filter->ref_allocno);
64 0 : if (hr < 0)
65 0 : continue;
66 0 : ref_regno = (unsigned int) hr;
67 : }
68 : else
69 : {
70 0 : ref_regno = dep_filter->ref_hard_regno;
71 0 : if (ref_regno >= FIRST_PSEUDO_REGISTER)
72 0 : continue;
73 : }
74 0 : const HARD_REG_SET *filter
75 0 : = lra_get_dependent_filter (dep_filter->id, dep_filter->mode,
76 0 : ref_regno, dep_filter->ref_mode,
77 : false);
78 0 : allowed &= *filter;
79 : }
80 0 : return allowed;
81 : }
82 :
83 : typedef struct allocno_hard_regs *allocno_hard_regs_t;
84 :
85 : /* The structure contains information about hard registers can be
86 : assigned to allocnos. Usually it is allocno profitable hard
87 : registers but in some cases this set can be a bit different. Major
88 : reason of the difference is a requirement to use hard register sets
89 : that form a tree or a forest (set of trees), i.e. hard register set
90 : of a node should contain hard register sets of its subnodes. */
91 : struct allocno_hard_regs
92 : {
93 : /* Hard registers can be assigned to an allocno. */
94 : HARD_REG_SET set;
95 : /* Overall (spilling) cost of all allocnos with given register
96 : set. */
97 : int64_t cost;
98 : };
99 :
100 : typedef struct allocno_hard_regs_node *allocno_hard_regs_node_t;
101 :
102 : /* A node representing allocno hard registers. Such nodes form a
103 : forest (set of trees). Each subnode of given node in the forest
104 : refers for hard register set (usually allocno profitable hard
105 : register set) which is a subset of one referred from given
106 : node. */
107 : struct allocno_hard_regs_node
108 : {
109 : /* Set up number of the node in preorder traversing of the forest. */
110 : int preorder_num;
111 : /* Used for different calculation like finding conflict size of an
112 : allocno. */
113 : int check;
114 : /* Used for calculation of conflict size of an allocno. The
115 : conflict size of the allocno is maximal number of given allocno
116 : hard registers needed for allocation of the conflicting allocnos.
117 : Given allocno is trivially colored if this number plus the number
118 : of hard registers needed for given allocno is not greater than
119 : the number of given allocno hard register set. */
120 : int conflict_size;
121 : /* The number of hard registers given by member hard_regs. */
122 : int hard_regs_num;
123 : /* The following member is used to form the final forest. */
124 : bool used_p;
125 : /* Pointer to the corresponding profitable hard registers. */
126 : allocno_hard_regs_t hard_regs;
127 : /* Parent, first subnode, previous and next node with the same
128 : parent in the forest. */
129 : allocno_hard_regs_node_t parent, first, prev, next;
130 : };
131 :
132 : /* Info about changing hard reg costs of an allocno. */
133 : struct update_cost_record
134 : {
135 : /* Hard regno for which we changed the cost. */
136 : int hard_regno;
137 : /* Divisor used when we changed the cost of HARD_REGNO. */
138 : int divisor;
139 : /* Next record for given allocno. */
140 : struct update_cost_record *next;
141 : };
142 :
143 : /* To decrease footprint of ira_allocno structure we store all data
144 : needed only for coloring in the following structure. */
145 : struct allocno_color_data
146 : {
147 : /* TRUE value means that the allocno was not removed yet from the
148 : conflicting graph during coloring. */
149 : unsigned int in_graph_p : 1;
150 : /* TRUE if it is put on the stack to make other allocnos
151 : colorable. */
152 : unsigned int may_be_spilled_p : 1;
153 : /* TRUE if the allocno is trivially colorable. */
154 : unsigned int colorable_p : 1;
155 : /* Number of hard registers of the allocno class really
156 : available for the allocno allocation. It is number of the
157 : profitable hard regs. */
158 : int available_regs_num;
159 : /* Sum of frequencies of hard register preferences of all
160 : conflicting allocnos which are not the coloring stack yet. */
161 : int conflict_allocno_hard_prefs;
162 : /* Allocnos in a bucket (used in coloring) chained by the following
163 : two members. */
164 : ira_allocno_t next_bucket_allocno;
165 : ira_allocno_t prev_bucket_allocno;
166 : /* Used for temporary purposes. */
167 : int temp;
168 : /* Used to exclude repeated processing. */
169 : int last_process;
170 : /* Profitable hard regs available for this pseudo allocation. It
171 : means that the set excludes unavailable hard regs and hard regs
172 : conflicting with given pseudo. They should be of the allocno
173 : class. */
174 : HARD_REG_SET profitable_hard_regs;
175 : /* The allocno hard registers node. */
176 : allocno_hard_regs_node_t hard_regs_node;
177 : /* Array of structures allocno_hard_regs_subnode representing
178 : given allocno hard registers node (the 1st element in the array)
179 : and all its subnodes in the tree (forest) of allocno hard
180 : register nodes (see comments above). */
181 : int hard_regs_subnodes_start;
182 : /* The length of the previous array. */
183 : int hard_regs_subnodes_num;
184 : /* Records about updating allocno hard reg costs from copies. If
185 : the allocno did not get expected hard register, these records are
186 : used to restore original hard reg costs of allocnos connected to
187 : this allocno by copies. */
188 : struct update_cost_record *update_cost_records;
189 : /* Threads. We collect allocnos connected by copies into threads
190 : and try to assign hard regs to allocnos by threads. */
191 : /* Allocno representing all thread. */
192 : ira_allocno_t first_thread_allocno;
193 : /* Allocnos in thread forms a cycle list through the following
194 : member. */
195 : ira_allocno_t next_thread_allocno;
196 : /* All thread frequency. Defined only for first thread allocno. */
197 : int thread_freq;
198 : /* Sum of frequencies of hard register preferences of the allocno. */
199 : int hard_reg_prefs;
200 : };
201 :
202 : /* See above. */
203 : typedef struct allocno_color_data *allocno_color_data_t;
204 :
205 : /* Container for storing allocno data concerning coloring. */
206 : static allocno_color_data_t allocno_color_data;
207 :
208 : /* Macro to access the data concerning coloring. */
209 : #define ALLOCNO_COLOR_DATA(a) ((allocno_color_data_t) ALLOCNO_ADD_DATA (a))
210 :
211 : /* Used for finding allocno colorability to exclude repeated allocno
212 : processing and for updating preferencing to exclude repeated
213 : allocno processing during assignment. */
214 : static int curr_allocno_process;
215 :
216 : /* This file contains code for regional graph coloring, spill/restore
217 : code placement optimization, and code helping the reload pass to do
218 : a better job. */
219 :
220 : /* Bitmap of allocnos which should be colored. */
221 : static bitmap coloring_allocno_bitmap;
222 :
223 : /* Bitmap of allocnos which should be taken into account during
224 : coloring. In general case it contains allocnos from
225 : coloring_allocno_bitmap plus other already colored conflicting
226 : allocnos. */
227 : static bitmap consideration_allocno_bitmap;
228 :
229 : /* All allocnos sorted according their priorities. */
230 : static ira_allocno_t *sorted_allocnos;
231 :
232 : /* Vec representing the stack of allocnos used during coloring. */
233 : static vec<ira_allocno_t> allocno_stack_vec;
234 :
235 : /* Helper for qsort comparison callbacks - return a positive integer if
236 : X > Y, or a negative value otherwise. Use a conditional expression
237 : instead of a difference computation to insulate from possible overflow
238 : issues, e.g. X - Y < 0 for some X > 0 and Y < 0. */
239 : #define SORTGT(x,y) (((x) > (y)) ? 1 : -1)
240 :
241 :
242 :
243 : /* Definition of vector of allocno hard registers. */
244 :
245 : /* Vector of unique allocno hard registers. */
246 : static vec<allocno_hard_regs_t> allocno_hard_regs_vec;
247 :
248 : struct allocno_hard_regs_hasher : nofree_ptr_hash <allocno_hard_regs>
249 : {
250 : static inline hashval_t hash (const allocno_hard_regs *);
251 : static inline bool equal (const allocno_hard_regs *,
252 : const allocno_hard_regs *);
253 : };
254 :
255 : /* Returns hash value for allocno hard registers V. */
256 : inline hashval_t
257 314515173 : allocno_hard_regs_hasher::hash (const allocno_hard_regs *hv)
258 : {
259 314515173 : return iterative_hash (&hv->set, sizeof (HARD_REG_SET), 0);
260 : }
261 :
262 : /* Compares allocno hard registers V1 and V2. */
263 : inline bool
264 203244656 : allocno_hard_regs_hasher::equal (const allocno_hard_regs *hv1,
265 : const allocno_hard_regs *hv2)
266 : {
267 406489312 : return hv1->set == hv2->set;
268 : }
269 :
270 : /* Hash table of unique allocno hard registers. */
271 : static hash_table<allocno_hard_regs_hasher> *allocno_hard_regs_htab;
272 :
273 : /* Return allocno hard registers in the hash table equal to HV. */
274 : static allocno_hard_regs_t
275 86913802 : find_hard_regs (allocno_hard_regs_t hv)
276 : {
277 0 : return allocno_hard_regs_htab->find (hv);
278 : }
279 :
280 : /* Insert allocno hard registers HV in the hash table (if it is not
281 : there yet) and return the value which in the table. */
282 : static allocno_hard_regs_t
283 63505897 : insert_hard_regs (allocno_hard_regs_t hv)
284 : {
285 63505897 : allocno_hard_regs **slot = allocno_hard_regs_htab->find_slot (hv, INSERT);
286 :
287 63505897 : if (*slot == NULL)
288 63505897 : *slot = hv;
289 63505897 : return *slot;
290 : }
291 :
292 : /* Initialize data concerning allocno hard registers. */
293 : static void
294 1240945 : init_allocno_hard_regs (void)
295 : {
296 1240945 : allocno_hard_regs_vec.create (200);
297 1240945 : allocno_hard_regs_htab
298 1240945 : = new hash_table<allocno_hard_regs_hasher> (200);
299 1240945 : }
300 :
301 : /* Add (or update info about) allocno hard registers with SET and
302 : COST. */
303 : static allocno_hard_regs_t
304 86913802 : add_allocno_hard_regs (HARD_REG_SET set, int64_t cost)
305 : {
306 86913802 : struct allocno_hard_regs temp;
307 86913802 : allocno_hard_regs_t hv;
308 :
309 173827604 : gcc_assert (! hard_reg_set_empty_p (set));
310 86913802 : temp.set = set;
311 86913802 : if ((hv = find_hard_regs (&temp)) != NULL)
312 23407905 : hv->cost += cost;
313 : else
314 : {
315 127011794 : hv = ((struct allocno_hard_regs *)
316 63505897 : ira_allocate (sizeof (struct allocno_hard_regs)));
317 63505897 : hv->set = set;
318 63505897 : hv->cost = cost;
319 63505897 : allocno_hard_regs_vec.safe_push (hv);
320 63505897 : insert_hard_regs (hv);
321 : }
322 86913802 : return hv;
323 : }
324 :
325 : /* Finalize data concerning allocno hard registers. */
326 : static void
327 1240945 : finish_allocno_hard_regs (void)
328 : {
329 1240945 : int i;
330 1240945 : allocno_hard_regs_t hv;
331 :
332 64746842 : for (i = 0;
333 64746842 : allocno_hard_regs_vec.iterate (i, &hv);
334 : i++)
335 63505897 : ira_free (hv);
336 1240945 : delete allocno_hard_regs_htab;
337 1240945 : allocno_hard_regs_htab = NULL;
338 1240945 : allocno_hard_regs_vec.release ();
339 1240945 : }
340 :
341 : /* Sort hard regs according to their frequency of usage. */
342 : static int
343 35803225 : allocno_hard_regs_compare (const void *v1p, const void *v2p)
344 : {
345 35803225 : allocno_hard_regs_t hv1 = *(const allocno_hard_regs_t *) v1p;
346 35803225 : allocno_hard_regs_t hv2 = *(const allocno_hard_regs_t *) v2p;
347 :
348 35803225 : if (hv2->cost > hv1->cost)
349 : return 1;
350 19317961 : else if (hv2->cost < hv1->cost)
351 : return -1;
352 :
353 : /* Break ties using the HARD_REG_SETs themselves. Avoid influencing sorting
354 : by such host features as word size and alignment, looking for the
355 : lowest-numbered hard register difference. */
356 2077094 : return hard_reg_set_first_diff (hv1->set, hv2->set, 0);
357 : }
358 :
359 :
360 :
361 : /* Used for finding a common ancestor of two allocno hard registers
362 : nodes in the forest. We use the current value of
363 : 'node_check_tick' to mark all nodes from one node to the top and
364 : then walking up from another node until we find a marked node.
365 :
366 : It is also used to figure out allocno colorability as a mark that
367 : we already reset value of member 'conflict_size' for the forest
368 : node corresponding to the processed allocno. */
369 : static int node_check_tick;
370 :
371 : /* Roots of the forest containing hard register sets can be assigned
372 : to allocnos. */
373 : static allocno_hard_regs_node_t hard_regs_roots;
374 :
375 : /* Definition of vector of allocno hard register nodes. */
376 :
377 : /* Vector used to create the forest. */
378 : static vec<allocno_hard_regs_node_t> hard_regs_node_vec;
379 :
380 : /* Create and return allocno hard registers node containing allocno
381 : hard registers HV. */
382 : static allocno_hard_regs_node_t
383 61423069 : create_new_allocno_hard_regs_node (allocno_hard_regs_t hv)
384 : {
385 61423069 : allocno_hard_regs_node_t new_node;
386 :
387 61423069 : new_node = ((struct allocno_hard_regs_node *)
388 61423069 : ira_allocate (sizeof (struct allocno_hard_regs_node)));
389 61423069 : new_node->check = 0;
390 61423069 : new_node->hard_regs = hv;
391 61423069 : new_node->hard_regs_num = hard_reg_set_popcount (hv->set);
392 61423069 : new_node->first = NULL;
393 61423069 : new_node->used_p = false;
394 61423069 : return new_node;
395 : }
396 :
397 : /* Add allocno hard registers node NEW_NODE to the forest on its level
398 : given by ROOTS. */
399 : static void
400 61423069 : add_new_allocno_hard_regs_node_to_forest (allocno_hard_regs_node_t *roots,
401 : allocno_hard_regs_node_t new_node)
402 : {
403 61423069 : new_node->next = *roots;
404 0 : if (new_node->next != NULL)
405 58941179 : new_node->next->prev = new_node;
406 61423069 : new_node->prev = NULL;
407 61423069 : *roots = new_node;
408 0 : }
409 :
410 : /* Add allocno hard registers HV (or its best approximation if it is
411 : not possible) to the forest on its level given by ROOTS. */
412 : static void
413 7924684 : add_allocno_hard_regs_to_forest (allocno_hard_regs_node_t *roots,
414 : allocno_hard_regs_t hv)
415 : {
416 13110100 : unsigned int i, start;
417 13110100 : allocno_hard_regs_node_t node, prev, new_node;
418 13110100 : HARD_REG_SET temp_set;
419 13110100 : allocno_hard_regs_t hv2;
420 :
421 13110100 : start = hard_regs_node_vec.length ();
422 142936684 : for (node = *roots; node != NULL; node = node->next)
423 : {
424 274575000 : if (hv->set == node->hard_regs->set)
425 2275500 : return;
426 135012000 : if (hard_reg_set_subset_p (hv->set, node->hard_regs->set))
427 : {
428 5185416 : add_allocno_hard_regs_to_forest (&node->first, hv);
429 5185416 : return;
430 : }
431 129826584 : if (hard_reg_set_subset_p (node->hard_regs->set, hv->set))
432 71509857 : hard_regs_node_vec.safe_push (node);
433 58316727 : else if (hard_reg_set_intersect_p (hv->set, node->hard_regs->set))
434 : {
435 1268147 : temp_set = hv->set & node->hard_regs->set;
436 1268147 : hv2 = add_allocno_hard_regs (temp_set, hv->cost);
437 1268147 : add_allocno_hard_regs_to_forest (&node->first, hv2);
438 : }
439 : }
440 5649184 : if (hard_regs_node_vec.length ()
441 5649184 : > start + 1)
442 : {
443 : /* Create a new node which contains nodes in hard_regs_node_vec. */
444 75085167 : CLEAR_HARD_REG_SET (temp_set);
445 70511458 : for (i = start;
446 75085167 : i < hard_regs_node_vec.length ();
447 : i++)
448 : {
449 70511458 : node = hard_regs_node_vec[i];
450 141022916 : temp_set |= node->hard_regs->set;
451 : }
452 4573709 : hv = add_allocno_hard_regs (temp_set, hv->cost);
453 4573709 : new_node = create_new_allocno_hard_regs_node (hv);
454 4573709 : prev = NULL;
455 4573709 : for (i = start;
456 75085167 : i < hard_regs_node_vec.length ();
457 : i++)
458 : {
459 70511458 : node = hard_regs_node_vec[i];
460 70511458 : if (node->prev == NULL)
461 47811033 : *roots = node->next;
462 : else
463 22700425 : node->prev->next = node->next;
464 70511458 : if (node->next != NULL)
465 67258631 : node->next->prev = node->prev;
466 70511458 : if (prev == NULL)
467 4573709 : new_node->first = node;
468 : else
469 65937749 : prev->next = node;
470 70511458 : node->prev = prev;
471 70511458 : node->next = NULL;
472 70511458 : prev = node;
473 : }
474 7906473 : add_new_allocno_hard_regs_node_to_forest (roots, new_node);
475 : }
476 5649184 : hard_regs_node_vec.truncate (start);
477 : }
478 :
479 : /* Add allocno hard registers nodes starting with the forest level
480 : given by FIRST which contains biggest set inside SET. */
481 : static void
482 68391711 : collect_allocno_hard_regs_cover (allocno_hard_regs_node_t first,
483 : HARD_REG_SET set)
484 : {
485 68391711 : allocno_hard_regs_node_t node;
486 :
487 68391711 : ira_assert (first != NULL);
488 684930831 : for (node = first; node != NULL; node = node->next)
489 1233078240 : if (hard_reg_set_subset_p (node->hard_regs->set, set))
490 24643377 : hard_regs_node_vec.safe_push (node);
491 591895743 : else if (hard_reg_set_intersect_p (set, node->hard_regs->set))
492 45410070 : collect_allocno_hard_regs_cover (node->first, set);
493 68391711 : }
494 :
495 : /* Set up field parent as PARENT in all allocno hard registers nodes
496 : in forest given by FIRST. */
497 : static void
498 62664014 : setup_allocno_hard_regs_nodes_parent (allocno_hard_regs_node_t first,
499 : allocno_hard_regs_node_t parent)
500 : {
501 62664014 : allocno_hard_regs_node_t node;
502 :
503 124087083 : for (node = first; node != NULL; node = node->next)
504 : {
505 61423069 : node->parent = parent;
506 61423069 : setup_allocno_hard_regs_nodes_parent (node->first, node);
507 : }
508 62664014 : }
509 :
510 : /* Return allocno hard registers node which is a first common ancestor
511 : node of FIRST and SECOND in the forest. */
512 : static allocno_hard_regs_node_t
513 1661736 : first_common_ancestor_node (allocno_hard_regs_node_t first,
514 : allocno_hard_regs_node_t second)
515 : {
516 1661736 : allocno_hard_regs_node_t node;
517 :
518 1661736 : node_check_tick++;
519 8950546 : for (node = first; node != NULL; node = node->parent)
520 7288810 : node->check = node_check_tick;
521 2719126 : for (node = second; node != NULL; node = node->parent)
522 4380862 : if (node->check == node_check_tick)
523 1661736 : return node;
524 : return first_common_ancestor_node (second, first);
525 : }
526 :
527 : /* Print hard reg set SET to F. */
528 : static void
529 2408 : print_hard_reg_set (FILE *f, HARD_REG_SET set, bool new_line_p)
530 : {
531 2408 : int i, start, end;
532 :
533 228760 : for (start = end = -1, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
534 : {
535 226352 : bool reg_included = TEST_HARD_REG_BIT (set, i);
536 :
537 226352 : if (reg_included)
538 : {
539 83881 : if (start == -1)
540 6311 : start = i;
541 : end = i;
542 : }
543 226352 : if (start >= 0 && (!reg_included || i == FIRST_PSEUDO_REGISTER - 1))
544 : {
545 6311 : if (start == end)
546 872 : fprintf (f, " %d", start);
547 : else
548 5439 : fprintf (f, " %d-%d", start, end);
549 : start = -1;
550 : }
551 : }
552 2408 : if (new_line_p)
553 0 : fprintf (f, "\n");
554 2408 : }
555 :
556 : /* Dump a hard reg set SET to stderr. */
557 : DEBUG_FUNCTION void
558 0 : debug_hard_reg_set (HARD_REG_SET set)
559 : {
560 0 : print_hard_reg_set (stderr, set, true);
561 0 : }
562 :
563 : /* Print allocno hard register subforest given by ROOTS and its LEVEL
564 : to F. */
565 : static void
566 285 : print_hard_regs_subforest (FILE *f, allocno_hard_regs_node_t roots,
567 : int level)
568 : {
569 285 : int i;
570 285 : allocno_hard_regs_node_t node;
571 :
572 518 : for (node = roots; node != NULL; node = node->next)
573 : {
574 233 : fprintf (f, " ");
575 2014 : for (i = 0; i < level * 2; i++)
576 1548 : fprintf (f, " ");
577 233 : fprintf (f, "%d:(", node->preorder_num);
578 233 : print_hard_reg_set (f, node->hard_regs->set, false);
579 233 : fprintf (f, ")@%" PRId64"\n", node->hard_regs->cost);
580 233 : print_hard_regs_subforest (f, node->first, level + 1);
581 : }
582 285 : }
583 :
584 : /* Print the allocno hard register forest to F. */
585 : static void
586 52 : print_hard_regs_forest (FILE *f)
587 : {
588 52 : fprintf (f, " Hard reg set forest:\n");
589 52 : print_hard_regs_subforest (f, hard_regs_roots, 1);
590 52 : }
591 :
592 : /* Print the allocno hard register forest to stderr. */
593 : void
594 0 : ira_debug_hard_regs_forest (void)
595 : {
596 0 : print_hard_regs_forest (stderr);
597 0 : }
598 :
599 : /* Remove unused allocno hard registers nodes from forest given by its
600 : *ROOTS. */
601 : static void
602 5664616 : remove_unused_allocno_hard_regs_nodes (allocno_hard_regs_node_t *roots)
603 : {
604 5664616 : allocno_hard_regs_node_t node, prev, next, last;
605 :
606 67087685 : for (prev = NULL, node = *roots; node != NULL; node = next)
607 : {
608 61423069 : next = node->next;
609 61423069 : if (node->used_p)
610 : {
611 4423671 : remove_unused_allocno_hard_regs_nodes (&node->first);
612 4423671 : prev = node;
613 : }
614 : else
615 : {
616 56999398 : for (last = node->first;
617 58305538 : last != NULL && last->next != NULL;
618 : last = last->next)
619 : ;
620 56999398 : if (last != NULL)
621 : {
622 347531 : if (prev == NULL)
623 334973 : *roots = node->first;
624 : else
625 12558 : prev->next = node->first;
626 347531 : if (next != NULL)
627 336689 : next->prev = last;
628 347531 : last->next = next;
629 347531 : next = node->first;
630 : }
631 : else
632 : {
633 56651867 : if (prev == NULL)
634 24144732 : *roots = next;
635 : else
636 32507135 : prev->next = next;
637 56651867 : if (next != NULL)
638 52673787 : next->prev = prev;
639 : }
640 56999398 : ira_free (node);
641 : }
642 : }
643 5664616 : }
644 :
645 : /* Set up fields preorder_num starting with START_NUM in all allocno
646 : hard registers nodes in forest given by FIRST. Return biggest set
647 : PREORDER_NUM increased by 1. */
648 : static int
649 5664616 : enumerate_allocno_hard_regs_nodes (allocno_hard_regs_node_t first,
650 : allocno_hard_regs_node_t parent,
651 : int start_num)
652 : {
653 5664616 : allocno_hard_regs_node_t node;
654 :
655 10088287 : for (node = first; node != NULL; node = node->next)
656 : {
657 4423671 : node->preorder_num = start_num++;
658 4423671 : node->parent = parent;
659 4423671 : start_num = enumerate_allocno_hard_regs_nodes (node->first, node,
660 : start_num);
661 : }
662 5664616 : return start_num;
663 : }
664 :
665 : /* Number of allocno hard registers nodes in the forest. */
666 : static int allocno_hard_regs_nodes_num;
667 :
668 : /* Table preorder number of allocno hard registers node in the forest
669 : -> the allocno hard registers node. */
670 : static allocno_hard_regs_node_t *allocno_hard_regs_nodes;
671 :
672 : /* See below. */
673 : typedef struct allocno_hard_regs_subnode *allocno_hard_regs_subnode_t;
674 :
675 : /* The structure is used to describes all subnodes (not only immediate
676 : ones) in the mentioned above tree for given allocno hard register
677 : node. The usage of such data accelerates calculation of
678 : colorability of given allocno. */
679 : struct allocno_hard_regs_subnode
680 : {
681 : /* The conflict size of conflicting allocnos whose hard register
682 : sets are equal sets (plus supersets if given node is given
683 : allocno hard registers node) of one in the given node. */
684 : int left_conflict_size;
685 : /* The summary conflict size of conflicting allocnos whose hard
686 : register sets are strict subsets of one in the given node.
687 : Overall conflict size is
688 : left_conflict_subnodes_size
689 : + MIN (max_node_impact - left_conflict_subnodes_size,
690 : left_conflict_size)
691 : */
692 : short left_conflict_subnodes_size;
693 : short max_node_impact;
694 : };
695 :
696 : /* Container for hard regs subnodes of all allocnos. */
697 : static allocno_hard_regs_subnode_t allocno_hard_regs_subnodes;
698 :
699 : /* Table (preorder number of allocno hard registers node in the
700 : forest, preorder number of allocno hard registers subnode) -> index
701 : of the subnode relative to the node. -1 if it is not a
702 : subnode. */
703 : static int *allocno_hard_regs_subnode_index;
704 :
705 : /* Setup arrays ALLOCNO_HARD_REGS_NODES and
706 : ALLOCNO_HARD_REGS_SUBNODE_INDEX. */
707 : static void
708 5664616 : setup_allocno_hard_regs_subnode_index (allocno_hard_regs_node_t first)
709 : {
710 5664616 : allocno_hard_regs_node_t node, parent;
711 5664616 : int index;
712 :
713 10088287 : for (node = first; node != NULL; node = node->next)
714 : {
715 4423671 : allocno_hard_regs_nodes[node->preorder_num] = node;
716 15816401 : for (parent = node; parent != NULL; parent = parent->parent)
717 : {
718 11392730 : index = parent->preorder_num * allocno_hard_regs_nodes_num;
719 11392730 : allocno_hard_regs_subnode_index[index + node->preorder_num]
720 11392730 : = node->preorder_num - parent->preorder_num;
721 : }
722 4423671 : setup_allocno_hard_regs_subnode_index (node->first);
723 : }
724 5664616 : }
725 :
726 : /* Count all allocno hard registers nodes in tree ROOT. */
727 : static int
728 70044847 : get_allocno_hard_regs_subnodes_num (allocno_hard_regs_node_t root)
729 : {
730 70044847 : int len = 1;
731 :
732 117108053 : for (root = root->first; root != NULL; root = root->next)
733 47063206 : len += get_allocno_hard_regs_subnodes_num (root);
734 70044847 : return len;
735 : }
736 :
737 : /* Build the forest of allocno hard registers nodes and assign each
738 : allocno a node from the forest. */
739 : static void
740 1240945 : form_allocno_hard_regs_nodes_forest (void)
741 : {
742 1240945 : unsigned int i, j, size, len;
743 1240945 : int start;
744 1240945 : ira_allocno_t a;
745 1240945 : allocno_hard_regs_t hv;
746 1240945 : bitmap_iterator bi;
747 1240945 : HARD_REG_SET temp;
748 1240945 : allocno_hard_regs_node_t node, allocno_hard_regs_node;
749 1240945 : allocno_color_data_t allocno_data;
750 :
751 1240945 : node_check_tick = 0;
752 1240945 : init_allocno_hard_regs ();
753 1240945 : hard_regs_roots = NULL;
754 1240945 : hard_regs_node_vec.create (100);
755 117889775 : for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
756 116648830 : if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
757 : {
758 56849360 : CLEAR_HARD_REG_SET (temp);
759 56849360 : SET_HARD_REG_BIT (temp, i);
760 56849360 : hv = add_allocno_hard_regs (temp, 0);
761 56849360 : node = create_new_allocno_hard_regs_node (hv);
762 112457775 : add_new_allocno_hard_regs_node_to_forest (&hard_regs_roots, node);
763 : }
764 1240945 : start = allocno_hard_regs_vec.length ();
765 26155548 : EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
766 : {
767 24914603 : a = ira_allocnos[i];
768 24914603 : allocno_data = ALLOCNO_COLOR_DATA (a);
769 :
770 49829206 : if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
771 1932962 : continue;
772 22981641 : hv = (add_allocno_hard_regs
773 22981641 : (allocno_data->profitable_hard_regs,
774 22981641 : ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a)));
775 : }
776 1240945 : temp = ~ira_no_alloc_regs;
777 1240945 : add_allocno_hard_regs (temp, 0);
778 3722835 : qsort (allocno_hard_regs_vec.address () + start,
779 : allocno_hard_regs_vec.length () - start,
780 : sizeof (allocno_hard_regs_t), allocno_hard_regs_compare);
781 1240945 : for (i = start;
782 7897482 : allocno_hard_regs_vec.iterate (i, &hv);
783 : i++)
784 : {
785 6656537 : add_allocno_hard_regs_to_forest (&hard_regs_roots, hv);
786 6656537 : ira_assert (hard_regs_node_vec.length () == 0);
787 : }
788 : /* We need to set up parent fields for right work of
789 : first_common_ancestor_node. */
790 1240945 : setup_allocno_hard_regs_nodes_parent (hard_regs_roots, NULL);
791 26155548 : EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
792 : {
793 24914603 : a = ira_allocnos[i];
794 24914603 : allocno_data = ALLOCNO_COLOR_DATA (a);
795 49829206 : if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
796 1932962 : continue;
797 22981641 : hard_regs_node_vec.truncate (0);
798 22981641 : collect_allocno_hard_regs_cover (hard_regs_roots,
799 : allocno_data->profitable_hard_regs);
800 22981641 : allocno_hard_regs_node = NULL;
801 70606659 : for (j = 0; hard_regs_node_vec.iterate (j, &node); j++)
802 24643377 : allocno_hard_regs_node
803 : = (j == 0
804 24643377 : ? node
805 1661736 : : first_common_ancestor_node (node, allocno_hard_regs_node));
806 : /* That is a temporary storage. */
807 22981641 : allocno_hard_regs_node->used_p = true;
808 22981641 : allocno_data->hard_regs_node = allocno_hard_regs_node;
809 : }
810 1240945 : ira_assert (hard_regs_roots->next == NULL);
811 1240945 : hard_regs_roots->used_p = true;
812 1240945 : remove_unused_allocno_hard_regs_nodes (&hard_regs_roots);
813 1240945 : allocno_hard_regs_nodes_num
814 1240945 : = enumerate_allocno_hard_regs_nodes (hard_regs_roots, NULL, 0);
815 1240945 : allocno_hard_regs_nodes
816 1240945 : = ((allocno_hard_regs_node_t *)
817 1240945 : ira_allocate (allocno_hard_regs_nodes_num
818 : * sizeof (allocno_hard_regs_node_t)));
819 1240945 : size = allocno_hard_regs_nodes_num * allocno_hard_regs_nodes_num;
820 1240945 : allocno_hard_regs_subnode_index
821 1240945 : = (int *) ira_allocate (size * sizeof (int));
822 22682816 : for (i = 0; i < size; i++)
823 21441871 : allocno_hard_regs_subnode_index[i] = -1;
824 1240945 : setup_allocno_hard_regs_subnode_index (hard_regs_roots);
825 1240945 : start = 0;
826 26155548 : EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
827 : {
828 24914603 : a = ira_allocnos[i];
829 24914603 : allocno_data = ALLOCNO_COLOR_DATA (a);
830 49829206 : if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
831 1932962 : continue;
832 22981641 : len = get_allocno_hard_regs_subnodes_num (allocno_data->hard_regs_node);
833 22981641 : allocno_data->hard_regs_subnodes_start = start;
834 22981641 : allocno_data->hard_regs_subnodes_num = len;
835 22981641 : start += len;
836 : }
837 1240945 : allocno_hard_regs_subnodes
838 1240945 : = ((allocno_hard_regs_subnode_t)
839 1240945 : ira_allocate (sizeof (struct allocno_hard_regs_subnode) * start));
840 1240945 : hard_regs_node_vec.release ();
841 1240945 : }
842 :
843 : /* Free tree of allocno hard registers nodes given by its ROOT. */
844 : static void
845 4423671 : finish_allocno_hard_regs_nodes_tree (allocno_hard_regs_node_t root)
846 : {
847 4423671 : allocno_hard_regs_node_t child, next;
848 :
849 7606397 : for (child = root->first; child != NULL; child = next)
850 : {
851 3182726 : next = child->next;
852 3182726 : finish_allocno_hard_regs_nodes_tree (child);
853 : }
854 4423671 : ira_free (root);
855 4423671 : }
856 :
857 : /* Finish work with the forest of allocno hard registers nodes. */
858 : static void
859 1240945 : finish_allocno_hard_regs_nodes_forest (void)
860 : {
861 1240945 : allocno_hard_regs_node_t node, next;
862 :
863 1240945 : ira_free (allocno_hard_regs_subnodes);
864 2481890 : for (node = hard_regs_roots; node != NULL; node = next)
865 : {
866 1240945 : next = node->next;
867 1240945 : finish_allocno_hard_regs_nodes_tree (node);
868 : }
869 1240945 : ira_free (allocno_hard_regs_nodes);
870 1240945 : ira_free (allocno_hard_regs_subnode_index);
871 1240945 : finish_allocno_hard_regs ();
872 1240945 : }
873 :
874 : /* Set up left conflict sizes and left conflict subnodes sizes of hard
875 : registers subnodes of allocno A. Return TRUE if allocno A is
876 : trivially colorable. */
877 : static bool
878 22981641 : setup_left_conflict_sizes_p (ira_allocno_t a)
879 : {
880 22981641 : int i, k, nobj, start;
881 22981641 : int conflict_size, left_conflict_subnodes_size, node_preorder_num;
882 22981641 : allocno_color_data_t data;
883 22981641 : HARD_REG_SET profitable_hard_regs;
884 22981641 : allocno_hard_regs_subnode_t subnodes;
885 22981641 : allocno_hard_regs_node_t node;
886 22981641 : HARD_REG_SET node_set;
887 :
888 22981641 : nobj = ALLOCNO_NUM_OBJECTS (a);
889 22981641 : data = ALLOCNO_COLOR_DATA (a);
890 22981641 : subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
891 22981641 : profitable_hard_regs = data->profitable_hard_regs;
892 22981641 : node = data->hard_regs_node;
893 22981641 : node_preorder_num = node->preorder_num;
894 22981641 : node_set = node->hard_regs->set;
895 22981641 : node_check_tick++;
896 46421190 : for (k = 0; k < nobj; k++)
897 : {
898 23439549 : ira_object_t obj = ALLOCNO_OBJECT (a, k);
899 23439549 : ira_object_t conflict_obj;
900 23439549 : ira_object_conflict_iterator oci;
901 :
902 512390251 : FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
903 : {
904 488950702 : int size;
905 488950702 : ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
906 488950702 : allocno_hard_regs_node_t conflict_node, temp_node;
907 488950702 : HARD_REG_SET conflict_node_set;
908 488950702 : allocno_color_data_t conflict_data;
909 :
910 488950702 : conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
911 548437256 : if (! ALLOCNO_COLOR_DATA (conflict_a)->in_graph_p
912 924477250 : || ! hard_reg_set_intersect_p (profitable_hard_regs,
913 : conflict_data
914 : ->profitable_hard_regs))
915 59486554 : continue;
916 429464148 : conflict_node = conflict_data->hard_regs_node;
917 429464148 : conflict_node_set = conflict_node->hard_regs->set;
918 858928296 : if (hard_reg_set_subset_p (node_set, conflict_node_set))
919 : temp_node = node;
920 : else
921 : {
922 109010940 : ira_assert (hard_reg_set_subset_p (conflict_node_set, node_set));
923 : temp_node = conflict_node;
924 : }
925 429464148 : if (temp_node->check != node_check_tick)
926 : {
927 40613958 : temp_node->check = node_check_tick;
928 40613958 : temp_node->conflict_size = 0;
929 : }
930 429464148 : size = (ira_reg_class_max_nregs
931 429464148 : [ALLOCNO_CLASS (conflict_a)][ALLOCNO_MODE (conflict_a)]);
932 429464148 : if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1)
933 : /* We will deal with the subwords individually. */
934 25412343 : size = 1;
935 429464148 : temp_node->conflict_size += size;
936 : }
937 : }
938 93026488 : for (i = 0; i < data->hard_regs_subnodes_num; i++)
939 : {
940 70044847 : allocno_hard_regs_node_t temp_node;
941 :
942 70044847 : temp_node = allocno_hard_regs_nodes[i + node_preorder_num];
943 70044847 : ira_assert (temp_node->preorder_num == i + node_preorder_num);
944 140089694 : subnodes[i].left_conflict_size = (temp_node->check != node_check_tick
945 70044847 : ? 0 : temp_node->conflict_size);
946 140089694 : if (hard_reg_set_subset_p (temp_node->hard_regs->set,
947 : profitable_hard_regs))
948 66701864 : subnodes[i].max_node_impact = temp_node->hard_regs_num;
949 : else
950 : {
951 3342983 : HARD_REG_SET temp_set;
952 3342983 : int j, n, hard_regno;
953 3342983 : enum reg_class aclass;
954 :
955 3342983 : temp_set = temp_node->hard_regs->set & profitable_hard_regs;
956 3342983 : aclass = ALLOCNO_CLASS (a);
957 58396710 : for (n = 0, j = ira_class_hard_regs_num[aclass] - 1; j >= 0; j--)
958 : {
959 55053727 : hard_regno = ira_class_hard_regs[aclass][j];
960 55053727 : if (TEST_HARD_REG_BIT (temp_set, hard_regno))
961 33440015 : n++;
962 : }
963 3342983 : subnodes[i].max_node_impact = n;
964 : }
965 70044847 : subnodes[i].left_conflict_subnodes_size = 0;
966 : }
967 22981641 : start = node_preorder_num * allocno_hard_regs_nodes_num;
968 70044847 : for (i = data->hard_regs_subnodes_num - 1; i > 0; i--)
969 : {
970 47063206 : int size, parent_i;
971 47063206 : allocno_hard_regs_node_t parent;
972 :
973 47063206 : size = (subnodes[i].left_conflict_subnodes_size
974 47063206 : + MIN (subnodes[i].max_node_impact
975 : - subnodes[i].left_conflict_subnodes_size,
976 : subnodes[i].left_conflict_size));
977 47063206 : parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
978 47063206 : gcc_checking_assert(parent);
979 47063206 : parent_i
980 47063206 : = allocno_hard_regs_subnode_index[start + parent->preorder_num];
981 47063206 : gcc_checking_assert(parent_i >= 0);
982 47063206 : subnodes[parent_i].left_conflict_subnodes_size += size;
983 : }
984 22981641 : left_conflict_subnodes_size = subnodes[0].left_conflict_subnodes_size;
985 22981641 : conflict_size
986 22981641 : = (left_conflict_subnodes_size
987 22981641 : + MIN (subnodes[0].max_node_impact - left_conflict_subnodes_size,
988 : subnodes[0].left_conflict_size));
989 22981641 : conflict_size += ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
990 22981641 : data->colorable_p = conflict_size <= data->available_regs_num;
991 22981641 : return data->colorable_p;
992 : }
993 :
994 : /* Update left conflict sizes of hard registers subnodes of allocno A
995 : after removing allocno REMOVED_A with SIZE from the conflict graph.
996 : Return TRUE if A is trivially colorable. */
997 : static bool
998 184618693 : update_left_conflict_sizes_p (ira_allocno_t a,
999 : ira_allocno_t removed_a, int size)
1000 : {
1001 184618693 : int i, conflict_size, before_conflict_size, diff, start;
1002 184618693 : int node_preorder_num, parent_i;
1003 184618693 : allocno_hard_regs_node_t node, removed_node, parent;
1004 184618693 : allocno_hard_regs_subnode_t subnodes;
1005 184618693 : allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
1006 :
1007 184618693 : ira_assert (! data->colorable_p);
1008 184618693 : node = data->hard_regs_node;
1009 184618693 : node_preorder_num = node->preorder_num;
1010 184618693 : removed_node = ALLOCNO_COLOR_DATA (removed_a)->hard_regs_node;
1011 446158675 : ira_assert (hard_reg_set_subset_p (removed_node->hard_regs->set,
1012 : node->hard_regs->set)
1013 : || hard_reg_set_subset_p (node->hard_regs->set,
1014 : removed_node->hard_regs->set));
1015 184618693 : start = node_preorder_num * allocno_hard_regs_nodes_num;
1016 184618693 : i = allocno_hard_regs_subnode_index[start + removed_node->preorder_num];
1017 184618693 : if (i < 0)
1018 : i = 0;
1019 184618693 : subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
1020 184618693 : before_conflict_size
1021 184618693 : = (subnodes[i].left_conflict_subnodes_size
1022 184618693 : + MIN (subnodes[i].max_node_impact
1023 : - subnodes[i].left_conflict_subnodes_size,
1024 : subnodes[i].left_conflict_size));
1025 184618693 : subnodes[i].left_conflict_size -= size;
1026 207878263 : for (;;)
1027 : {
1028 196248478 : conflict_size
1029 196248478 : = (subnodes[i].left_conflict_subnodes_size
1030 196248478 : + MIN (subnodes[i].max_node_impact
1031 : - subnodes[i].left_conflict_subnodes_size,
1032 : subnodes[i].left_conflict_size));
1033 196248478 : if ((diff = before_conflict_size - conflict_size) == 0)
1034 : break;
1035 17158096 : ira_assert (conflict_size < before_conflict_size);
1036 17158096 : parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
1037 17158096 : if (parent == NULL)
1038 : break;
1039 17156726 : parent_i
1040 17156726 : = allocno_hard_regs_subnode_index[start + parent->preorder_num];
1041 17156726 : if (parent_i < 0)
1042 : break;
1043 11629785 : i = parent_i;
1044 11629785 : before_conflict_size
1045 11629785 : = (subnodes[i].left_conflict_subnodes_size
1046 11629785 : + MIN (subnodes[i].max_node_impact
1047 : - subnodes[i].left_conflict_subnodes_size,
1048 : subnodes[i].left_conflict_size));
1049 11629785 : subnodes[i].left_conflict_subnodes_size -= diff;
1050 : }
1051 184618693 : if (i != 0
1052 167063146 : || (conflict_size
1053 167063146 : + ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
1054 167063146 : > data->available_regs_num))
1055 : return false;
1056 5395678 : data->colorable_p = true;
1057 5395678 : return true;
1058 : }
1059 :
1060 : /* Return true if allocno A has empty profitable hard regs. */
1061 : static bool
1062 73746688 : empty_profitable_hard_regs (ira_allocno_t a)
1063 : {
1064 73746688 : allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
1065 :
1066 48832482 : return hard_reg_set_empty_p (data->profitable_hard_regs);
1067 : }
1068 :
1069 : /* Set up profitable hard registers for each allocno being
1070 : colored. */
1071 : static void
1072 1240980 : setup_profitable_hard_regs (void)
1073 : {
1074 1240980 : unsigned int i;
1075 1240980 : int j, k, nobj, hard_regno, nregs, class_size;
1076 1240980 : ira_allocno_t a;
1077 1240980 : bitmap_iterator bi;
1078 1240980 : enum reg_class aclass;
1079 1240980 : machine_mode mode;
1080 1240980 : allocno_color_data_t data;
1081 :
1082 : /* Initial set up from allocno classes and explicitly conflicting
1083 : hard regs. */
1084 26156556 : EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1085 : {
1086 24915576 : a = ira_allocnos[i];
1087 24915576 : if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS)
1088 498860 : continue;
1089 24416716 : data = ALLOCNO_COLOR_DATA (a);
1090 24416716 : if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL
1091 23220900 : && ALLOCNO_CLASS_COST (a) > ALLOCNO_MEMORY_COST (a)
1092 : /* Do not empty profitable regs for static chain pointer
1093 : pseudo when non-local goto is used. */
1094 24533695 : && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1095 24915576 : CLEAR_HARD_REG_SET (data->profitable_hard_regs);
1096 : else
1097 : {
1098 24299737 : mode = ALLOCNO_MODE (a);
1099 24299737 : data->profitable_hard_regs
1100 24299737 : = ira_useful_class_mode_regs[aclass][mode];
1101 24299737 : nobj = ALLOCNO_NUM_OBJECTS (a);
1102 49090969 : for (k = 0; k < nobj; k++)
1103 : {
1104 24791232 : ira_object_t obj = ALLOCNO_OBJECT (a, k);
1105 :
1106 24791232 : data->profitable_hard_regs
1107 49582464 : &= ~OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
1108 : }
1109 : }
1110 : }
1111 : /* Exclude hard regs already assigned for conflicting objects. */
1112 27172026 : EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, i, bi)
1113 : {
1114 25931046 : a = ira_allocnos[i];
1115 51267754 : if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1116 25258811 : || ! ALLOCNO_ASSIGNED_P (a)
1117 26773141 : || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1118 25336708 : continue;
1119 594338 : mode = ALLOCNO_MODE (a);
1120 594338 : nregs = hard_regno_nregs (hard_regno, mode);
1121 594338 : nobj = ALLOCNO_NUM_OBJECTS (a);
1122 1197368 : for (k = 0; k < nobj; k++)
1123 : {
1124 603030 : ira_object_t obj = ALLOCNO_OBJECT (a, k);
1125 603030 : ira_object_t conflict_obj;
1126 603030 : ira_object_conflict_iterator oci;
1127 :
1128 7269963 : FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1129 : {
1130 6666933 : ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1131 :
1132 : /* We can process the conflict allocno repeatedly with
1133 : the same result. */
1134 6666933 : if (nregs == nobj && nregs > 1)
1135 : {
1136 399433 : int num = OBJECT_SUBWORD (conflict_obj);
1137 :
1138 399433 : if (REG_WORDS_BIG_ENDIAN)
1139 : CLEAR_HARD_REG_BIT
1140 : (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1141 : hard_regno + nobj - num - 1);
1142 : else
1143 399433 : CLEAR_HARD_REG_BIT
1144 399433 : (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1145 399433 : hard_regno + num);
1146 : }
1147 : else
1148 6267500 : ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs
1149 12535000 : &= ~ira_reg_mode_hard_regset[hard_regno][mode];
1150 : }
1151 : }
1152 : }
1153 : /* Exclude too costly hard regs. */
1154 26156556 : EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1155 : {
1156 24915576 : int min_cost = INT_MAX;
1157 24915576 : int *costs;
1158 :
1159 24915576 : a = ira_allocnos[i];
1160 25549905 : if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1161 49332292 : || empty_profitable_hard_regs (a))
1162 634329 : continue;
1163 24281247 : data = ALLOCNO_COLOR_DATA (a);
1164 24281247 : if ((costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a)) != NULL
1165 24281247 : || (costs = ALLOCNO_HARD_REG_COSTS (a)) != NULL)
1166 : {
1167 10014436 : class_size = ira_class_hard_regs_num[aclass];
1168 163955806 : for (j = 0; j < class_size; j++)
1169 : {
1170 153941370 : hard_regno = ira_class_hard_regs[aclass][j];
1171 153941370 : if (! TEST_HARD_REG_BIT (data->profitable_hard_regs,
1172 : hard_regno))
1173 16071603 : continue;
1174 137869767 : if (ALLOCNO_UPDATED_MEMORY_COST (a) < costs[j]
1175 : /* Do not remove HARD_REGNO for static chain pointer
1176 : pseudo when non-local goto is used. */
1177 137869767 : && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1178 10462298 : CLEAR_HARD_REG_BIT (data->profitable_hard_regs,
1179 : hard_regno);
1180 127407469 : else if (min_cost > costs[j])
1181 153941370 : min_cost = costs[j];
1182 : }
1183 : }
1184 14266811 : else if (ALLOCNO_UPDATED_MEMORY_COST (a)
1185 14266811 : < ALLOCNO_UPDATED_CLASS_COST (a)
1186 : /* Do not empty profitable regs for static chain
1187 : pointer pseudo when non-local goto is used. */
1188 14266811 : && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1189 24915576 : CLEAR_HARD_REG_SET (data->profitable_hard_regs);
1190 10947487 : if (ALLOCNO_UPDATED_CLASS_COST (a) > min_cost)
1191 55174 : ALLOCNO_UPDATED_CLASS_COST (a) = min_cost;
1192 : }
1193 1240980 : }
1194 :
1195 :
1196 :
1197 : /* This page contains functions used to choose hard registers for
1198 : allocnos. */
1199 :
1200 : /* Pool for update cost records. */
1201 : static object_allocator<update_cost_record> update_cost_record_pool
1202 : ("update cost records");
1203 :
1204 : /* Return new update cost record with given params. */
1205 : static struct update_cost_record *
1206 8617117 : get_update_cost_record (int hard_regno, int divisor,
1207 : struct update_cost_record *next)
1208 : {
1209 8617117 : struct update_cost_record *record;
1210 :
1211 0 : record = update_cost_record_pool.allocate ();
1212 8617117 : record->hard_regno = hard_regno;
1213 8617117 : record->divisor = divisor;
1214 8617117 : record->next = next;
1215 8617117 : return record;
1216 : }
1217 :
1218 : /* Free memory for all records in LIST. */
1219 : static void
1220 23267679 : free_update_cost_record_list (struct update_cost_record *list)
1221 : {
1222 23267679 : struct update_cost_record *next;
1223 :
1224 31884796 : while (list != NULL)
1225 : {
1226 8617117 : next = list->next;
1227 8617117 : update_cost_record_pool.remove (list);
1228 8617117 : list = next;
1229 : }
1230 23267679 : }
1231 :
1232 : /* Free memory allocated for all update cost records. */
1233 : static void
1234 1070228 : finish_update_cost_records (void)
1235 : {
1236 0 : update_cost_record_pool.release ();
1237 0 : }
1238 :
1239 : /* True if we have allocated memory, or intend to do so. */
1240 : static bool allocated_memory_p;
1241 :
1242 : /* Array whose element value is TRUE if the corresponding hard
1243 : register was already allocated for an allocno. */
1244 : static bool allocated_hardreg_p[FIRST_PSEUDO_REGISTER];
1245 :
1246 : /* Which callee-saved hard registers we've decided to save. */
1247 : static HARD_REG_SET allocated_callee_save_regs;
1248 :
1249 : /* Describes one element in a queue of allocnos whose costs need to be
1250 : updated. Each allocno in the queue is known to have an allocno
1251 : class. */
1252 : struct update_cost_queue_elem
1253 : {
1254 : /* This element is in the queue iff CHECK == update_cost_check. */
1255 : int check;
1256 :
1257 : /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
1258 : connecting this allocno to the one being allocated. */
1259 : int divisor;
1260 :
1261 : /* Allocno from which we started chaining costs of connected
1262 : allocnos. */
1263 : ira_allocno_t start;
1264 :
1265 : /* Allocno from which we are chaining costs of connected allocnos.
1266 : It is used not go back in graph of allocnos connected by
1267 : copies. */
1268 : ira_allocno_t from;
1269 :
1270 : /* The next allocno in the queue, or null if this is the last element. */
1271 : ira_allocno_t next;
1272 : };
1273 :
1274 : /* The first element in a queue of allocnos whose copy costs need to be
1275 : updated. Null if the queue is empty. */
1276 : static ira_allocno_t update_cost_queue;
1277 :
1278 : /* The last element in the queue described by update_cost_queue.
1279 : Not valid if update_cost_queue is null. */
1280 : static struct update_cost_queue_elem *update_cost_queue_tail;
1281 :
1282 : /* A pool of elements in the queue described by update_cost_queue.
1283 : Elements are indexed by ALLOCNO_NUM. */
1284 : static struct update_cost_queue_elem *update_cost_queue_elems;
1285 :
1286 : /* The current value of update_costs_from_copies call count. */
1287 : static int update_cost_check;
1288 :
1289 : /* Allocate and initialize data necessary for function
1290 : update_costs_from_copies. */
1291 : static void
1292 1070228 : initiate_cost_update (void)
1293 : {
1294 1070228 : size_t size;
1295 :
1296 1070228 : size = ira_allocnos_num * sizeof (struct update_cost_queue_elem);
1297 1070228 : update_cost_queue_elems
1298 1070228 : = (struct update_cost_queue_elem *) ira_allocate (size);
1299 1070228 : memset (update_cost_queue_elems, 0, size);
1300 1070228 : update_cost_check = 0;
1301 1070228 : }
1302 :
1303 : /* Deallocate data used by function update_costs_from_copies. */
1304 : static void
1305 1070228 : finish_cost_update (void)
1306 : {
1307 1070228 : ira_free (update_cost_queue_elems);
1308 1070228 : finish_update_cost_records ();
1309 1070228 : }
1310 :
1311 : /* When we traverse allocnos to update hard register costs, the cost
1312 : divisor will be multiplied by the following macro value for each
1313 : hop from given allocno to directly connected allocnos. */
1314 : #define COST_HOP_DIVISOR 4
1315 :
1316 : /* Start a new cost-updating pass. */
1317 : static void
1318 109416911 : start_update_cost (void)
1319 : {
1320 109416911 : update_cost_check++;
1321 109416911 : update_cost_queue = NULL;
1322 0 : }
1323 :
1324 : /* Add (ALLOCNO, START, FROM, DIVISOR) to the end of update_cost_queue, unless
1325 : ALLOCNO is already in the queue, or has NO_REGS class. */
1326 : static inline void
1327 199358362 : queue_update_cost (ira_allocno_t allocno, ira_allocno_t start,
1328 : ira_allocno_t from, int divisor)
1329 : {
1330 199358362 : struct update_cost_queue_elem *elem;
1331 :
1332 199358362 : elem = &update_cost_queue_elems[ALLOCNO_NUM (allocno)];
1333 199358362 : if (elem->check != update_cost_check
1334 146733449 : && ALLOCNO_CLASS (allocno) != NO_REGS)
1335 : {
1336 146733449 : elem->check = update_cost_check;
1337 146733449 : elem->start = start;
1338 146733449 : elem->from = from;
1339 146733449 : elem->divisor = divisor;
1340 146733449 : elem->next = NULL;
1341 146733449 : if (update_cost_queue == NULL)
1342 51038085 : update_cost_queue = allocno;
1343 : else
1344 95695364 : update_cost_queue_tail->next = allocno;
1345 146733449 : update_cost_queue_tail = elem;
1346 : }
1347 199358362 : }
1348 :
1349 : /* Try to remove the first element from update_cost_queue. Return
1350 : false if the queue was empty, otherwise make (*ALLOCNO, *START,
1351 : *FROM, *DIVISOR) describe the removed element. */
1352 : static inline bool
1353 216377840 : get_next_update_cost (ira_allocno_t *allocno, ira_allocno_t *start,
1354 : ira_allocno_t *from, int *divisor)
1355 : {
1356 216377840 : struct update_cost_queue_elem *elem;
1357 :
1358 216377840 : if (update_cost_queue == NULL)
1359 : return false;
1360 :
1361 137139290 : *allocno = update_cost_queue;
1362 137139290 : elem = &update_cost_queue_elems[ALLOCNO_NUM (*allocno)];
1363 137139290 : *start = elem->start;
1364 137139290 : *from = elem->from;
1365 137139290 : *divisor = elem->divisor;
1366 137139290 : update_cost_queue = elem->next;
1367 137139290 : return true;
1368 : }
1369 :
1370 : /* Increase costs of HARD_REGNO by UPDATE_COST and conflict cost by
1371 : UPDATE_CONFLICT_COST for ALLOCNO. Return true if we really
1372 : modified the cost. */
1373 : static bool
1374 13057868 : update_allocno_cost (ira_allocno_t allocno, int hard_regno,
1375 : int update_cost, int update_conflict_cost)
1376 : {
1377 13057868 : int i;
1378 13057868 : enum reg_class aclass = ALLOCNO_CLASS (allocno);
1379 :
1380 13057868 : i = ira_class_hard_reg_index[aclass][hard_regno];
1381 13057868 : if (i < 0)
1382 : return false;
1383 13057868 : ira_allocate_and_set_or_copy_costs
1384 13057868 : (&ALLOCNO_UPDATED_HARD_REG_COSTS (allocno), aclass,
1385 : ALLOCNO_UPDATED_CLASS_COST (allocno),
1386 : ALLOCNO_HARD_REG_COSTS (allocno));
1387 13057868 : ira_allocate_and_set_or_copy_costs
1388 13057868 : (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno),
1389 : aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (allocno));
1390 13057868 : ALLOCNO_UPDATED_HARD_REG_COSTS (allocno)[i] += update_cost;
1391 13057868 : ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno)[i] += update_conflict_cost;
1392 13057868 : return true;
1393 : }
1394 :
1395 : /* Return TRUE if the object OBJ conflicts with the allocno A. */
1396 : static bool
1397 80085003 : object_conflicts_with_allocno_p (ira_object_t obj, ira_allocno_t a)
1398 : {
1399 80085003 : if (!OBJECT_CONFLICT_VEC_P (obj))
1400 126124791 : for (int word = 0; word < ALLOCNO_NUM_OBJECTS (a); word++)
1401 : {
1402 64402802 : ira_object_t another_obj = ALLOCNO_OBJECT (a, word);
1403 64402802 : if (OBJECT_CONFLICT_ID (another_obj) >= OBJECT_MIN (obj)
1404 61895026 : && OBJECT_CONFLICT_ID (another_obj) <= OBJECT_MAX (obj)
1405 105523723 : && TEST_MINMAX_SET_BIT (OBJECT_CONFLICT_BITVEC (obj),
1406 : OBJECT_CONFLICT_ID (another_obj),
1407 : OBJECT_MIN (obj), OBJECT_MAX (obj)))
1408 : return true;
1409 : }
1410 : else
1411 : {
1412 : /* If this linear walk ever becomes a bottleneck we could add a
1413 : conflict_vec_sorted_p flag and if not set, sort the conflicts after
1414 : their ID so we can use a binary search. That would also require
1415 : tracking the actual number of conflicts in the vector to not rely
1416 : on the NULL termination. */
1417 17129248 : ira_object_conflict_iterator oci;
1418 17129248 : ira_object_t conflict_obj;
1419 498149454 : FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1420 481524515 : if (OBJECT_ALLOCNO (conflict_obj) == a)
1421 504309 : return true;
1422 : }
1423 : return false;
1424 : }
1425 :
1426 : /* Return TRUE if allocnos A1 and A2 conflicts. Here we are
1427 : interested only in conflicts of allocnos with intersecting allocno
1428 : classes. */
1429 : static bool
1430 79210430 : allocnos_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
1431 : {
1432 : /* Compute the upper bound for the linear iteration when the object
1433 : conflicts are represented as a sparse vector. In particular this
1434 : will make sure we prefer O(1) bitvector testing. */
1435 79210430 : int num_conflicts_in_vec1 = 0, num_conflicts_in_vec2 = 0;
1436 159401001 : for (int word = 0; word < ALLOCNO_NUM_OBJECTS (a1); ++word)
1437 80190571 : if (OBJECT_CONFLICT_VEC_P (ALLOCNO_OBJECT (a1, word)))
1438 18834544 : num_conflicts_in_vec1 += OBJECT_NUM_CONFLICTS (ALLOCNO_OBJECT (a1, word));
1439 159496812 : for (int word = 0; word < ALLOCNO_NUM_OBJECTS (a2); ++word)
1440 80286382 : if (OBJECT_CONFLICT_VEC_P (ALLOCNO_OBJECT (a2, word)))
1441 17685172 : num_conflicts_in_vec2 += OBJECT_NUM_CONFLICTS (ALLOCNO_OBJECT (a2, word));
1442 79210430 : if (num_conflicts_in_vec2 < num_conflicts_in_vec1)
1443 5486539 : std::swap (a1, a2);
1444 :
1445 157557358 : for (int word = 0; word < ALLOCNO_NUM_OBJECTS (a1); word++)
1446 : {
1447 80085003 : ira_object_t obj = ALLOCNO_OBJECT (a1, word);
1448 : /* Take preferences of conflicting allocnos into account. */
1449 80085003 : if (object_conflicts_with_allocno_p (obj, a2))
1450 : return true;
1451 : }
1452 : return false;
1453 : }
1454 :
1455 : /* Update (decrease if DECR_P) HARD_REGNO cost of allocnos connected
1456 : by copies to ALLOCNO to increase chances to remove some copies as
1457 : the result of subsequent assignment. Update conflict costs.
1458 : Record cost updates if RECORD_P is true. */
1459 : static void
1460 35172714 : update_costs_from_allocno (ira_allocno_t allocno, int hard_regno,
1461 : int divisor, bool decr_p, bool record_p)
1462 : {
1463 35172714 : int cost, update_cost, update_conflict_cost;
1464 35172714 : machine_mode mode;
1465 35172714 : enum reg_class rclass, aclass;
1466 35172714 : ira_allocno_t another_allocno, start = allocno, from = NULL;
1467 35172714 : ira_copy_t cp, next_cp;
1468 :
1469 35172714 : rclass = REGNO_REG_CLASS (hard_regno);
1470 46930138 : do
1471 : {
1472 46930138 : mode = ALLOCNO_MODE (allocno);
1473 46930138 : ira_init_register_move_cost_if_necessary (mode);
1474 95232112 : for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1475 : {
1476 48301974 : if (cp->first == allocno)
1477 : {
1478 22262705 : next_cp = cp->next_first_allocno_copy;
1479 22262705 : another_allocno = cp->second;
1480 : }
1481 26039269 : else if (cp->second == allocno)
1482 : {
1483 26039269 : next_cp = cp->next_second_allocno_copy;
1484 26039269 : another_allocno = cp->first;
1485 : }
1486 : else
1487 0 : gcc_unreachable ();
1488 :
1489 48301974 : if (another_allocno == from
1490 36508214 : || (ALLOCNO_COLOR_DATA (another_allocno) != NULL
1491 35892260 : && (ALLOCNO_COLOR_DATA (allocno)->first_thread_allocno
1492 35892260 : != ALLOCNO_COLOR_DATA (another_allocno)->first_thread_allocno)))
1493 18026242 : continue;
1494 :
1495 30275732 : aclass = ALLOCNO_CLASS (another_allocno);
1496 30275732 : if (! TEST_HARD_REG_BIT (reg_class_contents[aclass],
1497 : hard_regno)
1498 30275732 : || ALLOCNO_ASSIGNED_P (another_allocno))
1499 16215164 : continue;
1500 :
1501 : /* If we have different modes use the smallest one. It is
1502 : a sub-register move. It is hard to predict what LRA
1503 : will reload (the pseudo or its sub-register) but LRA
1504 : will try to minimize the data movement. Also for some
1505 : register classes bigger modes might be invalid,
1506 : e.g. DImode for AREG on x86. For such cases the
1507 : register move cost will be maximal. */
1508 28121136 : mode = narrower_subreg_mode (ALLOCNO_MODE (cp->first),
1509 14060568 : ALLOCNO_MODE (cp->second));
1510 :
1511 14060568 : ira_init_register_move_cost_if_necessary (mode);
1512 :
1513 28121136 : cost = (cp->second == allocno
1514 14060568 : ? ira_register_move_cost[mode][rclass][aclass]
1515 10568567 : : ira_register_move_cost[mode][aclass][rclass]);
1516 14060568 : if (decr_p)
1517 14060568 : cost = -cost;
1518 :
1519 14060568 : update_cost = cp->freq * cost / divisor;
1520 14060568 : update_conflict_cost = update_cost;
1521 :
1522 14060568 : if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
1523 0 : fprintf (ira_dump_file,
1524 : " a%dr%d (hr%d): update cost by %d, conflict cost by %d\n",
1525 : ALLOCNO_NUM (another_allocno), ALLOCNO_REGNO (another_allocno),
1526 : hard_regno, update_cost, update_conflict_cost);
1527 14060568 : if (update_cost == 0)
1528 1002700 : continue;
1529 :
1530 13057868 : if (! update_allocno_cost (another_allocno, hard_regno,
1531 : update_cost, update_conflict_cost))
1532 0 : continue;
1533 13057868 : queue_update_cost (another_allocno, start, allocno,
1534 : divisor * COST_HOP_DIVISOR);
1535 13057868 : if (record_p && ALLOCNO_COLOR_DATA (another_allocno) != NULL)
1536 8617117 : ALLOCNO_COLOR_DATA (another_allocno)->update_cost_records
1537 8617117 : = get_update_cost_record (hard_regno, divisor,
1538 : ALLOCNO_COLOR_DATA (another_allocno)
1539 : ->update_cost_records);
1540 : }
1541 : }
1542 46930138 : while (get_next_update_cost (&allocno, &start, &from, &divisor));
1543 35172714 : }
1544 :
1545 : /* Decrease preferred ALLOCNO hard register costs and costs of
1546 : allocnos connected to ALLOCNO through copy. */
1547 : static void
1548 18483198 : update_costs_from_prefs (ira_allocno_t allocno)
1549 : {
1550 18483198 : ira_pref_t pref;
1551 :
1552 18483198 : start_update_cost ();
1553 22673358 : for (pref = ALLOCNO_PREFS (allocno); pref != NULL; pref = pref->next_pref)
1554 : {
1555 4190160 : if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
1556 0 : fprintf (ira_dump_file, " Start updating from pref of hr%d for a%dr%d:\n",
1557 : pref->hard_regno, ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno));
1558 4190160 : update_costs_from_allocno (allocno, pref->hard_regno,
1559 : COST_HOP_DIVISOR, true, true);
1560 : }
1561 18483198 : }
1562 :
1563 : /* Update (decrease if DECR_P) the cost of allocnos connected to
1564 : ALLOCNO through copies to increase chances to remove some copies as
1565 : the result of subsequent assignment. ALLOCNO was just assigned to
1566 : a hard register. Record cost updates if RECORD_P is true. */
1567 : static void
1568 22365437 : update_costs_from_copies (ira_allocno_t allocno, bool decr_p, bool record_p)
1569 : {
1570 22365437 : int hard_regno;
1571 :
1572 22365437 : hard_regno = ALLOCNO_HARD_REGNO (allocno);
1573 22365437 : ira_assert (hard_regno >= 0 && ALLOCNO_CLASS (allocno) != NO_REGS);
1574 22365437 : start_update_cost ();
1575 22365437 : if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
1576 0 : fprintf (ira_dump_file, " Start updating from a%dr%d by copies:\n",
1577 : ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno));
1578 22365437 : update_costs_from_allocno (allocno, hard_regno, 1, decr_p, record_p);
1579 22365437 : }
1580 :
1581 : /* Update conflict_allocno_hard_prefs of allocnos conflicting with
1582 : ALLOCNO. */
1583 : static void
1584 22981641 : update_conflict_allocno_hard_prefs (ira_allocno_t allocno)
1585 : {
1586 22981641 : int l, nr = ALLOCNO_NUM_OBJECTS (allocno);
1587 :
1588 46421190 : for (l = 0; l < nr; l++)
1589 : {
1590 23439549 : ira_object_t conflict_obj, obj = ALLOCNO_OBJECT (allocno, l);
1591 23439549 : ira_object_conflict_iterator oci;
1592 :
1593 512390251 : FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1594 : {
1595 488950702 : ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1596 488950702 : allocno_color_data_t conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
1597 488950702 : ira_pref_t pref;
1598 :
1599 1037387958 : if (!(hard_reg_set_intersect_p
1600 977901404 : (ALLOCNO_COLOR_DATA (allocno)->profitable_hard_regs,
1601 : conflict_data->profitable_hard_regs)))
1602 59486554 : continue;
1603 429464148 : for (pref = ALLOCNO_PREFS (allocno);
1604 457856664 : pref != NULL;
1605 28392516 : pref = pref->next_pref)
1606 28392516 : conflict_data->conflict_allocno_hard_prefs += pref->freq;
1607 : }
1608 : }
1609 22981641 : }
1610 :
1611 : /* Restore costs of allocnos connected to ALLOCNO by copies as it was
1612 : before updating costs of these allocnos from given allocno. This
1613 : is a wise thing to do as if given allocno did not get an expected
1614 : hard reg, using smaller cost of the hard reg for allocnos connected
1615 : by copies to given allocno becomes actually misleading. Free all
1616 : update cost records for ALLOCNO as we don't need them anymore. */
1617 : static void
1618 23267679 : restore_costs_from_copies (ira_allocno_t allocno)
1619 : {
1620 23267679 : struct update_cost_record *records, *curr;
1621 :
1622 23267679 : if (ALLOCNO_COLOR_DATA (allocno) == NULL)
1623 : return;
1624 23267679 : records = ALLOCNO_COLOR_DATA (allocno)->update_cost_records;
1625 23267679 : start_update_cost ();
1626 23267679 : if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
1627 0 : fprintf (ira_dump_file, " Start restoring from a%dr%d:\n",
1628 : ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno));
1629 31884796 : for (curr = records; curr != NULL; curr = curr->next)
1630 8617117 : update_costs_from_allocno (allocno, curr->hard_regno,
1631 : curr->divisor, true, false);
1632 23267679 : free_update_cost_record_list (records);
1633 23267679 : ALLOCNO_COLOR_DATA (allocno)->update_cost_records = NULL;
1634 : }
1635 :
1636 : /* This function updates COSTS (decrease if DECR_P) for hard_registers
1637 : of ACLASS by conflict costs of the unassigned allocnos
1638 : connected by copies with allocnos in update_cost_queue. This
1639 : update increases chances to remove some copies. */
1640 : static void
1641 44065836 : update_conflict_hard_regno_costs (int *costs, enum reg_class aclass,
1642 : bool decr_p)
1643 : {
1644 44065836 : int i, cost, class_size, freq, mult, div, divisor;
1645 44065836 : int index, hard_regno;
1646 44065836 : int *conflict_costs;
1647 44065836 : bool cont_p;
1648 44065836 : enum reg_class another_aclass;
1649 44065836 : ira_allocno_t allocno, another_allocno, start, from;
1650 44065836 : ira_copy_t cp, next_cp;
1651 :
1652 169447702 : while (get_next_update_cost (&allocno, &start, &from, &divisor))
1653 226734848 : for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1654 : {
1655 101352982 : if (cp->first == allocno)
1656 : {
1657 49050456 : next_cp = cp->next_first_allocno_copy;
1658 49050456 : another_allocno = cp->second;
1659 : }
1660 52302526 : else if (cp->second == allocno)
1661 : {
1662 52302526 : next_cp = cp->next_second_allocno_copy;
1663 52302526 : another_allocno = cp->first;
1664 : }
1665 : else
1666 0 : gcc_unreachable ();
1667 :
1668 101352982 : another_aclass = ALLOCNO_CLASS (another_allocno);
1669 101352982 : if (another_allocno == from
1670 101352982 : || ALLOCNO_ASSIGNED_P (another_allocno)
1671 83702821 : || ALLOCNO_COLOR_DATA (another_allocno)->may_be_spilled_p
1672 80621576 : || ! ira_reg_classes_intersect_p[aclass][another_aclass])
1673 22142552 : continue;
1674 79210430 : if (allocnos_conflict_p (another_allocno, start))
1675 1738075 : continue;
1676 :
1677 77472355 : class_size = ira_class_hard_regs_num[another_aclass];
1678 77472355 : ira_allocate_and_copy_costs
1679 77472355 : (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
1680 : another_aclass, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
1681 77472355 : conflict_costs
1682 77472355 : = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno);
1683 77472355 : if (conflict_costs == NULL)
1684 : cont_p = true;
1685 : else
1686 : {
1687 16708635 : mult = cp->freq;
1688 16708635 : freq = ALLOCNO_FREQ (another_allocno);
1689 16708635 : if (freq == 0)
1690 0 : freq = 1;
1691 16708635 : div = freq * divisor;
1692 16708635 : cont_p = false;
1693 301483968 : for (i = class_size - 1; i >= 0; i--)
1694 : {
1695 284775333 : hard_regno = ira_class_hard_regs[another_aclass][i];
1696 284775333 : ira_assert (hard_regno >= 0);
1697 284775333 : index = ira_class_hard_reg_index[aclass][hard_regno];
1698 284775333 : if (index < 0)
1699 22324494 : continue;
1700 262450839 : cost = (int) (((int64_t) conflict_costs [i] * mult) / div);
1701 262450839 : if (cost == 0)
1702 252269733 : continue;
1703 10181106 : cont_p = true;
1704 10181106 : if (decr_p)
1705 6520174 : cost = -cost;
1706 10181106 : costs[index] += cost;
1707 : }
1708 : }
1709 : /* Probably 5 hops will be enough. */
1710 16708635 : if (cont_p
1711 70175963 : && divisor <= (COST_HOP_DIVISOR
1712 : * COST_HOP_DIVISOR
1713 : * COST_HOP_DIVISOR
1714 : * COST_HOP_DIVISOR))
1715 68488542 : queue_update_cost (another_allocno, start, from, divisor * COST_HOP_DIVISOR);
1716 : }
1717 44065836 : }
1718 :
1719 : /* Set up conflicting (through CONFLICT_REGS) for each object of
1720 : allocno A and the start allocno profitable regs (through
1721 : START_PROFITABLE_REGS). Remember that the start profitable regs
1722 : exclude hard regs which cannot hold value of mode of allocno A.
1723 : This covers mostly cases when multi-register value should be
1724 : aligned. */
1725 : static inline void
1726 32613033 : get_conflict_and_start_profitable_regs (ira_allocno_t a, bool retry_p,
1727 : HARD_REG_SET *conflict_regs,
1728 : HARD_REG_SET *start_profitable_regs)
1729 : {
1730 32613033 : int i, nwords;
1731 32613033 : ira_object_t obj;
1732 :
1733 32613033 : nwords = ALLOCNO_NUM_OBJECTS (a);
1734 66150260 : for (i = 0; i < nwords; i++)
1735 : {
1736 33537227 : obj = ALLOCNO_OBJECT (a, i);
1737 33537227 : conflict_regs[i] = OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
1738 : }
1739 32613033 : if (retry_p)
1740 0 : *start_profitable_regs
1741 0 : = (reg_class_contents[ALLOCNO_CLASS (a)]
1742 0 : &~ (ira_prohibited_class_mode_regs
1743 0 : [ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]));
1744 : else
1745 32613033 : *start_profitable_regs = ALLOCNO_COLOR_DATA (a)->profitable_hard_regs;
1746 32613033 : }
1747 :
1748 : /* Return true if HARD_REGNO is ok for assigning to allocno A with
1749 : PROFITABLE_REGS and whose objects have CONFLICT_REGS. */
1750 : static inline bool
1751 598922321 : check_hard_reg_p (ira_allocno_t a, int hard_regno,
1752 : HARD_REG_SET *conflict_regs, HARD_REG_SET profitable_regs)
1753 : {
1754 598922321 : int j, nwords, nregs;
1755 598922321 : enum reg_class aclass;
1756 598922321 : machine_mode mode;
1757 :
1758 598922321 : aclass = ALLOCNO_CLASS (a);
1759 598922321 : mode = ALLOCNO_MODE (a);
1760 598922321 : if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[aclass][mode],
1761 : hard_regno))
1762 : return false;
1763 : /* Checking only profitable hard regs. */
1764 598307886 : if (! TEST_HARD_REG_BIT (profitable_regs, hard_regno))
1765 : return false;
1766 502083402 : nregs = hard_regno_nregs (hard_regno, mode);
1767 502083402 : nwords = ALLOCNO_NUM_OBJECTS (a);
1768 937016237 : for (j = 0; j < nregs; j++)
1769 : {
1770 512966741 : int k;
1771 512966741 : int set_to_test_start = 0, set_to_test_end = nwords;
1772 :
1773 512966741 : if (nregs == nwords)
1774 : {
1775 512297034 : if (REG_WORDS_BIG_ENDIAN)
1776 : set_to_test_start = nwords - j - 1;
1777 : else
1778 512297034 : set_to_test_start = j;
1779 512297034 : set_to_test_end = set_to_test_start + 1;
1780 : }
1781 948541317 : for (k = set_to_test_start; k < set_to_test_end; k++)
1782 513608482 : if (TEST_HARD_REG_BIT (conflict_regs[k], hard_regno + j))
1783 : break;
1784 512966741 : if (k != set_to_test_end)
1785 : break;
1786 : }
1787 502083402 : return j == nregs;
1788 : }
1789 :
1790 : /* Record that we have allocated NREGS registers starting at HARD_REGNO. */
1791 :
1792 : static void
1793 22133249 : record_allocation (int hard_regno, int nregs)
1794 : {
1795 44650437 : for (int i = 0; i < nregs; ++i)
1796 22517188 : if (!allocated_hardreg_p[hard_regno + i])
1797 : {
1798 4639178 : allocated_hardreg_p[hard_regno + i] = true;
1799 4639178 : if (!crtl->abi->clobbers_full_reg_p (hard_regno + i))
1800 992535 : SET_HARD_REG_BIT (allocated_callee_save_regs, hard_regno + i);
1801 : }
1802 22133249 : }
1803 :
1804 : /* Return number of registers needed to be saved and restored at
1805 : function prologue/epilogue if we allocate HARD_REGNO to hold value
1806 : of MODE. */
1807 : static int
1808 337361783 : calculate_saved_nregs (int hard_regno, machine_mode mode)
1809 : {
1810 337361783 : int i;
1811 337361783 : int nregs = 0;
1812 :
1813 337361783 : ira_assert (hard_regno >= 0);
1814 680699107 : for (i = hard_regno_nregs (hard_regno, mode) - 1; i >= 0; i--)
1815 343337324 : if (!allocated_hardreg_p[hard_regno + i]
1816 187602181 : && ira_hard_regno_nrefs[hard_regno + i] == 0
1817 92629089 : && !crtl->abi->clobbers_full_reg_p (hard_regno + i)
1818 343337324 : && !LOCAL_REGNO (hard_regno + i))
1819 61681814 : nregs++;
1820 337361783 : return nregs;
1821 : }
1822 :
1823 : /* Return the cost of saving and restoring SAVED_NREGS callee-saved
1824 : registers in the prologue and epilogue if we allocate HARD_REGNO to
1825 : hold a value of MODE, given the callee-saved registers allocated so
1826 : far. The cost is TARGET_CALLEE_SAVE_COST weighted by the entry and
1827 : exit block frequencies. */
1828 : static int
1829 60919360 : calculate_saved_regs_cost (int hard_regno, machine_mode mode, int saved_nregs)
1830 : {
1831 60919360 : int nregs = hard_regno_nregs (hard_regno, mode);
1832 60919360 : enum reg_class rclass = REGNO_REG_CLASS (hard_regno);
1833 60919360 : bool existing_spills_p = allocated_memory_p || caller_save_needed;
1834 :
1835 60919360 : auto entry_cost = targetm.callee_save_cost
1836 121838720 : (spill_cost_type::SAVE, hard_regno, mode, saved_nregs,
1837 60919360 : ira_memory_move_cost[mode][rclass][0] * saved_nregs / nregs,
1838 : allocated_callee_save_regs, existing_spills_p);
1839 : /* In the event of a tie between caller-save and callee-save,
1840 : prefer callee-save. We apply this to the entry cost rather
1841 : than the exit cost since the entry frequency must be at
1842 : least as high as the exit frequency. */
1843 60919360 : if (entry_cost > 1)
1844 57971519 : entry_cost -= 1;
1845 :
1846 60919360 : auto exit_cost = targetm.callee_save_cost
1847 121838720 : (spill_cost_type::RESTORE, hard_regno, mode, saved_nregs,
1848 60919360 : ira_memory_move_cost[mode][rclass][1] * saved_nregs / nregs,
1849 : allocated_callee_save_regs, existing_spills_p);
1850 :
1851 60919360 : return (entry_cost * REG_FREQ_FROM_BB (ENTRY_BLOCK_PTR_FOR_FN (cfun))
1852 60919360 : + exit_cost * REG_FREQ_FROM_BB (EXIT_BLOCK_PTR_FOR_FN (cfun)));
1853 : }
1854 :
1855 : /* Allocnos A1 and A2 are known to conflict. Check whether, in some loop L
1856 : that is either the current loop or a nested subloop, the conflict is of
1857 : the following form:
1858 :
1859 : - One allocno (X) is a cap allocno for some non-cap allocno X2.
1860 :
1861 : - X2 belongs to some loop L2.
1862 :
1863 : - The other allocno (Y) is a non-cap allocno.
1864 :
1865 : - Y is an ancestor of some allocno Y2 in L2. (Note that such a Y2
1866 : must exist, given that X and Y conflict.)
1867 :
1868 : - Y2 is not referenced in L2 (that is, ALLOCNO_NREFS (Y2) == 0).
1869 :
1870 : - Y can use a different allocation from Y2.
1871 :
1872 : In this case, Y's register is live across L2 but is not used within it,
1873 : whereas X's register is used only within L2. The conflict is therefore
1874 : only "soft", in that it can easily be avoided by spilling Y2 inside L2
1875 : without affecting any insn references.
1876 :
1877 : If the conflict does have this form, return the Y2 that would need to be
1878 : spilled in order to allow X and Y (and thus A1 and A2) to use the same
1879 : register. Return null otherwise. Returning null is conservatively correct;
1880 : any nonnnull return value is an optimization. */
1881 : ira_allocno_t
1882 122550215 : ira_soft_conflict (ira_allocno_t a1, ira_allocno_t a2)
1883 : {
1884 : /* Search for the loop L and its associated allocnos X and Y. */
1885 122550215 : int search_depth = 0;
1886 180331938 : while (ALLOCNO_CAP_MEMBER (a1) && ALLOCNO_CAP_MEMBER (a2))
1887 : {
1888 57781723 : a1 = ALLOCNO_CAP_MEMBER (a1);
1889 57781723 : a2 = ALLOCNO_CAP_MEMBER (a2);
1890 57781723 : if (search_depth++ > max_soft_conflict_loop_depth)
1891 : return nullptr;
1892 : }
1893 : /* This must be true if A1 and A2 conflict. */
1894 122550215 : ira_assert (ALLOCNO_LOOP_TREE_NODE (a1) == ALLOCNO_LOOP_TREE_NODE (a2));
1895 :
1896 : /* Make A1 the cap allocno (X in the comment above) and A2 the
1897 : non-cap allocno (Y in the comment above). */
1898 122550215 : if (ALLOCNO_CAP_MEMBER (a2))
1899 14704114 : std::swap (a1, a2);
1900 122550215 : if (!ALLOCNO_CAP_MEMBER (a1))
1901 : return nullptr;
1902 :
1903 : /* Search for the real allocno that A1 caps (X2 in the comment above). */
1904 57137097 : do
1905 : {
1906 57137097 : a1 = ALLOCNO_CAP_MEMBER (a1);
1907 57137097 : if (search_depth++ > max_soft_conflict_loop_depth)
1908 : return nullptr;
1909 : }
1910 57137097 : while (ALLOCNO_CAP_MEMBER (a1));
1911 :
1912 : /* Find the associated allocno for A2 (Y2 in the comment above). */
1913 32151930 : auto node = ALLOCNO_LOOP_TREE_NODE (a1);
1914 32151930 : auto local_a2 = node->regno_allocno_map[ALLOCNO_REGNO (a2)];
1915 :
1916 : /* Find the parent of LOCAL_A2/Y2. LOCAL_A2 must be a descendant of A2
1917 : for the conflict query to make sense, so this parent lookup must succeed.
1918 :
1919 : If the parent allocno has no references, it is usually cheaper to
1920 : spill at that loop level instead. Keep searching until we find
1921 : a parent allocno that does have references (but don't look past
1922 : the starting allocno). */
1923 44199257 : ira_allocno_t local_parent_a2;
1924 44199257 : for (;;)
1925 : {
1926 44199257 : local_parent_a2 = ira_parent_allocno (local_a2);
1927 44199257 : if (local_parent_a2 == a2 || ALLOCNO_NREFS (local_parent_a2) != 0)
1928 : break;
1929 : local_a2 = local_parent_a2;
1930 : }
1931 : if (CHECKING_P)
1932 : {
1933 : /* Sanity check to make sure that the conflict we've been given
1934 : makes sense. */
1935 : auto test_a2 = local_parent_a2;
1936 45089770 : while (test_a2 != a2)
1937 : {
1938 12937840 : test_a2 = ira_parent_allocno (test_a2);
1939 12937840 : ira_assert (test_a2);
1940 : }
1941 : }
1942 32151930 : if (local_a2
1943 32151930 : && ALLOCNO_NREFS (local_a2) == 0
1944 47790858 : && ira_subloop_allocnos_can_differ_p (local_parent_a2))
1945 15209750 : return local_a2;
1946 : return nullptr;
1947 : }
1948 :
1949 : /* The caller has decided to allocate HREGNO to A and has proved that
1950 : this is safe. However, the allocation might require the kind of
1951 : spilling described in the comment above ira_soft_conflict.
1952 : The caller has recorded that:
1953 :
1954 : - The allocnos in ALLOCNOS_TO_SPILL are the ones that would need
1955 : to be spilled to satisfy soft conflicts for at least one allocation
1956 : (not necessarily HREGNO).
1957 :
1958 : - The soft conflicts apply only to A allocations that overlap
1959 : SOFT_CONFLICT_REGS.
1960 :
1961 : If allocating HREGNO is subject to any soft conflicts, record the
1962 : subloop allocnos that need to be spilled. */
1963 : static void
1964 21771099 : spill_soft_conflicts (ira_allocno_t a, bitmap allocnos_to_spill,
1965 : HARD_REG_SET soft_conflict_regs, int hregno)
1966 : {
1967 21771099 : auto nregs = hard_regno_nregs (hregno, ALLOCNO_MODE (a));
1968 21771099 : bitmap_iterator bi;
1969 21771099 : unsigned int i;
1970 25582905 : EXECUTE_IF_SET_IN_BITMAP (allocnos_to_spill, 0, i, bi)
1971 : {
1972 : /* SPILL_A needs to be spilled for at least one allocation
1973 : (not necessarily this one). */
1974 3811806 : auto spill_a = ira_allocnos[i];
1975 :
1976 : /* Find the corresponding allocno for this loop. */
1977 3811806 : auto conflict_a = spill_a;
1978 7688621 : do
1979 : {
1980 7688621 : conflict_a = ira_parent_or_cap_allocno (conflict_a);
1981 7688621 : ira_assert (conflict_a);
1982 : }
1983 7688621 : while (ALLOCNO_LOOP_TREE_NODE (conflict_a)->level
1984 7688621 : > ALLOCNO_LOOP_TREE_NODE (a)->level);
1985 :
1986 3811806 : ira_assert (ALLOCNO_LOOP_TREE_NODE (conflict_a)
1987 : == ALLOCNO_LOOP_TREE_NODE (a));
1988 :
1989 3811806 : if (conflict_a == a)
1990 : {
1991 : /* SPILL_A is a descendant of A. We don't know (and don't need
1992 : to know) which cap allocnos have a soft conflict with A.
1993 : All we need to do is test whether the soft conflict applies
1994 : to the chosen allocation. */
1995 302125 : if (ira_hard_reg_set_intersection_p (hregno, ALLOCNO_MODE (a),
1996 : soft_conflict_regs))
1997 22474 : ALLOCNO_MIGHT_CONFLICT_WITH_PARENT_P (spill_a) = true;
1998 : }
1999 : else
2000 : {
2001 : /* SPILL_A is a descendant of CONFLICT_A, which has a soft conflict
2002 : with A. Test whether the soft conflict applies to the current
2003 : allocation. */
2004 3509681 : ira_assert (ira_soft_conflict (a, conflict_a) == spill_a);
2005 3509681 : auto conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a);
2006 3509681 : ira_assert (conflict_hregno >= 0);
2007 3509681 : auto conflict_nregs = hard_regno_nregs (conflict_hregno,
2008 3509681 : ALLOCNO_MODE (conflict_a));
2009 3509681 : if (hregno + nregs > conflict_hregno
2010 1129859 : && conflict_hregno + conflict_nregs > hregno)
2011 27613 : ALLOCNO_MIGHT_CONFLICT_WITH_PARENT_P (spill_a) = true;
2012 : }
2013 : }
2014 21771099 : }
2015 :
2016 : /* Choose a hard register for allocno A. If RETRY_P is TRUE, it means
2017 : that the function called from function
2018 : `ira_reassign_conflict_allocnos' and `allocno_reload_assign'. In
2019 : this case some allocno data are not defined or updated and we
2020 : should not touch these data. The function returns true if we
2021 : managed to assign a hard register to the allocno.
2022 :
2023 : To assign a hard register, first of all we calculate all conflict
2024 : hard registers which can come from conflicting allocnos with
2025 : already assigned hard registers. After that we find first free
2026 : hard register with the minimal cost. During hard register cost
2027 : calculation we take conflict hard register costs into account to
2028 : give a chance for conflicting allocnos to get a better hard
2029 : register in the future.
2030 :
2031 : If the best hard register cost is bigger than cost of memory usage
2032 : for the allocno, we don't assign a hard register to given allocno
2033 : at all.
2034 :
2035 : If we assign a hard register to the allocno, we update costs of the
2036 : hard register for allocnos connected by copies to improve a chance
2037 : to coalesce insns represented by the copies when we assign hard
2038 : registers to the allocnos connected by the copies. */
2039 : static bool
2040 23267679 : assign_hard_reg (ira_allocno_t a, bool retry_p)
2041 : {
2042 23267679 : HARD_REG_SET conflicting_regs[2], profitable_hard_regs, dep_allowed;
2043 23267679 : int i, j, hard_regno, best_hard_regno, class_size;
2044 23267679 : int cost, mem_cost, min_cost, full_cost, min_full_cost, nwords, word;
2045 23267679 : int *a_costs;
2046 23267679 : enum reg_class aclass;
2047 23267679 : machine_mode mode;
2048 23267679 : bool dep_filter_p;
2049 23267679 : static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
2050 23267679 : int saved_nregs;
2051 23267679 : int add_cost;
2052 : #ifdef STACK_REGS
2053 23267679 : bool no_stack_reg_p;
2054 : #endif
2055 23267679 : auto_bitmap allocnos_to_spill;
2056 23267679 : HARD_REG_SET soft_conflict_regs = {};
2057 23267679 : int entry_freq = REG_FREQ_FROM_BB (ENTRY_BLOCK_PTR_FOR_FN (cfun));
2058 23267679 : int exit_freq = REG_FREQ_FROM_BB (EXIT_BLOCK_PTR_FOR_FN (cfun));
2059 23267679 : int spill_cost = 0;
2060 : /* Whether we have spilled pseudos or used caller-saved registers for values
2061 : that are live across a call. */
2062 23267679 : bool existing_spills_p = allocated_memory_p || caller_save_needed;
2063 :
2064 23267679 : ira_assert (! ALLOCNO_ASSIGNED_P (a));
2065 23267679 : get_conflict_and_start_profitable_regs (a, retry_p,
2066 : conflicting_regs,
2067 : &profitable_hard_regs);
2068 23267679 : aclass = ALLOCNO_CLASS (a);
2069 23267679 : class_size = ira_class_hard_regs_num[aclass];
2070 23267679 : best_hard_regno = -1;
2071 23267679 : mem_cost = 0;
2072 23267679 : memset (costs, 0, sizeof (int) * class_size);
2073 23267679 : memset (full_costs, 0, sizeof (int) * class_size);
2074 : #ifdef STACK_REGS
2075 23267679 : no_stack_reg_p = false;
2076 : #endif
2077 23267679 : if (! retry_p)
2078 23267679 : start_update_cost ();
2079 23267679 : mem_cost += ALLOCNO_UPDATED_MEMORY_COST (a);
2080 :
2081 23267679 : if (!existing_spills_p)
2082 : {
2083 8491089 : auto entry_cost = targetm.frame_allocation_cost
2084 8491089 : (frame_cost_type::ALLOCATION, allocated_callee_save_regs);
2085 8491089 : spill_cost += entry_cost * entry_freq;
2086 :
2087 8491089 : auto exit_cost = targetm.frame_allocation_cost
2088 8491089 : (frame_cost_type::DEALLOCATION, allocated_callee_save_regs);
2089 8491089 : spill_cost += exit_cost * exit_freq;
2090 : }
2091 23267679 : mem_cost += spill_cost;
2092 :
2093 23267679 : ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
2094 : aclass, ALLOCNO_HARD_REG_COSTS (a));
2095 23267679 : a_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
2096 : #ifdef STACK_REGS
2097 23267679 : no_stack_reg_p = no_stack_reg_p || ALLOCNO_TOTAL_NO_STACK_REG_P (a);
2098 : #endif
2099 23267679 : cost = ALLOCNO_UPDATED_CLASS_COST (a);
2100 351780885 : for (i = 0; i < class_size; i++)
2101 328513206 : if (a_costs != NULL)
2102 : {
2103 204094269 : costs[i] += a_costs[i];
2104 204094269 : full_costs[i] += a_costs[i];
2105 : }
2106 : else
2107 : {
2108 124418937 : costs[i] += cost;
2109 124418937 : full_costs[i] += cost;
2110 : }
2111 23267679 : nwords = ALLOCNO_NUM_OBJECTS (a);
2112 23267679 : curr_allocno_process++;
2113 45705435 : for (word = 0; word < nwords; word++)
2114 : {
2115 23672517 : ira_object_t conflict_obj;
2116 23672517 : ira_object_t obj = ALLOCNO_OBJECT (a, word);
2117 23672517 : ira_object_conflict_iterator oci;
2118 :
2119 : /* Take preferences of conflicting allocnos into account. */
2120 447189191 : FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2121 : {
2122 424751435 : ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2123 424751435 : enum reg_class conflict_aclass;
2124 424751435 : allocno_color_data_t data = ALLOCNO_COLOR_DATA (conflict_a);
2125 :
2126 : /* Reload can give another class so we need to check all
2127 : allocnos. */
2128 479899767 : if (!retry_p
2129 424751435 : && ((!ALLOCNO_ASSIGNED_P (conflict_a)
2130 237417208 : || ALLOCNO_HARD_REGNO (conflict_a) < 0)
2131 302669660 : && !(hard_reg_set_intersect_p
2132 302669660 : (profitable_hard_regs,
2133 : ALLOCNO_COLOR_DATA
2134 : (conflict_a)->profitable_hard_regs))))
2135 : {
2136 : /* All conflict allocnos are in consideration bitmap
2137 : when retry_p is false. It might change in future and
2138 : if it happens the assert will be broken. It means
2139 : the code should be modified for the new
2140 : assumptions. */
2141 55148332 : ira_assert (bitmap_bit_p (consideration_allocno_bitmap,
2142 : ALLOCNO_NUM (conflict_a)));
2143 55148332 : continue;
2144 : }
2145 369603103 : conflict_aclass = ALLOCNO_CLASS (conflict_a);
2146 369603103 : ira_assert (ira_reg_classes_intersect_p
2147 : [aclass][conflict_aclass]);
2148 369603103 : if (ALLOCNO_ASSIGNED_P (conflict_a))
2149 : {
2150 184307308 : hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
2151 184307308 : if (hard_regno >= 0
2152 306389083 : && (ira_hard_reg_set_intersection_p
2153 122081775 : (hard_regno, ALLOCNO_MODE (conflict_a),
2154 : reg_class_contents[aclass])))
2155 : {
2156 119040534 : int n_objects = ALLOCNO_NUM_OBJECTS (conflict_a);
2157 119040534 : int conflict_nregs;
2158 :
2159 119040534 : mode = ALLOCNO_MODE (conflict_a);
2160 119040534 : conflict_nregs = hard_regno_nregs (hard_regno, mode);
2161 119040534 : auto spill_a = (retry_p
2162 119040534 : ? nullptr
2163 119040534 : : ira_soft_conflict (a, conflict_a));
2164 119040534 : if (spill_a)
2165 : {
2166 11700069 : if (bitmap_set_bit (allocnos_to_spill,
2167 : ALLOCNO_NUM (spill_a)))
2168 : {
2169 4047391 : ira_loop_border_costs border_costs (spill_a);
2170 4047391 : auto cost = border_costs.spill_inside_loop_cost ();
2171 8165614 : auto note_conflict = [&](int r)
2172 : {
2173 4118223 : SET_HARD_REG_BIT (soft_conflict_regs, r);
2174 4118223 : auto hri = ira_class_hard_reg_index[aclass][r];
2175 4118223 : if (hri >= 0)
2176 : {
2177 4078587 : costs[hri] += cost;
2178 4078587 : full_costs[hri] += cost;
2179 : }
2180 8165614 : };
2181 4047391 : enum machine_mode a_mode = ALLOCNO_MODE (a);
2182 8155989 : for (int r = hard_regno;
2183 8155989 : r >= 0 && (int) end_hard_regno (a_mode, r) > hard_regno;
2184 : r--)
2185 4108598 : note_conflict (r);
2186 4057016 : for (int r = hard_regno + 1;
2187 4057016 : r < hard_regno + conflict_nregs;
2188 : r++)
2189 9625 : note_conflict (r);
2190 : }
2191 : }
2192 : else
2193 : {
2194 107340465 : if (conflict_nregs == n_objects && conflict_nregs > 1)
2195 : {
2196 3515685 : int num = OBJECT_SUBWORD (conflict_obj);
2197 :
2198 3515685 : if (REG_WORDS_BIG_ENDIAN)
2199 : SET_HARD_REG_BIT (conflicting_regs[word],
2200 : hard_regno + n_objects - num - 1);
2201 : else
2202 3515685 : SET_HARD_REG_BIT (conflicting_regs[word],
2203 3515685 : hard_regno + num);
2204 : }
2205 : else
2206 103824780 : conflicting_regs[word]
2207 103824780 : |= ira_reg_mode_hard_regset[hard_regno][mode];
2208 107340465 : if (hard_reg_set_subset_p (profitable_hard_regs,
2209 107340465 : conflicting_regs[word]))
2210 1234761 : goto fail;
2211 : }
2212 : }
2213 : }
2214 185295795 : else if (! retry_p
2215 185295795 : && ! ALLOCNO_COLOR_DATA (conflict_a)->may_be_spilled_p
2216 : /* Don't process the conflict allocno twice. */
2217 97868013 : && (ALLOCNO_COLOR_DATA (conflict_a)->last_process
2218 97868013 : != curr_allocno_process))
2219 : {
2220 95779034 : int k, *conflict_costs;
2221 :
2222 95779034 : ALLOCNO_COLOR_DATA (conflict_a)->last_process
2223 95779034 : = curr_allocno_process;
2224 95779034 : ira_allocate_and_copy_costs
2225 95779034 : (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a),
2226 : conflict_aclass,
2227 : ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_a));
2228 95779034 : conflict_costs
2229 95779034 : = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a);
2230 95779034 : if (conflict_costs != NULL)
2231 328707918 : for (j = class_size - 1; j >= 0; j--)
2232 : {
2233 308363228 : hard_regno = ira_class_hard_regs[aclass][j];
2234 308363228 : ira_assert (hard_regno >= 0);
2235 308363228 : k = ira_class_hard_reg_index[conflict_aclass][hard_regno];
2236 335010711 : if (k < 0
2237 : /* If HARD_REGNO is not available for CONFLICT_A,
2238 : the conflict would be ignored, since HARD_REGNO
2239 : will never be assigned to CONFLICT_A. */
2240 308363228 : || !TEST_HARD_REG_BIT (data->profitable_hard_regs,
2241 : hard_regno))
2242 26647483 : continue;
2243 281715745 : full_costs[j] -= conflict_costs[k];
2244 : }
2245 95779034 : queue_update_cost (conflict_a, conflict_a, NULL, COST_HOP_DIVISOR);
2246 : }
2247 : }
2248 : }
2249 22032918 : if (! retry_p)
2250 : /* Take into account preferences of allocnos connected by copies to
2251 : the conflict allocnos. */
2252 22032918 : update_conflict_hard_regno_costs (full_costs, aclass, true);
2253 :
2254 : /* Take preferences of allocnos connected by copies into
2255 : account. */
2256 22032918 : if (! retry_p)
2257 : {
2258 22032918 : start_update_cost ();
2259 22032918 : queue_update_cost (a, a, NULL, COST_HOP_DIVISOR);
2260 22032918 : update_conflict_hard_regno_costs (full_costs, aclass, false);
2261 : }
2262 22032918 : min_cost = min_full_cost = INT_MAX;
2263 : /* We don't care about giving callee saved registers to allocnos no
2264 : living through calls because call clobbered registers are
2265 : allocated first (it is usual practice to put them first in
2266 : REG_ALLOC_ORDER). */
2267 22032918 : mode = ALLOCNO_MODE (a);
2268 22032918 : dep_filter_p = NUM_DEPENDENT_FILTERS && ALLOCNO_DEPENDENT_FILTERS (a);
2269 22032918 : if (dep_filter_p)
2270 : dep_allowed = ira_dependent_filter (a);
2271 336404551 : for (i = 0; i < class_size; i++)
2272 : {
2273 314371633 : hard_regno = ira_class_hard_regs[aclass][i];
2274 : #ifdef STACK_REGS
2275 314371633 : if (no_stack_reg_p
2276 314371633 : && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
2277 0 : continue;
2278 : #endif
2279 314371633 : if (! check_hard_reg_p (a, hard_regno,
2280 : conflicting_regs, profitable_hard_regs))
2281 98202690 : continue;
2282 216168943 : if (NUM_REGISTER_FILTERS
2283 : && !test_register_filters (ALLOCNO_REGISTER_FILTERS (a), hard_regno))
2284 : continue;
2285 216168943 : if (dep_filter_p && !TEST_HARD_REG_BIT (dep_allowed, hard_regno))
2286 : continue;
2287 216168943 : cost = costs[i];
2288 216168943 : full_cost = full_costs[i];
2289 216168943 : if (!HONOR_REG_ALLOC_ORDER)
2290 : {
2291 216168943 : if ((saved_nregs = calculate_saved_nregs (hard_regno, mode)) != 0)
2292 : /* We need to save/restore the hard register in
2293 : epilogue/prologue. Therefore we increase the cost. */
2294 : {
2295 42066843 : add_cost = calculate_saved_regs_cost (hard_regno, mode,
2296 : saved_nregs);
2297 42066843 : cost += add_cost;
2298 42066843 : full_cost += add_cost;
2299 : }
2300 : }
2301 216168943 : if (ira_need_caller_save_p (a, hard_regno))
2302 : {
2303 6509818 : cost += spill_cost;
2304 6509818 : full_cost += spill_cost;
2305 : }
2306 216168943 : if (min_cost > cost)
2307 : min_cost = cost;
2308 216168943 : if (min_full_cost > full_cost)
2309 : {
2310 28487280 : min_full_cost = full_cost;
2311 28487280 : best_hard_regno = hard_regno;
2312 28487280 : ira_assert (hard_regno >= 0);
2313 : }
2314 216168943 : if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
2315 0 : fprintf (ira_dump_file, "(%d=%d,%d) ", hard_regno, cost, full_cost);
2316 : }
2317 22032918 : if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
2318 0 : fprintf (ira_dump_file, "\n");
2319 22032918 : if (min_full_cost > mem_cost
2320 : /* Do not spill static chain pointer pseudo when non-local goto
2321 : is used. */
2322 22032918 : && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
2323 : {
2324 261819 : if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2325 4 : fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
2326 : mem_cost, min_full_cost);
2327 : best_hard_regno = -1;
2328 : }
2329 23005864 : fail:
2330 23005864 : if (best_hard_regno >= 0)
2331 : {
2332 21771099 : record_allocation (best_hard_regno,
2333 21771099 : hard_regno_nregs (best_hard_regno, mode));
2334 21771099 : spill_soft_conflicts (a, allocnos_to_spill, soft_conflict_regs,
2335 : best_hard_regno);
2336 : }
2337 : else
2338 1496580 : allocated_memory_p = true;
2339 23267679 : if (! retry_p)
2340 23267679 : restore_costs_from_copies (a);
2341 23267679 : ALLOCNO_HARD_REGNO (a) = best_hard_regno;
2342 23267679 : ALLOCNO_ASSIGNED_P (a) = true;
2343 23267679 : if (best_hard_regno >= 0 && !retry_p)
2344 21771099 : update_costs_from_copies (a, true, true);
2345 23267679 : ira_assert (ALLOCNO_CLASS (a) == aclass);
2346 : /* We don't need updated costs anymore. */
2347 23267679 : ira_free_allocno_updated_costs (a);
2348 23267679 : return best_hard_regno >= 0;
2349 23267679 : }
2350 :
2351 :
2352 :
2353 : /* An array used to sort copies. */
2354 : static ira_copy_t *sorted_copies;
2355 :
2356 : /* If allocno A is a cap, return non-cap allocno from which A is
2357 : created. Otherwise, return A. */
2358 : static ira_allocno_t
2359 0 : get_cap_member (ira_allocno_t a)
2360 : {
2361 0 : ira_allocno_t member;
2362 :
2363 26949524 : while ((member = ALLOCNO_CAP_MEMBER (a)) != NULL)
2364 : a = member;
2365 0 : return a;
2366 : }
2367 :
2368 : /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
2369 : used to find a conflict for new allocnos or allocnos with the
2370 : different allocno classes. */
2371 : static bool
2372 20435938 : allocnos_conflict_by_live_ranges_p (ira_allocno_t a1, ira_allocno_t a2)
2373 : {
2374 20435938 : rtx reg1, reg2;
2375 20435938 : int i, j;
2376 20435938 : int n1 = ALLOCNO_NUM_OBJECTS (a1);
2377 20435938 : int n2 = ALLOCNO_NUM_OBJECTS (a2);
2378 :
2379 20435938 : if (a1 == a2)
2380 : return false;
2381 20435938 : reg1 = regno_reg_rtx[ALLOCNO_REGNO (a1)];
2382 20435938 : reg2 = regno_reg_rtx[ALLOCNO_REGNO (a2)];
2383 20435938 : if (reg1 != NULL && reg2 != NULL
2384 20435938 : && ORIGINAL_REGNO (reg1) == ORIGINAL_REGNO (reg2))
2385 : return false;
2386 :
2387 : /* We don't keep live ranges for caps because they can be quite big.
2388 : Use ranges of non-cap allocno from which caps are created. */
2389 26780118 : a1 = get_cap_member (a1);
2390 39612408 : a2 = get_cap_member (a2);
2391 39612408 : for (i = 0; i < n1; i++)
2392 : {
2393 20506610 : ira_object_t c1 = ALLOCNO_OBJECT (a1, i);
2394 :
2395 40121047 : for (j = 0; j < n2; j++)
2396 : {
2397 20864569 : ira_object_t c2 = ALLOCNO_OBJECT (a2, j);
2398 :
2399 20864569 : if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1),
2400 : OBJECT_LIVE_RANGES (c2)))
2401 : return true;
2402 : }
2403 : }
2404 : return false;
2405 : }
2406 :
2407 : /* The function is used to sort copies according to their execution
2408 : frequencies. */
2409 : static int
2410 122096169 : copy_freq_compare_func (const void *v1p, const void *v2p)
2411 : {
2412 122096169 : ira_copy_t cp1 = *(const ira_copy_t *) v1p, cp2 = *(const ira_copy_t *) v2p;
2413 122096169 : int pri1, pri2;
2414 :
2415 122096169 : pri1 = cp1->freq;
2416 122096169 : pri2 = cp2->freq;
2417 122096169 : if (pri2 - pri1)
2418 46690146 : return pri2 - pri1;
2419 :
2420 : /* If frequencies are equal, sort by copies, so that the results of
2421 : qsort leave nothing to chance. */
2422 75406023 : return cp1->num - cp2->num;
2423 : }
2424 :
2425 :
2426 :
2427 : /* Return true if any allocno from thread of A1 conflicts with any
2428 : allocno from thread A2. */
2429 : static bool
2430 7014573 : allocno_thread_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
2431 : {
2432 7014573 : ira_allocno_t a, conflict_a;
2433 :
2434 7014573 : for (a = ALLOCNO_COLOR_DATA (a2)->next_thread_allocno;;
2435 5917764 : a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2436 : {
2437 12932337 : for (conflict_a = ALLOCNO_COLOR_DATA (a1)->next_thread_allocno;;
2438 7503601 : conflict_a = ALLOCNO_COLOR_DATA (conflict_a)->next_thread_allocno)
2439 : {
2440 20435938 : if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
2441 : return true;
2442 19185806 : if (conflict_a == a1)
2443 : break;
2444 : }
2445 11682205 : if (a == a2)
2446 : break;
2447 : }
2448 : return false;
2449 : }
2450 :
2451 : /* Merge two threads given correspondingly by their first allocnos T1
2452 : and T2 (more accurately merging T2 into T1). */
2453 : static void
2454 5764441 : merge_threads (ira_allocno_t t1, ira_allocno_t t2)
2455 : {
2456 5764441 : ira_allocno_t a, next, last;
2457 :
2458 5764441 : gcc_assert (t1 != t2
2459 : && ALLOCNO_COLOR_DATA (t1)->first_thread_allocno == t1
2460 : && ALLOCNO_COLOR_DATA (t2)->first_thread_allocno == t2);
2461 5764441 : for (last = t2, a = ALLOCNO_COLOR_DATA (t2)->next_thread_allocno;;
2462 5594283 : a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2463 : {
2464 11358724 : ALLOCNO_COLOR_DATA (a)->first_thread_allocno = t1;
2465 11358724 : if (a == t2)
2466 : break;
2467 5594283 : last = a;
2468 : }
2469 5764441 : next = ALLOCNO_COLOR_DATA (t1)->next_thread_allocno;
2470 5764441 : ALLOCNO_COLOR_DATA (t1)->next_thread_allocno = t2;
2471 5764441 : ALLOCNO_COLOR_DATA (last)->next_thread_allocno = next;
2472 5764441 : ALLOCNO_COLOR_DATA (t1)->thread_freq += ALLOCNO_COLOR_DATA (t2)->thread_freq;
2473 5764441 : }
2474 :
2475 : /* Create threads by processing CP_NUM copies from sorted copies. We
2476 : process the most expensive copies first. */
2477 : static void
2478 8162764 : form_threads_from_copies (int cp_num)
2479 : {
2480 8162764 : ira_allocno_t a, thread1, thread2;
2481 8162764 : ira_copy_t cp;
2482 :
2483 8162764 : qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
2484 : /* Form threads processing copies, most frequently executed
2485 : first. */
2486 24413016 : for (int i = 0; i < cp_num; i++)
2487 : {
2488 8087488 : cp = sorted_copies[i];
2489 8087488 : thread1 = ALLOCNO_COLOR_DATA (cp->first)->first_thread_allocno;
2490 8087488 : thread2 = ALLOCNO_COLOR_DATA (cp->second)->first_thread_allocno;
2491 8087488 : if (thread1 == thread2)
2492 1072915 : continue;
2493 7014573 : if (! allocno_thread_conflict_p (thread1, thread2))
2494 : {
2495 5764441 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2496 232 : fprintf
2497 232 : (ira_dump_file,
2498 : " Forming thread by copy %d:a%dr%d-a%dr%d (freq=%d):\n",
2499 232 : cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
2500 232 : ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
2501 : cp->freq);
2502 5764441 : merge_threads (thread1, thread2);
2503 5764441 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2504 : {
2505 232 : thread1 = ALLOCNO_COLOR_DATA (thread1)->first_thread_allocno;
2506 232 : fprintf (ira_dump_file, " Result (freq=%d): a%dr%d(%d)",
2507 232 : ALLOCNO_COLOR_DATA (thread1)->thread_freq,
2508 : ALLOCNO_NUM (thread1), ALLOCNO_REGNO (thread1),
2509 : ALLOCNO_FREQ (thread1));
2510 232 : for (a = ALLOCNO_COLOR_DATA (thread1)->next_thread_allocno;
2511 600 : a != thread1;
2512 368 : a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2513 368 : fprintf (ira_dump_file, " a%dr%d(%d)",
2514 : ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2515 : ALLOCNO_FREQ (a));
2516 232 : fprintf (ira_dump_file, "\n");
2517 : }
2518 : }
2519 : }
2520 8162764 : }
2521 :
2522 : /* Create threads by processing copies of all alocnos from BUCKET. We
2523 : process the most expensive copies first. */
2524 : static void
2525 2767086 : form_threads_from_bucket (ira_allocno_t bucket)
2526 : {
2527 2767086 : ira_allocno_t a;
2528 2767086 : ira_copy_t cp, next_cp;
2529 2767086 : int cp_num = 0;
2530 :
2531 21250284 : for (a = bucket; a != NULL; a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2532 : {
2533 30380031 : for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2534 : {
2535 11896833 : if (cp->first == a)
2536 : {
2537 5821014 : next_cp = cp->next_first_allocno_copy;
2538 5821014 : sorted_copies[cp_num++] = cp;
2539 : }
2540 6075819 : else if (cp->second == a)
2541 6075819 : next_cp = cp->next_second_allocno_copy;
2542 : else
2543 0 : gcc_unreachable ();
2544 : }
2545 : }
2546 2767086 : form_threads_from_copies (cp_num);
2547 2767086 : }
2548 :
2549 : /* Create threads by processing copies of colorable allocno A. We
2550 : process most expensive copies first. */
2551 : static void
2552 5395678 : form_threads_from_colorable_allocno (ira_allocno_t a)
2553 : {
2554 5395678 : ira_allocno_t another_a;
2555 5395678 : ira_copy_t cp, next_cp;
2556 5395678 : int cp_num = 0;
2557 :
2558 5395678 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2559 136 : fprintf (ira_dump_file, " Forming thread from allocno a%dr%d:\n",
2560 : ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2561 9025535 : for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2562 : {
2563 3629857 : if (cp->first == a)
2564 : {
2565 2035272 : next_cp = cp->next_first_allocno_copy;
2566 2035272 : another_a = cp->second;
2567 : }
2568 1594585 : else if (cp->second == a)
2569 : {
2570 1594585 : next_cp = cp->next_second_allocno_copy;
2571 1594585 : another_a = cp->first;
2572 : }
2573 : else
2574 0 : gcc_unreachable ();
2575 3629857 : if ((! ALLOCNO_COLOR_DATA (another_a)->in_graph_p
2576 1852864 : && !ALLOCNO_COLOR_DATA (another_a)->may_be_spilled_p)
2577 2034208 : || ALLOCNO_COLOR_DATA (another_a)->colorable_p)
2578 2266474 : sorted_copies[cp_num++] = cp;
2579 : }
2580 5395678 : form_threads_from_copies (cp_num);
2581 5395678 : }
2582 :
2583 : /* Form initial threads which contain only one allocno. */
2584 : static void
2585 1240980 : init_allocno_threads (void)
2586 : {
2587 1240980 : ira_allocno_t a;
2588 1240980 : unsigned int j;
2589 1240980 : bitmap_iterator bi;
2590 1240980 : ira_pref_t pref;
2591 :
2592 27172026 : EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2593 : {
2594 25931046 : a = ira_allocnos[j];
2595 : /* Set up initial thread data: */
2596 25931046 : ALLOCNO_COLOR_DATA (a)->first_thread_allocno
2597 25931046 : = ALLOCNO_COLOR_DATA (a)->next_thread_allocno = a;
2598 25931046 : ALLOCNO_COLOR_DATA (a)->thread_freq = ALLOCNO_FREQ (a);
2599 25931046 : ALLOCNO_COLOR_DATA (a)->hard_reg_prefs = 0;
2600 31323027 : for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
2601 5391981 : ALLOCNO_COLOR_DATA (a)->hard_reg_prefs += pref->freq;
2602 : }
2603 1240980 : }
2604 :
2605 :
2606 :
2607 : /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
2608 :
2609 : /* Bucket of allocnos that can colored currently without spilling. */
2610 : static ira_allocno_t colorable_allocno_bucket;
2611 :
2612 : /* Bucket of allocnos that might be not colored currently without
2613 : spilling. */
2614 : static ira_allocno_t uncolorable_allocno_bucket;
2615 :
2616 : /* The current number of allocnos in the uncolorable_bucket. */
2617 : static int uncolorable_allocnos_num;
2618 :
2619 : /* Return the current spill priority of allocno A. The less the
2620 : number, the more preferable the allocno for spilling. */
2621 : static inline int
2622 393921963 : allocno_spill_priority (ira_allocno_t a)
2623 : {
2624 393921963 : allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
2625 :
2626 393921963 : return (data->temp
2627 393921963 : / (ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
2628 393921963 : * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
2629 393921963 : + 1));
2630 : }
2631 :
2632 : /* Add allocno A to bucket *BUCKET_PTR. A should be not in a bucket
2633 : before the call. */
2634 : static void
2635 22981641 : add_allocno_to_bucket (ira_allocno_t a, ira_allocno_t *bucket_ptr)
2636 : {
2637 22981641 : ira_allocno_t first_a;
2638 22981641 : allocno_color_data_t data;
2639 :
2640 22981641 : if (bucket_ptr == &uncolorable_allocno_bucket
2641 6921819 : && ALLOCNO_CLASS (a) != NO_REGS)
2642 : {
2643 6921819 : uncolorable_allocnos_num++;
2644 6921819 : ira_assert (uncolorable_allocnos_num > 0);
2645 : }
2646 22981641 : first_a = *bucket_ptr;
2647 22981641 : data = ALLOCNO_COLOR_DATA (a);
2648 22981641 : data->next_bucket_allocno = first_a;
2649 22981641 : data->prev_bucket_allocno = NULL;
2650 22981641 : if (first_a != NULL)
2651 21536895 : ALLOCNO_COLOR_DATA (first_a)->prev_bucket_allocno = a;
2652 22981641 : *bucket_ptr = a;
2653 22981641 : }
2654 :
2655 : /* Compare two allocnos to define which allocno should be pushed first
2656 : into the coloring stack. If the return is a negative number, the
2657 : allocno given by the first parameter will be pushed first. In this
2658 : case such allocno has less priority than the second one and the
2659 : hard register will be assigned to it after assignment to the second
2660 : one. As the result of such assignment order, the second allocno
2661 : has a better chance to get the best hard register. */
2662 : static int
2663 516682806 : bucket_allocno_compare_func (const void *v1p, const void *v2p)
2664 : {
2665 516682806 : ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2666 516682806 : ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2667 516682806 : int diff, freq1, freq2, a1_num, a2_num, pref1, pref2;
2668 516682806 : ira_allocno_t t1 = ALLOCNO_COLOR_DATA (a1)->first_thread_allocno;
2669 516682806 : ira_allocno_t t2 = ALLOCNO_COLOR_DATA (a2)->first_thread_allocno;
2670 516682806 : int cl1 = ALLOCNO_CLASS (a1), cl2 = ALLOCNO_CLASS (a2);
2671 :
2672 516682806 : freq1 = ALLOCNO_COLOR_DATA (t1)->thread_freq;
2673 516682806 : freq2 = ALLOCNO_COLOR_DATA (t2)->thread_freq;
2674 516682806 : if ((diff = freq1 - freq2) != 0)
2675 : return diff;
2676 :
2677 180732420 : if ((diff = ALLOCNO_NUM (t2) - ALLOCNO_NUM (t1)) != 0)
2678 : return diff;
2679 :
2680 : /* Push pseudos requiring less hard registers first. It means that
2681 : we will assign pseudos requiring more hard registers first
2682 : avoiding creation small holes in free hard register file into
2683 : which the pseudos requiring more hard registers cannot fit. */
2684 25142744 : if ((diff = (ira_reg_class_max_nregs[cl1][ALLOCNO_MODE (a1)]
2685 25142744 : - ira_reg_class_max_nregs[cl2][ALLOCNO_MODE (a2)])) != 0)
2686 : return diff;
2687 :
2688 24934258 : freq1 = ALLOCNO_FREQ (a1);
2689 24934258 : freq2 = ALLOCNO_FREQ (a2);
2690 24934258 : if ((diff = freq1 - freq2) != 0)
2691 : return diff;
2692 :
2693 14591041 : a1_num = ALLOCNO_COLOR_DATA (a1)->available_regs_num;
2694 14591041 : a2_num = ALLOCNO_COLOR_DATA (a2)->available_regs_num;
2695 14591041 : if ((diff = a2_num - a1_num) != 0)
2696 : return diff;
2697 : /* Push allocnos with minimal conflict_allocno_hard_prefs first. */
2698 12303508 : pref1 = ALLOCNO_COLOR_DATA (a1)->conflict_allocno_hard_prefs;
2699 12303508 : pref2 = ALLOCNO_COLOR_DATA (a2)->conflict_allocno_hard_prefs;
2700 12303508 : if ((diff = pref1 - pref2) != 0)
2701 : return diff;
2702 11979534 : return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
2703 : }
2704 :
2705 : /* Sort bucket *BUCKET_PTR and return the result through
2706 : BUCKET_PTR. */
2707 : static void
2708 4008031 : sort_bucket (ira_allocno_t *bucket_ptr,
2709 : int (*compare_func) (const void *, const void *))
2710 : {
2711 4008031 : ira_allocno_t a, head;
2712 4008031 : int n;
2713 :
2714 4008031 : for (n = 0, a = *bucket_ptr;
2715 29413048 : a != NULL;
2716 25405017 : a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2717 25405017 : sorted_allocnos[n++] = a;
2718 4008031 : if (n <= 1)
2719 : return;
2720 1692441 : qsort (sorted_allocnos, n, sizeof (ira_allocno_t), compare_func);
2721 1692441 : head = NULL;
2722 26833166 : for (n--; n >= 0; n--)
2723 : {
2724 25140725 : a = sorted_allocnos[n];
2725 25140725 : ALLOCNO_COLOR_DATA (a)->next_bucket_allocno = head;
2726 25140725 : ALLOCNO_COLOR_DATA (a)->prev_bucket_allocno = NULL;
2727 25140725 : if (head != NULL)
2728 23448284 : ALLOCNO_COLOR_DATA (head)->prev_bucket_allocno = a;
2729 25140725 : head = a;
2730 : }
2731 1692441 : *bucket_ptr = head;
2732 : }
2733 :
2734 : /* Add ALLOCNO to colorable bucket maintaining the order according
2735 : their priority. ALLOCNO should be not in a bucket before the
2736 : call. */
2737 : static void
2738 5395678 : add_allocno_to_ordered_colorable_bucket (ira_allocno_t allocno)
2739 : {
2740 5395678 : ira_allocno_t before, after;
2741 :
2742 5395678 : form_threads_from_colorable_allocno (allocno);
2743 5395678 : for (before = colorable_allocno_bucket, after = NULL;
2744 36497267 : before != NULL;
2745 31101589 : after = before,
2746 31101589 : before = ALLOCNO_COLOR_DATA (before)->next_bucket_allocno)
2747 35200904 : if (bucket_allocno_compare_func (&allocno, &before) < 0)
2748 : break;
2749 5395678 : ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno = before;
2750 5395678 : ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno = after;
2751 5395678 : if (after == NULL)
2752 2709565 : colorable_allocno_bucket = allocno;
2753 : else
2754 2686113 : ALLOCNO_COLOR_DATA (after)->next_bucket_allocno = allocno;
2755 5395678 : if (before != NULL)
2756 4099315 : ALLOCNO_COLOR_DATA (before)->prev_bucket_allocno = allocno;
2757 5395678 : }
2758 :
2759 : /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
2760 : the call. */
2761 : static void
2762 28377319 : delete_allocno_from_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
2763 : {
2764 28377319 : ira_allocno_t prev_allocno, next_allocno;
2765 :
2766 28377319 : if (bucket_ptr == &uncolorable_allocno_bucket
2767 6921819 : && ALLOCNO_CLASS (allocno) != NO_REGS)
2768 : {
2769 6921819 : uncolorable_allocnos_num--;
2770 6921819 : ira_assert (uncolorable_allocnos_num >= 0);
2771 : }
2772 28377319 : prev_allocno = ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno;
2773 28377319 : next_allocno = ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno;
2774 28377319 : if (prev_allocno != NULL)
2775 4298211 : ALLOCNO_COLOR_DATA (prev_allocno)->next_bucket_allocno = next_allocno;
2776 : else
2777 : {
2778 24079108 : ira_assert (*bucket_ptr == allocno);
2779 24079108 : *bucket_ptr = next_allocno;
2780 : }
2781 28377319 : if (next_allocno != NULL)
2782 25627158 : ALLOCNO_COLOR_DATA (next_allocno)->prev_bucket_allocno = prev_allocno;
2783 28377319 : }
2784 :
2785 : /* Put allocno A onto the coloring stack without removing it from its
2786 : bucket. Pushing allocno to the coloring stack can result in moving
2787 : conflicting allocnos from the uncolorable bucket to the colorable
2788 : one. Update conflict_allocno_hard_prefs of the conflicting
2789 : allocnos which are not on stack yet. */
2790 : static void
2791 22981641 : push_allocno_to_stack (ira_allocno_t a)
2792 : {
2793 22981641 : enum reg_class aclass;
2794 22981641 : allocno_color_data_t data, conflict_data;
2795 22981641 : int size, i, n = ALLOCNO_NUM_OBJECTS (a);
2796 :
2797 22981641 : data = ALLOCNO_COLOR_DATA (a);
2798 22981641 : data->in_graph_p = false;
2799 22981641 : allocno_stack_vec.safe_push (a);
2800 22981641 : aclass = ALLOCNO_CLASS (a);
2801 22981641 : if (aclass == NO_REGS)
2802 : return;
2803 22981641 : size = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
2804 22981641 : if (n > 1)
2805 : {
2806 : /* We will deal with the subwords individually. */
2807 457908 : gcc_assert (size == ALLOCNO_NUM_OBJECTS (a));
2808 : size = 1;
2809 : }
2810 46421190 : for (i = 0; i < n; i++)
2811 : {
2812 23439549 : ira_object_t obj = ALLOCNO_OBJECT (a, i);
2813 23439549 : ira_object_t conflict_obj;
2814 23439549 : ira_object_conflict_iterator oci;
2815 :
2816 512390251 : FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2817 : {
2818 488950702 : ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2819 488950702 : ira_pref_t pref;
2820 :
2821 488950702 : conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
2822 763169330 : if (! conflict_data->in_graph_p
2823 217763274 : || ALLOCNO_ASSIGNED_P (conflict_a)
2824 488950702 : || !(hard_reg_set_intersect_p
2825 435526548 : (ALLOCNO_COLOR_DATA (a)->profitable_hard_regs,
2826 : conflict_data->profitable_hard_regs)))
2827 274218628 : continue;
2828 234149870 : for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
2829 19417796 : conflict_data->conflict_allocno_hard_prefs -= pref->freq;
2830 214732074 : if (conflict_data->colorable_p)
2831 30113381 : continue;
2832 184618693 : ira_assert (bitmap_bit_p (coloring_allocno_bitmap,
2833 : ALLOCNO_NUM (conflict_a)));
2834 184618693 : if (update_left_conflict_sizes_p (conflict_a, a, size))
2835 : {
2836 5395678 : delete_allocno_from_bucket
2837 5395678 : (conflict_a, &uncolorable_allocno_bucket);
2838 5395678 : add_allocno_to_ordered_colorable_bucket (conflict_a);
2839 5395678 : if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2840 : {
2841 136 : fprintf (ira_dump_file, " Making");
2842 136 : ira_print_expanded_allocno (conflict_a);
2843 136 : fprintf (ira_dump_file, " colorable\n");
2844 : }
2845 : }
2846 :
2847 : }
2848 : }
2849 : }
2850 :
2851 : /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
2852 : The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
2853 : static void
2854 22981641 : remove_allocno_from_bucket_and_push (ira_allocno_t allocno, bool colorable_p)
2855 : {
2856 22981641 : if (colorable_p)
2857 21455500 : delete_allocno_from_bucket (allocno, &colorable_allocno_bucket);
2858 : else
2859 1526141 : delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
2860 22981641 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2861 : {
2862 725 : fprintf (ira_dump_file, " Pushing");
2863 725 : ira_print_expanded_allocno (allocno);
2864 725 : if (colorable_p)
2865 710 : fprintf (ira_dump_file, "(cost %d)\n",
2866 710 : ALLOCNO_COLOR_DATA (allocno)->temp);
2867 : else
2868 15 : fprintf (ira_dump_file, "(potential spill: %spri=%d, cost=%d)\n",
2869 15 : ALLOCNO_BAD_SPILL_P (allocno) ? "bad spill, " : "",
2870 : allocno_spill_priority (allocno),
2871 15 : ALLOCNO_COLOR_DATA (allocno)->temp);
2872 : }
2873 22981641 : if (! colorable_p)
2874 1526141 : ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p = true;
2875 22981641 : push_allocno_to_stack (allocno);
2876 22981641 : }
2877 :
2878 : /* Put all allocnos from colorable bucket onto the coloring stack. */
2879 : static void
2880 2767086 : push_only_colorable (void)
2881 : {
2882 2767086 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2883 67 : fprintf (ira_dump_file, " Forming thread from colorable bucket:\n");
2884 2767086 : form_threads_from_bucket (colorable_allocno_bucket);
2885 2767086 : for (ira_allocno_t a = colorable_allocno_bucket;
2886 21250284 : a != NULL;
2887 18483198 : a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2888 18483198 : update_costs_from_prefs (a);
2889 2767086 : sort_bucket (&colorable_allocno_bucket, bucket_allocno_compare_func);
2890 26989672 : for (;colorable_allocno_bucket != NULL;)
2891 21455500 : remove_allocno_from_bucket_and_push (colorable_allocno_bucket, true);
2892 2767086 : }
2893 :
2894 : /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
2895 : loop given by its LOOP_NODE. */
2896 : int
2897 26350776 : ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
2898 : {
2899 26350776 : int freq, i;
2900 26350776 : edge_iterator ei;
2901 26350776 : edge e;
2902 :
2903 26350776 : ira_assert (current_loops != NULL && loop_node->loop != NULL
2904 : && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
2905 26350776 : freq = 0;
2906 26350776 : if (! exit_p)
2907 : {
2908 42265923 : FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
2909 29090535 : if (e->src != loop_node->loop->latch
2910 29090535 : && (regno < 0
2911 16464448 : || (bitmap_bit_p (df_get_live_out (e->src), regno)
2912 16135471 : && bitmap_bit_p (df_get_live_in (e->dest), regno))))
2913 16126214 : freq += EDGE_FREQUENCY (e);
2914 : }
2915 : else
2916 : {
2917 13175388 : auto_vec<edge> edges = get_loop_exit_edges (loop_node->loop);
2918 74837534 : FOR_EACH_VEC_ELT (edges, i, e)
2919 35311937 : if (regno < 0
2920 35311937 : || (bitmap_bit_p (df_get_live_out (e->src), regno)
2921 31824299 : && bitmap_bit_p (df_get_live_in (e->dest), regno)))
2922 15611642 : freq += EDGE_FREQUENCY (e);
2923 13175388 : }
2924 :
2925 26350776 : return REG_FREQ_FROM_EDGE_FREQ (freq);
2926 : }
2927 :
2928 : /* Construct an object that describes the boundary between A and its
2929 : parent allocno. */
2930 13175302 : ira_loop_border_costs::ira_loop_border_costs (ira_allocno_t a)
2931 13175302 : : m_mode (ALLOCNO_MODE (a)),
2932 13175302 : m_class (ALLOCNO_CLASS (a)),
2933 13175302 : m_entry_freq (ira_loop_edge_freq (ALLOCNO_LOOP_TREE_NODE (a),
2934 : ALLOCNO_REGNO (a), false)),
2935 13175302 : m_exit_freq (ira_loop_edge_freq (ALLOCNO_LOOP_TREE_NODE (a),
2936 : ALLOCNO_REGNO (a), true))
2937 : {
2938 13175302 : }
2939 :
2940 : /* Calculate and return the cost of putting allocno A into memory. */
2941 : static int
2942 6921819 : calculate_allocno_spill_cost (ira_allocno_t a)
2943 : {
2944 6921819 : int regno, cost;
2945 6921819 : ira_allocno_t parent_allocno;
2946 6921819 : ira_loop_tree_node_t parent_node, loop_node;
2947 :
2948 6921819 : regno = ALLOCNO_REGNO (a);
2949 6921819 : cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_UPDATED_CLASS_COST (a);
2950 6921819 : if (ALLOCNO_CAP (a) != NULL)
2951 : return cost;
2952 4997581 : loop_node = ALLOCNO_LOOP_TREE_NODE (a);
2953 4997581 : if ((parent_node = loop_node->parent) == NULL)
2954 : return cost;
2955 922094 : if ((parent_allocno = parent_node->regno_allocno_map[regno]) == NULL)
2956 : return cost;
2957 922094 : ira_loop_border_costs border_costs (a);
2958 922094 : if (ALLOCNO_HARD_REGNO (parent_allocno) < 0)
2959 262476 : cost -= border_costs.spill_outside_loop_cost ();
2960 : else
2961 1319236 : cost += (border_costs.spill_inside_loop_cost ()
2962 659618 : - border_costs.move_between_loops_cost ());
2963 : return cost;
2964 : }
2965 :
2966 : /* Used for sorting allocnos for spilling. */
2967 : static inline int
2968 213117229 : allocno_spill_priority_compare (ira_allocno_t a1, ira_allocno_t a2)
2969 : {
2970 213117229 : int pri1, pri2, diff;
2971 :
2972 : /* Avoid spilling static chain pointer pseudo when non-local goto is
2973 : used. */
2974 213117229 : if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))
2975 : return 1;
2976 213117229 : else if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2)))
2977 : return -1;
2978 213117229 : if (ALLOCNO_BAD_SPILL_P (a1) && ! ALLOCNO_BAD_SPILL_P (a2))
2979 : return 1;
2980 205244738 : if (ALLOCNO_BAD_SPILL_P (a2) && ! ALLOCNO_BAD_SPILL_P (a1))
2981 : return -1;
2982 196960974 : pri1 = allocno_spill_priority (a1);
2983 196960974 : pri2 = allocno_spill_priority (a2);
2984 196960974 : if ((diff = pri1 - pri2) != 0)
2985 : return diff;
2986 52781242 : if ((diff
2987 52781242 : = ALLOCNO_COLOR_DATA (a1)->temp - ALLOCNO_COLOR_DATA (a2)->temp) != 0)
2988 : return diff;
2989 39882064 : return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2990 : }
2991 :
2992 : /* Used for sorting allocnos for spilling. */
2993 : static int
2994 213117229 : allocno_spill_sort_compare (const void *v1p, const void *v2p)
2995 : {
2996 213117229 : ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2997 213117229 : ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2998 :
2999 213117229 : return allocno_spill_priority_compare (p1, p2);
3000 : }
3001 :
3002 : /* Push allocnos to the coloring stack. The order of allocnos in the
3003 : stack defines the order for the subsequent coloring. */
3004 : static void
3005 1240945 : push_allocnos_to_stack (void)
3006 : {
3007 1240945 : ira_allocno_t a;
3008 1240945 : int cost;
3009 :
3010 : /* Calculate uncolorable allocno spill costs. */
3011 1240945 : for (a = uncolorable_allocno_bucket;
3012 8162764 : a != NULL;
3013 6921819 : a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
3014 6921819 : if (ALLOCNO_CLASS (a) != NO_REGS)
3015 : {
3016 6921819 : cost = calculate_allocno_spill_cost (a);
3017 : /* ??? Remove cost of copies between the coalesced
3018 : allocnos. */
3019 6921819 : ALLOCNO_COLOR_DATA (a)->temp = cost;
3020 : }
3021 1240945 : sort_bucket (&uncolorable_allocno_bucket, allocno_spill_sort_compare);
3022 4293227 : for (;;)
3023 : {
3024 2767086 : push_only_colorable ();
3025 2767086 : a = uncolorable_allocno_bucket;
3026 2767086 : if (a == NULL)
3027 : break;
3028 1526141 : remove_allocno_from_bucket_and_push (a, false);
3029 : }
3030 1240945 : ira_assert (colorable_allocno_bucket == NULL
3031 : && uncolorable_allocno_bucket == NULL);
3032 1240945 : ira_assert (uncolorable_allocnos_num == 0);
3033 1240945 : }
3034 :
3035 : /* Pop the coloring stack and assign hard registers to the popped
3036 : allocnos. */
3037 : static void
3038 1240945 : pop_allocnos_from_stack (void)
3039 : {
3040 1240945 : ira_allocno_t allocno;
3041 1240945 : enum reg_class aclass;
3042 :
3043 24222586 : for (;allocno_stack_vec.length () != 0;)
3044 : {
3045 22981641 : allocno = allocno_stack_vec.pop ();
3046 22981641 : aclass = ALLOCNO_CLASS (allocno);
3047 22981641 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3048 : {
3049 725 : fprintf (ira_dump_file, " Popping");
3050 725 : ira_print_expanded_allocno (allocno);
3051 725 : fprintf (ira_dump_file, " -- ");
3052 : }
3053 22981641 : if (aclass == NO_REGS)
3054 : {
3055 0 : ALLOCNO_HARD_REGNO (allocno) = -1;
3056 0 : ALLOCNO_ASSIGNED_P (allocno) = true;
3057 0 : ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
3058 0 : ira_assert
3059 : (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
3060 0 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3061 0 : fprintf (ira_dump_file, "assign memory\n");
3062 : }
3063 22981641 : else if (assign_hard_reg (allocno, false))
3064 : {
3065 21701668 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3066 706 : fprintf (ira_dump_file, " assign reg %d\n",
3067 706 : ALLOCNO_HARD_REGNO (allocno));
3068 : }
3069 1279973 : else if (ALLOCNO_ASSIGNED_P (allocno))
3070 : {
3071 1279973 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3072 19 : fprintf (ira_dump_file, "spill%s\n",
3073 19 : ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p
3074 : ? "" : "!");
3075 : }
3076 22981641 : ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
3077 : }
3078 1240945 : }
3079 :
3080 : /* Set up number of available hard registers for allocno A. */
3081 : static void
3082 22981641 : setup_allocno_available_regs_num (ira_allocno_t a)
3083 : {
3084 22981641 : int i, n, hard_regno, hard_regs_num, nwords;
3085 22981641 : enum reg_class aclass;
3086 22981641 : allocno_color_data_t data;
3087 :
3088 22981641 : aclass = ALLOCNO_CLASS (a);
3089 22981641 : data = ALLOCNO_COLOR_DATA (a);
3090 22981641 : data->available_regs_num = 0;
3091 22981641 : if (aclass == NO_REGS)
3092 : return;
3093 22981641 : hard_regs_num = ira_class_hard_regs_num[aclass];
3094 22981641 : nwords = ALLOCNO_NUM_OBJECTS (a);
3095 348072808 : for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
3096 : {
3097 325091167 : hard_regno = ira_class_hard_regs[aclass][i];
3098 : /* Checking only profitable hard regs. */
3099 325091167 : if (TEST_HARD_REG_BIT (data->profitable_hard_regs, hard_regno))
3100 300867711 : n++;
3101 : }
3102 22981641 : data->available_regs_num = n;
3103 22981641 : if (internal_flag_ira_verbose <= 2 || ira_dump_file == NULL)
3104 : return;
3105 725 : fprintf
3106 725 : (ira_dump_file,
3107 : " Allocno a%dr%d of %s(%d) has %d avail. regs ",
3108 : ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
3109 : reg_class_names[aclass], ira_class_hard_regs_num[aclass], n);
3110 725 : print_hard_reg_set (ira_dump_file, data->profitable_hard_regs, false);
3111 725 : fprintf (ira_dump_file, ", %snode: ",
3112 1450 : data->profitable_hard_regs == data->hard_regs_node->hard_regs->set
3113 : ? "" : "^");
3114 725 : print_hard_reg_set (ira_dump_file,
3115 725 : data->hard_regs_node->hard_regs->set, false);
3116 2175 : for (i = 0; i < nwords; i++)
3117 : {
3118 725 : ira_object_t obj = ALLOCNO_OBJECT (a, i);
3119 :
3120 725 : if (nwords != 1)
3121 : {
3122 0 : if (i != 0)
3123 0 : fprintf (ira_dump_file, ", ");
3124 0 : fprintf (ira_dump_file, " obj %d", i);
3125 : }
3126 725 : fprintf (ira_dump_file, " (confl regs = ");
3127 725 : print_hard_reg_set (ira_dump_file, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
3128 : false);
3129 725 : fprintf (ira_dump_file, ")");
3130 : }
3131 725 : fprintf (ira_dump_file, "\n");
3132 : }
3133 :
3134 : /* Put ALLOCNO in a bucket corresponding to its number and size of its
3135 : conflicting allocnos and hard registers. */
3136 : static void
3137 22981641 : put_allocno_into_bucket (ira_allocno_t allocno)
3138 : {
3139 22981641 : ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
3140 22981641 : setup_allocno_available_regs_num (allocno);
3141 22981641 : if (setup_left_conflict_sizes_p (allocno))
3142 16059822 : add_allocno_to_bucket (allocno, &colorable_allocno_bucket);
3143 : else
3144 6921819 : add_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
3145 22981641 : }
3146 :
3147 : /* Map: allocno number -> allocno priority. */
3148 : static int *allocno_priorities;
3149 :
3150 : /* Set up priorities for N allocnos in array
3151 : CONSIDERATION_ALLOCNOS. */
3152 : static void
3153 460927 : setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
3154 : {
3155 460927 : int i, length, nrefs, priority, max_priority, mult, diff;
3156 460927 : ira_allocno_t a;
3157 :
3158 460927 : max_priority = 0;
3159 12353906 : for (i = 0; i < n; i++)
3160 : {
3161 11892979 : a = consideration_allocnos[i];
3162 11892979 : nrefs = ALLOCNO_NREFS (a);
3163 11892979 : ira_assert (nrefs >= 0);
3164 11892979 : mult = floor_log2 (ALLOCNO_NREFS (a)) + 1;
3165 11892979 : ira_assert (mult >= 0);
3166 11892979 : mult *= ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
3167 11892979 : diff = ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
3168 : /* Multiplication can overflow for very large functions.
3169 : Check the overflow and constrain the result if necessary: */
3170 : #if STAGE0_CXX_HAS_BUILTIN (smul_overflow)
3171 11892979 : if (__builtin_smul_overflow (mult, diff, &priority)
3172 11892979 : || priority < -INT_MAX)
3173 1 : priority = diff >= 0 ? INT_MAX : -INT_MAX;
3174 : #else
3175 : static_assert
3176 : (sizeof (long long) >= 2 * sizeof (int),
3177 : "overflow code does not work for such int and long long sizes");
3178 : long long priorityll = (long long) mult * diff;
3179 : if (priorityll < -INT_MAX || priorityll > INT_MAX)
3180 : priority = diff >= 0 ? INT_MAX : -INT_MAX;
3181 : else
3182 : priority = priorityll;
3183 : #endif
3184 11892979 : allocno_priorities[ALLOCNO_NUM (a)] = priority;
3185 11892979 : if (priority < 0)
3186 : priority = -priority;
3187 11892979 : if (max_priority < priority)
3188 : max_priority = priority;
3189 : }
3190 460927 : mult = max_priority == 0 ? 1 : INT_MAX / max_priority;
3191 12353906 : for (i = 0; i < n; i++)
3192 : {
3193 11892979 : a = consideration_allocnos[i];
3194 11892979 : length = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
3195 11892979 : if (ALLOCNO_NUM_OBJECTS (a) > 1)
3196 849337 : length /= ALLOCNO_NUM_OBJECTS (a);
3197 11892979 : if (length <= 0)
3198 : length = 1;
3199 11892979 : allocno_priorities[ALLOCNO_NUM (a)]
3200 11892979 : = allocno_priorities[ALLOCNO_NUM (a)] * mult / length;
3201 : }
3202 460927 : }
3203 :
3204 : /* Sort allocnos according to the profit of usage of a hard register
3205 : instead of memory for them. */
3206 : static int
3207 2981096 : allocno_cost_compare_func (const void *v1p, const void *v2p)
3208 : {
3209 2981096 : ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
3210 2981096 : ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
3211 2981096 : int c1, c2;
3212 :
3213 2981096 : c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_UPDATED_CLASS_COST (p1);
3214 2981096 : c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_UPDATED_CLASS_COST (p2);
3215 2981096 : if (c1 - c2)
3216 2558020 : return c1 - c2;
3217 :
3218 : /* If regs are equally good, sort by allocno numbers, so that the
3219 : results of qsort leave nothing to chance. */
3220 423076 : return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
3221 : }
3222 :
3223 : /* Return savings on removed copies when ALLOCNO is assigned to
3224 : HARD_REGNO. */
3225 : static int
3226 236139339 : allocno_copy_cost_saving (ira_allocno_t allocno, int hard_regno)
3227 : {
3228 236139339 : int cost = 0;
3229 236139339 : machine_mode allocno_mode = ALLOCNO_MODE (allocno);
3230 236139339 : enum reg_class rclass;
3231 236139339 : ira_copy_t cp, next_cp;
3232 :
3233 236139339 : rclass = REGNO_REG_CLASS (hard_regno);
3234 236139339 : if (ira_reg_class_max_nregs[rclass][allocno_mode]
3235 236139339 : > ira_class_hard_regs_num[rclass])
3236 : /* For the above condition the cost can be wrong. Use the allocno
3237 : class in this case. */
3238 4933895 : rclass = ALLOCNO_CLASS (allocno);
3239 400821684 : for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
3240 : {
3241 164682345 : if (cp->first == allocno)
3242 : {
3243 84202572 : next_cp = cp->next_first_allocno_copy;
3244 84202572 : if (ALLOCNO_HARD_REGNO (cp->second) != hard_regno)
3245 55261412 : continue;
3246 : }
3247 80479773 : else if (cp->second == allocno)
3248 : {
3249 80479773 : next_cp = cp->next_second_allocno_copy;
3250 80479773 : if (ALLOCNO_HARD_REGNO (cp->first) != hard_regno)
3251 51768915 : continue;
3252 : }
3253 : else
3254 0 : gcc_unreachable ();
3255 57652018 : ira_init_register_move_cost_if_necessary (allocno_mode);
3256 57652018 : cost += cp->freq * ira_register_move_cost[allocno_mode][rclass][rclass];
3257 : }
3258 236139339 : return cost;
3259 : }
3260 :
3261 : /* We used Chaitin-Briggs coloring to assign as many pseudos as
3262 : possible to hard registers. Let us try to improve allocation with
3263 : cost point of view. This function improves the allocation by
3264 : spilling some allocnos and assigning the freed hard registers to
3265 : other allocnos if it decreases the overall allocation cost. */
3266 : static void
3267 1240980 : improve_allocation (void)
3268 : {
3269 1240980 : unsigned int i;
3270 1240980 : int j, k, n, hregno, conflict_hregno, base_cost, class_size, word, nwords;
3271 1240980 : int check, spill_cost, min_cost, nregs, conflict_nregs, r, best;
3272 1240980 : bool try_p;
3273 1240980 : enum reg_class aclass;
3274 1240980 : machine_mode mode;
3275 1240980 : int *allocno_costs;
3276 1240980 : int costs[FIRST_PSEUDO_REGISTER];
3277 1240980 : HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
3278 1240980 : ira_allocno_t a;
3279 1240980 : bitmap_iterator bi;
3280 1240980 : int saved_nregs;
3281 1240980 : int add_cost;
3282 :
3283 : /* Don't bother to optimize the code with static chain pointer and
3284 : non-local goto in order not to spill the chain pointer
3285 : pseudo. */
3286 1240980 : if (cfun->static_chain_decl && crtl->has_nonlocal_goto)
3287 1152683 : return;
3288 : /* Clear counts used to process conflicting allocnos only once for
3289 : each allocno. */
3290 26154825 : EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3291 24914206 : ALLOCNO_COLOR_DATA (ira_allocnos[i])->temp = 0;
3292 1240619 : check = n = 0;
3293 : /* Process each allocno and try to assign a hard register to it by
3294 : spilling some its conflicting allocnos. */
3295 26154825 : EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3296 : {
3297 24914206 : a = ira_allocnos[i];
3298 24914206 : ALLOCNO_COLOR_DATA (a)->temp = 0;
3299 49828412 : if (empty_profitable_hard_regs (a))
3300 1932985 : continue;
3301 22981221 : check++;
3302 22981221 : aclass = ALLOCNO_CLASS (a);
3303 22981221 : allocno_costs = ALLOCNO_HARD_REG_COSTS (a);
3304 22981221 : if ((hregno = ALLOCNO_HARD_REGNO (a)) < 0)
3305 1455604 : base_cost = ALLOCNO_UPDATED_MEMORY_COST (a);
3306 21525617 : else if (allocno_costs == NULL)
3307 : /* It means that assigning a hard register is not profitable
3308 : (we don't waste memory for hard register costs in this
3309 : case). */
3310 13635867 : continue;
3311 : else
3312 7889750 : base_cost = (allocno_costs[ira_class_hard_reg_index[aclass][hregno]]
3313 7889750 : - allocno_copy_cost_saving (a, hregno));
3314 9345354 : try_p = false;
3315 9345354 : get_conflict_and_start_profitable_regs (a, false,
3316 : conflicting_regs,
3317 : &profitable_hard_regs);
3318 9345354 : class_size = ira_class_hard_regs_num[aclass];
3319 9345354 : mode = ALLOCNO_MODE (a);
3320 9345354 : HARD_REG_SET dep_filter_allowed;
3321 9345354 : bool dep_filter_p
3322 : = NUM_DEPENDENT_FILTERS && ALLOCNO_DEPENDENT_FILTERS (a);
3323 9345354 : if (dep_filter_p)
3324 : dep_filter_allowed = ira_dependent_filter (a);
3325 : /* Set up cost improvement for usage of each profitable hard
3326 : register for allocno A. */
3327 152658063 : for (j = 0; j < class_size; j++)
3328 : {
3329 143312709 : hregno = ira_class_hard_regs[aclass][j];
3330 143312709 : if (! check_hard_reg_p (a, hregno,
3331 : conflicting_regs, profitable_hard_regs))
3332 22119869 : continue;
3333 121192840 : if (NUM_REGISTER_FILTERS
3334 : && !test_register_filters (ALLOCNO_REGISTER_FILTERS (a), hregno))
3335 : continue;
3336 121192840 : if (dep_filter_p && !TEST_HARD_REG_BIT (dep_filter_allowed, hregno))
3337 : continue;
3338 121192840 : ira_assert (ira_class_hard_reg_index[aclass][hregno] == j);
3339 121192840 : k = allocno_costs == NULL ? 0 : j;
3340 242385680 : costs[hregno] = (allocno_costs == NULL
3341 121192840 : ? ALLOCNO_UPDATED_CLASS_COST (a) : allocno_costs[k]);
3342 121192840 : costs[hregno] -= allocno_copy_cost_saving (a, hregno);
3343 :
3344 121192840 : if ((saved_nregs = calculate_saved_nregs (hregno, mode)) != 0)
3345 : {
3346 : /* We need to save/restore the hard register in
3347 : epilogue/prologue. Therefore we increase the cost. */
3348 18852517 : add_cost = calculate_saved_regs_cost (hregno, mode, saved_nregs);
3349 18852517 : costs[hregno] += add_cost;
3350 : }
3351 :
3352 121192840 : costs[hregno] -= base_cost;
3353 121192840 : if (costs[hregno] < 0)
3354 143312709 : try_p = true;
3355 : }
3356 9345354 : if (! try_p)
3357 : /* There is no chance to improve the allocation cost by
3358 : assigning hard register to allocno A even without spilling
3359 : conflicting allocnos. */
3360 7187541 : continue;
3361 2157813 : mode = ALLOCNO_MODE (a);
3362 2157813 : nwords = ALLOCNO_NUM_OBJECTS (a);
3363 : /* Process each allocno conflicting with A and update the cost
3364 : improvement for profitable hard registers of A. To use a
3365 : hard register for A we need to spill some conflicting
3366 : allocnos and that creates penalty for the cost
3367 : improvement. */
3368 4433016 : for (word = 0; word < nwords; word++)
3369 : {
3370 2275203 : ira_object_t conflict_obj;
3371 2275203 : ira_object_t obj = ALLOCNO_OBJECT (a, word);
3372 2275203 : ira_object_conflict_iterator oci;
3373 :
3374 181126258 : FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3375 : {
3376 178851055 : ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3377 :
3378 178851055 : if (ALLOCNO_COLOR_DATA (conflict_a)->temp == check)
3379 : /* We already processed this conflicting allocno
3380 : because we processed earlier another object of the
3381 : conflicting allocno. */
3382 71794306 : continue;
3383 165900001 : ALLOCNO_COLOR_DATA (conflict_a)->temp = check;
3384 165900001 : if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
3385 58843252 : continue;
3386 107056749 : spill_cost = ALLOCNO_UPDATED_MEMORY_COST (conflict_a);
3387 107056749 : k = (ira_class_hard_reg_index
3388 107056749 : [ALLOCNO_CLASS (conflict_a)][conflict_hregno]);
3389 107056749 : ira_assert (k >= 0);
3390 107056749 : if ((allocno_costs = ALLOCNO_HARD_REG_COSTS (conflict_a))
3391 : != NULL)
3392 35034190 : spill_cost -= allocno_costs[k];
3393 : else
3394 72022559 : spill_cost -= ALLOCNO_UPDATED_CLASS_COST (conflict_a);
3395 107056749 : spill_cost
3396 107056749 : += allocno_copy_cost_saving (conflict_a, conflict_hregno);
3397 107056749 : conflict_nregs = hard_regno_nregs (conflict_hregno,
3398 107056749 : ALLOCNO_MODE (conflict_a));
3399 220148837 : auto note_conflict = [&](int r)
3400 : {
3401 113092088 : if (check_hard_reg_p (a, r,
3402 : conflicting_regs, profitable_hard_regs))
3403 64975266 : costs[r] += spill_cost;
3404 220148837 : };
3405 217606556 : for (r = conflict_hregno;
3406 217606556 : r >= 0 && (int) end_hard_regno (mode, r) > conflict_hregno;
3407 : r--)
3408 110549807 : note_conflict (r);
3409 109599030 : for (r = conflict_hregno + 1;
3410 109599030 : r < conflict_hregno + conflict_nregs;
3411 : r++)
3412 2542281 : note_conflict (r);
3413 : }
3414 : }
3415 : min_cost = INT_MAX;
3416 : best = -1;
3417 : /* Now we choose hard register for A which results in highest
3418 : allocation cost improvement. */
3419 30303704 : for (j = 0; j < class_size; j++)
3420 : {
3421 28145891 : hregno = ira_class_hard_regs[aclass][j];
3422 28145891 : if (NUM_REGISTER_FILTERS
3423 : && !test_register_filters (ALLOCNO_REGISTER_FILTERS (a), hregno))
3424 : continue;
3425 28145891 : if (dep_filter_p && !TEST_HARD_REG_BIT (dep_filter_allowed, hregno))
3426 : continue;
3427 28145891 : if (check_hard_reg_p (a, hregno,
3428 : conflicting_regs, profitable_hard_regs)
3429 28145891 : && min_cost > costs[hregno])
3430 : {
3431 28145891 : best = hregno;
3432 28145891 : min_cost = costs[hregno];
3433 : }
3434 : }
3435 2157813 : if (min_cost >= 0)
3436 : /* We are in a situation when assigning any hard register to A
3437 : by spilling some conflicting allocnos does not improve the
3438 : allocation cost. */
3439 1795663 : continue;
3440 362150 : nregs = hard_regno_nregs (best, mode);
3441 : /* Now spill conflicting allocnos which contain a hard register
3442 : of A when we assign the best chosen hard register to it. */
3443 741740 : for (word = 0; word < nwords; word++)
3444 : {
3445 379590 : ira_object_t conflict_obj;
3446 379590 : ira_object_t obj = ALLOCNO_OBJECT (a, word);
3447 379590 : ira_object_conflict_iterator oci;
3448 :
3449 15118047 : FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3450 : {
3451 14738457 : ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3452 :
3453 14738457 : if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
3454 5798503 : continue;
3455 8939954 : conflict_nregs = hard_regno_nregs (conflict_hregno,
3456 8939954 : ALLOCNO_MODE (conflict_a));
3457 8939954 : if (best + nregs <= conflict_hregno
3458 7131784 : || conflict_hregno + conflict_nregs <= best)
3459 : /* No intersection. */
3460 8654866 : continue;
3461 285088 : ALLOCNO_HARD_REGNO (conflict_a) = -1;
3462 285088 : sorted_allocnos[n++] = conflict_a;
3463 285088 : if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3464 14 : fprintf (ira_dump_file, "Spilling a%dr%d for a%dr%d\n",
3465 : ALLOCNO_NUM (conflict_a), ALLOCNO_REGNO (conflict_a),
3466 : ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3467 : }
3468 : }
3469 : /* Assign the best chosen hard register to A. */
3470 362150 : ALLOCNO_HARD_REGNO (a) = best;
3471 :
3472 362150 : record_allocation (best, nregs);
3473 :
3474 362150 : if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3475 17 : fprintf (ira_dump_file, "Assigning %d to a%dr%d\n",
3476 : best, ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3477 : }
3478 1240619 : if (n == 0)
3479 : return;
3480 : /* We spilled some allocnos to assign their hard registers to other
3481 : allocnos. The spilled allocnos are now in array
3482 : 'sorted_allocnos'. There is still a possibility that some of the
3483 : spilled allocnos can get hard registers. So let us try assign
3484 : them hard registers again (just a reminder -- function
3485 : 'assign_hard_reg' assigns hard registers only if it is possible
3486 : and profitable). We process the spilled allocnos with biggest
3487 : benefit to get hard register first -- see function
3488 : 'allocno_cost_compare_func'. */
3489 88297 : qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
3490 : allocno_cost_compare_func);
3491 461682 : for (j = 0; j < n; j++)
3492 : {
3493 285088 : a = sorted_allocnos[j];
3494 285088 : ALLOCNO_ASSIGNED_P (a) = false;
3495 285088 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3496 : {
3497 14 : fprintf (ira_dump_file, " ");
3498 14 : ira_print_expanded_allocno (a);
3499 14 : fprintf (ira_dump_file, " -- ");
3500 : }
3501 285088 : if (assign_hard_reg (a, false))
3502 : {
3503 68542 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3504 0 : fprintf (ira_dump_file, "assign hard reg %d\n",
3505 0 : ALLOCNO_HARD_REGNO (a));
3506 : }
3507 : else
3508 : {
3509 216546 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3510 14 : fprintf (ira_dump_file, "assign memory\n");
3511 : }
3512 : }
3513 : }
3514 :
3515 : /* Sort allocnos according to their priorities. */
3516 : static int
3517 421122198 : allocno_priority_compare_func (const void *v1p, const void *v2p)
3518 : {
3519 421122198 : ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
3520 421122198 : ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
3521 421122198 : int pri1, pri2, diff;
3522 :
3523 : /* Assign hard reg to static chain pointer pseudo first when
3524 : non-local goto is used. */
3525 421122198 : if ((diff = (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2))
3526 421122198 : - non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))) != 0)
3527 : return diff;
3528 421121264 : pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
3529 421121264 : pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
3530 421121264 : if (pri2 != pri1)
3531 143043830 : return SORTGT (pri2, pri1);
3532 :
3533 : /* If regs are equally good, sort by allocnos, so that the results of
3534 : qsort leave nothing to chance. */
3535 278077434 : return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
3536 : }
3537 :
3538 : /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
3539 : taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
3540 : static void
3541 1240980 : color_allocnos (void)
3542 : {
3543 1240980 : unsigned int i, n;
3544 1240980 : bitmap_iterator bi;
3545 1240980 : ira_allocno_t a;
3546 :
3547 1240980 : setup_profitable_hard_regs ();
3548 26156556 : EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3549 : {
3550 24915576 : allocno_color_data_t data;
3551 24915576 : ira_pref_t pref, next_pref;
3552 :
3553 24915576 : a = ira_allocnos[i];
3554 24915576 : data = ALLOCNO_COLOR_DATA (a);
3555 24915576 : data->conflict_allocno_hard_prefs = 0;
3556 30289584 : for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = next_pref)
3557 : {
3558 5374008 : next_pref = pref->next_pref;
3559 5374008 : if (! ira_hard_reg_in_set_p (pref->hard_regno,
3560 5374008 : ALLOCNO_MODE (a),
3561 : data->profitable_hard_regs))
3562 885103 : ira_remove_pref (pref);
3563 : }
3564 : }
3565 :
3566 1240980 : if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
3567 : {
3568 35 : n = 0;
3569 1008 : EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3570 : {
3571 973 : a = ira_allocnos[i];
3572 973 : if (ALLOCNO_CLASS (a) == NO_REGS)
3573 : {
3574 23 : ALLOCNO_HARD_REGNO (a) = -1;
3575 23 : ALLOCNO_ASSIGNED_P (a) = true;
3576 23 : ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3577 23 : ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3578 23 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3579 : {
3580 0 : fprintf (ira_dump_file, " Spill");
3581 0 : ira_print_expanded_allocno (a);
3582 0 : fprintf (ira_dump_file, "\n");
3583 : }
3584 23 : continue;
3585 : }
3586 950 : sorted_allocnos[n++] = a;
3587 : }
3588 35 : if (n != 0)
3589 : {
3590 32 : setup_allocno_priorities (sorted_allocnos, n);
3591 32 : qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
3592 : allocno_priority_compare_func);
3593 982 : for (i = 0; i < n; i++)
3594 : {
3595 950 : a = sorted_allocnos[i];
3596 950 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3597 : {
3598 0 : fprintf (ira_dump_file, " ");
3599 0 : ira_print_expanded_allocno (a);
3600 0 : fprintf (ira_dump_file, " -- ");
3601 : }
3602 950 : if (assign_hard_reg (a, false))
3603 : {
3604 889 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3605 0 : fprintf (ira_dump_file, "assign hard reg %d\n",
3606 0 : ALLOCNO_HARD_REGNO (a));
3607 : }
3608 : else
3609 : {
3610 61 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3611 0 : fprintf (ira_dump_file, "assign memory\n");
3612 : }
3613 : }
3614 : }
3615 : }
3616 : else
3617 : {
3618 1240945 : form_allocno_hard_regs_nodes_forest ();
3619 1240945 : if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3620 52 : print_hard_regs_forest (ira_dump_file);
3621 26155548 : EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3622 : {
3623 24914603 : a = ira_allocnos[i];
3624 49330369 : if (ALLOCNO_CLASS (a) != NO_REGS && ! empty_profitable_hard_regs (a))
3625 : {
3626 22981641 : ALLOCNO_COLOR_DATA (a)->in_graph_p = true;
3627 22981641 : update_conflict_allocno_hard_prefs (a);
3628 : }
3629 : else
3630 : {
3631 1932962 : ALLOCNO_HARD_REGNO (a) = -1;
3632 1932962 : ALLOCNO_ASSIGNED_P (a) = true;
3633 : /* We don't need updated costs anymore. */
3634 1932962 : ira_free_allocno_updated_costs (a);
3635 1932962 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3636 : {
3637 65 : fprintf (ira_dump_file, " Spill");
3638 65 : ira_print_expanded_allocno (a);
3639 65 : fprintf (ira_dump_file, "\n");
3640 : }
3641 : }
3642 : }
3643 : /* Put the allocnos into the corresponding buckets. */
3644 1240945 : colorable_allocno_bucket = NULL;
3645 1240945 : uncolorable_allocno_bucket = NULL;
3646 26155548 : EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3647 : {
3648 24914603 : a = ira_allocnos[i];
3649 24914603 : if (ALLOCNO_COLOR_DATA (a)->in_graph_p)
3650 22981641 : put_allocno_into_bucket (a);
3651 : }
3652 1240945 : push_allocnos_to_stack ();
3653 1240945 : pop_allocnos_from_stack ();
3654 1240945 : finish_allocno_hard_regs_nodes_forest ();
3655 : }
3656 1240980 : improve_allocation ();
3657 1240980 : }
3658 :
3659 :
3660 :
3661 : /* Output information about the loop given by its LOOP_TREE_NODE. */
3662 : static void
3663 52 : print_loop_title (ira_loop_tree_node_t loop_tree_node)
3664 : {
3665 52 : unsigned int j;
3666 52 : bitmap_iterator bi;
3667 52 : ira_loop_tree_node_t subloop_node, dest_loop_node;
3668 52 : edge e;
3669 52 : edge_iterator ei;
3670 :
3671 52 : if (loop_tree_node->parent == NULL)
3672 45 : fprintf (ira_dump_file,
3673 : "\n Loop 0 (parent -1, header bb%d, depth 0)\n bbs:",
3674 : NUM_FIXED_BLOCKS);
3675 : else
3676 : {
3677 7 : ira_assert (current_loops != NULL && loop_tree_node->loop != NULL);
3678 14 : fprintf (ira_dump_file,
3679 : "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
3680 : loop_tree_node->loop_num, loop_tree_node->parent->loop_num,
3681 7 : loop_tree_node->loop->header->index,
3682 7 : loop_depth (loop_tree_node->loop));
3683 : }
3684 52 : for (subloop_node = loop_tree_node->children;
3685 411 : subloop_node != NULL;
3686 359 : subloop_node = subloop_node->next)
3687 359 : if (subloop_node->bb != NULL)
3688 : {
3689 352 : fprintf (ira_dump_file, " %d", subloop_node->bb->index);
3690 870 : FOR_EACH_EDGE (e, ei, subloop_node->bb->succs)
3691 518 : if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
3692 518 : && ((dest_loop_node = IRA_BB_NODE (e->dest)->parent)
3693 : != loop_tree_node))
3694 17 : fprintf (ira_dump_file, "(->%d:l%d)",
3695 : e->dest->index, dest_loop_node->loop_num);
3696 : }
3697 52 : fprintf (ira_dump_file, "\n all:");
3698 857 : EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
3699 805 : fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
3700 52 : fprintf (ira_dump_file, "\n modified regnos:");
3701 762 : EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
3702 710 : fprintf (ira_dump_file, " %d", j);
3703 52 : fprintf (ira_dump_file, "\n border:");
3704 159 : EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
3705 107 : fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
3706 52 : fprintf (ira_dump_file, "\n Pressure:");
3707 262 : for (j = 0; (int) j < ira_pressure_classes_num; j++)
3708 : {
3709 210 : enum reg_class pclass;
3710 :
3711 210 : pclass = ira_pressure_classes[j];
3712 210 : if (loop_tree_node->reg_pressure[pclass] == 0)
3713 141 : continue;
3714 69 : fprintf (ira_dump_file, " %s=%d", reg_class_names[pclass],
3715 : loop_tree_node->reg_pressure[pclass]);
3716 : }
3717 52 : fprintf (ira_dump_file, "\n");
3718 52 : }
3719 :
3720 : /* Color the allocnos inside loop (in the extreme case it can be all
3721 : of the function) given the corresponding LOOP_TREE_NODE. The
3722 : function is called for each loop during top-down traverse of the
3723 : loop tree. */
3724 : static void
3725 1240980 : color_pass (ira_loop_tree_node_t loop_tree_node)
3726 : {
3727 1240980 : int regno, hard_regno, index = -1, n;
3728 1240980 : int cost;
3729 1240980 : unsigned int j;
3730 1240980 : bitmap_iterator bi;
3731 1240980 : machine_mode mode;
3732 1240980 : enum reg_class rclass, aclass;
3733 1240980 : ira_allocno_t a, subloop_allocno;
3734 1240980 : ira_loop_tree_node_t subloop_node;
3735 :
3736 1240980 : ira_assert (loop_tree_node->bb == NULL);
3737 1240980 : if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3738 52 : print_loop_title (loop_tree_node);
3739 :
3740 1240980 : bitmap_copy (coloring_allocno_bitmap, loop_tree_node->all_allocnos);
3741 1240980 : bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
3742 1240980 : n = 0;
3743 27172026 : EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3744 : {
3745 25931046 : a = ira_allocnos[j];
3746 25931046 : n++;
3747 25931046 : if (! ALLOCNO_ASSIGNED_P (a))
3748 24915576 : continue;
3749 1015470 : bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
3750 : }
3751 1240980 : allocno_color_data
3752 2481960 : = (allocno_color_data_t) ira_allocate (sizeof (struct allocno_color_data)
3753 1240980 : * n);
3754 1240980 : memset (allocno_color_data, 0, sizeof (struct allocno_color_data) * n);
3755 1240980 : curr_allocno_process = 0;
3756 1240980 : n = 0;
3757 27172026 : EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3758 : {
3759 25931046 : a = ira_allocnos[j];
3760 25931046 : ALLOCNO_ADD_DATA (a) = allocno_color_data + n;
3761 25931046 : n++;
3762 : }
3763 1240980 : init_allocno_threads ();
3764 : /* Color all mentioned allocnos including transparent ones. */
3765 1240980 : color_allocnos ();
3766 : /* Process caps. They are processed just once. */
3767 1240980 : if (flag_ira_region == IRA_REGION_MIXED
3768 1240980 : || flag_ira_region == IRA_REGION_ALL)
3769 26559312 : EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
3770 : {
3771 25366623 : a = ira_allocnos[j];
3772 25366623 : if (ALLOCNO_CAP_MEMBER (a) == NULL)
3773 21649623 : continue;
3774 : /* Remove from processing in the next loop. */
3775 3717000 : bitmap_clear_bit (consideration_allocno_bitmap, j);
3776 3717000 : rclass = ALLOCNO_CLASS (a);
3777 3717000 : subloop_allocno = ALLOCNO_CAP_MEMBER (a);
3778 3717000 : subloop_node = ALLOCNO_LOOP_TREE_NODE (subloop_allocno);
3779 3717000 : if (ira_single_region_allocno_p (a, subloop_allocno))
3780 : {
3781 519030 : mode = ALLOCNO_MODE (a);
3782 519030 : hard_regno = ALLOCNO_HARD_REGNO (a);
3783 519030 : if (hard_regno >= 0)
3784 : {
3785 417276 : index = ira_class_hard_reg_index[rclass][hard_regno];
3786 417276 : ira_assert (index >= 0);
3787 : }
3788 519030 : regno = ALLOCNO_REGNO (a);
3789 519030 : ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno));
3790 519030 : ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3791 519030 : ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3792 519030 : if (hard_regno >= 0)
3793 417276 : update_costs_from_copies (subloop_allocno, true, true);
3794 : /* We don't need updated costs anymore. */
3795 519030 : ira_free_allocno_updated_costs (subloop_allocno);
3796 : }
3797 : }
3798 : /* Update costs of the corresponding allocnos (not caps) in the
3799 : subloops. */
3800 1240980 : for (subloop_node = loop_tree_node->subloops;
3801 1411732 : subloop_node != NULL;
3802 170752 : subloop_node = subloop_node->subloop_next)
3803 : {
3804 170752 : ira_assert (subloop_node->bb == NULL);
3805 30263066 : EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3806 : {
3807 30092314 : a = ira_allocnos[j];
3808 30092314 : ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3809 30092314 : mode = ALLOCNO_MODE (a);
3810 30092314 : rclass = ALLOCNO_CLASS (a);
3811 30092314 : hard_regno = ALLOCNO_HARD_REGNO (a);
3812 : /* Use hard register class here. ??? */
3813 30092314 : if (hard_regno >= 0)
3814 : {
3815 25871642 : index = ira_class_hard_reg_index[rclass][hard_regno];
3816 25871642 : ira_assert (index >= 0);
3817 : }
3818 30092314 : regno = ALLOCNO_REGNO (a);
3819 : /* ??? conflict costs */
3820 30092314 : subloop_allocno = subloop_node->regno_allocno_map[regno];
3821 30092314 : if (subloop_allocno == NULL
3822 3241855 : || ALLOCNO_CAP (subloop_allocno) != NULL)
3823 26852078 : continue;
3824 3240236 : ira_assert (ALLOCNO_CLASS (subloop_allocno) == rclass);
3825 3240236 : ira_assert (bitmap_bit_p (subloop_node->all_allocnos,
3826 : ALLOCNO_NUM (subloop_allocno)));
3827 3240236 : if (ira_single_region_allocno_p (a, subloop_allocno)
3828 3240236 : || !ira_subloop_allocnos_can_differ_p (a, hard_regno >= 0,
3829 : false))
3830 : {
3831 496440 : gcc_assert (!ALLOCNO_MIGHT_CONFLICT_WITH_PARENT_P
3832 : (subloop_allocno));
3833 496440 : if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
3834 : {
3835 496440 : ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3836 496440 : ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3837 496440 : if (hard_regno >= 0)
3838 177062 : update_costs_from_copies (subloop_allocno, true, true);
3839 : /* We don't need updated costs anymore. */
3840 496440 : ira_free_allocno_updated_costs (subloop_allocno);
3841 : }
3842 : }
3843 2743796 : else if (hard_regno < 0)
3844 : {
3845 : /* If we allocate a register to SUBLOOP_ALLOCNO, we'll need
3846 : to load the register on entry to the subloop and store
3847 : the register back on exit from the subloop. This incurs
3848 : a fixed cost for all registers. Since UPDATED_MEMORY_COST
3849 : is (and should only be) used relative to the register costs
3850 : for the same allocno, we can subtract this shared register
3851 : cost from the memory cost. */
3852 1575745 : ira_loop_border_costs border_costs (subloop_allocno);
3853 1575745 : ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
3854 1575745 : -= border_costs.spill_outside_loop_cost ();
3855 : }
3856 : else
3857 : {
3858 1168051 : ira_loop_border_costs border_costs (subloop_allocno);
3859 1168051 : aclass = ALLOCNO_CLASS (subloop_allocno);
3860 1168051 : ira_init_register_move_cost_if_necessary (mode);
3861 1168051 : cost = border_costs.move_between_loops_cost ();
3862 1168051 : ira_allocate_and_set_or_copy_costs
3863 1168051 : (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno), aclass,
3864 : ALLOCNO_UPDATED_CLASS_COST (subloop_allocno),
3865 : ALLOCNO_HARD_REG_COSTS (subloop_allocno));
3866 1168051 : ira_allocate_and_set_or_copy_costs
3867 1168051 : (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno),
3868 : aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno));
3869 1168051 : ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
3870 1168051 : ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
3871 1168051 : -= cost;
3872 1168051 : if (ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
3873 1168051 : > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index])
3874 1124530 : ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
3875 1124530 : = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index];
3876 : /* If we spill SUBLOOP_ALLOCNO, we'll need to store HARD_REGNO
3877 : on entry to the subloop and restore HARD_REGNO on exit from
3878 : the subloop. */
3879 1168051 : ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
3880 1168051 : += border_costs.spill_inside_loop_cost ();
3881 : }
3882 : }
3883 : }
3884 1240980 : ira_free (allocno_color_data);
3885 23455026 : EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3886 : {
3887 22214046 : a = ira_allocnos[j];
3888 22214046 : ALLOCNO_ADD_DATA (a) = NULL;
3889 : }
3890 1240980 : }
3891 :
3892 : /* Initialize the common data for coloring and calls functions to do
3893 : Chaitin-Briggs and regional coloring. */
3894 : static void
3895 1070228 : do_coloring (void)
3896 : {
3897 1070228 : coloring_allocno_bitmap = ira_allocate_bitmap ();
3898 1070228 : if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3899 45 : fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
3900 :
3901 1070228 : ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
3902 :
3903 1070228 : if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3904 45 : ira_print_disposition (ira_dump_file);
3905 :
3906 1070228 : ira_free_bitmap (coloring_allocno_bitmap);
3907 1070228 : }
3908 :
3909 :
3910 :
3911 : /* Move spill/restore code, which are to be generated in ira-emit.cc,
3912 : to less frequent points (if it is profitable) by reassigning some
3913 : allocnos (in loop with subloops containing in another loop) to
3914 : memory which results in longer live-range where the corresponding
3915 : pseudo-registers will be in memory. */
3916 : static void
3917 1070228 : move_spill_restore (void)
3918 : {
3919 1074735 : int cost, regno, hard_regno, hard_regno2, index;
3920 1074735 : bool changed_p;
3921 1074735 : machine_mode mode;
3922 1074735 : enum reg_class rclass;
3923 1074735 : ira_allocno_t a, parent_allocno, subloop_allocno;
3924 1074735 : ira_loop_tree_node_t parent, loop_node, subloop_node;
3925 1074735 : ira_allocno_iterator ai;
3926 :
3927 1074735 : for (;;)
3928 : {
3929 1074735 : changed_p = false;
3930 1074735 : if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3931 46 : fprintf (ira_dump_file, "New iteration of spill/restore move\n");
3932 33642183 : FOR_EACH_ALLOCNO (a, ai)
3933 : {
3934 32567448 : regno = ALLOCNO_REGNO (a);
3935 32567448 : loop_node = ALLOCNO_LOOP_TREE_NODE (a);
3936 63498877 : if (ALLOCNO_CAP_MEMBER (a) != NULL
3937 25410572 : || ALLOCNO_CAP (a) != NULL
3938 22496126 : || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
3939 17894010 : || loop_node->children == NULL
3940 : /* don't do the optimization because it can create
3941 : copies and the reload pass can spill the allocno set
3942 : by copy although the allocno will not get memory
3943 : slot. */
3944 17894010 : || ira_equiv_no_lvalue_p (regno)
3945 16040335 : || !bitmap_bit_p (loop_node->border_allocnos, ALLOCNO_NUM (a))
3946 : /* Do not spill static chain pointer pseudo when
3947 : non-local goto is used. */
3948 34203467 : || non_spilled_static_chain_regno_p (regno))
3949 30931429 : continue;
3950 1636019 : mode = ALLOCNO_MODE (a);
3951 1636019 : rclass = ALLOCNO_CLASS (a);
3952 1636019 : index = ira_class_hard_reg_index[rclass][hard_regno];
3953 1636019 : ira_assert (index >= 0);
3954 3272038 : cost = (ALLOCNO_MEMORY_COST (a)
3955 1636019 : - (ALLOCNO_HARD_REG_COSTS (a) == NULL
3956 1636019 : ? ALLOCNO_CLASS_COST (a)
3957 326922 : : ALLOCNO_HARD_REG_COSTS (a)[index]));
3958 1636019 : ira_init_register_move_cost_if_necessary (mode);
3959 1636019 : for (subloop_node = loop_node->subloops;
3960 2291497 : subloop_node != NULL;
3961 655478 : subloop_node = subloop_node->subloop_next)
3962 : {
3963 655478 : ira_assert (subloop_node->bb == NULL);
3964 655478 : subloop_allocno = subloop_node->regno_allocno_map[regno];
3965 655478 : if (subloop_allocno == NULL)
3966 69712 : continue;
3967 585766 : ira_assert (rclass == ALLOCNO_CLASS (subloop_allocno));
3968 585766 : ira_loop_border_costs border_costs (subloop_allocno);
3969 :
3970 : /* We have accumulated cost. To get the real cost of
3971 : allocno usage in the loop we should subtract the costs
3972 : added by propagate_allocno_info for the subloop allocnos. */
3973 483163 : int reg_cost
3974 585766 : = (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
3975 585766 : ? ALLOCNO_CLASS_COST (subloop_allocno)
3976 102603 : : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]);
3977 :
3978 585766 : int spill_cost
3979 585766 : = (border_costs.spill_inside_loop_cost ()
3980 585766 : + ALLOCNO_MEMORY_COST (subloop_allocno));
3981 :
3982 : /* If HARD_REGNO conflicts with SUBLOOP_A then
3983 : propagate_allocno_info will have propagated
3984 : the cost of spilling HARD_REGNO in SUBLOOP_NODE.
3985 : (ira_subloop_allocnos_can_differ_p must be true
3986 : in that case.) If HARD_REGNO is a caller-saved
3987 : register, we might have modelled it in the same way.
3988 :
3989 : Otherwise, SPILL_COST acted as a cap on the propagated
3990 : register cost, in cases where the allocations can differ. */
3991 585766 : auto conflicts = ira_total_conflict_hard_regs (subloop_allocno);
3992 585766 : if (TEST_HARD_REG_BIT (conflicts, hard_regno)
3993 585766 : || (ira_need_caller_save_p (subloop_allocno, hard_regno)
3994 12346 : && ira_caller_save_loop_spill_p (a, subloop_allocno,
3995 : spill_cost)))
3996 : reg_cost = spill_cost;
3997 574498 : else if (ira_subloop_allocnos_can_differ_p (a))
3998 582454 : reg_cost = MIN (reg_cost, spill_cost);
3999 :
4000 585766 : cost -= ALLOCNO_MEMORY_COST (subloop_allocno) - reg_cost;
4001 :
4002 585766 : if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
4003 : /* The register was spilled in the subloop. If we spill
4004 : it in the outer loop too then we'll no longer need to
4005 : save the register on entry to the subloop and restore
4006 : the register on exit from the subloop. */
4007 74387 : cost -= border_costs.spill_inside_loop_cost ();
4008 : else
4009 : {
4010 : /* The register was also allocated in the subloop. If we
4011 : spill it in the outer loop then we'll need to load the
4012 : register on entry to the subloop and store the register
4013 : back on exit from the subloop. */
4014 511379 : cost += border_costs.spill_outside_loop_cost ();
4015 511379 : if (hard_regno2 != hard_regno)
4016 28165 : cost -= border_costs.move_between_loops_cost ();
4017 : }
4018 : }
4019 1636019 : if ((parent = loop_node->parent) != NULL
4020 1636019 : && (parent_allocno = parent->regno_allocno_map[regno]) != NULL)
4021 : {
4022 1636019 : ira_assert (rclass == ALLOCNO_CLASS (parent_allocno));
4023 1636019 : ira_loop_border_costs border_costs (a);
4024 1636019 : if ((hard_regno2 = ALLOCNO_HARD_REGNO (parent_allocno)) < 0)
4025 : /* The register was spilled in the parent loop. If we spill
4026 : it in this loop too then we'll no longer need to load the
4027 : register on entry to this loop and save the register back
4028 : on exit from this loop. */
4029 64914 : cost -= border_costs.spill_outside_loop_cost ();
4030 : else
4031 : {
4032 : /* The register was also allocated in the parent loop.
4033 : If we spill it in this loop then we'll need to save
4034 : the register on entry to this loop and restore the
4035 : register on exit from this loop. */
4036 1571105 : cost += border_costs.spill_inside_loop_cost ();
4037 1571105 : if (hard_regno2 != hard_regno)
4038 101906 : cost -= border_costs.move_between_loops_cost ();
4039 : }
4040 : }
4041 1636019 : if (cost < 0)
4042 : {
4043 13142 : ALLOCNO_HARD_REGNO (a) = -1;
4044 13142 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4045 : {
4046 2 : fprintf
4047 2 : (ira_dump_file,
4048 : " Moving spill/restore for a%dr%d up from loop %d",
4049 : ALLOCNO_NUM (a), regno, loop_node->loop_num);
4050 2 : fprintf (ira_dump_file, " - profit %d\n", -cost);
4051 : }
4052 32567448 : changed_p = true;
4053 : }
4054 : }
4055 1074735 : if (! changed_p)
4056 : break;
4057 : }
4058 1070228 : }
4059 :
4060 :
4061 :
4062 : /* Update current hard reg costs and current conflict hard reg costs
4063 : for allocno A. It is done by processing its copies containing
4064 : other allocnos already assigned. */
4065 : static void
4066 0 : update_curr_costs (ira_allocno_t a)
4067 : {
4068 0 : int i, hard_regno, cost;
4069 0 : machine_mode mode;
4070 0 : enum reg_class aclass, rclass;
4071 0 : ira_allocno_t another_a;
4072 0 : ira_copy_t cp, next_cp;
4073 :
4074 0 : ira_free_allocno_updated_costs (a);
4075 0 : ira_assert (! ALLOCNO_ASSIGNED_P (a));
4076 0 : aclass = ALLOCNO_CLASS (a);
4077 0 : if (aclass == NO_REGS)
4078 : return;
4079 0 : mode = ALLOCNO_MODE (a);
4080 0 : ira_init_register_move_cost_if_necessary (mode);
4081 0 : for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
4082 : {
4083 0 : if (cp->first == a)
4084 : {
4085 0 : next_cp = cp->next_first_allocno_copy;
4086 0 : another_a = cp->second;
4087 : }
4088 0 : else if (cp->second == a)
4089 : {
4090 0 : next_cp = cp->next_second_allocno_copy;
4091 0 : another_a = cp->first;
4092 : }
4093 : else
4094 0 : gcc_unreachable ();
4095 0 : if (! ira_reg_classes_intersect_p[aclass][ALLOCNO_CLASS (another_a)]
4096 0 : || ! ALLOCNO_ASSIGNED_P (another_a)
4097 0 : || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
4098 0 : continue;
4099 0 : rclass = REGNO_REG_CLASS (hard_regno);
4100 0 : i = ira_class_hard_reg_index[aclass][hard_regno];
4101 0 : if (i < 0)
4102 0 : continue;
4103 0 : cost = (cp->first == a
4104 0 : ? ira_register_move_cost[mode][rclass][aclass]
4105 0 : : ira_register_move_cost[mode][aclass][rclass]);
4106 0 : ira_allocate_and_set_or_copy_costs
4107 0 : (&ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a),
4108 : ALLOCNO_HARD_REG_COSTS (a));
4109 0 : ira_allocate_and_set_or_copy_costs
4110 0 : (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
4111 : aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
4112 0 : ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
4113 0 : ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
4114 : }
4115 : }
4116 :
4117 : /* Try to assign hard registers to the unassigned allocnos and
4118 : allocnos conflicting with them or conflicting with allocnos whose
4119 : regno >= START_REGNO. The function is called after ira_flattening,
4120 : so more allocnos (including ones created in ira-emit.cc) will have a
4121 : chance to get a hard register. We use simple assignment algorithm
4122 : based on priorities. */
4123 : void
4124 0 : ira_reassign_conflict_allocnos (int start_regno)
4125 : {
4126 0 : int i, allocnos_to_color_num;
4127 0 : ira_allocno_t a;
4128 0 : enum reg_class aclass;
4129 0 : bitmap allocnos_to_color;
4130 0 : ira_allocno_iterator ai;
4131 :
4132 0 : allocnos_to_color = ira_allocate_bitmap ();
4133 0 : allocnos_to_color_num = 0;
4134 0 : FOR_EACH_ALLOCNO (a, ai)
4135 : {
4136 0 : int n = ALLOCNO_NUM_OBJECTS (a);
4137 :
4138 0 : if (! ALLOCNO_ASSIGNED_P (a)
4139 0 : && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
4140 : {
4141 0 : if (ALLOCNO_CLASS (a) != NO_REGS)
4142 0 : sorted_allocnos[allocnos_to_color_num++] = a;
4143 : else
4144 : {
4145 0 : ALLOCNO_ASSIGNED_P (a) = true;
4146 0 : ALLOCNO_HARD_REGNO (a) = -1;
4147 0 : ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
4148 0 : ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
4149 : }
4150 0 : bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
4151 : }
4152 0 : if (ALLOCNO_REGNO (a) < start_regno
4153 0 : || (aclass = ALLOCNO_CLASS (a)) == NO_REGS)
4154 0 : continue;
4155 0 : for (i = 0; i < n; i++)
4156 : {
4157 0 : ira_object_t obj = ALLOCNO_OBJECT (a, i);
4158 0 : ira_object_t conflict_obj;
4159 0 : ira_object_conflict_iterator oci;
4160 :
4161 0 : FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
4162 : {
4163 0 : ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
4164 :
4165 0 : ira_assert (ira_reg_classes_intersect_p
4166 : [aclass][ALLOCNO_CLASS (conflict_a)]);
4167 0 : if (!bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
4168 0 : continue;
4169 0 : sorted_allocnos[allocnos_to_color_num++] = conflict_a;
4170 : }
4171 : }
4172 : }
4173 0 : ira_free_bitmap (allocnos_to_color);
4174 0 : if (allocnos_to_color_num > 1)
4175 : {
4176 0 : setup_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
4177 0 : qsort (sorted_allocnos, allocnos_to_color_num, sizeof (ira_allocno_t),
4178 : allocno_priority_compare_func);
4179 : }
4180 0 : for (i = 0; i < allocnos_to_color_num; i++)
4181 : {
4182 0 : a = sorted_allocnos[i];
4183 0 : ALLOCNO_ASSIGNED_P (a) = false;
4184 0 : update_curr_costs (a);
4185 : }
4186 0 : for (i = 0; i < allocnos_to_color_num; i++)
4187 : {
4188 0 : a = sorted_allocnos[i];
4189 0 : if (assign_hard_reg (a, true))
4190 : {
4191 0 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4192 0 : fprintf
4193 0 : (ira_dump_file,
4194 : " Secondary allocation: assign hard reg %d to reg %d\n",
4195 0 : ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
4196 : }
4197 : }
4198 0 : }
4199 :
4200 :
4201 :
4202 : /* This page contains functions used to find conflicts using allocno
4203 : live ranges. */
4204 :
4205 : #ifdef ENABLE_IRA_CHECKING
4206 :
4207 : /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
4208 : intersect. This should be used when there is only one region.
4209 : Currently this is used during reload. */
4210 : static bool
4211 0 : conflict_by_live_ranges_p (int regno1, int regno2)
4212 : {
4213 0 : ira_allocno_t a1, a2;
4214 :
4215 0 : ira_assert (regno1 >= FIRST_PSEUDO_REGISTER
4216 : && regno2 >= FIRST_PSEUDO_REGISTER);
4217 : /* Reg info calculated by dataflow infrastructure can be different
4218 : from one calculated by regclass. */
4219 0 : if ((a1 = ira_loop_tree_root->regno_allocno_map[regno1]) == NULL
4220 0 : || (a2 = ira_loop_tree_root->regno_allocno_map[regno2]) == NULL)
4221 : return false;
4222 0 : return allocnos_conflict_by_live_ranges_p (a1, a2);
4223 : }
4224 :
4225 : #endif
4226 :
4227 :
4228 :
4229 : /* This page contains code to coalesce memory stack slots used by
4230 : spilled allocnos. This results in smaller stack frame, better data
4231 : locality, and in smaller code for some architectures like
4232 : x86/x86_64 where insn size depends on address displacement value.
4233 : On the other hand, it can worsen insn scheduling after the RA but
4234 : in practice it is less important than smaller stack frames. */
4235 :
4236 : /* TRUE if we coalesced some allocnos. In other words, if we got
4237 : loops formed by members first_coalesced_allocno and
4238 : next_coalesced_allocno containing more one allocno. */
4239 : static bool allocno_coalesced_p;
4240 :
4241 : /* Bitmap used to prevent a repeated allocno processing because of
4242 : coalescing. */
4243 : static bitmap processed_coalesced_allocno_bitmap;
4244 :
4245 : /* See below. */
4246 : typedef struct coalesce_data *coalesce_data_t;
4247 :
4248 : /* To decrease footprint of ira_allocno structure we store all data
4249 : needed only for coalescing in the following structure. */
4250 : struct coalesce_data
4251 : {
4252 : /* Coalesced allocnos form a cyclic list. One allocno given by
4253 : FIRST represents all coalesced allocnos. The
4254 : list is chained by NEXT. */
4255 : ira_allocno_t first;
4256 : ira_allocno_t next;
4257 : int temp;
4258 : };
4259 :
4260 : /* Container for storing allocno data concerning coalescing. */
4261 : static coalesce_data_t allocno_coalesce_data;
4262 :
4263 : /* Macro to access the data concerning coalescing. */
4264 : #define ALLOCNO_COALESCE_DATA(a) ((coalesce_data_t) ALLOCNO_ADD_DATA (a))
4265 :
4266 : /* Merge two sets of coalesced allocnos given correspondingly by
4267 : allocnos A1 and A2 (more accurately merging A2 set into A1
4268 : set). */
4269 : static void
4270 0 : merge_allocnos (ira_allocno_t a1, ira_allocno_t a2)
4271 : {
4272 0 : ira_allocno_t a, first, last, next;
4273 :
4274 0 : first = ALLOCNO_COALESCE_DATA (a1)->first;
4275 0 : a = ALLOCNO_COALESCE_DATA (a2)->first;
4276 0 : if (first == a)
4277 : return;
4278 0 : for (last = a2, a = ALLOCNO_COALESCE_DATA (a2)->next;;
4279 0 : a = ALLOCNO_COALESCE_DATA (a)->next)
4280 : {
4281 0 : ALLOCNO_COALESCE_DATA (a)->first = first;
4282 0 : if (a == a2)
4283 : break;
4284 0 : last = a;
4285 : }
4286 0 : next = allocno_coalesce_data[ALLOCNO_NUM (first)].next;
4287 0 : allocno_coalesce_data[ALLOCNO_NUM (first)].next = a2;
4288 0 : allocno_coalesce_data[ALLOCNO_NUM (last)].next = next;
4289 : }
4290 :
4291 : /* Return TRUE if there are conflicting allocnos from two sets of
4292 : coalesced allocnos given correspondingly by allocnos A1 and A2. We
4293 : use live ranges to find conflicts because conflicts are represented
4294 : only for allocnos of the same allocno class and during the reload
4295 : pass we coalesce allocnos for sharing stack memory slots. */
4296 : static bool
4297 0 : coalesced_allocno_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
4298 : {
4299 0 : ira_allocno_t a, conflict_a;
4300 :
4301 0 : if (allocno_coalesced_p)
4302 : {
4303 0 : bitmap_clear (processed_coalesced_allocno_bitmap);
4304 0 : for (a = ALLOCNO_COALESCE_DATA (a1)->next;;
4305 0 : a = ALLOCNO_COALESCE_DATA (a)->next)
4306 : {
4307 0 : bitmap_set_bit (processed_coalesced_allocno_bitmap, ALLOCNO_NUM (a));
4308 0 : if (a == a1)
4309 : break;
4310 : }
4311 : }
4312 0 : for (a = ALLOCNO_COALESCE_DATA (a2)->next;;
4313 0 : a = ALLOCNO_COALESCE_DATA (a)->next)
4314 : {
4315 0 : for (conflict_a = ALLOCNO_COALESCE_DATA (a1)->next;;
4316 0 : conflict_a = ALLOCNO_COALESCE_DATA (conflict_a)->next)
4317 : {
4318 0 : if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
4319 : return true;
4320 0 : if (conflict_a == a1)
4321 : break;
4322 : }
4323 0 : if (a == a2)
4324 : break;
4325 : }
4326 : return false;
4327 : }
4328 :
4329 : /* The major function for aggressive allocno coalescing. We coalesce
4330 : only spilled allocnos. If some allocnos have been coalesced, we
4331 : set up flag allocno_coalesced_p. */
4332 : static void
4333 0 : coalesce_allocnos (void)
4334 : {
4335 0 : ira_allocno_t a;
4336 0 : ira_copy_t cp, next_cp;
4337 0 : unsigned int j;
4338 0 : int i, n, cp_num, regno;
4339 0 : bitmap_iterator bi;
4340 :
4341 0 : cp_num = 0;
4342 : /* Collect copies. */
4343 0 : EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
4344 : {
4345 0 : a = ira_allocnos[j];
4346 0 : regno = ALLOCNO_REGNO (a);
4347 0 : if (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
4348 0 : || ira_equiv_no_lvalue_p (regno))
4349 0 : continue;
4350 0 : for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
4351 : {
4352 0 : if (cp->first == a)
4353 : {
4354 0 : next_cp = cp->next_first_allocno_copy;
4355 0 : regno = ALLOCNO_REGNO (cp->second);
4356 : /* For priority coloring we coalesce allocnos only with
4357 : the same allocno class not with intersected allocno
4358 : classes as it were possible. It is done for
4359 : simplicity. */
4360 0 : if ((cp->insn != NULL || cp->constraint_p)
4361 0 : && ALLOCNO_ASSIGNED_P (cp->second)
4362 0 : && ALLOCNO_HARD_REGNO (cp->second) < 0
4363 0 : && ! ira_equiv_no_lvalue_p (regno))
4364 0 : sorted_copies[cp_num++] = cp;
4365 : }
4366 0 : else if (cp->second == a)
4367 0 : next_cp = cp->next_second_allocno_copy;
4368 : else
4369 0 : gcc_unreachable ();
4370 : }
4371 : }
4372 0 : qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
4373 : /* Coalesced copies, most frequently executed first. */
4374 0 : for (; cp_num != 0;)
4375 : {
4376 0 : for (i = 0; i < cp_num; i++)
4377 : {
4378 0 : cp = sorted_copies[i];
4379 0 : if (! coalesced_allocno_conflict_p (cp->first, cp->second))
4380 : {
4381 0 : allocno_coalesced_p = true;
4382 0 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4383 0 : fprintf
4384 0 : (ira_dump_file,
4385 : " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
4386 0 : cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
4387 0 : ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
4388 : cp->freq);
4389 0 : merge_allocnos (cp->first, cp->second);
4390 0 : i++;
4391 0 : break;
4392 : }
4393 : }
4394 : /* Collect the rest of copies. */
4395 0 : for (n = 0; i < cp_num; i++)
4396 : {
4397 0 : cp = sorted_copies[i];
4398 0 : if (allocno_coalesce_data[ALLOCNO_NUM (cp->first)].first
4399 0 : != allocno_coalesce_data[ALLOCNO_NUM (cp->second)].first)
4400 0 : sorted_copies[n++] = cp;
4401 : }
4402 : cp_num = n;
4403 : }
4404 0 : }
4405 :
4406 : /* Usage cost and order number of coalesced allocno set to which
4407 : given pseudo register belongs to. */
4408 : static int *regno_coalesced_allocno_cost;
4409 : static int *regno_coalesced_allocno_num;
4410 :
4411 : /* Sort pseudos according frequencies of coalesced allocno sets they
4412 : belong to (putting most frequently ones first), and according to
4413 : coalesced allocno set order numbers. */
4414 : static int
4415 0 : coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
4416 : {
4417 0 : const int regno1 = *(const int *) v1p;
4418 0 : const int regno2 = *(const int *) v2p;
4419 0 : int diff;
4420 :
4421 0 : if ((diff = (regno_coalesced_allocno_cost[regno2]
4422 0 : - regno_coalesced_allocno_cost[regno1])) != 0)
4423 : return diff;
4424 0 : if ((diff = (regno_coalesced_allocno_num[regno1]
4425 0 : - regno_coalesced_allocno_num[regno2])) != 0)
4426 : return diff;
4427 0 : return regno1 - regno2;
4428 : }
4429 :
4430 : /* Widest width in which each pseudo reg is referred to (via subreg).
4431 : It is used for sorting pseudo registers. */
4432 : static machine_mode *regno_max_ref_mode;
4433 :
4434 : /* Sort pseudos according their slot numbers (putting ones with
4435 : smaller numbers first, or last when the frame pointer is not
4436 : needed). */
4437 : static int
4438 0 : coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
4439 : {
4440 0 : const int regno1 = *(const int *) v1p;
4441 0 : const int regno2 = *(const int *) v2p;
4442 0 : ira_allocno_t a1 = ira_regno_allocno_map[regno1];
4443 0 : ira_allocno_t a2 = ira_regno_allocno_map[regno2];
4444 0 : int diff, slot_num1, slot_num2;
4445 0 : machine_mode mode1, mode2;
4446 :
4447 0 : if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
4448 : {
4449 0 : if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
4450 0 : return regno1 - regno2;
4451 : return 1;
4452 : }
4453 0 : else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
4454 : return -1;
4455 0 : slot_num1 = -ALLOCNO_HARD_REGNO (a1);
4456 0 : slot_num2 = -ALLOCNO_HARD_REGNO (a2);
4457 0 : if ((diff = slot_num1 - slot_num2) != 0)
4458 0 : return (frame_pointer_needed
4459 0 : || (!FRAME_GROWS_DOWNWARD) == STACK_GROWS_DOWNWARD ? diff : -diff);
4460 0 : mode1 = wider_subreg_mode (PSEUDO_REGNO_MODE (regno1),
4461 0 : regno_max_ref_mode[regno1]);
4462 0 : mode2 = wider_subreg_mode (PSEUDO_REGNO_MODE (regno2),
4463 0 : regno_max_ref_mode[regno2]);
4464 0 : if ((diff = compare_sizes_for_sort (GET_MODE_SIZE (mode2),
4465 0 : GET_MODE_SIZE (mode1))) != 0)
4466 : return diff;
4467 0 : return regno1 - regno2;
4468 : }
4469 :
4470 : /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
4471 : for coalesced allocno sets containing allocnos with their regnos
4472 : given in array PSEUDO_REGNOS of length N. */
4473 : static void
4474 0 : setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
4475 : {
4476 0 : int i, num, regno, cost;
4477 0 : ira_allocno_t allocno, a;
4478 :
4479 0 : for (num = i = 0; i < n; i++)
4480 : {
4481 0 : regno = pseudo_regnos[i];
4482 0 : allocno = ira_regno_allocno_map[regno];
4483 0 : if (allocno == NULL)
4484 : {
4485 0 : regno_coalesced_allocno_cost[regno] = 0;
4486 0 : regno_coalesced_allocno_num[regno] = ++num;
4487 0 : continue;
4488 : }
4489 0 : if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
4490 0 : continue;
4491 0 : num++;
4492 0 : for (cost = 0, a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4493 0 : a = ALLOCNO_COALESCE_DATA (a)->next)
4494 : {
4495 0 : cost += ALLOCNO_FREQ (a);
4496 0 : if (a == allocno)
4497 : break;
4498 : }
4499 0 : for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4500 0 : a = ALLOCNO_COALESCE_DATA (a)->next)
4501 : {
4502 0 : regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
4503 0 : regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
4504 0 : if (a == allocno)
4505 : break;
4506 : }
4507 : }
4508 0 : }
4509 :
4510 : /* Collect spilled allocnos representing coalesced allocno sets (the
4511 : first coalesced allocno). The collected allocnos are returned
4512 : through array SPILLED_COALESCED_ALLOCNOS. The function returns the
4513 : number of the collected allocnos. The allocnos are given by their
4514 : regnos in array PSEUDO_REGNOS of length N. */
4515 : static int
4516 0 : collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
4517 : ira_allocno_t *spilled_coalesced_allocnos)
4518 : {
4519 0 : int i, num, regno;
4520 0 : ira_allocno_t allocno;
4521 :
4522 0 : for (num = i = 0; i < n; i++)
4523 : {
4524 0 : regno = pseudo_regnos[i];
4525 0 : allocno = ira_regno_allocno_map[regno];
4526 0 : if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
4527 0 : || ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
4528 0 : continue;
4529 0 : spilled_coalesced_allocnos[num++] = allocno;
4530 : }
4531 0 : return num;
4532 : }
4533 :
4534 : /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
4535 : given slot contains live ranges of coalesced allocnos assigned to
4536 : given slot. */
4537 : static live_range_t *slot_coalesced_allocnos_live_ranges;
4538 :
4539 : /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
4540 : ranges intersected with live ranges of coalesced allocnos assigned
4541 : to slot with number N. */
4542 : static bool
4543 0 : slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno, int n)
4544 : {
4545 0 : ira_allocno_t a;
4546 :
4547 0 : for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4548 0 : a = ALLOCNO_COALESCE_DATA (a)->next)
4549 : {
4550 0 : int i;
4551 0 : int nr = ALLOCNO_NUM_OBJECTS (a);
4552 0 : gcc_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
4553 0 : for (i = 0; i < nr; i++)
4554 : {
4555 0 : ira_object_t obj = ALLOCNO_OBJECT (a, i);
4556 :
4557 0 : if (ira_live_ranges_intersect_p
4558 0 : (slot_coalesced_allocnos_live_ranges[n],
4559 : OBJECT_LIVE_RANGES (obj)))
4560 : return true;
4561 : }
4562 0 : if (a == allocno)
4563 : break;
4564 0 : }
4565 : return false;
4566 : }
4567 :
4568 : /* Update live ranges of slot to which coalesced allocnos represented
4569 : by ALLOCNO were assigned. */
4570 : static void
4571 0 : setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno)
4572 : {
4573 0 : int i, n;
4574 0 : ira_allocno_t a;
4575 0 : live_range_t r;
4576 :
4577 0 : n = ALLOCNO_COALESCE_DATA (allocno)->temp;
4578 0 : for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4579 0 : a = ALLOCNO_COALESCE_DATA (a)->next)
4580 : {
4581 0 : int nr = ALLOCNO_NUM_OBJECTS (a);
4582 0 : gcc_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
4583 0 : for (i = 0; i < nr; i++)
4584 : {
4585 0 : ira_object_t obj = ALLOCNO_OBJECT (a, i);
4586 :
4587 0 : r = ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj));
4588 0 : slot_coalesced_allocnos_live_ranges[n]
4589 0 : = ira_merge_live_ranges
4590 0 : (slot_coalesced_allocnos_live_ranges[n], r);
4591 : }
4592 0 : if (a == allocno)
4593 : break;
4594 0 : }
4595 0 : }
4596 :
4597 : /* We have coalesced allocnos involving in copies. Coalesce allocnos
4598 : further in order to share the same memory stack slot. Allocnos
4599 : representing sets of allocnos coalesced before the call are given
4600 : in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
4601 : some allocnos were coalesced in the function. */
4602 : static bool
4603 0 : coalesce_spill_slots (ira_allocno_t *spilled_coalesced_allocnos, int num)
4604 : {
4605 0 : int i, j, n, last_coalesced_allocno_num;
4606 0 : ira_allocno_t allocno, a;
4607 0 : bool merged_p = false;
4608 0 : bitmap set_jump_crosses = regstat_get_setjmp_crosses ();
4609 :
4610 0 : slot_coalesced_allocnos_live_ranges
4611 0 : = (live_range_t *) ira_allocate (sizeof (live_range_t) * ira_allocnos_num);
4612 0 : memset (slot_coalesced_allocnos_live_ranges, 0,
4613 0 : sizeof (live_range_t) * ira_allocnos_num);
4614 0 : last_coalesced_allocno_num = 0;
4615 : /* Coalesce non-conflicting spilled allocnos preferring most
4616 : frequently used. */
4617 0 : for (i = 0; i < num; i++)
4618 : {
4619 0 : allocno = spilled_coalesced_allocnos[i];
4620 0 : if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
4621 0 : || bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (allocno))
4622 0 : || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
4623 0 : continue;
4624 0 : for (j = 0; j < i; j++)
4625 : {
4626 0 : a = spilled_coalesced_allocnos[j];
4627 0 : n = ALLOCNO_COALESCE_DATA (a)->temp;
4628 0 : if (ALLOCNO_COALESCE_DATA (a)->first == a
4629 0 : && ! bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (a))
4630 0 : && ! ira_equiv_no_lvalue_p (ALLOCNO_REGNO (a))
4631 0 : && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno, n))
4632 : break;
4633 : }
4634 0 : if (j >= i)
4635 : {
4636 : /* No coalescing: set up number for coalesced allocnos
4637 : represented by ALLOCNO. */
4638 0 : ALLOCNO_COALESCE_DATA (allocno)->temp = last_coalesced_allocno_num++;
4639 0 : setup_slot_coalesced_allocno_live_ranges (allocno);
4640 : }
4641 : else
4642 : {
4643 0 : allocno_coalesced_p = true;
4644 0 : merged_p = true;
4645 0 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4646 0 : fprintf (ira_dump_file,
4647 : " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
4648 : ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
4649 : ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
4650 0 : ALLOCNO_COALESCE_DATA (allocno)->temp
4651 0 : = ALLOCNO_COALESCE_DATA (a)->temp;
4652 0 : setup_slot_coalesced_allocno_live_ranges (allocno);
4653 0 : merge_allocnos (a, allocno);
4654 0 : ira_assert (ALLOCNO_COALESCE_DATA (a)->first == a);
4655 : }
4656 : }
4657 0 : for (i = 0; i < ira_allocnos_num; i++)
4658 0 : ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges[i]);
4659 0 : ira_free (slot_coalesced_allocnos_live_ranges);
4660 0 : return merged_p;
4661 : }
4662 :
4663 : /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
4664 : subsequent assigning stack slots to them in the reload pass. To do
4665 : this we coalesce spilled allocnos first to decrease the number of
4666 : memory-memory move insns. This function is called by the
4667 : reload. */
4668 : void
4669 0 : ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
4670 : machine_mode *reg_max_ref_mode)
4671 : {
4672 0 : int max_regno = max_reg_num ();
4673 0 : int i, regno, num, slot_num;
4674 0 : ira_allocno_t allocno, a;
4675 0 : ira_allocno_iterator ai;
4676 0 : ira_allocno_t *spilled_coalesced_allocnos;
4677 :
4678 0 : ira_assert (! ira_use_lra_p);
4679 :
4680 : /* Set up allocnos can be coalesced. */
4681 0 : coloring_allocno_bitmap = ira_allocate_bitmap ();
4682 0 : for (i = 0; i < n; i++)
4683 : {
4684 0 : regno = pseudo_regnos[i];
4685 0 : allocno = ira_regno_allocno_map[regno];
4686 0 : if (allocno != NULL)
4687 0 : bitmap_set_bit (coloring_allocno_bitmap, ALLOCNO_NUM (allocno));
4688 : }
4689 0 : allocno_coalesced_p = false;
4690 0 : processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
4691 0 : allocno_coalesce_data
4692 0 : = (coalesce_data_t) ira_allocate (sizeof (struct coalesce_data)
4693 0 : * ira_allocnos_num);
4694 : /* Initialize coalesce data for allocnos. */
4695 0 : FOR_EACH_ALLOCNO (a, ai)
4696 : {
4697 0 : ALLOCNO_ADD_DATA (a) = allocno_coalesce_data + ALLOCNO_NUM (a);
4698 0 : ALLOCNO_COALESCE_DATA (a)->first = a;
4699 0 : ALLOCNO_COALESCE_DATA (a)->next = a;
4700 : }
4701 0 : coalesce_allocnos ();
4702 0 : ira_free_bitmap (coloring_allocno_bitmap);
4703 0 : regno_coalesced_allocno_cost
4704 0 : = (int *) ira_allocate (max_regno * sizeof (int));
4705 0 : regno_coalesced_allocno_num
4706 0 : = (int *) ira_allocate (max_regno * sizeof (int));
4707 0 : memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
4708 0 : setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
4709 : /* Sort regnos according frequencies of the corresponding coalesced
4710 : allocno sets. */
4711 0 : qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
4712 0 : spilled_coalesced_allocnos
4713 0 : = (ira_allocno_t *) ira_allocate (ira_allocnos_num
4714 : * sizeof (ira_allocno_t));
4715 : /* Collect allocnos representing the spilled coalesced allocno
4716 : sets. */
4717 0 : num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
4718 : spilled_coalesced_allocnos);
4719 0 : if (flag_ira_share_spill_slots
4720 0 : && coalesce_spill_slots (spilled_coalesced_allocnos, num))
4721 : {
4722 0 : setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
4723 0 : qsort (pseudo_regnos, n, sizeof (int),
4724 : coalesced_pseudo_reg_freq_compare);
4725 0 : num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
4726 : spilled_coalesced_allocnos);
4727 : }
4728 0 : ira_free_bitmap (processed_coalesced_allocno_bitmap);
4729 0 : allocno_coalesced_p = false;
4730 : /* Assign stack slot numbers to spilled allocno sets, use smaller
4731 : numbers for most frequently used coalesced allocnos. -1 is
4732 : reserved for dynamic search of stack slots for pseudos spilled by
4733 : the reload. */
4734 0 : slot_num = 1;
4735 0 : for (i = 0; i < num; i++)
4736 : {
4737 0 : allocno = spilled_coalesced_allocnos[i];
4738 0 : if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
4739 0 : || ALLOCNO_HARD_REGNO (allocno) >= 0
4740 0 : || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
4741 0 : continue;
4742 0 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4743 0 : fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
4744 0 : slot_num++;
4745 0 : for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4746 0 : a = ALLOCNO_COALESCE_DATA (a)->next)
4747 : {
4748 0 : ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
4749 0 : ALLOCNO_HARD_REGNO (a) = -slot_num;
4750 0 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4751 : {
4752 0 : machine_mode mode = wider_subreg_mode
4753 0 : (PSEUDO_REGNO_MODE (ALLOCNO_REGNO (a)),
4754 0 : reg_max_ref_mode[ALLOCNO_REGNO (a)]);
4755 0 : fprintf (ira_dump_file, " a%dr%d(%d,",
4756 : ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a));
4757 0 : print_dec (GET_MODE_SIZE (mode), ira_dump_file, SIGNED);
4758 0 : fprintf (ira_dump_file, ")\n");
4759 : }
4760 :
4761 0 : if (a == allocno)
4762 : break;
4763 0 : }
4764 0 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4765 0 : fprintf (ira_dump_file, "\n");
4766 : }
4767 0 : ira_spilled_reg_stack_slots_num = slot_num - 1;
4768 0 : ira_free (spilled_coalesced_allocnos);
4769 : /* Sort regnos according the slot numbers. */
4770 0 : regno_max_ref_mode = reg_max_ref_mode;
4771 0 : qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
4772 0 : FOR_EACH_ALLOCNO (a, ai)
4773 0 : ALLOCNO_ADD_DATA (a) = NULL;
4774 0 : ira_free (allocno_coalesce_data);
4775 0 : ira_free (regno_coalesced_allocno_num);
4776 0 : ira_free (regno_coalesced_allocno_cost);
4777 0 : }
4778 :
4779 :
4780 :
4781 : /* This page contains code used by the reload pass to improve the
4782 : final code. */
4783 :
4784 : /* The function is called from reload to mark changes in the
4785 : allocation of REGNO made by the reload. Remember that reg_renumber
4786 : reflects the change result. */
4787 : void
4788 0 : ira_mark_allocation_change (int regno)
4789 : {
4790 0 : ira_allocno_t a = ira_regno_allocno_map[regno];
4791 0 : int old_hard_regno, hard_regno, cost;
4792 0 : enum reg_class aclass = ALLOCNO_CLASS (a);
4793 :
4794 0 : ira_assert (a != NULL);
4795 0 : hard_regno = reg_renumber[regno];
4796 0 : if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
4797 : return;
4798 0 : if (old_hard_regno < 0)
4799 0 : cost = -ALLOCNO_MEMORY_COST (a);
4800 : else
4801 : {
4802 0 : ira_assert (ira_class_hard_reg_index[aclass][old_hard_regno] >= 0);
4803 0 : cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
4804 0 : ? ALLOCNO_CLASS_COST (a)
4805 : : ALLOCNO_HARD_REG_COSTS (a)
4806 0 : [ira_class_hard_reg_index[aclass][old_hard_regno]]);
4807 0 : update_costs_from_copies (a, false, false);
4808 : }
4809 0 : ira_overall_cost -= cost;
4810 0 : ALLOCNO_HARD_REGNO (a) = hard_regno;
4811 0 : if (hard_regno < 0)
4812 : {
4813 0 : ALLOCNO_HARD_REGNO (a) = -1;
4814 0 : cost += ALLOCNO_MEMORY_COST (a);
4815 : }
4816 0 : else if (ira_class_hard_reg_index[aclass][hard_regno] >= 0)
4817 : {
4818 0 : cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
4819 0 : ? ALLOCNO_CLASS_COST (a)
4820 : : ALLOCNO_HARD_REG_COSTS (a)
4821 0 : [ira_class_hard_reg_index[aclass][hard_regno]]);
4822 0 : update_costs_from_copies (a, true, false);
4823 : }
4824 : else
4825 : /* Reload changed class of the allocno. */
4826 : cost = 0;
4827 0 : ira_overall_cost += cost;
4828 : }
4829 :
4830 : /* This function is called when reload deletes memory-memory move. In
4831 : this case we marks that the allocation of the corresponding
4832 : allocnos should be not changed in future. Otherwise we risk to get
4833 : a wrong code. */
4834 : void
4835 0 : ira_mark_memory_move_deletion (int dst_regno, int src_regno)
4836 : {
4837 0 : ira_allocno_t dst = ira_regno_allocno_map[dst_regno];
4838 0 : ira_allocno_t src = ira_regno_allocno_map[src_regno];
4839 :
4840 0 : ira_assert (dst != NULL && src != NULL
4841 : && ALLOCNO_HARD_REGNO (dst) < 0
4842 : && ALLOCNO_HARD_REGNO (src) < 0);
4843 0 : ALLOCNO_DONT_REASSIGN_P (dst) = true;
4844 0 : ALLOCNO_DONT_REASSIGN_P (src) = true;
4845 0 : }
4846 :
4847 : /* Try to assign a hard register (except for FORBIDDEN_REGS) to
4848 : allocno A and return TRUE in the case of success. */
4849 : static bool
4850 0 : allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
4851 : {
4852 0 : int hard_regno;
4853 0 : enum reg_class aclass;
4854 0 : int regno = ALLOCNO_REGNO (a);
4855 0 : HARD_REG_SET saved[2];
4856 0 : int i, n;
4857 :
4858 0 : n = ALLOCNO_NUM_OBJECTS (a);
4859 0 : for (i = 0; i < n; i++)
4860 : {
4861 0 : ira_object_t obj = ALLOCNO_OBJECT (a, i);
4862 0 : saved[i] = OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
4863 0 : OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) |= forbidden_regs;
4864 0 : if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
4865 0 : OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) |= ira_need_caller_save_regs (a);
4866 : }
4867 0 : ALLOCNO_ASSIGNED_P (a) = false;
4868 0 : aclass = ALLOCNO_CLASS (a);
4869 0 : update_curr_costs (a);
4870 0 : assign_hard_reg (a, true);
4871 0 : hard_regno = ALLOCNO_HARD_REGNO (a);
4872 0 : reg_renumber[regno] = hard_regno;
4873 0 : if (hard_regno < 0)
4874 0 : ALLOCNO_HARD_REGNO (a) = -1;
4875 : else
4876 : {
4877 0 : ira_assert (ira_class_hard_reg_index[aclass][hard_regno] >= 0);
4878 0 : ira_overall_cost
4879 0 : -= (ALLOCNO_MEMORY_COST (a)
4880 0 : - (ALLOCNO_HARD_REG_COSTS (a) == NULL
4881 0 : ? ALLOCNO_CLASS_COST (a)
4882 : : ALLOCNO_HARD_REG_COSTS (a)[ira_class_hard_reg_index
4883 0 : [aclass][hard_regno]]));
4884 0 : if (ira_need_caller_save_p (a, hard_regno))
4885 : {
4886 0 : ira_assert (flag_caller_saves);
4887 0 : caller_save_needed = 1;
4888 : }
4889 : }
4890 :
4891 : /* If we found a hard register, modify the RTL for the pseudo
4892 : register to show the hard register, and mark the pseudo register
4893 : live. */
4894 0 : if (reg_renumber[regno] >= 0)
4895 : {
4896 0 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4897 0 : fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
4898 0 : SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
4899 0 : mark_home_live (regno);
4900 : }
4901 0 : else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4902 0 : fprintf (ira_dump_file, "\n");
4903 0 : for (i = 0; i < n; i++)
4904 : {
4905 0 : ira_object_t obj = ALLOCNO_OBJECT (a, i);
4906 0 : OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) = saved[i];
4907 : }
4908 0 : return reg_renumber[regno] >= 0;
4909 : }
4910 :
4911 : /* Sort pseudos according their usage frequencies (putting most
4912 : frequently ones first). */
4913 : static int
4914 0 : pseudo_reg_compare (const void *v1p, const void *v2p)
4915 : {
4916 0 : int regno1 = *(const int *) v1p;
4917 0 : int regno2 = *(const int *) v2p;
4918 0 : int diff;
4919 :
4920 0 : if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
4921 : return diff;
4922 0 : return regno1 - regno2;
4923 : }
4924 :
4925 : /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
4926 : NUM of them) or spilled pseudos conflicting with pseudos in
4927 : SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
4928 : allocation has been changed. The function doesn't use
4929 : BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
4930 : PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
4931 : is called by the reload pass at the end of each reload
4932 : iteration. */
4933 : bool
4934 0 : ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
4935 : HARD_REG_SET bad_spill_regs,
4936 : HARD_REG_SET *pseudo_forbidden_regs,
4937 : HARD_REG_SET *pseudo_previous_regs,
4938 : bitmap spilled)
4939 : {
4940 0 : int i, n, regno;
4941 0 : bool changed_p;
4942 0 : ira_allocno_t a;
4943 0 : HARD_REG_SET forbidden_regs;
4944 0 : bitmap temp = BITMAP_ALLOC (NULL);
4945 :
4946 : /* Add pseudos which conflict with pseudos already in
4947 : SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
4948 : to allocating in two steps as some of the conflicts might have
4949 : a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
4950 0 : for (i = 0; i < num; i++)
4951 0 : bitmap_set_bit (temp, spilled_pseudo_regs[i]);
4952 :
4953 0 : for (i = 0, n = num; i < n; i++)
4954 : {
4955 0 : int nr, j;
4956 0 : int regno = spilled_pseudo_regs[i];
4957 0 : bitmap_set_bit (temp, regno);
4958 :
4959 0 : a = ira_regno_allocno_map[regno];
4960 0 : nr = ALLOCNO_NUM_OBJECTS (a);
4961 0 : for (j = 0; j < nr; j++)
4962 : {
4963 0 : ira_object_t conflict_obj;
4964 0 : ira_object_t obj = ALLOCNO_OBJECT (a, j);
4965 0 : ira_object_conflict_iterator oci;
4966 :
4967 0 : FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
4968 : {
4969 0 : ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
4970 0 : if (ALLOCNO_HARD_REGNO (conflict_a) < 0
4971 0 : && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
4972 0 : && bitmap_set_bit (temp, ALLOCNO_REGNO (conflict_a)))
4973 : {
4974 0 : spilled_pseudo_regs[num++] = ALLOCNO_REGNO (conflict_a);
4975 : /* ?!? This seems wrong. */
4976 0 : bitmap_set_bit (consideration_allocno_bitmap,
4977 : ALLOCNO_NUM (conflict_a));
4978 : }
4979 : }
4980 : }
4981 : }
4982 :
4983 0 : if (num > 1)
4984 0 : qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
4985 0 : changed_p = false;
4986 : /* Try to assign hard registers to pseudos from
4987 : SPILLED_PSEUDO_REGS. */
4988 0 : for (i = 0; i < num; i++)
4989 : {
4990 0 : regno = spilled_pseudo_regs[i];
4991 0 : forbidden_regs = (bad_spill_regs
4992 0 : | pseudo_forbidden_regs[regno]
4993 0 : | pseudo_previous_regs[regno]);
4994 0 : gcc_assert (reg_renumber[regno] < 0);
4995 0 : a = ira_regno_allocno_map[regno];
4996 0 : ira_mark_allocation_change (regno);
4997 0 : ira_assert (reg_renumber[regno] < 0);
4998 0 : if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4999 0 : fprintf (ira_dump_file,
5000 : " Try Assign %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
5001 0 : ALLOCNO_MEMORY_COST (a)
5002 0 : - ALLOCNO_CLASS_COST (a));
5003 0 : allocno_reload_assign (a, forbidden_regs);
5004 0 : if (reg_renumber[regno] >= 0)
5005 : {
5006 0 : CLEAR_REGNO_REG_SET (spilled, regno);
5007 0 : changed_p = true;
5008 : }
5009 : }
5010 0 : BITMAP_FREE (temp);
5011 0 : return changed_p;
5012 : }
5013 :
5014 : /* The function is called by reload and returns already allocated
5015 : stack slot (if any) for REGNO with given INHERENT_SIZE and
5016 : TOTAL_SIZE. In the case of failure to find a slot which can be
5017 : used for REGNO, the function returns NULL. */
5018 : rtx
5019 0 : ira_reuse_stack_slot (int regno, poly_uint64 inherent_size,
5020 : poly_uint64 total_size)
5021 : {
5022 0 : unsigned int i;
5023 0 : int slot_num, best_slot_num;
5024 0 : int cost, best_cost;
5025 0 : ira_copy_t cp, next_cp;
5026 0 : ira_allocno_t another_allocno, allocno = ira_regno_allocno_map[regno];
5027 0 : rtx x;
5028 0 : bitmap_iterator bi;
5029 0 : class ira_spilled_reg_stack_slot *slot = NULL;
5030 :
5031 0 : ira_assert (! ira_use_lra_p);
5032 :
5033 0 : ira_assert (known_eq (inherent_size, PSEUDO_REGNO_BYTES (regno))
5034 : && known_le (inherent_size, total_size)
5035 : && ALLOCNO_HARD_REGNO (allocno) < 0);
5036 0 : if (! flag_ira_share_spill_slots)
5037 : return NULL_RTX;
5038 0 : slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
5039 0 : if (slot_num != -1)
5040 : {
5041 0 : slot = &ira_spilled_reg_stack_slots[slot_num];
5042 0 : x = slot->mem;
5043 : }
5044 : else
5045 : {
5046 : best_cost = best_slot_num = -1;
5047 0 : x = NULL_RTX;
5048 : /* It means that the pseudo was spilled in the reload pass, try
5049 : to reuse a slot. */
5050 0 : for (slot_num = 0;
5051 0 : slot_num < ira_spilled_reg_stack_slots_num;
5052 : slot_num++)
5053 : {
5054 0 : slot = &ira_spilled_reg_stack_slots[slot_num];
5055 0 : if (slot->mem == NULL_RTX)
5056 0 : continue;
5057 0 : if (maybe_lt (slot->width, total_size)
5058 0 : || maybe_lt (GET_MODE_SIZE (GET_MODE (slot->mem)), inherent_size))
5059 0 : continue;
5060 :
5061 0 : EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
5062 : FIRST_PSEUDO_REGISTER, i, bi)
5063 : {
5064 0 : another_allocno = ira_regno_allocno_map[i];
5065 0 : if (allocnos_conflict_by_live_ranges_p (allocno,
5066 : another_allocno))
5067 0 : goto cont;
5068 : }
5069 0 : for (cost = 0, cp = ALLOCNO_COPIES (allocno);
5070 0 : cp != NULL;
5071 0 : cp = next_cp)
5072 : {
5073 0 : if (cp->first == allocno)
5074 : {
5075 0 : next_cp = cp->next_first_allocno_copy;
5076 0 : another_allocno = cp->second;
5077 : }
5078 0 : else if (cp->second == allocno)
5079 : {
5080 0 : next_cp = cp->next_second_allocno_copy;
5081 0 : another_allocno = cp->first;
5082 : }
5083 : else
5084 0 : gcc_unreachable ();
5085 0 : if (cp->insn == NULL_RTX)
5086 0 : continue;
5087 0 : if (bitmap_bit_p (&slot->spilled_regs,
5088 : ALLOCNO_REGNO (another_allocno)))
5089 0 : cost += cp->freq;
5090 : }
5091 0 : if (cost > best_cost)
5092 : {
5093 0 : best_cost = cost;
5094 0 : best_slot_num = slot_num;
5095 : }
5096 0 : cont:
5097 0 : ;
5098 : }
5099 0 : if (best_cost >= 0)
5100 : {
5101 0 : slot_num = best_slot_num;
5102 0 : slot = &ira_spilled_reg_stack_slots[slot_num];
5103 0 : SET_REGNO_REG_SET (&slot->spilled_regs, regno);
5104 0 : x = slot->mem;
5105 0 : ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
5106 : }
5107 : }
5108 0 : if (x != NULL_RTX)
5109 : {
5110 0 : ira_assert (known_ge (slot->width, total_size));
5111 : #ifdef ENABLE_IRA_CHECKING
5112 0 : EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
5113 : FIRST_PSEUDO_REGISTER, i, bi)
5114 : {
5115 0 : ira_assert (! conflict_by_live_ranges_p (regno, i));
5116 : }
5117 : #endif
5118 0 : SET_REGNO_REG_SET (&slot->spilled_regs, regno);
5119 0 : if (internal_flag_ira_verbose > 3 && ira_dump_file)
5120 : {
5121 0 : fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
5122 0 : regno, REG_FREQ (regno), slot_num);
5123 0 : EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
5124 : FIRST_PSEUDO_REGISTER, i, bi)
5125 : {
5126 0 : if ((unsigned) regno != i)
5127 0 : fprintf (ira_dump_file, " %d", i);
5128 : }
5129 0 : fprintf (ira_dump_file, "\n");
5130 : }
5131 : }
5132 : return x;
5133 : }
5134 :
5135 : /* This is called by reload every time a new stack slot X with
5136 : TOTAL_SIZE was allocated for REGNO. We store this info for
5137 : subsequent ira_reuse_stack_slot calls. */
5138 : void
5139 0 : ira_mark_new_stack_slot (rtx x, int regno, poly_uint64 total_size)
5140 : {
5141 0 : class ira_spilled_reg_stack_slot *slot;
5142 0 : int slot_num;
5143 0 : ira_allocno_t allocno;
5144 :
5145 0 : ira_assert (! ira_use_lra_p);
5146 :
5147 0 : ira_assert (known_le (PSEUDO_REGNO_BYTES (regno), total_size));
5148 0 : allocno = ira_regno_allocno_map[regno];
5149 0 : slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
5150 0 : if (slot_num == -1)
5151 : {
5152 0 : slot_num = ira_spilled_reg_stack_slots_num++;
5153 0 : ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
5154 : }
5155 0 : slot = &ira_spilled_reg_stack_slots[slot_num];
5156 0 : INIT_REG_SET (&slot->spilled_regs);
5157 0 : SET_REGNO_REG_SET (&slot->spilled_regs, regno);
5158 0 : slot->mem = x;
5159 0 : slot->width = total_size;
5160 0 : if (internal_flag_ira_verbose > 3 && ira_dump_file)
5161 0 : fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
5162 0 : regno, REG_FREQ (regno), slot_num);
5163 0 : }
5164 :
5165 :
5166 : /* Return spill cost for pseudo-registers whose numbers are in array
5167 : REGNOS (with a negative number as an end marker) for reload with
5168 : given IN and OUT for INSN. Return also number points (through
5169 : EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
5170 : the register pressure is high, number of references of the
5171 : pseudo-registers (through NREFS), the number of pseudo registers
5172 : whose allocated register wouldn't need saving in the prologue
5173 : (through CALL_USED_COUNT), and the first hard regno occupied by the
5174 : pseudo-registers (through FIRST_HARD_REGNO). */
5175 : static int
5176 0 : calculate_spill_cost (int *regnos, rtx in, rtx out, rtx_insn *insn,
5177 : int *excess_pressure_live_length,
5178 : int *nrefs, int *call_used_count, int *first_hard_regno)
5179 : {
5180 0 : int i, cost, regno, hard_regno, count, saved_cost;
5181 0 : bool in_p, out_p;
5182 0 : int length;
5183 0 : ira_allocno_t a;
5184 :
5185 0 : *nrefs = 0;
5186 0 : for (length = count = cost = i = 0;; i++)
5187 : {
5188 0 : regno = regnos[i];
5189 0 : if (regno < 0)
5190 : break;
5191 0 : *nrefs += REG_N_REFS (regno);
5192 0 : hard_regno = reg_renumber[regno];
5193 0 : ira_assert (hard_regno >= 0);
5194 0 : a = ira_regno_allocno_map[regno];
5195 0 : length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) / ALLOCNO_NUM_OBJECTS (a);
5196 0 : cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
5197 0 : if (in_hard_reg_set_p (crtl->abi->full_reg_clobbers (),
5198 0 : ALLOCNO_MODE (a), hard_regno))
5199 0 : count++;
5200 0 : in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
5201 0 : out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
5202 0 : if ((in_p || out_p)
5203 0 : && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
5204 : {
5205 0 : saved_cost = 0;
5206 0 : if (in_p)
5207 0 : saved_cost += ira_memory_move_cost
5208 0 : [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][1];
5209 0 : if (out_p)
5210 0 : saved_cost
5211 0 : += ira_memory_move_cost
5212 0 : [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][0];
5213 0 : cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
5214 : }
5215 : }
5216 0 : *excess_pressure_live_length = length;
5217 0 : *call_used_count = count;
5218 0 : hard_regno = -1;
5219 0 : if (regnos[0] >= 0)
5220 : {
5221 0 : hard_regno = reg_renumber[regnos[0]];
5222 : }
5223 0 : *first_hard_regno = hard_regno;
5224 0 : return cost;
5225 : }
5226 :
5227 : /* Return TRUE if spilling pseudo-registers whose numbers are in array
5228 : REGNOS is better than spilling pseudo-registers with numbers in
5229 : OTHER_REGNOS for reload with given IN and OUT for INSN. The
5230 : function used by the reload pass to make better register spilling
5231 : decisions. */
5232 : bool
5233 0 : ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
5234 : rtx in, rtx out, rtx_insn *insn)
5235 : {
5236 0 : int cost, other_cost;
5237 0 : int length, other_length;
5238 0 : int nrefs, other_nrefs;
5239 0 : int call_used_count, other_call_used_count;
5240 0 : int hard_regno, other_hard_regno;
5241 :
5242 0 : cost = calculate_spill_cost (regnos, in, out, insn,
5243 : &length, &nrefs, &call_used_count, &hard_regno);
5244 0 : other_cost = calculate_spill_cost (other_regnos, in, out, insn,
5245 : &other_length, &other_nrefs,
5246 : &other_call_used_count,
5247 : &other_hard_regno);
5248 0 : if (nrefs == 0 && other_nrefs != 0)
5249 : return true;
5250 0 : if (nrefs != 0 && other_nrefs == 0)
5251 : return false;
5252 0 : if (cost != other_cost)
5253 0 : return cost < other_cost;
5254 0 : if (length != other_length)
5255 0 : return length > other_length;
5256 : #ifdef REG_ALLOC_ORDER
5257 0 : if (hard_regno >= 0 && other_hard_regno >= 0)
5258 0 : return (inv_reg_alloc_order[hard_regno]
5259 0 : < inv_reg_alloc_order[other_hard_regno]);
5260 : #else
5261 : if (call_used_count != other_call_used_count)
5262 : return call_used_count > other_call_used_count;
5263 : #endif
5264 : return false;
5265 : }
5266 :
5267 :
5268 :
5269 : /* Allocate and initialize data necessary for assign_hard_reg. */
5270 : void
5271 1070228 : ira_initiate_assign (void)
5272 : {
5273 1070228 : sorted_allocnos
5274 2140456 : = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
5275 1070228 : * ira_allocnos_num);
5276 1070228 : consideration_allocno_bitmap = ira_allocate_bitmap ();
5277 1070228 : initiate_cost_update ();
5278 1070228 : allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
5279 1070228 : sorted_copies = (ira_copy_t *) ira_allocate (ira_copies_num
5280 : * sizeof (ira_copy_t));
5281 1070228 : }
5282 :
5283 : /* Deallocate data used by assign_hard_reg. */
5284 : void
5285 1070228 : ira_finish_assign (void)
5286 : {
5287 1070228 : ira_free (sorted_allocnos);
5288 1070228 : ira_free_bitmap (consideration_allocno_bitmap);
5289 1070228 : finish_cost_update ();
5290 1070228 : ira_free (allocno_priorities);
5291 1070228 : ira_free (sorted_copies);
5292 1070228 : }
5293 :
5294 :
5295 :
5296 : /* Entry function doing color-based register allocation. */
5297 : static void
5298 1070228 : color (void)
5299 : {
5300 1070228 : allocno_stack_vec.create (ira_allocnos_num);
5301 1070228 : memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
5302 1070228 : CLEAR_HARD_REG_SET (allocated_callee_save_regs);
5303 1070228 : ira_initiate_assign ();
5304 1070228 : do_coloring ();
5305 1070228 : ira_finish_assign ();
5306 1070228 : allocno_stack_vec.release ();
5307 1070228 : move_spill_restore ();
5308 1070228 : }
5309 :
5310 :
5311 :
5312 : /* This page contains a simple register allocator without usage of
5313 : allocno conflicts. This is used for fast allocation for -O0. */
5314 :
5315 : /* Do register allocation by not using allocno conflicts. It uses
5316 : only allocno live ranges. The algorithm is close to Chow's
5317 : priority coloring. */
5318 : static void
5319 460895 : fast_allocation (void)
5320 : {
5321 460895 : int i, j, k, num, class_size, hard_regno, best_hard_regno, cost, min_cost;
5322 460895 : int *costs;
5323 : #ifdef STACK_REGS
5324 460895 : bool no_stack_reg_p;
5325 : #endif
5326 460895 : enum reg_class aclass;
5327 460895 : machine_mode mode;
5328 460895 : ira_allocno_t a;
5329 460895 : ira_allocno_iterator ai;
5330 460895 : live_range_t r;
5331 460895 : HARD_REG_SET conflict_hard_regs, *used_hard_regs;
5332 :
5333 921790 : sorted_allocnos = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
5334 460895 : * ira_allocnos_num);
5335 460895 : num = 0;
5336 12352924 : FOR_EACH_ALLOCNO (a, ai)
5337 11892029 : sorted_allocnos[num++] = a;
5338 460895 : allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
5339 460895 : setup_allocno_priorities (sorted_allocnos, num);
5340 460895 : used_hard_regs = (HARD_REG_SET *) ira_allocate (sizeof (HARD_REG_SET)
5341 460895 : * ira_max_point);
5342 19809497 : for (i = 0; i < ira_max_point; i++)
5343 37775414 : CLEAR_HARD_REG_SET (used_hard_regs[i]);
5344 460895 : qsort (sorted_allocnos, num, sizeof (ira_allocno_t),
5345 : allocno_priority_compare_func);
5346 12813819 : for (i = 0; i < num; i++)
5347 : {
5348 11892029 : int nr, l;
5349 :
5350 11892029 : a = sorted_allocnos[i];
5351 11892029 : nr = ALLOCNO_NUM_OBJECTS (a);
5352 11892029 : CLEAR_HARD_REG_SET (conflict_hard_regs);
5353 24633236 : for (l = 0; l < nr; l++)
5354 : {
5355 12741207 : ira_object_t obj = ALLOCNO_OBJECT (a, l);
5356 12741207 : conflict_hard_regs |= OBJECT_CONFLICT_HARD_REGS (obj);
5357 26969896 : for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
5358 948589871 : for (j = r->start; j <= r->finish; j++)
5359 1868722364 : conflict_hard_regs |= used_hard_regs[j];
5360 : }
5361 11892029 : aclass = ALLOCNO_CLASS (a);
5362 11892029 : ALLOCNO_ASSIGNED_P (a) = true;
5363 11892029 : ALLOCNO_HARD_REGNO (a) = -1;
5364 23784058 : if (hard_reg_set_subset_p (reg_class_contents[aclass],
5365 : conflict_hard_regs))
5366 66603 : continue;
5367 11825426 : mode = ALLOCNO_MODE (a);
5368 : #ifdef STACK_REGS
5369 11825426 : no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
5370 : #endif
5371 11825426 : class_size = ira_class_hard_regs_num[aclass];
5372 11825426 : costs = ALLOCNO_HARD_REG_COSTS (a);
5373 11825426 : min_cost = INT_MAX;
5374 11825426 : best_hard_regno = -1;
5375 41962987 : for (j = 0; j < class_size; j++)
5376 : {
5377 41000290 : hard_regno = ira_class_hard_regs[aclass][j];
5378 : #ifdef STACK_REGS
5379 41000290 : if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
5380 41419 : && hard_regno <= LAST_STACK_REG)
5381 0 : continue;
5382 : #endif
5383 41000290 : if (ira_hard_reg_set_intersection_p (hard_regno, mode, conflict_hard_regs)
5384 41000290 : || (TEST_HARD_REG_BIT
5385 31811198 : (ira_prohibited_class_mode_regs[aclass][mode], hard_regno)))
5386 9872403 : continue;
5387 31127887 : if (NUM_REGISTER_FILTERS
5388 : && !test_register_filters (ALLOCNO_REGISTER_FILTERS (a),
5389 : hard_regno))
5390 : continue;
5391 31127887 : if (costs == NULL)
5392 : {
5393 : best_hard_regno = hard_regno;
5394 : break;
5395 : }
5396 20265158 : cost = costs[j];
5397 20265158 : if (min_cost > cost)
5398 : {
5399 30137561 : min_cost = cost;
5400 30137561 : best_hard_regno = hard_regno;
5401 : }
5402 : }
5403 11825426 : if (best_hard_regno < 0)
5404 24609 : continue;
5405 11800817 : ALLOCNO_HARD_REGNO (a) = hard_regno = best_hard_regno;
5406 24411334 : for (l = 0; l < nr; l++)
5407 : {
5408 12610517 : ira_object_t obj = ALLOCNO_OBJECT (a, l);
5409 25483097 : for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
5410 52438557 : for (k = r->start; k <= r->finish; k++)
5411 79131954 : used_hard_regs[k] |= ira_reg_mode_hard_regset[hard_regno][mode];
5412 : }
5413 : }
5414 460895 : ira_free (sorted_allocnos);
5415 460895 : ira_free (used_hard_regs);
5416 460895 : ira_free (allocno_priorities);
5417 460895 : if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
5418 56 : ira_print_disposition (ira_dump_file);
5419 460895 : }
5420 :
5421 :
5422 :
5423 : /* Entry function doing coloring. */
5424 : void
5425 1531123 : ira_color (void)
5426 : {
5427 1531123 : ira_allocno_t a;
5428 1531123 : ira_allocno_iterator ai;
5429 :
5430 : /* Setup updated costs. */
5431 1531123 : allocated_memory_p = false;
5432 39354198 : FOR_EACH_ALLOCNO (a, ai)
5433 : {
5434 37823075 : ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
5435 37823075 : ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
5436 37823075 : if (ALLOCNO_CLASS (a) == NO_REGS
5437 37823075 : && !ira_equiv_no_lvalue_p (ALLOCNO_REGNO (a)))
5438 386783 : allocated_memory_p = true;
5439 : }
5440 1531123 : if (ira_conflicts_p)
5441 1070228 : color ();
5442 : else
5443 460895 : fast_allocation ();
5444 1531123 : }
|