Fortran: Fix PR 47485.
[official-gcc.git] / gcc / ira-color.cc
blob0699b349a1afd2da363b155e98cc381f5d57afca
1 /* IRA allocation based on graph coloring.
2 Copyright (C) 2006-2025 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
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.
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.
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/>. */
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"
39 /* To prevent soft conflict detection becoming quadratic in the
40 loop depth. Only for very pathological cases, so it hardly
41 seems worth a --param. */
42 const int max_soft_conflict_loop_depth = 64;
44 typedef struct allocno_hard_regs *allocno_hard_regs_t;
46 /* The structure contains information about hard registers can be
47 assigned to allocnos. Usually it is allocno profitable hard
48 registers but in some cases this set can be a bit different. Major
49 reason of the difference is a requirement to use hard register sets
50 that form a tree or a forest (set of trees), i.e. hard register set
51 of a node should contain hard register sets of its subnodes. */
52 struct allocno_hard_regs
54 /* Hard registers can be assigned to an allocno. */
55 HARD_REG_SET set;
56 /* Overall (spilling) cost of all allocnos with given register
57 set. */
58 int64_t cost;
61 typedef struct allocno_hard_regs_node *allocno_hard_regs_node_t;
63 /* A node representing allocno hard registers. Such nodes form a
64 forest (set of trees). Each subnode of given node in the forest
65 refers for hard register set (usually allocno profitable hard
66 register set) which is a subset of one referred from given
67 node. */
68 struct allocno_hard_regs_node
70 /* Set up number of the node in preorder traversing of the forest. */
71 int preorder_num;
72 /* Used for different calculation like finding conflict size of an
73 allocno. */
74 int check;
75 /* Used for calculation of conflict size of an allocno. The
76 conflict size of the allocno is maximal number of given allocno
77 hard registers needed for allocation of the conflicting allocnos.
78 Given allocno is trivially colored if this number plus the number
79 of hard registers needed for given allocno is not greater than
80 the number of given allocno hard register set. */
81 int conflict_size;
82 /* The number of hard registers given by member hard_regs. */
83 int hard_regs_num;
84 /* The following member is used to form the final forest. */
85 bool used_p;
86 /* Pointer to the corresponding profitable hard registers. */
87 allocno_hard_regs_t hard_regs;
88 /* Parent, first subnode, previous and next node with the same
89 parent in the forest. */
90 allocno_hard_regs_node_t parent, first, prev, next;
93 /* Info about changing hard reg costs of an allocno. */
94 struct update_cost_record
96 /* Hard regno for which we changed the cost. */
97 int hard_regno;
98 /* Divisor used when we changed the cost of HARD_REGNO. */
99 int divisor;
100 /* Next record for given allocno. */
101 struct update_cost_record *next;
104 /* To decrease footprint of ira_allocno structure we store all data
105 needed only for coloring in the following structure. */
106 struct allocno_color_data
108 /* TRUE value means that the allocno was not removed yet from the
109 conflicting graph during coloring. */
110 unsigned int in_graph_p : 1;
111 /* TRUE if it is put on the stack to make other allocnos
112 colorable. */
113 unsigned int may_be_spilled_p : 1;
114 /* TRUE if the allocno is trivially colorable. */
115 unsigned int colorable_p : 1;
116 /* Number of hard registers of the allocno class really
117 available for the allocno allocation. It is number of the
118 profitable hard regs. */
119 int available_regs_num;
120 /* Sum of frequencies of hard register preferences of all
121 conflicting allocnos which are not the coloring stack yet. */
122 int conflict_allocno_hard_prefs;
123 /* Allocnos in a bucket (used in coloring) chained by the following
124 two members. */
125 ira_allocno_t next_bucket_allocno;
126 ira_allocno_t prev_bucket_allocno;
127 /* Used for temporary purposes. */
128 int temp;
129 /* Used to exclude repeated processing. */
130 int last_process;
131 /* Profitable hard regs available for this pseudo allocation. It
132 means that the set excludes unavailable hard regs and hard regs
133 conflicting with given pseudo. They should be of the allocno
134 class. */
135 HARD_REG_SET profitable_hard_regs;
136 /* The allocno hard registers node. */
137 allocno_hard_regs_node_t hard_regs_node;
138 /* Array of structures allocno_hard_regs_subnode representing
139 given allocno hard registers node (the 1st element in the array)
140 and all its subnodes in the tree (forest) of allocno hard
141 register nodes (see comments above). */
142 int hard_regs_subnodes_start;
143 /* The length of the previous array. */
144 int hard_regs_subnodes_num;
145 /* Records about updating allocno hard reg costs from copies. If
146 the allocno did not get expected hard register, these records are
147 used to restore original hard reg costs of allocnos connected to
148 this allocno by copies. */
149 struct update_cost_record *update_cost_records;
150 /* Threads. We collect allocnos connected by copies into threads
151 and try to assign hard regs to allocnos by threads. */
152 /* Allocno representing all thread. */
153 ira_allocno_t first_thread_allocno;
154 /* Allocnos in thread forms a cycle list through the following
155 member. */
156 ira_allocno_t next_thread_allocno;
157 /* All thread frequency. Defined only for first thread allocno. */
158 int thread_freq;
159 /* Sum of frequencies of hard register preferences of the allocno. */
160 int hard_reg_prefs;
163 /* See above. */
164 typedef struct allocno_color_data *allocno_color_data_t;
166 /* Container for storing allocno data concerning coloring. */
167 static allocno_color_data_t allocno_color_data;
169 /* Macro to access the data concerning coloring. */
170 #define ALLOCNO_COLOR_DATA(a) ((allocno_color_data_t) ALLOCNO_ADD_DATA (a))
172 /* Used for finding allocno colorability to exclude repeated allocno
173 processing and for updating preferencing to exclude repeated
174 allocno processing during assignment. */
175 static int curr_allocno_process;
177 /* This file contains code for regional graph coloring, spill/restore
178 code placement optimization, and code helping the reload pass to do
179 a better job. */
181 /* Bitmap of allocnos which should be colored. */
182 static bitmap coloring_allocno_bitmap;
184 /* Bitmap of allocnos which should be taken into account during
185 coloring. In general case it contains allocnos from
186 coloring_allocno_bitmap plus other already colored conflicting
187 allocnos. */
188 static bitmap consideration_allocno_bitmap;
190 /* All allocnos sorted according their priorities. */
191 static ira_allocno_t *sorted_allocnos;
193 /* Vec representing the stack of allocnos used during coloring. */
194 static vec<ira_allocno_t> allocno_stack_vec;
196 /* Helper for qsort comparison callbacks - return a positive integer if
197 X > Y, or a negative value otherwise. Use a conditional expression
198 instead of a difference computation to insulate from possible overflow
199 issues, e.g. X - Y < 0 for some X > 0 and Y < 0. */
200 #define SORTGT(x,y) (((x) > (y)) ? 1 : -1)
204 /* Definition of vector of allocno hard registers. */
206 /* Vector of unique allocno hard registers. */
207 static vec<allocno_hard_regs_t> allocno_hard_regs_vec;
209 struct allocno_hard_regs_hasher : nofree_ptr_hash <allocno_hard_regs>
211 static inline hashval_t hash (const allocno_hard_regs *);
212 static inline bool equal (const allocno_hard_regs *,
213 const allocno_hard_regs *);
216 /* Returns hash value for allocno hard registers V. */
217 inline hashval_t
218 allocno_hard_regs_hasher::hash (const allocno_hard_regs *hv)
220 return iterative_hash (&hv->set, sizeof (HARD_REG_SET), 0);
223 /* Compares allocno hard registers V1 and V2. */
224 inline bool
225 allocno_hard_regs_hasher::equal (const allocno_hard_regs *hv1,
226 const allocno_hard_regs *hv2)
228 return hv1->set == hv2->set;
231 /* Hash table of unique allocno hard registers. */
232 static hash_table<allocno_hard_regs_hasher> *allocno_hard_regs_htab;
234 /* Return allocno hard registers in the hash table equal to HV. */
235 static allocno_hard_regs_t
236 find_hard_regs (allocno_hard_regs_t hv)
238 return allocno_hard_regs_htab->find (hv);
241 /* Insert allocno hard registers HV in the hash table (if it is not
242 there yet) and return the value which in the table. */
243 static allocno_hard_regs_t
244 insert_hard_regs (allocno_hard_regs_t hv)
246 allocno_hard_regs **slot = allocno_hard_regs_htab->find_slot (hv, INSERT);
248 if (*slot == NULL)
249 *slot = hv;
250 return *slot;
253 /* Initialize data concerning allocno hard registers. */
254 static void
255 init_allocno_hard_regs (void)
257 allocno_hard_regs_vec.create (200);
258 allocno_hard_regs_htab
259 = new hash_table<allocno_hard_regs_hasher> (200);
262 /* Add (or update info about) allocno hard registers with SET and
263 COST. */
264 static allocno_hard_regs_t
265 add_allocno_hard_regs (HARD_REG_SET set, int64_t cost)
267 struct allocno_hard_regs temp;
268 allocno_hard_regs_t hv;
270 gcc_assert (! hard_reg_set_empty_p (set));
271 temp.set = set;
272 if ((hv = find_hard_regs (&temp)) != NULL)
273 hv->cost += cost;
274 else
276 hv = ((struct allocno_hard_regs *)
277 ira_allocate (sizeof (struct allocno_hard_regs)));
278 hv->set = set;
279 hv->cost = cost;
280 allocno_hard_regs_vec.safe_push (hv);
281 insert_hard_regs (hv);
283 return hv;
286 /* Finalize data concerning allocno hard registers. */
287 static void
288 finish_allocno_hard_regs (void)
290 int i;
291 allocno_hard_regs_t hv;
293 for (i = 0;
294 allocno_hard_regs_vec.iterate (i, &hv);
295 i++)
296 ira_free (hv);
297 delete allocno_hard_regs_htab;
298 allocno_hard_regs_htab = NULL;
299 allocno_hard_regs_vec.release ();
302 /* Sort hard regs according to their frequency of usage. */
303 static int
304 allocno_hard_regs_compare (const void *v1p, const void *v2p)
306 allocno_hard_regs_t hv1 = *(const allocno_hard_regs_t *) v1p;
307 allocno_hard_regs_t hv2 = *(const allocno_hard_regs_t *) v2p;
309 if (hv2->cost > hv1->cost)
310 return 1;
311 else if (hv2->cost < hv1->cost)
312 return -1;
313 return SORTGT (allocno_hard_regs_hasher::hash(hv2), allocno_hard_regs_hasher::hash(hv1));
318 /* Used for finding a common ancestor of two allocno hard registers
319 nodes in the forest. We use the current value of
320 'node_check_tick' to mark all nodes from one node to the top and
321 then walking up from another node until we find a marked node.
323 It is also used to figure out allocno colorability as a mark that
324 we already reset value of member 'conflict_size' for the forest
325 node corresponding to the processed allocno. */
326 static int node_check_tick;
328 /* Roots of the forest containing hard register sets can be assigned
329 to allocnos. */
330 static allocno_hard_regs_node_t hard_regs_roots;
332 /* Definition of vector of allocno hard register nodes. */
334 /* Vector used to create the forest. */
335 static vec<allocno_hard_regs_node_t> hard_regs_node_vec;
337 /* Create and return allocno hard registers node containing allocno
338 hard registers HV. */
339 static allocno_hard_regs_node_t
340 create_new_allocno_hard_regs_node (allocno_hard_regs_t hv)
342 allocno_hard_regs_node_t new_node;
344 new_node = ((struct allocno_hard_regs_node *)
345 ira_allocate (sizeof (struct allocno_hard_regs_node)));
346 new_node->check = 0;
347 new_node->hard_regs = hv;
348 new_node->hard_regs_num = hard_reg_set_size (hv->set);
349 new_node->first = NULL;
350 new_node->used_p = false;
351 return new_node;
354 /* Add allocno hard registers node NEW_NODE to the forest on its level
355 given by ROOTS. */
356 static void
357 add_new_allocno_hard_regs_node_to_forest (allocno_hard_regs_node_t *roots,
358 allocno_hard_regs_node_t new_node)
360 new_node->next = *roots;
361 if (new_node->next != NULL)
362 new_node->next->prev = new_node;
363 new_node->prev = NULL;
364 *roots = new_node;
367 /* Add allocno hard registers HV (or its best approximation if it is
368 not possible) to the forest on its level given by ROOTS. */
369 static void
370 add_allocno_hard_regs_to_forest (allocno_hard_regs_node_t *roots,
371 allocno_hard_regs_t hv)
373 unsigned int i, start;
374 allocno_hard_regs_node_t node, prev, new_node;
375 HARD_REG_SET temp_set;
376 allocno_hard_regs_t hv2;
378 start = hard_regs_node_vec.length ();
379 for (node = *roots; node != NULL; node = node->next)
381 if (hv->set == node->hard_regs->set)
382 return;
383 if (hard_reg_set_subset_p (hv->set, node->hard_regs->set))
385 add_allocno_hard_regs_to_forest (&node->first, hv);
386 return;
388 if (hard_reg_set_subset_p (node->hard_regs->set, hv->set))
389 hard_regs_node_vec.safe_push (node);
390 else if (hard_reg_set_intersect_p (hv->set, node->hard_regs->set))
392 temp_set = hv->set & node->hard_regs->set;
393 hv2 = add_allocno_hard_regs (temp_set, hv->cost);
394 add_allocno_hard_regs_to_forest (&node->first, hv2);
397 if (hard_regs_node_vec.length ()
398 > start + 1)
400 /* Create a new node which contains nodes in hard_regs_node_vec. */
401 CLEAR_HARD_REG_SET (temp_set);
402 for (i = start;
403 i < hard_regs_node_vec.length ();
404 i++)
406 node = hard_regs_node_vec[i];
407 temp_set |= node->hard_regs->set;
409 hv = add_allocno_hard_regs (temp_set, hv->cost);
410 new_node = create_new_allocno_hard_regs_node (hv);
411 prev = NULL;
412 for (i = start;
413 i < hard_regs_node_vec.length ();
414 i++)
416 node = hard_regs_node_vec[i];
417 if (node->prev == NULL)
418 *roots = node->next;
419 else
420 node->prev->next = node->next;
421 if (node->next != NULL)
422 node->next->prev = node->prev;
423 if (prev == NULL)
424 new_node->first = node;
425 else
426 prev->next = node;
427 node->prev = prev;
428 node->next = NULL;
429 prev = node;
431 add_new_allocno_hard_regs_node_to_forest (roots, new_node);
433 hard_regs_node_vec.truncate (start);
436 /* Add allocno hard registers nodes starting with the forest level
437 given by FIRST which contains biggest set inside SET. */
438 static void
439 collect_allocno_hard_regs_cover (allocno_hard_regs_node_t first,
440 HARD_REG_SET set)
442 allocno_hard_regs_node_t node;
444 ira_assert (first != NULL);
445 for (node = first; node != NULL; node = node->next)
446 if (hard_reg_set_subset_p (node->hard_regs->set, set))
447 hard_regs_node_vec.safe_push (node);
448 else if (hard_reg_set_intersect_p (set, node->hard_regs->set))
449 collect_allocno_hard_regs_cover (node->first, set);
452 /* Set up field parent as PARENT in all allocno hard registers nodes
453 in forest given by FIRST. */
454 static void
455 setup_allocno_hard_regs_nodes_parent (allocno_hard_regs_node_t first,
456 allocno_hard_regs_node_t parent)
458 allocno_hard_regs_node_t node;
460 for (node = first; node != NULL; node = node->next)
462 node->parent = parent;
463 setup_allocno_hard_regs_nodes_parent (node->first, node);
467 /* Return allocno hard registers node which is a first common ancestor
468 node of FIRST and SECOND in the forest. */
469 static allocno_hard_regs_node_t
470 first_common_ancestor_node (allocno_hard_regs_node_t first,
471 allocno_hard_regs_node_t second)
473 allocno_hard_regs_node_t node;
475 node_check_tick++;
476 for (node = first; node != NULL; node = node->parent)
477 node->check = node_check_tick;
478 for (node = second; node != NULL; node = node->parent)
479 if (node->check == node_check_tick)
480 return node;
481 return first_common_ancestor_node (second, first);
484 /* Print hard reg set SET to F. */
485 static void
486 print_hard_reg_set (FILE *f, HARD_REG_SET set, bool new_line_p)
488 int i, start, end;
490 for (start = end = -1, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
492 bool reg_included = TEST_HARD_REG_BIT (set, i);
494 if (reg_included)
496 if (start == -1)
497 start = i;
498 end = i;
500 if (start >= 0 && (!reg_included || i == FIRST_PSEUDO_REGISTER - 1))
502 if (start == end)
503 fprintf (f, " %d", start);
504 else if (start == end + 1)
505 fprintf (f, " %d %d", start, end);
506 else
507 fprintf (f, " %d-%d", start, end);
508 start = -1;
511 if (new_line_p)
512 fprintf (f, "\n");
515 /* Dump a hard reg set SET to stderr. */
516 DEBUG_FUNCTION void
517 debug_hard_reg_set (HARD_REG_SET set)
519 print_hard_reg_set (stderr, set, true);
522 /* Print allocno hard register subforest given by ROOTS and its LEVEL
523 to F. */
524 static void
525 print_hard_regs_subforest (FILE *f, allocno_hard_regs_node_t roots,
526 int level)
528 int i;
529 allocno_hard_regs_node_t node;
531 for (node = roots; node != NULL; node = node->next)
533 fprintf (f, " ");
534 for (i = 0; i < level * 2; i++)
535 fprintf (f, " ");
536 fprintf (f, "%d:(", node->preorder_num);
537 print_hard_reg_set (f, node->hard_regs->set, false);
538 fprintf (f, ")@%" PRId64"\n", node->hard_regs->cost);
539 print_hard_regs_subforest (f, node->first, level + 1);
543 /* Print the allocno hard register forest to F. */
544 static void
545 print_hard_regs_forest (FILE *f)
547 fprintf (f, " Hard reg set forest:\n");
548 print_hard_regs_subforest (f, hard_regs_roots, 1);
551 /* Print the allocno hard register forest to stderr. */
552 void
553 ira_debug_hard_regs_forest (void)
555 print_hard_regs_forest (stderr);
558 /* Remove unused allocno hard registers nodes from forest given by its
559 *ROOTS. */
560 static void
561 remove_unused_allocno_hard_regs_nodes (allocno_hard_regs_node_t *roots)
563 allocno_hard_regs_node_t node, prev, next, last;
565 for (prev = NULL, node = *roots; node != NULL; node = next)
567 next = node->next;
568 if (node->used_p)
570 remove_unused_allocno_hard_regs_nodes (&node->first);
571 prev = node;
573 else
575 for (last = node->first;
576 last != NULL && last->next != NULL;
577 last = last->next)
579 if (last != NULL)
581 if (prev == NULL)
582 *roots = node->first;
583 else
584 prev->next = node->first;
585 if (next != NULL)
586 next->prev = last;
587 last->next = next;
588 next = node->first;
590 else
592 if (prev == NULL)
593 *roots = next;
594 else
595 prev->next = next;
596 if (next != NULL)
597 next->prev = prev;
599 ira_free (node);
604 /* Set up fields preorder_num starting with START_NUM in all allocno
605 hard registers nodes in forest given by FIRST. Return biggest set
606 PREORDER_NUM increased by 1. */
607 static int
608 enumerate_allocno_hard_regs_nodes (allocno_hard_regs_node_t first,
609 allocno_hard_regs_node_t parent,
610 int start_num)
612 allocno_hard_regs_node_t node;
614 for (node = first; node != NULL; node = node->next)
616 node->preorder_num = start_num++;
617 node->parent = parent;
618 start_num = enumerate_allocno_hard_regs_nodes (node->first, node,
619 start_num);
621 return start_num;
624 /* Number of allocno hard registers nodes in the forest. */
625 static int allocno_hard_regs_nodes_num;
627 /* Table preorder number of allocno hard registers node in the forest
628 -> the allocno hard registers node. */
629 static allocno_hard_regs_node_t *allocno_hard_regs_nodes;
631 /* See below. */
632 typedef struct allocno_hard_regs_subnode *allocno_hard_regs_subnode_t;
634 /* The structure is used to describes all subnodes (not only immediate
635 ones) in the mentioned above tree for given allocno hard register
636 node. The usage of such data accelerates calculation of
637 colorability of given allocno. */
638 struct allocno_hard_regs_subnode
640 /* The conflict size of conflicting allocnos whose hard register
641 sets are equal sets (plus supersets if given node is given
642 allocno hard registers node) of one in the given node. */
643 int left_conflict_size;
644 /* The summary conflict size of conflicting allocnos whose hard
645 register sets are strict subsets of one in the given node.
646 Overall conflict size is
647 left_conflict_subnodes_size
648 + MIN (max_node_impact - left_conflict_subnodes_size,
649 left_conflict_size)
651 short left_conflict_subnodes_size;
652 short max_node_impact;
655 /* Container for hard regs subnodes of all allocnos. */
656 static allocno_hard_regs_subnode_t allocno_hard_regs_subnodes;
658 /* Table (preorder number of allocno hard registers node in the
659 forest, preorder number of allocno hard registers subnode) -> index
660 of the subnode relative to the node. -1 if it is not a
661 subnode. */
662 static int *allocno_hard_regs_subnode_index;
664 /* Setup arrays ALLOCNO_HARD_REGS_NODES and
665 ALLOCNO_HARD_REGS_SUBNODE_INDEX. */
666 static void
667 setup_allocno_hard_regs_subnode_index (allocno_hard_regs_node_t first)
669 allocno_hard_regs_node_t node, parent;
670 int index;
672 for (node = first; node != NULL; node = node->next)
674 allocno_hard_regs_nodes[node->preorder_num] = node;
675 for (parent = node; parent != NULL; parent = parent->parent)
677 index = parent->preorder_num * allocno_hard_regs_nodes_num;
678 allocno_hard_regs_subnode_index[index + node->preorder_num]
679 = node->preorder_num - parent->preorder_num;
681 setup_allocno_hard_regs_subnode_index (node->first);
685 /* Count all allocno hard registers nodes in tree ROOT. */
686 static int
687 get_allocno_hard_regs_subnodes_num (allocno_hard_regs_node_t root)
689 int len = 1;
691 for (root = root->first; root != NULL; root = root->next)
692 len += get_allocno_hard_regs_subnodes_num (root);
693 return len;
696 /* Build the forest of allocno hard registers nodes and assign each
697 allocno a node from the forest. */
698 static void
699 form_allocno_hard_regs_nodes_forest (void)
701 unsigned int i, j, size, len;
702 int start;
703 ira_allocno_t a;
704 allocno_hard_regs_t hv;
705 bitmap_iterator bi;
706 HARD_REG_SET temp;
707 allocno_hard_regs_node_t node, allocno_hard_regs_node;
708 allocno_color_data_t allocno_data;
710 node_check_tick = 0;
711 init_allocno_hard_regs ();
712 hard_regs_roots = NULL;
713 hard_regs_node_vec.create (100);
714 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
715 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
717 CLEAR_HARD_REG_SET (temp);
718 SET_HARD_REG_BIT (temp, i);
719 hv = add_allocno_hard_regs (temp, 0);
720 node = create_new_allocno_hard_regs_node (hv);
721 add_new_allocno_hard_regs_node_to_forest (&hard_regs_roots, node);
723 start = allocno_hard_regs_vec.length ();
724 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
726 a = ira_allocnos[i];
727 allocno_data = ALLOCNO_COLOR_DATA (a);
729 if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
730 continue;
731 hv = (add_allocno_hard_regs
732 (allocno_data->profitable_hard_regs,
733 ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a)));
735 temp = ~ira_no_alloc_regs;
736 add_allocno_hard_regs (temp, 0);
737 qsort (allocno_hard_regs_vec.address () + start,
738 allocno_hard_regs_vec.length () - start,
739 sizeof (allocno_hard_regs_t), allocno_hard_regs_compare);
740 for (i = start;
741 allocno_hard_regs_vec.iterate (i, &hv);
742 i++)
744 add_allocno_hard_regs_to_forest (&hard_regs_roots, hv);
745 ira_assert (hard_regs_node_vec.length () == 0);
747 /* We need to set up parent fields for right work of
748 first_common_ancestor_node. */
749 setup_allocno_hard_regs_nodes_parent (hard_regs_roots, NULL);
750 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
752 a = ira_allocnos[i];
753 allocno_data = ALLOCNO_COLOR_DATA (a);
754 if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
755 continue;
756 hard_regs_node_vec.truncate (0);
757 collect_allocno_hard_regs_cover (hard_regs_roots,
758 allocno_data->profitable_hard_regs);
759 allocno_hard_regs_node = NULL;
760 for (j = 0; hard_regs_node_vec.iterate (j, &node); j++)
761 allocno_hard_regs_node
762 = (j == 0
763 ? node
764 : first_common_ancestor_node (node, allocno_hard_regs_node));
765 /* That is a temporary storage. */
766 allocno_hard_regs_node->used_p = true;
767 allocno_data->hard_regs_node = allocno_hard_regs_node;
769 ira_assert (hard_regs_roots->next == NULL);
770 hard_regs_roots->used_p = true;
771 remove_unused_allocno_hard_regs_nodes (&hard_regs_roots);
772 allocno_hard_regs_nodes_num
773 = enumerate_allocno_hard_regs_nodes (hard_regs_roots, NULL, 0);
774 allocno_hard_regs_nodes
775 = ((allocno_hard_regs_node_t *)
776 ira_allocate (allocno_hard_regs_nodes_num
777 * sizeof (allocno_hard_regs_node_t)));
778 size = allocno_hard_regs_nodes_num * allocno_hard_regs_nodes_num;
779 allocno_hard_regs_subnode_index
780 = (int *) ira_allocate (size * sizeof (int));
781 for (i = 0; i < size; i++)
782 allocno_hard_regs_subnode_index[i] = -1;
783 setup_allocno_hard_regs_subnode_index (hard_regs_roots);
784 start = 0;
785 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
787 a = ira_allocnos[i];
788 allocno_data = ALLOCNO_COLOR_DATA (a);
789 if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
790 continue;
791 len = get_allocno_hard_regs_subnodes_num (allocno_data->hard_regs_node);
792 allocno_data->hard_regs_subnodes_start = start;
793 allocno_data->hard_regs_subnodes_num = len;
794 start += len;
796 allocno_hard_regs_subnodes
797 = ((allocno_hard_regs_subnode_t)
798 ira_allocate (sizeof (struct allocno_hard_regs_subnode) * start));
799 hard_regs_node_vec.release ();
802 /* Free tree of allocno hard registers nodes given by its ROOT. */
803 static void
804 finish_allocno_hard_regs_nodes_tree (allocno_hard_regs_node_t root)
806 allocno_hard_regs_node_t child, next;
808 for (child = root->first; child != NULL; child = next)
810 next = child->next;
811 finish_allocno_hard_regs_nodes_tree (child);
813 ira_free (root);
816 /* Finish work with the forest of allocno hard registers nodes. */
817 static void
818 finish_allocno_hard_regs_nodes_forest (void)
820 allocno_hard_regs_node_t node, next;
822 ira_free (allocno_hard_regs_subnodes);
823 for (node = hard_regs_roots; node != NULL; node = next)
825 next = node->next;
826 finish_allocno_hard_regs_nodes_tree (node);
828 ira_free (allocno_hard_regs_nodes);
829 ira_free (allocno_hard_regs_subnode_index);
830 finish_allocno_hard_regs ();
833 /* Set up left conflict sizes and left conflict subnodes sizes of hard
834 registers subnodes of allocno A. Return TRUE if allocno A is
835 trivially colorable. */
836 static bool
837 setup_left_conflict_sizes_p (ira_allocno_t a)
839 int i, k, nobj, start;
840 int conflict_size, left_conflict_subnodes_size, node_preorder_num;
841 allocno_color_data_t data;
842 HARD_REG_SET profitable_hard_regs;
843 allocno_hard_regs_subnode_t subnodes;
844 allocno_hard_regs_node_t node;
845 HARD_REG_SET node_set;
847 nobj = ALLOCNO_NUM_OBJECTS (a);
848 data = ALLOCNO_COLOR_DATA (a);
849 subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
850 profitable_hard_regs = data->profitable_hard_regs;
851 node = data->hard_regs_node;
852 node_preorder_num = node->preorder_num;
853 node_set = node->hard_regs->set;
854 node_check_tick++;
855 for (k = 0; k < nobj; k++)
857 ira_object_t obj = ALLOCNO_OBJECT (a, k);
858 ira_object_t conflict_obj;
859 ira_object_conflict_iterator oci;
861 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
863 int size;
864 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
865 allocno_hard_regs_node_t conflict_node, temp_node;
866 HARD_REG_SET conflict_node_set;
867 allocno_color_data_t conflict_data;
869 conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
870 if (! ALLOCNO_COLOR_DATA (conflict_a)->in_graph_p
871 || ! hard_reg_set_intersect_p (profitable_hard_regs,
872 conflict_data
873 ->profitable_hard_regs))
874 continue;
875 conflict_node = conflict_data->hard_regs_node;
876 conflict_node_set = conflict_node->hard_regs->set;
877 if (hard_reg_set_subset_p (node_set, conflict_node_set))
878 temp_node = node;
879 else
881 ira_assert (hard_reg_set_subset_p (conflict_node_set, node_set));
882 temp_node = conflict_node;
884 if (temp_node->check != node_check_tick)
886 temp_node->check = node_check_tick;
887 temp_node->conflict_size = 0;
889 size = (ira_reg_class_max_nregs
890 [ALLOCNO_CLASS (conflict_a)][ALLOCNO_MODE (conflict_a)]);
891 if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1)
892 /* We will deal with the subwords individually. */
893 size = 1;
894 temp_node->conflict_size += size;
897 for (i = 0; i < data->hard_regs_subnodes_num; i++)
899 allocno_hard_regs_node_t temp_node;
901 temp_node = allocno_hard_regs_nodes[i + node_preorder_num];
902 ira_assert (temp_node->preorder_num == i + node_preorder_num);
903 subnodes[i].left_conflict_size = (temp_node->check != node_check_tick
904 ? 0 : temp_node->conflict_size);
905 if (hard_reg_set_subset_p (temp_node->hard_regs->set,
906 profitable_hard_regs))
907 subnodes[i].max_node_impact = temp_node->hard_regs_num;
908 else
910 HARD_REG_SET temp_set;
911 int j, n, hard_regno;
912 enum reg_class aclass;
914 temp_set = temp_node->hard_regs->set & profitable_hard_regs;
915 aclass = ALLOCNO_CLASS (a);
916 for (n = 0, j = ira_class_hard_regs_num[aclass] - 1; j >= 0; j--)
918 hard_regno = ira_class_hard_regs[aclass][j];
919 if (TEST_HARD_REG_BIT (temp_set, hard_regno))
920 n++;
922 subnodes[i].max_node_impact = n;
924 subnodes[i].left_conflict_subnodes_size = 0;
926 start = node_preorder_num * allocno_hard_regs_nodes_num;
927 for (i = data->hard_regs_subnodes_num - 1; i > 0; i--)
929 int size, parent_i;
930 allocno_hard_regs_node_t parent;
932 size = (subnodes[i].left_conflict_subnodes_size
933 + MIN (subnodes[i].max_node_impact
934 - subnodes[i].left_conflict_subnodes_size,
935 subnodes[i].left_conflict_size));
936 parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
937 gcc_checking_assert(parent);
938 parent_i
939 = allocno_hard_regs_subnode_index[start + parent->preorder_num];
940 gcc_checking_assert(parent_i >= 0);
941 subnodes[parent_i].left_conflict_subnodes_size += size;
943 left_conflict_subnodes_size = subnodes[0].left_conflict_subnodes_size;
944 conflict_size
945 = (left_conflict_subnodes_size
946 + MIN (subnodes[0].max_node_impact - left_conflict_subnodes_size,
947 subnodes[0].left_conflict_size));
948 conflict_size += ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
949 data->colorable_p = conflict_size <= data->available_regs_num;
950 return data->colorable_p;
953 /* Update left conflict sizes of hard registers subnodes of allocno A
954 after removing allocno REMOVED_A with SIZE from the conflict graph.
955 Return TRUE if A is trivially colorable. */
956 static bool
957 update_left_conflict_sizes_p (ira_allocno_t a,
958 ira_allocno_t removed_a, int size)
960 int i, conflict_size, before_conflict_size, diff, start;
961 int node_preorder_num, parent_i;
962 allocno_hard_regs_node_t node, removed_node, parent;
963 allocno_hard_regs_subnode_t subnodes;
964 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
966 ira_assert (! data->colorable_p);
967 node = data->hard_regs_node;
968 node_preorder_num = node->preorder_num;
969 removed_node = ALLOCNO_COLOR_DATA (removed_a)->hard_regs_node;
970 ira_assert (hard_reg_set_subset_p (removed_node->hard_regs->set,
971 node->hard_regs->set)
972 || hard_reg_set_subset_p (node->hard_regs->set,
973 removed_node->hard_regs->set));
974 start = node_preorder_num * allocno_hard_regs_nodes_num;
975 i = allocno_hard_regs_subnode_index[start + removed_node->preorder_num];
976 if (i < 0)
977 i = 0;
978 subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
979 before_conflict_size
980 = (subnodes[i].left_conflict_subnodes_size
981 + MIN (subnodes[i].max_node_impact
982 - subnodes[i].left_conflict_subnodes_size,
983 subnodes[i].left_conflict_size));
984 subnodes[i].left_conflict_size -= size;
985 for (;;)
987 conflict_size
988 = (subnodes[i].left_conflict_subnodes_size
989 + MIN (subnodes[i].max_node_impact
990 - subnodes[i].left_conflict_subnodes_size,
991 subnodes[i].left_conflict_size));
992 if ((diff = before_conflict_size - conflict_size) == 0)
993 break;
994 ira_assert (conflict_size < before_conflict_size);
995 parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
996 if (parent == NULL)
997 break;
998 parent_i
999 = allocno_hard_regs_subnode_index[start + parent->preorder_num];
1000 if (parent_i < 0)
1001 break;
1002 i = parent_i;
1003 before_conflict_size
1004 = (subnodes[i].left_conflict_subnodes_size
1005 + MIN (subnodes[i].max_node_impact
1006 - subnodes[i].left_conflict_subnodes_size,
1007 subnodes[i].left_conflict_size));
1008 subnodes[i].left_conflict_subnodes_size -= diff;
1010 if (i != 0
1011 || (conflict_size
1012 + ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
1013 > data->available_regs_num))
1014 return false;
1015 data->colorable_p = true;
1016 return true;
1019 /* Return true if allocno A has empty profitable hard regs. */
1020 static bool
1021 empty_profitable_hard_regs (ira_allocno_t a)
1023 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
1025 return hard_reg_set_empty_p (data->profitable_hard_regs);
1028 /* Set up profitable hard registers for each allocno being
1029 colored. */
1030 static void
1031 setup_profitable_hard_regs (void)
1033 unsigned int i;
1034 int j, k, nobj, hard_regno, nregs, class_size;
1035 ira_allocno_t a;
1036 bitmap_iterator bi;
1037 enum reg_class aclass;
1038 machine_mode mode;
1039 allocno_color_data_t data;
1041 /* Initial set up from allocno classes and explicitly conflicting
1042 hard regs. */
1043 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1045 a = ira_allocnos[i];
1046 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS)
1047 continue;
1048 data = ALLOCNO_COLOR_DATA (a);
1049 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL
1050 && ALLOCNO_CLASS_COST (a) > ALLOCNO_MEMORY_COST (a)
1051 /* Do not empty profitable regs for static chain pointer
1052 pseudo when non-local goto is used. */
1053 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1054 CLEAR_HARD_REG_SET (data->profitable_hard_regs);
1055 else
1057 mode = ALLOCNO_MODE (a);
1058 data->profitable_hard_regs
1059 = ira_useful_class_mode_regs[aclass][mode];
1060 nobj = ALLOCNO_NUM_OBJECTS (a);
1061 for (k = 0; k < nobj; k++)
1063 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1065 data->profitable_hard_regs
1066 &= ~OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
1070 /* Exclude hard regs already assigned for conflicting objects. */
1071 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, i, bi)
1073 a = ira_allocnos[i];
1074 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1075 || ! ALLOCNO_ASSIGNED_P (a)
1076 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1077 continue;
1078 mode = ALLOCNO_MODE (a);
1079 nregs = hard_regno_nregs (hard_regno, mode);
1080 nobj = ALLOCNO_NUM_OBJECTS (a);
1081 for (k = 0; k < nobj; k++)
1083 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1084 ira_object_t conflict_obj;
1085 ira_object_conflict_iterator oci;
1087 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1089 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1091 /* We can process the conflict allocno repeatedly with
1092 the same result. */
1093 if (nregs == nobj && nregs > 1)
1095 int num = OBJECT_SUBWORD (conflict_obj);
1097 if (REG_WORDS_BIG_ENDIAN)
1098 CLEAR_HARD_REG_BIT
1099 (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1100 hard_regno + nobj - num - 1);
1101 else
1102 CLEAR_HARD_REG_BIT
1103 (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1104 hard_regno + num);
1106 else
1107 ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs
1108 &= ~ira_reg_mode_hard_regset[hard_regno][mode];
1112 /* Exclude too costly hard regs. */
1113 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1115 int min_cost = INT_MAX;
1116 int *costs;
1118 a = ira_allocnos[i];
1119 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1120 || empty_profitable_hard_regs (a))
1121 continue;
1122 data = ALLOCNO_COLOR_DATA (a);
1123 if ((costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a)) != NULL
1124 || (costs = ALLOCNO_HARD_REG_COSTS (a)) != NULL)
1126 class_size = ira_class_hard_regs_num[aclass];
1127 for (j = 0; j < class_size; j++)
1129 hard_regno = ira_class_hard_regs[aclass][j];
1130 if (! TEST_HARD_REG_BIT (data->profitable_hard_regs,
1131 hard_regno))
1132 continue;
1133 if (ALLOCNO_UPDATED_MEMORY_COST (a) < costs[j]
1134 /* Do not remove HARD_REGNO for static chain pointer
1135 pseudo when non-local goto is used. */
1136 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1137 CLEAR_HARD_REG_BIT (data->profitable_hard_regs,
1138 hard_regno);
1139 else if (min_cost > costs[j])
1140 min_cost = costs[j];
1143 else if (ALLOCNO_UPDATED_MEMORY_COST (a)
1144 < ALLOCNO_UPDATED_CLASS_COST (a)
1145 /* Do not empty profitable regs for static chain
1146 pointer pseudo when non-local goto is used. */
1147 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1148 CLEAR_HARD_REG_SET (data->profitable_hard_regs);
1149 if (ALLOCNO_UPDATED_CLASS_COST (a) > min_cost)
1150 ALLOCNO_UPDATED_CLASS_COST (a) = min_cost;
1156 /* This page contains functions used to choose hard registers for
1157 allocnos. */
1159 /* Pool for update cost records. */
1160 static object_allocator<update_cost_record> update_cost_record_pool
1161 ("update cost records");
1163 /* Return new update cost record with given params. */
1164 static struct update_cost_record *
1165 get_update_cost_record (int hard_regno, int divisor,
1166 struct update_cost_record *next)
1168 struct update_cost_record *record;
1170 record = update_cost_record_pool.allocate ();
1171 record->hard_regno = hard_regno;
1172 record->divisor = divisor;
1173 record->next = next;
1174 return record;
1177 /* Free memory for all records in LIST. */
1178 static void
1179 free_update_cost_record_list (struct update_cost_record *list)
1181 struct update_cost_record *next;
1183 while (list != NULL)
1185 next = list->next;
1186 update_cost_record_pool.remove (list);
1187 list = next;
1191 /* Free memory allocated for all update cost records. */
1192 static void
1193 finish_update_cost_records (void)
1195 update_cost_record_pool.release ();
1198 /* Array whose element value is TRUE if the corresponding hard
1199 register was already allocated for an allocno. */
1200 static bool allocated_hardreg_p[FIRST_PSEUDO_REGISTER];
1202 /* Describes one element in a queue of allocnos whose costs need to be
1203 updated. Each allocno in the queue is known to have an allocno
1204 class. */
1205 struct update_cost_queue_elem
1207 /* This element is in the queue iff CHECK == update_cost_check. */
1208 int check;
1210 /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
1211 connecting this allocno to the one being allocated. */
1212 int divisor;
1214 /* Allocno from which we started chaining costs of connected
1215 allocnos. */
1216 ira_allocno_t start;
1218 /* Allocno from which we are chaining costs of connected allocnos.
1219 It is used not go back in graph of allocnos connected by
1220 copies. */
1221 ira_allocno_t from;
1223 /* The next allocno in the queue, or null if this is the last element. */
1224 ira_allocno_t next;
1227 /* The first element in a queue of allocnos whose copy costs need to be
1228 updated. Null if the queue is empty. */
1229 static ira_allocno_t update_cost_queue;
1231 /* The last element in the queue described by update_cost_queue.
1232 Not valid if update_cost_queue is null. */
1233 static struct update_cost_queue_elem *update_cost_queue_tail;
1235 /* A pool of elements in the queue described by update_cost_queue.
1236 Elements are indexed by ALLOCNO_NUM. */
1237 static struct update_cost_queue_elem *update_cost_queue_elems;
1239 /* The current value of update_costs_from_copies call count. */
1240 static int update_cost_check;
1242 /* Allocate and initialize data necessary for function
1243 update_costs_from_copies. */
1244 static void
1245 initiate_cost_update (void)
1247 size_t size;
1249 size = ira_allocnos_num * sizeof (struct update_cost_queue_elem);
1250 update_cost_queue_elems
1251 = (struct update_cost_queue_elem *) ira_allocate (size);
1252 memset (update_cost_queue_elems, 0, size);
1253 update_cost_check = 0;
1256 /* Deallocate data used by function update_costs_from_copies. */
1257 static void
1258 finish_cost_update (void)
1260 ira_free (update_cost_queue_elems);
1261 finish_update_cost_records ();
1264 /* When we traverse allocnos to update hard register costs, the cost
1265 divisor will be multiplied by the following macro value for each
1266 hop from given allocno to directly connected allocnos. */
1267 #define COST_HOP_DIVISOR 4
1269 /* Start a new cost-updating pass. */
1270 static void
1271 start_update_cost (void)
1273 update_cost_check++;
1274 update_cost_queue = NULL;
1277 /* Add (ALLOCNO, START, FROM, DIVISOR) to the end of update_cost_queue, unless
1278 ALLOCNO is already in the queue, or has NO_REGS class. */
1279 static inline void
1280 queue_update_cost (ira_allocno_t allocno, ira_allocno_t start,
1281 ira_allocno_t from, int divisor)
1283 struct update_cost_queue_elem *elem;
1285 elem = &update_cost_queue_elems[ALLOCNO_NUM (allocno)];
1286 if (elem->check != update_cost_check
1287 && ALLOCNO_CLASS (allocno) != NO_REGS)
1289 elem->check = update_cost_check;
1290 elem->start = start;
1291 elem->from = from;
1292 elem->divisor = divisor;
1293 elem->next = NULL;
1294 if (update_cost_queue == NULL)
1295 update_cost_queue = allocno;
1296 else
1297 update_cost_queue_tail->next = allocno;
1298 update_cost_queue_tail = elem;
1302 /* Try to remove the first element from update_cost_queue. Return
1303 false if the queue was empty, otherwise make (*ALLOCNO, *START,
1304 *FROM, *DIVISOR) describe the removed element. */
1305 static inline bool
1306 get_next_update_cost (ira_allocno_t *allocno, ira_allocno_t *start,
1307 ira_allocno_t *from, int *divisor)
1309 struct update_cost_queue_elem *elem;
1311 if (update_cost_queue == NULL)
1312 return false;
1314 *allocno = update_cost_queue;
1315 elem = &update_cost_queue_elems[ALLOCNO_NUM (*allocno)];
1316 *start = elem->start;
1317 *from = elem->from;
1318 *divisor = elem->divisor;
1319 update_cost_queue = elem->next;
1320 return true;
1323 /* Increase costs of HARD_REGNO by UPDATE_COST and conflict cost by
1324 UPDATE_CONFLICT_COST for ALLOCNO. Return true if we really
1325 modified the cost. */
1326 static bool
1327 update_allocno_cost (ira_allocno_t allocno, int hard_regno,
1328 int update_cost, int update_conflict_cost)
1330 int i;
1331 enum reg_class aclass = ALLOCNO_CLASS (allocno);
1333 i = ira_class_hard_reg_index[aclass][hard_regno];
1334 if (i < 0)
1335 return false;
1336 ira_allocate_and_set_or_copy_costs
1337 (&ALLOCNO_UPDATED_HARD_REG_COSTS (allocno), aclass,
1338 ALLOCNO_UPDATED_CLASS_COST (allocno),
1339 ALLOCNO_HARD_REG_COSTS (allocno));
1340 ira_allocate_and_set_or_copy_costs
1341 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno),
1342 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (allocno));
1343 ALLOCNO_UPDATED_HARD_REG_COSTS (allocno)[i] += update_cost;
1344 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno)[i] += update_conflict_cost;
1345 return true;
1348 /* Return TRUE if the object OBJ conflicts with the allocno A. */
1349 static bool
1350 object_conflicts_with_allocno_p (ira_object_t obj, ira_allocno_t a)
1352 if (!OBJECT_CONFLICT_VEC_P (obj))
1353 for (int word = 0; word < ALLOCNO_NUM_OBJECTS (a); word++)
1355 ira_object_t another_obj = ALLOCNO_OBJECT (a, word);
1356 if (OBJECT_CONFLICT_ID (another_obj) >= OBJECT_MIN (obj)
1357 && OBJECT_CONFLICT_ID (another_obj) <= OBJECT_MAX (obj)
1358 && TEST_MINMAX_SET_BIT (OBJECT_CONFLICT_BITVEC (obj),
1359 OBJECT_CONFLICT_ID (another_obj),
1360 OBJECT_MIN (obj), OBJECT_MAX (obj)))
1361 return true;
1363 else
1365 /* If this linear walk ever becomes a bottleneck we could add a
1366 conflict_vec_sorted_p flag and if not set, sort the conflicts after
1367 their ID so we can use a binary search. That would also require
1368 tracking the actual number of conflicts in the vector to not rely
1369 on the NULL termination. */
1370 ira_object_conflict_iterator oci;
1371 ira_object_t conflict_obj;
1372 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1373 if (OBJECT_ALLOCNO (conflict_obj) == a)
1374 return true;
1376 return false;
1379 /* Return TRUE if allocnos A1 and A2 conflicts. Here we are
1380 interested only in conflicts of allocnos with intersecting allocno
1381 classes. */
1382 static bool
1383 allocnos_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
1385 /* Compute the upper bound for the linear iteration when the object
1386 conflicts are represented as a sparse vector. In particular this
1387 will make sure we prefer O(1) bitvector testing. */
1388 int num_conflicts_in_vec1 = 0, num_conflicts_in_vec2 = 0;
1389 for (int word = 0; word < ALLOCNO_NUM_OBJECTS (a1); ++word)
1390 if (OBJECT_CONFLICT_VEC_P (ALLOCNO_OBJECT (a1, word)))
1391 num_conflicts_in_vec1 += OBJECT_NUM_CONFLICTS (ALLOCNO_OBJECT (a1, word));
1392 for (int word = 0; word < ALLOCNO_NUM_OBJECTS (a2); ++word)
1393 if (OBJECT_CONFLICT_VEC_P (ALLOCNO_OBJECT (a2, word)))
1394 num_conflicts_in_vec2 += OBJECT_NUM_CONFLICTS (ALLOCNO_OBJECT (a2, word));
1395 if (num_conflicts_in_vec2 < num_conflicts_in_vec1)
1396 std::swap (a1, a2);
1398 for (int word = 0; word < ALLOCNO_NUM_OBJECTS (a1); word++)
1400 ira_object_t obj = ALLOCNO_OBJECT (a1, word);
1401 /* Take preferences of conflicting allocnos into account. */
1402 if (object_conflicts_with_allocno_p (obj, a2))
1403 return true;
1405 return false;
1408 /* Update (decrease if DECR_P) HARD_REGNO cost of allocnos connected
1409 by copies to ALLOCNO to increase chances to remove some copies as
1410 the result of subsequent assignment. Update conflict costs.
1411 Record cost updates if RECORD_P is true. */
1412 static void
1413 update_costs_from_allocno (ira_allocno_t allocno, int hard_regno,
1414 int divisor, bool decr_p, bool record_p)
1416 int cost, update_cost, update_conflict_cost;
1417 machine_mode mode;
1418 enum reg_class rclass, aclass;
1419 ira_allocno_t another_allocno, start = allocno, from = NULL;
1420 ira_copy_t cp, next_cp;
1422 rclass = REGNO_REG_CLASS (hard_regno);
1425 mode = ALLOCNO_MODE (allocno);
1426 ira_init_register_move_cost_if_necessary (mode);
1427 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1429 if (cp->first == allocno)
1431 next_cp = cp->next_first_allocno_copy;
1432 another_allocno = cp->second;
1434 else if (cp->second == allocno)
1436 next_cp = cp->next_second_allocno_copy;
1437 another_allocno = cp->first;
1439 else
1440 gcc_unreachable ();
1442 if (another_allocno == from
1443 || (ALLOCNO_COLOR_DATA (another_allocno) != NULL
1444 && (ALLOCNO_COLOR_DATA (allocno)->first_thread_allocno
1445 != ALLOCNO_COLOR_DATA (another_allocno)->first_thread_allocno)))
1446 continue;
1448 aclass = ALLOCNO_CLASS (another_allocno);
1449 if (! TEST_HARD_REG_BIT (reg_class_contents[aclass],
1450 hard_regno)
1451 || ALLOCNO_ASSIGNED_P (another_allocno))
1452 continue;
1454 /* If we have different modes use the smallest one. It is
1455 a sub-register move. It is hard to predict what LRA
1456 will reload (the pseudo or its sub-register) but LRA
1457 will try to minimize the data movement. Also for some
1458 register classes bigger modes might be invalid,
1459 e.g. DImode for AREG on x86. For such cases the
1460 register move cost will be maximal. */
1461 mode = narrower_subreg_mode (ALLOCNO_MODE (cp->first),
1462 ALLOCNO_MODE (cp->second));
1464 ira_init_register_move_cost_if_necessary (mode);
1466 cost = (cp->second == allocno
1467 ? ira_register_move_cost[mode][rclass][aclass]
1468 : ira_register_move_cost[mode][aclass][rclass]);
1469 if (decr_p)
1470 cost = -cost;
1472 update_cost = cp->freq * cost / divisor;
1473 update_conflict_cost = update_cost;
1475 if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
1476 fprintf (ira_dump_file,
1477 " a%dr%d (hr%d): update cost by %d, conflict cost by %d\n",
1478 ALLOCNO_NUM (another_allocno), ALLOCNO_REGNO (another_allocno),
1479 hard_regno, update_cost, update_conflict_cost);
1480 if (update_cost == 0)
1481 continue;
1483 if (! update_allocno_cost (another_allocno, hard_regno,
1484 update_cost, update_conflict_cost))
1485 continue;
1486 queue_update_cost (another_allocno, start, allocno,
1487 divisor * COST_HOP_DIVISOR);
1488 if (record_p && ALLOCNO_COLOR_DATA (another_allocno) != NULL)
1489 ALLOCNO_COLOR_DATA (another_allocno)->update_cost_records
1490 = get_update_cost_record (hard_regno, divisor,
1491 ALLOCNO_COLOR_DATA (another_allocno)
1492 ->update_cost_records);
1495 while (get_next_update_cost (&allocno, &start, &from, &divisor));
1498 /* Decrease preferred ALLOCNO hard register costs and costs of
1499 allocnos connected to ALLOCNO through copy. */
1500 static void
1501 update_costs_from_prefs (ira_allocno_t allocno)
1503 ira_pref_t pref;
1505 start_update_cost ();
1506 for (pref = ALLOCNO_PREFS (allocno); pref != NULL; pref = pref->next_pref)
1508 if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
1509 fprintf (ira_dump_file, " Start updating from pref of hr%d for a%dr%d:\n",
1510 pref->hard_regno, ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno));
1511 update_costs_from_allocno (allocno, pref->hard_regno,
1512 COST_HOP_DIVISOR, true, true);
1516 /* Update (decrease if DECR_P) the cost of allocnos connected to
1517 ALLOCNO through copies to increase chances to remove some copies as
1518 the result of subsequent assignment. ALLOCNO was just assigned to
1519 a hard register. Record cost updates if RECORD_P is true. */
1520 static void
1521 update_costs_from_copies (ira_allocno_t allocno, bool decr_p, bool record_p)
1523 int hard_regno;
1525 hard_regno = ALLOCNO_HARD_REGNO (allocno);
1526 ira_assert (hard_regno >= 0 && ALLOCNO_CLASS (allocno) != NO_REGS);
1527 start_update_cost ();
1528 if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
1529 fprintf (ira_dump_file, " Start updating from a%dr%d by copies:\n",
1530 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno));
1531 update_costs_from_allocno (allocno, hard_regno, 1, decr_p, record_p);
1534 /* Update conflict_allocno_hard_prefs of allocnos conflicting with
1535 ALLOCNO. */
1536 static void
1537 update_conflict_allocno_hard_prefs (ira_allocno_t allocno)
1539 int l, nr = ALLOCNO_NUM_OBJECTS (allocno);
1541 for (l = 0; l < nr; l++)
1543 ira_object_t conflict_obj, obj = ALLOCNO_OBJECT (allocno, l);
1544 ira_object_conflict_iterator oci;
1546 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1548 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1549 allocno_color_data_t conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
1550 ira_pref_t pref;
1552 if (!(hard_reg_set_intersect_p
1553 (ALLOCNO_COLOR_DATA (allocno)->profitable_hard_regs,
1554 conflict_data->profitable_hard_regs)))
1555 continue;
1556 for (pref = ALLOCNO_PREFS (allocno);
1557 pref != NULL;
1558 pref = pref->next_pref)
1559 conflict_data->conflict_allocno_hard_prefs += pref->freq;
1564 /* Restore costs of allocnos connected to ALLOCNO by copies as it was
1565 before updating costs of these allocnos from given allocno. This
1566 is a wise thing to do as if given allocno did not get an expected
1567 hard reg, using smaller cost of the hard reg for allocnos connected
1568 by copies to given allocno becomes actually misleading. Free all
1569 update cost records for ALLOCNO as we don't need them anymore. */
1570 static void
1571 restore_costs_from_copies (ira_allocno_t allocno)
1573 struct update_cost_record *records, *curr;
1575 if (ALLOCNO_COLOR_DATA (allocno) == NULL)
1576 return;
1577 records = ALLOCNO_COLOR_DATA (allocno)->update_cost_records;
1578 start_update_cost ();
1579 if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
1580 fprintf (ira_dump_file, " Start restoring from a%dr%d:\n",
1581 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno));
1582 for (curr = records; curr != NULL; curr = curr->next)
1583 update_costs_from_allocno (allocno, curr->hard_regno,
1584 curr->divisor, true, false);
1585 free_update_cost_record_list (records);
1586 ALLOCNO_COLOR_DATA (allocno)->update_cost_records = NULL;
1589 /* This function updates COSTS (decrease if DECR_P) for hard_registers
1590 of ACLASS by conflict costs of the unassigned allocnos
1591 connected by copies with allocnos in update_cost_queue. This
1592 update increases chances to remove some copies. */
1593 static void
1594 update_conflict_hard_regno_costs (int *costs, enum reg_class aclass,
1595 bool decr_p)
1597 int i, cost, class_size, freq, mult, div, divisor;
1598 int index, hard_regno;
1599 int *conflict_costs;
1600 bool cont_p;
1601 enum reg_class another_aclass;
1602 ira_allocno_t allocno, another_allocno, start, from;
1603 ira_copy_t cp, next_cp;
1605 while (get_next_update_cost (&allocno, &start, &from, &divisor))
1606 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1608 if (cp->first == allocno)
1610 next_cp = cp->next_first_allocno_copy;
1611 another_allocno = cp->second;
1613 else if (cp->second == allocno)
1615 next_cp = cp->next_second_allocno_copy;
1616 another_allocno = cp->first;
1618 else
1619 gcc_unreachable ();
1621 another_aclass = ALLOCNO_CLASS (another_allocno);
1622 if (another_allocno == from
1623 || ALLOCNO_ASSIGNED_P (another_allocno)
1624 || ALLOCNO_COLOR_DATA (another_allocno)->may_be_spilled_p
1625 || ! ira_reg_classes_intersect_p[aclass][another_aclass])
1626 continue;
1627 if (allocnos_conflict_p (another_allocno, start))
1628 continue;
1630 class_size = ira_class_hard_regs_num[another_aclass];
1631 ira_allocate_and_copy_costs
1632 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
1633 another_aclass, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
1634 conflict_costs
1635 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno);
1636 if (conflict_costs == NULL)
1637 cont_p = true;
1638 else
1640 mult = cp->freq;
1641 freq = ALLOCNO_FREQ (another_allocno);
1642 if (freq == 0)
1643 freq = 1;
1644 div = freq * divisor;
1645 cont_p = false;
1646 for (i = class_size - 1; i >= 0; i--)
1648 hard_regno = ira_class_hard_regs[another_aclass][i];
1649 ira_assert (hard_regno >= 0);
1650 index = ira_class_hard_reg_index[aclass][hard_regno];
1651 if (index < 0)
1652 continue;
1653 cost = (int) (((int64_t) conflict_costs [i] * mult) / div);
1654 if (cost == 0)
1655 continue;
1656 cont_p = true;
1657 if (decr_p)
1658 cost = -cost;
1659 costs[index] += cost;
1662 /* Probably 5 hops will be enough. */
1663 if (cont_p
1664 && divisor <= (COST_HOP_DIVISOR
1665 * COST_HOP_DIVISOR
1666 * COST_HOP_DIVISOR
1667 * COST_HOP_DIVISOR))
1668 queue_update_cost (another_allocno, start, from, divisor * COST_HOP_DIVISOR);
1672 /* Set up conflicting (through CONFLICT_REGS) for each object of
1673 allocno A and the start allocno profitable regs (through
1674 START_PROFITABLE_REGS). Remember that the start profitable regs
1675 exclude hard regs which cannot hold value of mode of allocno A.
1676 This covers mostly cases when multi-register value should be
1677 aligned. */
1678 static inline void
1679 get_conflict_and_start_profitable_regs (ira_allocno_t a, bool retry_p,
1680 HARD_REG_SET *conflict_regs,
1681 HARD_REG_SET *start_profitable_regs)
1683 int i, nwords;
1684 ira_object_t obj;
1686 nwords = ALLOCNO_NUM_OBJECTS (a);
1687 for (i = 0; i < nwords; i++)
1689 obj = ALLOCNO_OBJECT (a, i);
1690 conflict_regs[i] = OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
1692 if (retry_p)
1693 *start_profitable_regs
1694 = (reg_class_contents[ALLOCNO_CLASS (a)]
1695 &~ (ira_prohibited_class_mode_regs
1696 [ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]));
1697 else
1698 *start_profitable_regs = ALLOCNO_COLOR_DATA (a)->profitable_hard_regs;
1701 /* Return true if HARD_REGNO is ok for assigning to allocno A with
1702 PROFITABLE_REGS and whose objects have CONFLICT_REGS. */
1703 static inline bool
1704 check_hard_reg_p (ira_allocno_t a, int hard_regno,
1705 HARD_REG_SET *conflict_regs, HARD_REG_SET profitable_regs)
1707 int j, nwords, nregs;
1708 enum reg_class aclass;
1709 machine_mode mode;
1711 aclass = ALLOCNO_CLASS (a);
1712 mode = ALLOCNO_MODE (a);
1713 if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[aclass][mode],
1714 hard_regno))
1715 return false;
1716 /* Checking only profitable hard regs. */
1717 if (! TEST_HARD_REG_BIT (profitable_regs, hard_regno))
1718 return false;
1719 nregs = hard_regno_nregs (hard_regno, mode);
1720 nwords = ALLOCNO_NUM_OBJECTS (a);
1721 for (j = 0; j < nregs; j++)
1723 int k;
1724 int set_to_test_start = 0, set_to_test_end = nwords;
1726 if (nregs == nwords)
1728 if (REG_WORDS_BIG_ENDIAN)
1729 set_to_test_start = nwords - j - 1;
1730 else
1731 set_to_test_start = j;
1732 set_to_test_end = set_to_test_start + 1;
1734 for (k = set_to_test_start; k < set_to_test_end; k++)
1735 if (TEST_HARD_REG_BIT (conflict_regs[k], hard_regno + j))
1736 break;
1737 if (k != set_to_test_end)
1738 break;
1740 return j == nregs;
1743 /* Return number of registers needed to be saved and restored at
1744 function prologue/epilogue if we allocate HARD_REGNO to hold value
1745 of MODE. */
1746 static int
1747 calculate_saved_nregs (int hard_regno, machine_mode mode)
1749 int i;
1750 int nregs = 0;
1752 ira_assert (hard_regno >= 0);
1753 for (i = hard_regno_nregs (hard_regno, mode) - 1; i >= 0; i--)
1754 if (!allocated_hardreg_p[hard_regno + i]
1755 && ira_hard_regno_nrefs[hard_regno + i] == 0
1756 && !crtl->abi->clobbers_full_reg_p (hard_regno + i)
1757 && !LOCAL_REGNO (hard_regno + i))
1758 nregs++;
1759 return nregs;
1762 /* Allocnos A1 and A2 are known to conflict. Check whether, in some loop L
1763 that is either the current loop or a nested subloop, the conflict is of
1764 the following form:
1766 - One allocno (X) is a cap allocno for some non-cap allocno X2.
1768 - X2 belongs to some loop L2.
1770 - The other allocno (Y) is a non-cap allocno.
1772 - Y is an ancestor of some allocno Y2 in L2. (Note that such a Y2
1773 must exist, given that X and Y conflict.)
1775 - Y2 is not referenced in L2 (that is, ALLOCNO_NREFS (Y2) == 0).
1777 - Y can use a different allocation from Y2.
1779 In this case, Y's register is live across L2 but is not used within it,
1780 whereas X's register is used only within L2. The conflict is therefore
1781 only "soft", in that it can easily be avoided by spilling Y2 inside L2
1782 without affecting any insn references.
1784 If the conflict does have this form, return the Y2 that would need to be
1785 spilled in order to allow X and Y (and thus A1 and A2) to use the same
1786 register. Return null otherwise. Returning null is conservatively correct;
1787 any nonnnull return value is an optimization. */
1788 ira_allocno_t
1789 ira_soft_conflict (ira_allocno_t a1, ira_allocno_t a2)
1791 /* Search for the loop L and its associated allocnos X and Y. */
1792 int search_depth = 0;
1793 while (ALLOCNO_CAP_MEMBER (a1) && ALLOCNO_CAP_MEMBER (a2))
1795 a1 = ALLOCNO_CAP_MEMBER (a1);
1796 a2 = ALLOCNO_CAP_MEMBER (a2);
1797 if (search_depth++ > max_soft_conflict_loop_depth)
1798 return nullptr;
1800 /* This must be true if A1 and A2 conflict. */
1801 ira_assert (ALLOCNO_LOOP_TREE_NODE (a1) == ALLOCNO_LOOP_TREE_NODE (a2));
1803 /* Make A1 the cap allocno (X in the comment above) and A2 the
1804 non-cap allocno (Y in the comment above). */
1805 if (ALLOCNO_CAP_MEMBER (a2))
1806 std::swap (a1, a2);
1807 if (!ALLOCNO_CAP_MEMBER (a1))
1808 return nullptr;
1810 /* Search for the real allocno that A1 caps (X2 in the comment above). */
1813 a1 = ALLOCNO_CAP_MEMBER (a1);
1814 if (search_depth++ > max_soft_conflict_loop_depth)
1815 return nullptr;
1817 while (ALLOCNO_CAP_MEMBER (a1));
1819 /* Find the associated allocno for A2 (Y2 in the comment above). */
1820 auto node = ALLOCNO_LOOP_TREE_NODE (a1);
1821 auto local_a2 = node->regno_allocno_map[ALLOCNO_REGNO (a2)];
1823 /* Find the parent of LOCAL_A2/Y2. LOCAL_A2 must be a descendant of A2
1824 for the conflict query to make sense, so this parent lookup must succeed.
1826 If the parent allocno has no references, it is usually cheaper to
1827 spill at that loop level instead. Keep searching until we find
1828 a parent allocno that does have references (but don't look past
1829 the starting allocno). */
1830 ira_allocno_t local_parent_a2;
1831 for (;;)
1833 local_parent_a2 = ira_parent_allocno (local_a2);
1834 if (local_parent_a2 == a2 || ALLOCNO_NREFS (local_parent_a2) != 0)
1835 break;
1836 local_a2 = local_parent_a2;
1838 if (CHECKING_P)
1840 /* Sanity check to make sure that the conflict we've been given
1841 makes sense. */
1842 auto test_a2 = local_parent_a2;
1843 while (test_a2 != a2)
1845 test_a2 = ira_parent_allocno (test_a2);
1846 ira_assert (test_a2);
1849 if (local_a2
1850 && ALLOCNO_NREFS (local_a2) == 0
1851 && ira_subloop_allocnos_can_differ_p (local_parent_a2))
1852 return local_a2;
1853 return nullptr;
1856 /* The caller has decided to allocate HREGNO to A and has proved that
1857 this is safe. However, the allocation might require the kind of
1858 spilling described in the comment above ira_soft_conflict.
1859 The caller has recorded that:
1861 - The allocnos in ALLOCNOS_TO_SPILL are the ones that would need
1862 to be spilled to satisfy soft conflicts for at least one allocation
1863 (not necessarily HREGNO).
1865 - The soft conflicts apply only to A allocations that overlap
1866 SOFT_CONFLICT_REGS.
1868 If allocating HREGNO is subject to any soft conflicts, record the
1869 subloop allocnos that need to be spilled. */
1870 static void
1871 spill_soft_conflicts (ira_allocno_t a, bitmap allocnos_to_spill,
1872 HARD_REG_SET soft_conflict_regs, int hregno)
1874 auto nregs = hard_regno_nregs (hregno, ALLOCNO_MODE (a));
1875 bitmap_iterator bi;
1876 unsigned int i;
1877 EXECUTE_IF_SET_IN_BITMAP (allocnos_to_spill, 0, i, bi)
1879 /* SPILL_A needs to be spilled for at least one allocation
1880 (not necessarily this one). */
1881 auto spill_a = ira_allocnos[i];
1883 /* Find the corresponding allocno for this loop. */
1884 auto conflict_a = spill_a;
1887 conflict_a = ira_parent_or_cap_allocno (conflict_a);
1888 ira_assert (conflict_a);
1890 while (ALLOCNO_LOOP_TREE_NODE (conflict_a)->level
1891 > ALLOCNO_LOOP_TREE_NODE (a)->level);
1893 ira_assert (ALLOCNO_LOOP_TREE_NODE (conflict_a)
1894 == ALLOCNO_LOOP_TREE_NODE (a));
1896 if (conflict_a == a)
1898 /* SPILL_A is a descendant of A. We don't know (and don't need
1899 to know) which cap allocnos have a soft conflict with A.
1900 All we need to do is test whether the soft conflict applies
1901 to the chosen allocation. */
1902 if (ira_hard_reg_set_intersection_p (hregno, ALLOCNO_MODE (a),
1903 soft_conflict_regs))
1904 ALLOCNO_MIGHT_CONFLICT_WITH_PARENT_P (spill_a) = true;
1906 else
1908 /* SPILL_A is a descendant of CONFLICT_A, which has a soft conflict
1909 with A. Test whether the soft conflict applies to the current
1910 allocation. */
1911 ira_assert (ira_soft_conflict (a, conflict_a) == spill_a);
1912 auto conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a);
1913 ira_assert (conflict_hregno >= 0);
1914 auto conflict_nregs = hard_regno_nregs (conflict_hregno,
1915 ALLOCNO_MODE (conflict_a));
1916 if (hregno + nregs > conflict_hregno
1917 && conflict_hregno + conflict_nregs > hregno)
1918 ALLOCNO_MIGHT_CONFLICT_WITH_PARENT_P (spill_a) = true;
1923 /* Choose a hard register for allocno A. If RETRY_P is TRUE, it means
1924 that the function called from function
1925 `ira_reassign_conflict_allocnos' and `allocno_reload_assign'. In
1926 this case some allocno data are not defined or updated and we
1927 should not touch these data. The function returns true if we
1928 managed to assign a hard register to the allocno.
1930 To assign a hard register, first of all we calculate all conflict
1931 hard registers which can come from conflicting allocnos with
1932 already assigned hard registers. After that we find first free
1933 hard register with the minimal cost. During hard register cost
1934 calculation we take conflict hard register costs into account to
1935 give a chance for conflicting allocnos to get a better hard
1936 register in the future.
1938 If the best hard register cost is bigger than cost of memory usage
1939 for the allocno, we don't assign a hard register to given allocno
1940 at all.
1942 If we assign a hard register to the allocno, we update costs of the
1943 hard register for allocnos connected by copies to improve a chance
1944 to coalesce insns represented by the copies when we assign hard
1945 registers to the allocnos connected by the copies. */
1946 static bool
1947 assign_hard_reg (ira_allocno_t a, bool retry_p)
1949 HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
1950 int i, j, hard_regno, best_hard_regno, class_size;
1951 int cost, mem_cost, min_cost, full_cost, min_full_cost, nwords, word;
1952 int *a_costs;
1953 enum reg_class aclass;
1954 machine_mode mode;
1955 static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
1956 int saved_nregs;
1957 enum reg_class rclass;
1958 int add_cost;
1959 #ifdef STACK_REGS
1960 bool no_stack_reg_p;
1961 #endif
1962 auto_bitmap allocnos_to_spill;
1963 HARD_REG_SET soft_conflict_regs = {};
1965 ira_assert (! ALLOCNO_ASSIGNED_P (a));
1966 get_conflict_and_start_profitable_regs (a, retry_p,
1967 conflicting_regs,
1968 &profitable_hard_regs);
1969 aclass = ALLOCNO_CLASS (a);
1970 class_size = ira_class_hard_regs_num[aclass];
1971 best_hard_regno = -1;
1972 mem_cost = 0;
1973 memset (costs, 0, sizeof (int) * class_size);
1974 memset (full_costs, 0, sizeof (int) * class_size);
1975 #ifdef STACK_REGS
1976 no_stack_reg_p = false;
1977 #endif
1978 if (! retry_p)
1979 start_update_cost ();
1980 mem_cost += ALLOCNO_UPDATED_MEMORY_COST (a);
1982 ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
1983 aclass, ALLOCNO_HARD_REG_COSTS (a));
1984 a_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
1985 #ifdef STACK_REGS
1986 no_stack_reg_p = no_stack_reg_p || ALLOCNO_TOTAL_NO_STACK_REG_P (a);
1987 #endif
1988 cost = ALLOCNO_UPDATED_CLASS_COST (a);
1989 for (i = 0; i < class_size; i++)
1990 if (a_costs != NULL)
1992 costs[i] += a_costs[i];
1993 full_costs[i] += a_costs[i];
1995 else
1997 costs[i] += cost;
1998 full_costs[i] += cost;
2000 nwords = ALLOCNO_NUM_OBJECTS (a);
2001 curr_allocno_process++;
2002 for (word = 0; word < nwords; word++)
2004 ira_object_t conflict_obj;
2005 ira_object_t obj = ALLOCNO_OBJECT (a, word);
2006 ira_object_conflict_iterator oci;
2008 /* Take preferences of conflicting allocnos into account. */
2009 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2011 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2012 enum reg_class conflict_aclass;
2013 allocno_color_data_t data = ALLOCNO_COLOR_DATA (conflict_a);
2015 /* Reload can give another class so we need to check all
2016 allocnos. */
2017 if (!retry_p
2018 && ((!ALLOCNO_ASSIGNED_P (conflict_a)
2019 || ALLOCNO_HARD_REGNO (conflict_a) < 0)
2020 && !(hard_reg_set_intersect_p
2021 (profitable_hard_regs,
2022 ALLOCNO_COLOR_DATA
2023 (conflict_a)->profitable_hard_regs))))
2025 /* All conflict allocnos are in consideration bitmap
2026 when retry_p is false. It might change in future and
2027 if it happens the assert will be broken. It means
2028 the code should be modified for the new
2029 assumptions. */
2030 ira_assert (bitmap_bit_p (consideration_allocno_bitmap,
2031 ALLOCNO_NUM (conflict_a)));
2032 continue;
2034 conflict_aclass = ALLOCNO_CLASS (conflict_a);
2035 ira_assert (ira_reg_classes_intersect_p
2036 [aclass][conflict_aclass]);
2037 if (ALLOCNO_ASSIGNED_P (conflict_a))
2039 hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
2040 if (hard_regno >= 0
2041 && (ira_hard_reg_set_intersection_p
2042 (hard_regno, ALLOCNO_MODE (conflict_a),
2043 reg_class_contents[aclass])))
2045 int n_objects = ALLOCNO_NUM_OBJECTS (conflict_a);
2046 int conflict_nregs;
2048 mode = ALLOCNO_MODE (conflict_a);
2049 conflict_nregs = hard_regno_nregs (hard_regno, mode);
2050 auto spill_a = (retry_p
2051 ? nullptr
2052 : ira_soft_conflict (a, conflict_a));
2053 if (spill_a)
2055 if (bitmap_set_bit (allocnos_to_spill,
2056 ALLOCNO_NUM (spill_a)))
2058 ira_loop_border_costs border_costs (spill_a);
2059 auto cost = border_costs.spill_inside_loop_cost ();
2060 auto note_conflict = [&](int r)
2062 SET_HARD_REG_BIT (soft_conflict_regs, r);
2063 auto hri = ira_class_hard_reg_index[aclass][r];
2064 if (hri >= 0)
2066 costs[hri] += cost;
2067 full_costs[hri] += cost;
2070 for (int r = hard_regno;
2071 r >= 0 && (int) end_hard_regno (mode, r) > hard_regno;
2072 r--)
2073 note_conflict (r);
2074 for (int r = hard_regno + 1;
2075 r < hard_regno + conflict_nregs;
2076 r++)
2077 note_conflict (r);
2080 else
2082 if (conflict_nregs == n_objects && conflict_nregs > 1)
2084 int num = OBJECT_SUBWORD (conflict_obj);
2086 if (REG_WORDS_BIG_ENDIAN)
2087 SET_HARD_REG_BIT (conflicting_regs[word],
2088 hard_regno + n_objects - num - 1);
2089 else
2090 SET_HARD_REG_BIT (conflicting_regs[word],
2091 hard_regno + num);
2093 else
2094 conflicting_regs[word]
2095 |= ira_reg_mode_hard_regset[hard_regno][mode];
2096 if (hard_reg_set_subset_p (profitable_hard_regs,
2097 conflicting_regs[word]))
2098 goto fail;
2102 else if (! retry_p
2103 && ! ALLOCNO_COLOR_DATA (conflict_a)->may_be_spilled_p
2104 /* Don't process the conflict allocno twice. */
2105 && (ALLOCNO_COLOR_DATA (conflict_a)->last_process
2106 != curr_allocno_process))
2108 int k, *conflict_costs;
2110 ALLOCNO_COLOR_DATA (conflict_a)->last_process
2111 = curr_allocno_process;
2112 ira_allocate_and_copy_costs
2113 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a),
2114 conflict_aclass,
2115 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_a));
2116 conflict_costs
2117 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a);
2118 if (conflict_costs != NULL)
2119 for (j = class_size - 1; j >= 0; j--)
2121 hard_regno = ira_class_hard_regs[aclass][j];
2122 ira_assert (hard_regno >= 0);
2123 k = ira_class_hard_reg_index[conflict_aclass][hard_regno];
2124 if (k < 0
2125 /* If HARD_REGNO is not available for CONFLICT_A,
2126 the conflict would be ignored, since HARD_REGNO
2127 will never be assigned to CONFLICT_A. */
2128 || !TEST_HARD_REG_BIT (data->profitable_hard_regs,
2129 hard_regno))
2130 continue;
2131 full_costs[j] -= conflict_costs[k];
2133 queue_update_cost (conflict_a, conflict_a, NULL, COST_HOP_DIVISOR);
2137 if (! retry_p)
2138 /* Take into account preferences of allocnos connected by copies to
2139 the conflict allocnos. */
2140 update_conflict_hard_regno_costs (full_costs, aclass, true);
2142 /* Take preferences of allocnos connected by copies into
2143 account. */
2144 if (! retry_p)
2146 start_update_cost ();
2147 queue_update_cost (a, a, NULL, COST_HOP_DIVISOR);
2148 update_conflict_hard_regno_costs (full_costs, aclass, false);
2150 min_cost = min_full_cost = INT_MAX;
2151 /* We don't care about giving callee saved registers to allocnos no
2152 living through calls because call clobbered registers are
2153 allocated first (it is usual practice to put them first in
2154 REG_ALLOC_ORDER). */
2155 mode = ALLOCNO_MODE (a);
2156 for (i = 0; i < class_size; i++)
2158 hard_regno = ira_class_hard_regs[aclass][i];
2159 #ifdef STACK_REGS
2160 if (no_stack_reg_p
2161 && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
2162 continue;
2163 #endif
2164 if (! check_hard_reg_p (a, hard_regno,
2165 conflicting_regs, profitable_hard_regs))
2166 continue;
2167 if (NUM_REGISTER_FILTERS
2168 && !test_register_filters (ALLOCNO_REGISTER_FILTERS (a), hard_regno))
2169 continue;
2170 cost = costs[i];
2171 full_cost = full_costs[i];
2172 if (!HONOR_REG_ALLOC_ORDER)
2174 if ((saved_nregs = calculate_saved_nregs (hard_regno, mode)) != 0)
2175 /* We need to save/restore the hard register in
2176 epilogue/prologue. Therefore we increase the cost. */
2178 rclass = REGNO_REG_CLASS (hard_regno);
2179 add_cost = ((ira_memory_move_cost[mode][rclass][0]
2180 + ira_memory_move_cost[mode][rclass][1])
2181 * saved_nregs / hard_regno_nregs (hard_regno,
2182 mode) - 1)
2183 * (optimize_size ? 1 :
2184 REG_FREQ_FROM_BB (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
2185 cost += add_cost;
2186 full_cost += add_cost;
2189 if (min_cost > cost)
2190 min_cost = cost;
2191 if (min_full_cost > full_cost)
2193 min_full_cost = full_cost;
2194 best_hard_regno = hard_regno;
2195 ira_assert (hard_regno >= 0);
2197 if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
2198 fprintf (ira_dump_file, "(%d=%d,%d) ", hard_regno, cost, full_cost);
2200 if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
2201 fprintf (ira_dump_file, "\n");
2202 if (min_full_cost > mem_cost
2203 /* Do not spill static chain pointer pseudo when non-local goto
2204 is used. */
2205 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
2207 if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2208 fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
2209 mem_cost, min_full_cost);
2210 best_hard_regno = -1;
2212 fail:
2213 if (best_hard_regno >= 0)
2215 for (i = hard_regno_nregs (best_hard_regno, mode) - 1; i >= 0; i--)
2216 allocated_hardreg_p[best_hard_regno + i] = true;
2217 spill_soft_conflicts (a, allocnos_to_spill, soft_conflict_regs,
2218 best_hard_regno);
2220 if (! retry_p)
2221 restore_costs_from_copies (a);
2222 ALLOCNO_HARD_REGNO (a) = best_hard_regno;
2223 ALLOCNO_ASSIGNED_P (a) = true;
2224 if (best_hard_regno >= 0 && !retry_p)
2225 update_costs_from_copies (a, true, true);
2226 ira_assert (ALLOCNO_CLASS (a) == aclass);
2227 /* We don't need updated costs anymore. */
2228 ira_free_allocno_updated_costs (a);
2229 return best_hard_regno >= 0;
2234 /* An array used to sort copies. */
2235 static ira_copy_t *sorted_copies;
2237 /* If allocno A is a cap, return non-cap allocno from which A is
2238 created. Otherwise, return A. */
2239 static ira_allocno_t
2240 get_cap_member (ira_allocno_t a)
2242 ira_allocno_t member;
2244 while ((member = ALLOCNO_CAP_MEMBER (a)) != NULL)
2245 a = member;
2246 return a;
2249 /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
2250 used to find a conflict for new allocnos or allocnos with the
2251 different allocno classes. */
2252 static bool
2253 allocnos_conflict_by_live_ranges_p (ira_allocno_t a1, ira_allocno_t a2)
2255 rtx reg1, reg2;
2256 int i, j;
2257 int n1 = ALLOCNO_NUM_OBJECTS (a1);
2258 int n2 = ALLOCNO_NUM_OBJECTS (a2);
2260 if (a1 == a2)
2261 return false;
2262 reg1 = regno_reg_rtx[ALLOCNO_REGNO (a1)];
2263 reg2 = regno_reg_rtx[ALLOCNO_REGNO (a2)];
2264 if (reg1 != NULL && reg2 != NULL
2265 && ORIGINAL_REGNO (reg1) == ORIGINAL_REGNO (reg2))
2266 return false;
2268 /* We don't keep live ranges for caps because they can be quite big.
2269 Use ranges of non-cap allocno from which caps are created. */
2270 a1 = get_cap_member (a1);
2271 a2 = get_cap_member (a2);
2272 for (i = 0; i < n1; i++)
2274 ira_object_t c1 = ALLOCNO_OBJECT (a1, i);
2276 for (j = 0; j < n2; j++)
2278 ira_object_t c2 = ALLOCNO_OBJECT (a2, j);
2280 if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1),
2281 OBJECT_LIVE_RANGES (c2)))
2282 return true;
2285 return false;
2288 /* The function is used to sort copies according to their execution
2289 frequencies. */
2290 static int
2291 copy_freq_compare_func (const void *v1p, const void *v2p)
2293 ira_copy_t cp1 = *(const ira_copy_t *) v1p, cp2 = *(const ira_copy_t *) v2p;
2294 int pri1, pri2;
2296 pri1 = cp1->freq;
2297 pri2 = cp2->freq;
2298 if (pri2 - pri1)
2299 return pri2 - pri1;
2301 /* If frequencies are equal, sort by copies, so that the results of
2302 qsort leave nothing to chance. */
2303 return cp1->num - cp2->num;
2308 /* Return true if any allocno from thread of A1 conflicts with any
2309 allocno from thread A2. */
2310 static bool
2311 allocno_thread_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
2313 ira_allocno_t a, conflict_a;
2315 for (a = ALLOCNO_COLOR_DATA (a2)->next_thread_allocno;;
2316 a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2318 for (conflict_a = ALLOCNO_COLOR_DATA (a1)->next_thread_allocno;;
2319 conflict_a = ALLOCNO_COLOR_DATA (conflict_a)->next_thread_allocno)
2321 if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
2322 return true;
2323 if (conflict_a == a1)
2324 break;
2326 if (a == a2)
2327 break;
2329 return false;
2332 /* Merge two threads given correspondingly by their first allocnos T1
2333 and T2 (more accurately merging T2 into T1). */
2334 static void
2335 merge_threads (ira_allocno_t t1, ira_allocno_t t2)
2337 ira_allocno_t a, next, last;
2339 gcc_assert (t1 != t2
2340 && ALLOCNO_COLOR_DATA (t1)->first_thread_allocno == t1
2341 && ALLOCNO_COLOR_DATA (t2)->first_thread_allocno == t2);
2342 for (last = t2, a = ALLOCNO_COLOR_DATA (t2)->next_thread_allocno;;
2343 a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2345 ALLOCNO_COLOR_DATA (a)->first_thread_allocno = t1;
2346 if (a == t2)
2347 break;
2348 last = a;
2350 next = ALLOCNO_COLOR_DATA (t1)->next_thread_allocno;
2351 ALLOCNO_COLOR_DATA (t1)->next_thread_allocno = t2;
2352 ALLOCNO_COLOR_DATA (last)->next_thread_allocno = next;
2353 ALLOCNO_COLOR_DATA (t1)->thread_freq += ALLOCNO_COLOR_DATA (t2)->thread_freq;
2356 /* Create threads by processing CP_NUM copies from sorted copies. We
2357 process the most expensive copies first. */
2358 static void
2359 form_threads_from_copies (int cp_num)
2361 ira_allocno_t a, thread1, thread2;
2362 ira_copy_t cp;
2364 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
2365 /* Form threads processing copies, most frequently executed
2366 first. */
2367 for (int i = 0; i < cp_num; i++)
2369 cp = sorted_copies[i];
2370 thread1 = ALLOCNO_COLOR_DATA (cp->first)->first_thread_allocno;
2371 thread2 = ALLOCNO_COLOR_DATA (cp->second)->first_thread_allocno;
2372 if (thread1 == thread2)
2373 continue;
2374 if (! allocno_thread_conflict_p (thread1, thread2))
2376 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2377 fprintf
2378 (ira_dump_file,
2379 " Forming thread by copy %d:a%dr%d-a%dr%d (freq=%d):\n",
2380 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
2381 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
2382 cp->freq);
2383 merge_threads (thread1, thread2);
2384 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2386 thread1 = ALLOCNO_COLOR_DATA (thread1)->first_thread_allocno;
2387 fprintf (ira_dump_file, " Result (freq=%d): a%dr%d(%d)",
2388 ALLOCNO_COLOR_DATA (thread1)->thread_freq,
2389 ALLOCNO_NUM (thread1), ALLOCNO_REGNO (thread1),
2390 ALLOCNO_FREQ (thread1));
2391 for (a = ALLOCNO_COLOR_DATA (thread1)->next_thread_allocno;
2392 a != thread1;
2393 a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2394 fprintf (ira_dump_file, " a%dr%d(%d)",
2395 ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2396 ALLOCNO_FREQ (a));
2397 fprintf (ira_dump_file, "\n");
2403 /* Create threads by processing copies of all alocnos from BUCKET. We
2404 process the most expensive copies first. */
2405 static void
2406 form_threads_from_bucket (ira_allocno_t bucket)
2408 ira_allocno_t a;
2409 ira_copy_t cp, next_cp;
2410 int cp_num = 0;
2412 for (a = bucket; a != NULL; a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2414 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2416 if (cp->first == a)
2418 next_cp = cp->next_first_allocno_copy;
2419 sorted_copies[cp_num++] = cp;
2421 else if (cp->second == a)
2422 next_cp = cp->next_second_allocno_copy;
2423 else
2424 gcc_unreachable ();
2427 form_threads_from_copies (cp_num);
2430 /* Create threads by processing copies of colorable allocno A. We
2431 process most expensive copies first. */
2432 static void
2433 form_threads_from_colorable_allocno (ira_allocno_t a)
2435 ira_allocno_t another_a;
2436 ira_copy_t cp, next_cp;
2437 int cp_num = 0;
2439 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2440 fprintf (ira_dump_file, " Forming thread from allocno a%dr%d:\n",
2441 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2442 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2444 if (cp->first == a)
2446 next_cp = cp->next_first_allocno_copy;
2447 another_a = cp->second;
2449 else if (cp->second == a)
2451 next_cp = cp->next_second_allocno_copy;
2452 another_a = cp->first;
2454 else
2455 gcc_unreachable ();
2456 if ((! ALLOCNO_COLOR_DATA (another_a)->in_graph_p
2457 && !ALLOCNO_COLOR_DATA (another_a)->may_be_spilled_p)
2458 || ALLOCNO_COLOR_DATA (another_a)->colorable_p)
2459 sorted_copies[cp_num++] = cp;
2461 form_threads_from_copies (cp_num);
2464 /* Form initial threads which contain only one allocno. */
2465 static void
2466 init_allocno_threads (void)
2468 ira_allocno_t a;
2469 unsigned int j;
2470 bitmap_iterator bi;
2471 ira_pref_t pref;
2473 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2475 a = ira_allocnos[j];
2476 /* Set up initial thread data: */
2477 ALLOCNO_COLOR_DATA (a)->first_thread_allocno
2478 = ALLOCNO_COLOR_DATA (a)->next_thread_allocno = a;
2479 ALLOCNO_COLOR_DATA (a)->thread_freq = ALLOCNO_FREQ (a);
2480 ALLOCNO_COLOR_DATA (a)->hard_reg_prefs = 0;
2481 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
2482 ALLOCNO_COLOR_DATA (a)->hard_reg_prefs += pref->freq;
2488 /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
2490 /* Bucket of allocnos that can colored currently without spilling. */
2491 static ira_allocno_t colorable_allocno_bucket;
2493 /* Bucket of allocnos that might be not colored currently without
2494 spilling. */
2495 static ira_allocno_t uncolorable_allocno_bucket;
2497 /* The current number of allocnos in the uncolorable_bucket. */
2498 static int uncolorable_allocnos_num;
2500 /* Return the current spill priority of allocno A. The less the
2501 number, the more preferable the allocno for spilling. */
2502 static inline int
2503 allocno_spill_priority (ira_allocno_t a)
2505 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
2507 return (data->temp
2508 / (ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
2509 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
2510 + 1));
2513 /* Add allocno A to bucket *BUCKET_PTR. A should be not in a bucket
2514 before the call. */
2515 static void
2516 add_allocno_to_bucket (ira_allocno_t a, ira_allocno_t *bucket_ptr)
2518 ira_allocno_t first_a;
2519 allocno_color_data_t data;
2521 if (bucket_ptr == &uncolorable_allocno_bucket
2522 && ALLOCNO_CLASS (a) != NO_REGS)
2524 uncolorable_allocnos_num++;
2525 ira_assert (uncolorable_allocnos_num > 0);
2527 first_a = *bucket_ptr;
2528 data = ALLOCNO_COLOR_DATA (a);
2529 data->next_bucket_allocno = first_a;
2530 data->prev_bucket_allocno = NULL;
2531 if (first_a != NULL)
2532 ALLOCNO_COLOR_DATA (first_a)->prev_bucket_allocno = a;
2533 *bucket_ptr = a;
2536 /* Compare two allocnos to define which allocno should be pushed first
2537 into the coloring stack. If the return is a negative number, the
2538 allocno given by the first parameter will be pushed first. In this
2539 case such allocno has less priority than the second one and the
2540 hard register will be assigned to it after assignment to the second
2541 one. As the result of such assignment order, the second allocno
2542 has a better chance to get the best hard register. */
2543 static int
2544 bucket_allocno_compare_func (const void *v1p, const void *v2p)
2546 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2547 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2548 int diff, freq1, freq2, a1_num, a2_num, pref1, pref2;
2549 ira_allocno_t t1 = ALLOCNO_COLOR_DATA (a1)->first_thread_allocno;
2550 ira_allocno_t t2 = ALLOCNO_COLOR_DATA (a2)->first_thread_allocno;
2551 int cl1 = ALLOCNO_CLASS (a1), cl2 = ALLOCNO_CLASS (a2);
2553 freq1 = ALLOCNO_COLOR_DATA (t1)->thread_freq;
2554 freq2 = ALLOCNO_COLOR_DATA (t2)->thread_freq;
2555 if ((diff = freq1 - freq2) != 0)
2556 return diff;
2558 if ((diff = ALLOCNO_NUM (t2) - ALLOCNO_NUM (t1)) != 0)
2559 return diff;
2561 /* Push pseudos requiring less hard registers first. It means that
2562 we will assign pseudos requiring more hard registers first
2563 avoiding creation small holes in free hard register file into
2564 which the pseudos requiring more hard registers cannot fit. */
2565 if ((diff = (ira_reg_class_max_nregs[cl1][ALLOCNO_MODE (a1)]
2566 - ira_reg_class_max_nregs[cl2][ALLOCNO_MODE (a2)])) != 0)
2567 return diff;
2569 freq1 = ALLOCNO_FREQ (a1);
2570 freq2 = ALLOCNO_FREQ (a2);
2571 if ((diff = freq1 - freq2) != 0)
2572 return diff;
2574 a1_num = ALLOCNO_COLOR_DATA (a1)->available_regs_num;
2575 a2_num = ALLOCNO_COLOR_DATA (a2)->available_regs_num;
2576 if ((diff = a2_num - a1_num) != 0)
2577 return diff;
2578 /* Push allocnos with minimal conflict_allocno_hard_prefs first. */
2579 pref1 = ALLOCNO_COLOR_DATA (a1)->conflict_allocno_hard_prefs;
2580 pref2 = ALLOCNO_COLOR_DATA (a2)->conflict_allocno_hard_prefs;
2581 if ((diff = pref1 - pref2) != 0)
2582 return diff;
2583 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
2586 /* Sort bucket *BUCKET_PTR and return the result through
2587 BUCKET_PTR. */
2588 static void
2589 sort_bucket (ira_allocno_t *bucket_ptr,
2590 int (*compare_func) (const void *, const void *))
2592 ira_allocno_t a, head;
2593 int n;
2595 for (n = 0, a = *bucket_ptr;
2596 a != NULL;
2597 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2598 sorted_allocnos[n++] = a;
2599 if (n <= 1)
2600 return;
2601 qsort (sorted_allocnos, n, sizeof (ira_allocno_t), compare_func);
2602 head = NULL;
2603 for (n--; n >= 0; n--)
2605 a = sorted_allocnos[n];
2606 ALLOCNO_COLOR_DATA (a)->next_bucket_allocno = head;
2607 ALLOCNO_COLOR_DATA (a)->prev_bucket_allocno = NULL;
2608 if (head != NULL)
2609 ALLOCNO_COLOR_DATA (head)->prev_bucket_allocno = a;
2610 head = a;
2612 *bucket_ptr = head;
2615 /* Add ALLOCNO to colorable bucket maintaining the order according
2616 their priority. ALLOCNO should be not in a bucket before the
2617 call. */
2618 static void
2619 add_allocno_to_ordered_colorable_bucket (ira_allocno_t allocno)
2621 ira_allocno_t before, after;
2623 form_threads_from_colorable_allocno (allocno);
2624 for (before = colorable_allocno_bucket, after = NULL;
2625 before != NULL;
2626 after = before,
2627 before = ALLOCNO_COLOR_DATA (before)->next_bucket_allocno)
2628 if (bucket_allocno_compare_func (&allocno, &before) < 0)
2629 break;
2630 ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno = before;
2631 ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno = after;
2632 if (after == NULL)
2633 colorable_allocno_bucket = allocno;
2634 else
2635 ALLOCNO_COLOR_DATA (after)->next_bucket_allocno = allocno;
2636 if (before != NULL)
2637 ALLOCNO_COLOR_DATA (before)->prev_bucket_allocno = allocno;
2640 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
2641 the call. */
2642 static void
2643 delete_allocno_from_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
2645 ira_allocno_t prev_allocno, next_allocno;
2647 if (bucket_ptr == &uncolorable_allocno_bucket
2648 && ALLOCNO_CLASS (allocno) != NO_REGS)
2650 uncolorable_allocnos_num--;
2651 ira_assert (uncolorable_allocnos_num >= 0);
2653 prev_allocno = ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno;
2654 next_allocno = ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno;
2655 if (prev_allocno != NULL)
2656 ALLOCNO_COLOR_DATA (prev_allocno)->next_bucket_allocno = next_allocno;
2657 else
2659 ira_assert (*bucket_ptr == allocno);
2660 *bucket_ptr = next_allocno;
2662 if (next_allocno != NULL)
2663 ALLOCNO_COLOR_DATA (next_allocno)->prev_bucket_allocno = prev_allocno;
2666 /* Put allocno A onto the coloring stack without removing it from its
2667 bucket. Pushing allocno to the coloring stack can result in moving
2668 conflicting allocnos from the uncolorable bucket to the colorable
2669 one. Update conflict_allocno_hard_prefs of the conflicting
2670 allocnos which are not on stack yet. */
2671 static void
2672 push_allocno_to_stack (ira_allocno_t a)
2674 enum reg_class aclass;
2675 allocno_color_data_t data, conflict_data;
2676 int size, i, n = ALLOCNO_NUM_OBJECTS (a);
2678 data = ALLOCNO_COLOR_DATA (a);
2679 data->in_graph_p = false;
2680 allocno_stack_vec.safe_push (a);
2681 aclass = ALLOCNO_CLASS (a);
2682 if (aclass == NO_REGS)
2683 return;
2684 size = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
2685 if (n > 1)
2687 /* We will deal with the subwords individually. */
2688 gcc_assert (size == ALLOCNO_NUM_OBJECTS (a));
2689 size = 1;
2691 for (i = 0; i < n; i++)
2693 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2694 ira_object_t conflict_obj;
2695 ira_object_conflict_iterator oci;
2697 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2699 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2700 ira_pref_t pref;
2702 conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
2703 if (! conflict_data->in_graph_p
2704 || ALLOCNO_ASSIGNED_P (conflict_a)
2705 || !(hard_reg_set_intersect_p
2706 (ALLOCNO_COLOR_DATA (a)->profitable_hard_regs,
2707 conflict_data->profitable_hard_regs)))
2708 continue;
2709 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
2710 conflict_data->conflict_allocno_hard_prefs -= pref->freq;
2711 if (conflict_data->colorable_p)
2712 continue;
2713 ira_assert (bitmap_bit_p (coloring_allocno_bitmap,
2714 ALLOCNO_NUM (conflict_a)));
2715 if (update_left_conflict_sizes_p (conflict_a, a, size))
2717 delete_allocno_from_bucket
2718 (conflict_a, &uncolorable_allocno_bucket);
2719 add_allocno_to_ordered_colorable_bucket (conflict_a);
2720 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2722 fprintf (ira_dump_file, " Making");
2723 ira_print_expanded_allocno (conflict_a);
2724 fprintf (ira_dump_file, " colorable\n");
2732 /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
2733 The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
2734 static void
2735 remove_allocno_from_bucket_and_push (ira_allocno_t allocno, bool colorable_p)
2737 if (colorable_p)
2738 delete_allocno_from_bucket (allocno, &colorable_allocno_bucket);
2739 else
2740 delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
2741 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2743 fprintf (ira_dump_file, " Pushing");
2744 ira_print_expanded_allocno (allocno);
2745 if (colorable_p)
2746 fprintf (ira_dump_file, "(cost %d)\n",
2747 ALLOCNO_COLOR_DATA (allocno)->temp);
2748 else
2749 fprintf (ira_dump_file, "(potential spill: %spri=%d, cost=%d)\n",
2750 ALLOCNO_BAD_SPILL_P (allocno) ? "bad spill, " : "",
2751 allocno_spill_priority (allocno),
2752 ALLOCNO_COLOR_DATA (allocno)->temp);
2754 if (! colorable_p)
2755 ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p = true;
2756 push_allocno_to_stack (allocno);
2759 /* Put all allocnos from colorable bucket onto the coloring stack. */
2760 static void
2761 push_only_colorable (void)
2763 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2764 fprintf (ira_dump_file, " Forming thread from colorable bucket:\n");
2765 form_threads_from_bucket (colorable_allocno_bucket);
2766 for (ira_allocno_t a = colorable_allocno_bucket;
2767 a != NULL;
2768 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2769 update_costs_from_prefs (a);
2770 sort_bucket (&colorable_allocno_bucket, bucket_allocno_compare_func);
2771 for (;colorable_allocno_bucket != NULL;)
2772 remove_allocno_from_bucket_and_push (colorable_allocno_bucket, true);
2775 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
2776 loop given by its LOOP_NODE. */
2778 ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
2780 int freq, i;
2781 edge_iterator ei;
2782 edge e;
2784 ira_assert (current_loops != NULL && loop_node->loop != NULL
2785 && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
2786 freq = 0;
2787 if (! exit_p)
2789 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
2790 if (e->src != loop_node->loop->latch
2791 && (regno < 0
2792 || (bitmap_bit_p (df_get_live_out (e->src), regno)
2793 && bitmap_bit_p (df_get_live_in (e->dest), regno))))
2794 freq += EDGE_FREQUENCY (e);
2796 else
2798 auto_vec<edge> edges = get_loop_exit_edges (loop_node->loop);
2799 FOR_EACH_VEC_ELT (edges, i, e)
2800 if (regno < 0
2801 || (bitmap_bit_p (df_get_live_out (e->src), regno)
2802 && bitmap_bit_p (df_get_live_in (e->dest), regno)))
2803 freq += EDGE_FREQUENCY (e);
2806 return REG_FREQ_FROM_EDGE_FREQ (freq);
2809 /* Construct an object that describes the boundary between A and its
2810 parent allocno. */
2811 ira_loop_border_costs::ira_loop_border_costs (ira_allocno_t a)
2812 : m_mode (ALLOCNO_MODE (a)),
2813 m_class (ALLOCNO_CLASS (a)),
2814 m_entry_freq (ira_loop_edge_freq (ALLOCNO_LOOP_TREE_NODE (a),
2815 ALLOCNO_REGNO (a), false)),
2816 m_exit_freq (ira_loop_edge_freq (ALLOCNO_LOOP_TREE_NODE (a),
2817 ALLOCNO_REGNO (a), true))
2821 /* Calculate and return the cost of putting allocno A into memory. */
2822 static int
2823 calculate_allocno_spill_cost (ira_allocno_t a)
2825 int regno, cost;
2826 ira_allocno_t parent_allocno;
2827 ira_loop_tree_node_t parent_node, loop_node;
2829 regno = ALLOCNO_REGNO (a);
2830 cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_UPDATED_CLASS_COST (a);
2831 if (ALLOCNO_CAP (a) != NULL)
2832 return cost;
2833 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
2834 if ((parent_node = loop_node->parent) == NULL)
2835 return cost;
2836 if ((parent_allocno = parent_node->regno_allocno_map[regno]) == NULL)
2837 return cost;
2838 ira_loop_border_costs border_costs (a);
2839 if (ALLOCNO_HARD_REGNO (parent_allocno) < 0)
2840 cost -= border_costs.spill_outside_loop_cost ();
2841 else
2842 cost += (border_costs.spill_inside_loop_cost ()
2843 - border_costs.move_between_loops_cost ());
2844 return cost;
2847 /* Used for sorting allocnos for spilling. */
2848 static inline int
2849 allocno_spill_priority_compare (ira_allocno_t a1, ira_allocno_t a2)
2851 int pri1, pri2, diff;
2853 /* Avoid spilling static chain pointer pseudo when non-local goto is
2854 used. */
2855 if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))
2856 return 1;
2857 else if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2)))
2858 return -1;
2859 if (ALLOCNO_BAD_SPILL_P (a1) && ! ALLOCNO_BAD_SPILL_P (a2))
2860 return 1;
2861 if (ALLOCNO_BAD_SPILL_P (a2) && ! ALLOCNO_BAD_SPILL_P (a1))
2862 return -1;
2863 pri1 = allocno_spill_priority (a1);
2864 pri2 = allocno_spill_priority (a2);
2865 if ((diff = pri1 - pri2) != 0)
2866 return diff;
2867 if ((diff
2868 = ALLOCNO_COLOR_DATA (a1)->temp - ALLOCNO_COLOR_DATA (a2)->temp) != 0)
2869 return diff;
2870 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2873 /* Used for sorting allocnos for spilling. */
2874 static int
2875 allocno_spill_sort_compare (const void *v1p, const void *v2p)
2877 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2878 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2880 return allocno_spill_priority_compare (p1, p2);
2883 /* Push allocnos to the coloring stack. The order of allocnos in the
2884 stack defines the order for the subsequent coloring. */
2885 static void
2886 push_allocnos_to_stack (void)
2888 ira_allocno_t a;
2889 int cost;
2891 /* Calculate uncolorable allocno spill costs. */
2892 for (a = uncolorable_allocno_bucket;
2893 a != NULL;
2894 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2895 if (ALLOCNO_CLASS (a) != NO_REGS)
2897 cost = calculate_allocno_spill_cost (a);
2898 /* ??? Remove cost of copies between the coalesced
2899 allocnos. */
2900 ALLOCNO_COLOR_DATA (a)->temp = cost;
2902 sort_bucket (&uncolorable_allocno_bucket, allocno_spill_sort_compare);
2903 for (;;)
2905 push_only_colorable ();
2906 a = uncolorable_allocno_bucket;
2907 if (a == NULL)
2908 break;
2909 remove_allocno_from_bucket_and_push (a, false);
2911 ira_assert (colorable_allocno_bucket == NULL
2912 && uncolorable_allocno_bucket == NULL);
2913 ira_assert (uncolorable_allocnos_num == 0);
2916 /* Pop the coloring stack and assign hard registers to the popped
2917 allocnos. */
2918 static void
2919 pop_allocnos_from_stack (void)
2921 ira_allocno_t allocno;
2922 enum reg_class aclass;
2924 for (;allocno_stack_vec.length () != 0;)
2926 allocno = allocno_stack_vec.pop ();
2927 aclass = ALLOCNO_CLASS (allocno);
2928 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2930 fprintf (ira_dump_file, " Popping");
2931 ira_print_expanded_allocno (allocno);
2932 fprintf (ira_dump_file, " -- ");
2934 if (aclass == NO_REGS)
2936 ALLOCNO_HARD_REGNO (allocno) = -1;
2937 ALLOCNO_ASSIGNED_P (allocno) = true;
2938 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
2939 ira_assert
2940 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
2941 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2942 fprintf (ira_dump_file, "assign memory\n");
2944 else if (assign_hard_reg (allocno, false))
2946 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2947 fprintf (ira_dump_file, " assign reg %d\n",
2948 ALLOCNO_HARD_REGNO (allocno));
2950 else if (ALLOCNO_ASSIGNED_P (allocno))
2952 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2953 fprintf (ira_dump_file, "spill%s\n",
2954 ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p
2955 ? "" : "!");
2957 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2961 /* Set up number of available hard registers for allocno A. */
2962 static void
2963 setup_allocno_available_regs_num (ira_allocno_t a)
2965 int i, n, hard_regno, hard_regs_num, nwords;
2966 enum reg_class aclass;
2967 allocno_color_data_t data;
2969 aclass = ALLOCNO_CLASS (a);
2970 data = ALLOCNO_COLOR_DATA (a);
2971 data->available_regs_num = 0;
2972 if (aclass == NO_REGS)
2973 return;
2974 hard_regs_num = ira_class_hard_regs_num[aclass];
2975 nwords = ALLOCNO_NUM_OBJECTS (a);
2976 for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
2978 hard_regno = ira_class_hard_regs[aclass][i];
2979 /* Checking only profitable hard regs. */
2980 if (TEST_HARD_REG_BIT (data->profitable_hard_regs, hard_regno))
2981 n++;
2983 data->available_regs_num = n;
2984 if (internal_flag_ira_verbose <= 2 || ira_dump_file == NULL)
2985 return;
2986 fprintf
2987 (ira_dump_file,
2988 " Allocno a%dr%d of %s(%d) has %d avail. regs ",
2989 ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2990 reg_class_names[aclass], ira_class_hard_regs_num[aclass], n);
2991 print_hard_reg_set (ira_dump_file, data->profitable_hard_regs, false);
2992 fprintf (ira_dump_file, ", %snode: ",
2993 data->profitable_hard_regs == data->hard_regs_node->hard_regs->set
2994 ? "" : "^");
2995 print_hard_reg_set (ira_dump_file,
2996 data->hard_regs_node->hard_regs->set, false);
2997 for (i = 0; i < nwords; i++)
2999 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3001 if (nwords != 1)
3003 if (i != 0)
3004 fprintf (ira_dump_file, ", ");
3005 fprintf (ira_dump_file, " obj %d", i);
3007 fprintf (ira_dump_file, " (confl regs = ");
3008 print_hard_reg_set (ira_dump_file, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
3009 false);
3010 fprintf (ira_dump_file, ")");
3012 fprintf (ira_dump_file, "\n");
3015 /* Put ALLOCNO in a bucket corresponding to its number and size of its
3016 conflicting allocnos and hard registers. */
3017 static void
3018 put_allocno_into_bucket (ira_allocno_t allocno)
3020 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
3021 setup_allocno_available_regs_num (allocno);
3022 if (setup_left_conflict_sizes_p (allocno))
3023 add_allocno_to_bucket (allocno, &colorable_allocno_bucket);
3024 else
3025 add_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
3028 /* Map: allocno number -> allocno priority. */
3029 static int *allocno_priorities;
3031 /* Set up priorities for N allocnos in array
3032 CONSIDERATION_ALLOCNOS. */
3033 static void
3034 setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
3036 int i, length, nrefs, priority, max_priority, mult, diff;
3037 ira_allocno_t a;
3039 max_priority = 0;
3040 for (i = 0; i < n; i++)
3042 a = consideration_allocnos[i];
3043 nrefs = ALLOCNO_NREFS (a);
3044 ira_assert (nrefs >= 0);
3045 mult = floor_log2 (ALLOCNO_NREFS (a)) + 1;
3046 ira_assert (mult >= 0);
3047 mult *= ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
3048 diff = ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
3049 #ifdef __has_builtin
3050 #if __has_builtin(__builtin_smul_overflow)
3051 #define HAS_SMUL_OVERFLOW
3052 #endif
3053 #endif
3054 /* Multiplication can overflow for very large functions.
3055 Check the overflow and constrain the result if necessary: */
3056 #ifdef HAS_SMUL_OVERFLOW
3057 if (__builtin_smul_overflow (mult, diff, &priority)
3058 || priority < -INT_MAX)
3059 priority = diff >= 0 ? INT_MAX : -INT_MAX;
3060 #else
3061 static_assert
3062 (sizeof (long long) >= 2 * sizeof (int),
3063 "overflow code does not work for such int and long long sizes");
3064 long long priorityll = (long long) mult * diff;
3065 if (priorityll < -INT_MAX || priorityll > INT_MAX)
3066 priority = diff >= 0 ? INT_MAX : -INT_MAX;
3067 else
3068 priority = priorityll;
3069 #endif
3070 allocno_priorities[ALLOCNO_NUM (a)] = priority;
3071 if (priority < 0)
3072 priority = -priority;
3073 if (max_priority < priority)
3074 max_priority = priority;
3076 mult = max_priority == 0 ? 1 : INT_MAX / max_priority;
3077 for (i = 0; i < n; i++)
3079 a = consideration_allocnos[i];
3080 length = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
3081 if (ALLOCNO_NUM_OBJECTS (a) > 1)
3082 length /= ALLOCNO_NUM_OBJECTS (a);
3083 if (length <= 0)
3084 length = 1;
3085 allocno_priorities[ALLOCNO_NUM (a)]
3086 = allocno_priorities[ALLOCNO_NUM (a)] * mult / length;
3090 /* Sort allocnos according to the profit of usage of a hard register
3091 instead of memory for them. */
3092 static int
3093 allocno_cost_compare_func (const void *v1p, const void *v2p)
3095 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
3096 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
3097 int c1, c2;
3099 c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_UPDATED_CLASS_COST (p1);
3100 c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_UPDATED_CLASS_COST (p2);
3101 if (c1 - c2)
3102 return c1 - c2;
3104 /* If regs are equally good, sort by allocno numbers, so that the
3105 results of qsort leave nothing to chance. */
3106 return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
3109 /* Return savings on removed copies when ALLOCNO is assigned to
3110 HARD_REGNO. */
3111 static int
3112 allocno_copy_cost_saving (ira_allocno_t allocno, int hard_regno)
3114 int cost = 0;
3115 machine_mode allocno_mode = ALLOCNO_MODE (allocno);
3116 enum reg_class rclass;
3117 ira_copy_t cp, next_cp;
3119 rclass = REGNO_REG_CLASS (hard_regno);
3120 if (ira_reg_class_max_nregs[rclass][allocno_mode]
3121 > ira_class_hard_regs_num[rclass])
3122 /* For the above condition the cost can be wrong. Use the allocno
3123 class in this case. */
3124 rclass = ALLOCNO_CLASS (allocno);
3125 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
3127 if (cp->first == allocno)
3129 next_cp = cp->next_first_allocno_copy;
3130 if (ALLOCNO_HARD_REGNO (cp->second) != hard_regno)
3131 continue;
3133 else if (cp->second == allocno)
3135 next_cp = cp->next_second_allocno_copy;
3136 if (ALLOCNO_HARD_REGNO (cp->first) != hard_regno)
3137 continue;
3139 else
3140 gcc_unreachable ();
3141 ira_init_register_move_cost_if_necessary (allocno_mode);
3142 cost += cp->freq * ira_register_move_cost[allocno_mode][rclass][rclass];
3144 return cost;
3147 /* We used Chaitin-Briggs coloring to assign as many pseudos as
3148 possible to hard registers. Let us try to improve allocation with
3149 cost point of view. This function improves the allocation by
3150 spilling some allocnos and assigning the freed hard registers to
3151 other allocnos if it decreases the overall allocation cost. */
3152 static void
3153 improve_allocation (void)
3155 unsigned int i;
3156 int j, k, n, hregno, conflict_hregno, base_cost, class_size, word, nwords;
3157 int check, spill_cost, min_cost, nregs, conflict_nregs, r, best;
3158 bool try_p;
3159 enum reg_class aclass, rclass;
3160 machine_mode mode;
3161 int *allocno_costs;
3162 int costs[FIRST_PSEUDO_REGISTER];
3163 HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
3164 ira_allocno_t a;
3165 bitmap_iterator bi;
3166 int saved_nregs;
3167 int add_cost;
3169 /* Don't bother to optimize the code with static chain pointer and
3170 non-local goto in order not to spill the chain pointer
3171 pseudo. */
3172 if (cfun->static_chain_decl && crtl->has_nonlocal_goto)
3173 return;
3174 /* Clear counts used to process conflicting allocnos only once for
3175 each allocno. */
3176 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3177 ALLOCNO_COLOR_DATA (ira_allocnos[i])->temp = 0;
3178 check = n = 0;
3179 /* Process each allocno and try to assign a hard register to it by
3180 spilling some its conflicting allocnos. */
3181 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3183 a = ira_allocnos[i];
3184 ALLOCNO_COLOR_DATA (a)->temp = 0;
3185 if (empty_profitable_hard_regs (a))
3186 continue;
3187 check++;
3188 aclass = ALLOCNO_CLASS (a);
3189 allocno_costs = ALLOCNO_HARD_REG_COSTS (a);
3190 if ((hregno = ALLOCNO_HARD_REGNO (a)) < 0)
3191 base_cost = ALLOCNO_UPDATED_MEMORY_COST (a);
3192 else if (allocno_costs == NULL)
3193 /* It means that assigning a hard register is not profitable
3194 (we don't waste memory for hard register costs in this
3195 case). */
3196 continue;
3197 else
3198 base_cost = (allocno_costs[ira_class_hard_reg_index[aclass][hregno]]
3199 - allocno_copy_cost_saving (a, hregno));
3200 try_p = false;
3201 get_conflict_and_start_profitable_regs (a, false,
3202 conflicting_regs,
3203 &profitable_hard_regs);
3204 class_size = ira_class_hard_regs_num[aclass];
3205 mode = ALLOCNO_MODE (a);
3206 /* Set up cost improvement for usage of each profitable hard
3207 register for allocno A. */
3208 for (j = 0; j < class_size; j++)
3210 hregno = ira_class_hard_regs[aclass][j];
3211 if (! check_hard_reg_p (a, hregno,
3212 conflicting_regs, profitable_hard_regs))
3213 continue;
3214 if (NUM_REGISTER_FILTERS
3215 && !test_register_filters (ALLOCNO_REGISTER_FILTERS (a), hregno))
3216 continue;
3217 ira_assert (ira_class_hard_reg_index[aclass][hregno] == j);
3218 k = allocno_costs == NULL ? 0 : j;
3219 costs[hregno] = (allocno_costs == NULL
3220 ? ALLOCNO_UPDATED_CLASS_COST (a) : allocno_costs[k]);
3221 costs[hregno] -= allocno_copy_cost_saving (a, hregno);
3223 if ((saved_nregs = calculate_saved_nregs (hregno, mode)) != 0)
3225 /* We need to save/restore the hard register in
3226 epilogue/prologue. Therefore we increase the cost.
3227 Since the prolog is placed in the entry BB, the frequency
3228 of the entry BB is considered while computing the cost. */
3229 rclass = REGNO_REG_CLASS (hregno);
3230 add_cost = ((ira_memory_move_cost[mode][rclass][0]
3231 + ira_memory_move_cost[mode][rclass][1])
3232 * saved_nregs / hard_regno_nregs (hregno,
3233 mode) - 1)
3234 * REG_FREQ_FROM_BB (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3235 costs[hregno] += add_cost;
3238 costs[hregno] -= base_cost;
3239 if (costs[hregno] < 0)
3240 try_p = true;
3242 if (! try_p)
3243 /* There is no chance to improve the allocation cost by
3244 assigning hard register to allocno A even without spilling
3245 conflicting allocnos. */
3246 continue;
3247 auto_bitmap allocnos_to_spill;
3248 HARD_REG_SET soft_conflict_regs = {};
3249 mode = ALLOCNO_MODE (a);
3250 nwords = ALLOCNO_NUM_OBJECTS (a);
3251 /* Process each allocno conflicting with A and update the cost
3252 improvement for profitable hard registers of A. To use a
3253 hard register for A we need to spill some conflicting
3254 allocnos and that creates penalty for the cost
3255 improvement. */
3256 for (word = 0; word < nwords; word++)
3258 ira_object_t conflict_obj;
3259 ira_object_t obj = ALLOCNO_OBJECT (a, word);
3260 ira_object_conflict_iterator oci;
3262 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3264 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3266 if (ALLOCNO_COLOR_DATA (conflict_a)->temp == check)
3267 /* We already processed this conflicting allocno
3268 because we processed earlier another object of the
3269 conflicting allocno. */
3270 continue;
3271 ALLOCNO_COLOR_DATA (conflict_a)->temp = check;
3272 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
3273 continue;
3274 auto spill_a = ira_soft_conflict (a, conflict_a);
3275 if (spill_a)
3277 if (!bitmap_set_bit (allocnos_to_spill,
3278 ALLOCNO_NUM (spill_a)))
3279 continue;
3280 ira_loop_border_costs border_costs (spill_a);
3281 spill_cost = border_costs.spill_inside_loop_cost ();
3283 else
3285 spill_cost = ALLOCNO_UPDATED_MEMORY_COST (conflict_a);
3286 k = (ira_class_hard_reg_index
3287 [ALLOCNO_CLASS (conflict_a)][conflict_hregno]);
3288 ira_assert (k >= 0);
3289 if ((allocno_costs = ALLOCNO_HARD_REG_COSTS (conflict_a))
3290 != NULL)
3291 spill_cost -= allocno_costs[k];
3292 else
3293 spill_cost -= ALLOCNO_UPDATED_CLASS_COST (conflict_a);
3294 spill_cost
3295 += allocno_copy_cost_saving (conflict_a, conflict_hregno);
3297 conflict_nregs = hard_regno_nregs (conflict_hregno,
3298 ALLOCNO_MODE (conflict_a));
3299 auto note_conflict = [&](int r)
3301 if (check_hard_reg_p (a, r,
3302 conflicting_regs, profitable_hard_regs))
3304 if (spill_a)
3305 SET_HARD_REG_BIT (soft_conflict_regs, r);
3306 costs[r] += spill_cost;
3309 for (r = conflict_hregno;
3310 r >= 0 && (int) end_hard_regno (mode, r) > conflict_hregno;
3311 r--)
3312 note_conflict (r);
3313 for (r = conflict_hregno + 1;
3314 r < conflict_hregno + conflict_nregs;
3315 r++)
3316 note_conflict (r);
3319 min_cost = INT_MAX;
3320 best = -1;
3321 /* Now we choose hard register for A which results in highest
3322 allocation cost improvement. */
3323 for (j = 0; j < class_size; j++)
3325 hregno = ira_class_hard_regs[aclass][j];
3326 if (check_hard_reg_p (a, hregno,
3327 conflicting_regs, profitable_hard_regs)
3328 && min_cost > costs[hregno])
3330 best = hregno;
3331 min_cost = costs[hregno];
3334 if (min_cost >= 0)
3335 /* We are in a situation when assigning any hard register to A
3336 by spilling some conflicting allocnos does not improve the
3337 allocation cost. */
3338 continue;
3339 spill_soft_conflicts (a, allocnos_to_spill, soft_conflict_regs, best);
3340 nregs = hard_regno_nregs (best, mode);
3341 /* Now spill conflicting allocnos which contain a hard register
3342 of A when we assign the best chosen hard register to it. */
3343 for (word = 0; word < nwords; word++)
3345 ira_object_t conflict_obj;
3346 ira_object_t obj = ALLOCNO_OBJECT (a, word);
3347 ira_object_conflict_iterator oci;
3349 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3351 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3353 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
3354 continue;
3355 conflict_nregs = hard_regno_nregs (conflict_hregno,
3356 ALLOCNO_MODE (conflict_a));
3357 if (best + nregs <= conflict_hregno
3358 || conflict_hregno + conflict_nregs <= best)
3359 /* No intersection. */
3360 continue;
3361 ALLOCNO_HARD_REGNO (conflict_a) = -1;
3362 sorted_allocnos[n++] = conflict_a;
3363 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3364 fprintf (ira_dump_file, "Spilling a%dr%d for a%dr%d\n",
3365 ALLOCNO_NUM (conflict_a), ALLOCNO_REGNO (conflict_a),
3366 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3369 /* Assign the best chosen hard register to A. */
3370 ALLOCNO_HARD_REGNO (a) = best;
3372 for (j = nregs - 1; j >= 0; j--)
3373 allocated_hardreg_p[best + j] = true;
3375 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3376 fprintf (ira_dump_file, "Assigning %d to a%dr%d\n",
3377 best, ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3379 if (n == 0)
3380 return;
3381 /* We spilled some allocnos to assign their hard registers to other
3382 allocnos. The spilled allocnos are now in array
3383 'sorted_allocnos'. There is still a possibility that some of the
3384 spilled allocnos can get hard registers. So let us try assign
3385 them hard registers again (just a reminder -- function
3386 'assign_hard_reg' assigns hard registers only if it is possible
3387 and profitable). We process the spilled allocnos with biggest
3388 benefit to get hard register first -- see function
3389 'allocno_cost_compare_func'. */
3390 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
3391 allocno_cost_compare_func);
3392 for (j = 0; j < n; j++)
3394 a = sorted_allocnos[j];
3395 ALLOCNO_ASSIGNED_P (a) = false;
3396 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3398 fprintf (ira_dump_file, " ");
3399 ira_print_expanded_allocno (a);
3400 fprintf (ira_dump_file, " -- ");
3402 if (assign_hard_reg (a, false))
3404 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3405 fprintf (ira_dump_file, "assign hard reg %d\n",
3406 ALLOCNO_HARD_REGNO (a));
3408 else
3410 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3411 fprintf (ira_dump_file, "assign memory\n");
3416 /* Sort allocnos according to their priorities. */
3417 static int
3418 allocno_priority_compare_func (const void *v1p, const void *v2p)
3420 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
3421 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
3422 int pri1, pri2, diff;
3424 /* Assign hard reg to static chain pointer pseudo first when
3425 non-local goto is used. */
3426 if ((diff = (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2))
3427 - non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))) != 0)
3428 return diff;
3429 pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
3430 pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
3431 if (pri2 != pri1)
3432 return SORTGT (pri2, pri1);
3434 /* If regs are equally good, sort by allocnos, so that the results of
3435 qsort leave nothing to chance. */
3436 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
3439 /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
3440 taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
3441 static void
3442 color_allocnos (void)
3444 unsigned int i, n;
3445 bitmap_iterator bi;
3446 ira_allocno_t a;
3448 setup_profitable_hard_regs ();
3449 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3451 allocno_color_data_t data;
3452 ira_pref_t pref, next_pref;
3454 a = ira_allocnos[i];
3455 data = ALLOCNO_COLOR_DATA (a);
3456 data->conflict_allocno_hard_prefs = 0;
3457 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = next_pref)
3459 next_pref = pref->next_pref;
3460 if (! ira_hard_reg_in_set_p (pref->hard_regno,
3461 ALLOCNO_MODE (a),
3462 data->profitable_hard_regs))
3463 ira_remove_pref (pref);
3467 if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
3469 n = 0;
3470 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3472 a = ira_allocnos[i];
3473 if (ALLOCNO_CLASS (a) == NO_REGS)
3475 ALLOCNO_HARD_REGNO (a) = -1;
3476 ALLOCNO_ASSIGNED_P (a) = true;
3477 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3478 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3479 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3481 fprintf (ira_dump_file, " Spill");
3482 ira_print_expanded_allocno (a);
3483 fprintf (ira_dump_file, "\n");
3485 continue;
3487 sorted_allocnos[n++] = a;
3489 if (n != 0)
3491 setup_allocno_priorities (sorted_allocnos, n);
3492 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
3493 allocno_priority_compare_func);
3494 for (i = 0; i < n; i++)
3496 a = sorted_allocnos[i];
3497 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3499 fprintf (ira_dump_file, " ");
3500 ira_print_expanded_allocno (a);
3501 fprintf (ira_dump_file, " -- ");
3503 if (assign_hard_reg (a, false))
3505 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3506 fprintf (ira_dump_file, "assign hard reg %d\n",
3507 ALLOCNO_HARD_REGNO (a));
3509 else
3511 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3512 fprintf (ira_dump_file, "assign memory\n");
3517 else
3519 form_allocno_hard_regs_nodes_forest ();
3520 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3521 print_hard_regs_forest (ira_dump_file);
3522 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3524 a = ira_allocnos[i];
3525 if (ALLOCNO_CLASS (a) != NO_REGS && ! empty_profitable_hard_regs (a))
3527 ALLOCNO_COLOR_DATA (a)->in_graph_p = true;
3528 update_conflict_allocno_hard_prefs (a);
3530 else
3532 ALLOCNO_HARD_REGNO (a) = -1;
3533 ALLOCNO_ASSIGNED_P (a) = true;
3534 /* We don't need updated costs anymore. */
3535 ira_free_allocno_updated_costs (a);
3536 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3538 fprintf (ira_dump_file, " Spill");
3539 ira_print_expanded_allocno (a);
3540 fprintf (ira_dump_file, "\n");
3544 /* Put the allocnos into the corresponding buckets. */
3545 colorable_allocno_bucket = NULL;
3546 uncolorable_allocno_bucket = NULL;
3547 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3549 a = ira_allocnos[i];
3550 if (ALLOCNO_COLOR_DATA (a)->in_graph_p)
3551 put_allocno_into_bucket (a);
3553 push_allocnos_to_stack ();
3554 pop_allocnos_from_stack ();
3555 finish_allocno_hard_regs_nodes_forest ();
3557 improve_allocation ();
3562 /* Output information about the loop given by its LOOP_TREE_NODE. */
3563 static void
3564 print_loop_title (ira_loop_tree_node_t loop_tree_node)
3566 unsigned int j;
3567 bitmap_iterator bi;
3568 ira_loop_tree_node_t subloop_node, dest_loop_node;
3569 edge e;
3570 edge_iterator ei;
3572 if (loop_tree_node->parent == NULL)
3573 fprintf (ira_dump_file,
3574 "\n Loop 0 (parent -1, header bb%d, depth 0)\n bbs:",
3575 NUM_FIXED_BLOCKS);
3576 else
3578 ira_assert (current_loops != NULL && loop_tree_node->loop != NULL);
3579 fprintf (ira_dump_file,
3580 "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
3581 loop_tree_node->loop_num, loop_tree_node->parent->loop_num,
3582 loop_tree_node->loop->header->index,
3583 loop_depth (loop_tree_node->loop));
3585 for (subloop_node = loop_tree_node->children;
3586 subloop_node != NULL;
3587 subloop_node = subloop_node->next)
3588 if (subloop_node->bb != NULL)
3590 fprintf (ira_dump_file, " %d", subloop_node->bb->index);
3591 FOR_EACH_EDGE (e, ei, subloop_node->bb->succs)
3592 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
3593 && ((dest_loop_node = IRA_BB_NODE (e->dest)->parent)
3594 != loop_tree_node))
3595 fprintf (ira_dump_file, "(->%d:l%d)",
3596 e->dest->index, dest_loop_node->loop_num);
3598 fprintf (ira_dump_file, "\n all:");
3599 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
3600 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
3601 fprintf (ira_dump_file, "\n modified regnos:");
3602 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
3603 fprintf (ira_dump_file, " %d", j);
3604 fprintf (ira_dump_file, "\n border:");
3605 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
3606 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
3607 fprintf (ira_dump_file, "\n Pressure:");
3608 for (j = 0; (int) j < ira_pressure_classes_num; j++)
3610 enum reg_class pclass;
3612 pclass = ira_pressure_classes[j];
3613 if (loop_tree_node->reg_pressure[pclass] == 0)
3614 continue;
3615 fprintf (ira_dump_file, " %s=%d", reg_class_names[pclass],
3616 loop_tree_node->reg_pressure[pclass]);
3618 fprintf (ira_dump_file, "\n");
3621 /* Color the allocnos inside loop (in the extreme case it can be all
3622 of the function) given the corresponding LOOP_TREE_NODE. The
3623 function is called for each loop during top-down traverse of the
3624 loop tree. */
3625 static void
3626 color_pass (ira_loop_tree_node_t loop_tree_node)
3628 int regno, hard_regno, index = -1, n;
3629 int cost;
3630 unsigned int j;
3631 bitmap_iterator bi;
3632 machine_mode mode;
3633 enum reg_class rclass, aclass;
3634 ira_allocno_t a, subloop_allocno;
3635 ira_loop_tree_node_t subloop_node;
3637 ira_assert (loop_tree_node->bb == NULL);
3638 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3639 print_loop_title (loop_tree_node);
3641 bitmap_copy (coloring_allocno_bitmap, loop_tree_node->all_allocnos);
3642 bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
3643 n = 0;
3644 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3646 a = ira_allocnos[j];
3647 n++;
3648 if (! ALLOCNO_ASSIGNED_P (a))
3649 continue;
3650 bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
3652 allocno_color_data
3653 = (allocno_color_data_t) ira_allocate (sizeof (struct allocno_color_data)
3654 * n);
3655 memset (allocno_color_data, 0, sizeof (struct allocno_color_data) * n);
3656 curr_allocno_process = 0;
3657 n = 0;
3658 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3660 a = ira_allocnos[j];
3661 ALLOCNO_ADD_DATA (a) = allocno_color_data + n;
3662 n++;
3664 init_allocno_threads ();
3665 /* Color all mentioned allocnos including transparent ones. */
3666 color_allocnos ();
3667 /* Process caps. They are processed just once. */
3668 if (flag_ira_region == IRA_REGION_MIXED
3669 || flag_ira_region == IRA_REGION_ALL)
3670 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
3672 a = ira_allocnos[j];
3673 if (ALLOCNO_CAP_MEMBER (a) == NULL)
3674 continue;
3675 /* Remove from processing in the next loop. */
3676 bitmap_clear_bit (consideration_allocno_bitmap, j);
3677 rclass = ALLOCNO_CLASS (a);
3678 subloop_allocno = ALLOCNO_CAP_MEMBER (a);
3679 subloop_node = ALLOCNO_LOOP_TREE_NODE (subloop_allocno);
3680 if (ira_single_region_allocno_p (a, subloop_allocno))
3682 mode = ALLOCNO_MODE (a);
3683 hard_regno = ALLOCNO_HARD_REGNO (a);
3684 if (hard_regno >= 0)
3686 index = ira_class_hard_reg_index[rclass][hard_regno];
3687 ira_assert (index >= 0);
3689 regno = ALLOCNO_REGNO (a);
3690 ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno));
3691 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3692 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3693 if (hard_regno >= 0)
3694 update_costs_from_copies (subloop_allocno, true, true);
3695 /* We don't need updated costs anymore. */
3696 ira_free_allocno_updated_costs (subloop_allocno);
3699 /* Update costs of the corresponding allocnos (not caps) in the
3700 subloops. */
3701 for (subloop_node = loop_tree_node->subloops;
3702 subloop_node != NULL;
3703 subloop_node = subloop_node->subloop_next)
3705 ira_assert (subloop_node->bb == NULL);
3706 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3708 a = ira_allocnos[j];
3709 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3710 mode = ALLOCNO_MODE (a);
3711 rclass = ALLOCNO_CLASS (a);
3712 hard_regno = ALLOCNO_HARD_REGNO (a);
3713 /* Use hard register class here. ??? */
3714 if (hard_regno >= 0)
3716 index = ira_class_hard_reg_index[rclass][hard_regno];
3717 ira_assert (index >= 0);
3719 regno = ALLOCNO_REGNO (a);
3720 /* ??? conflict costs */
3721 subloop_allocno = subloop_node->regno_allocno_map[regno];
3722 if (subloop_allocno == NULL
3723 || ALLOCNO_CAP (subloop_allocno) != NULL)
3724 continue;
3725 ira_assert (ALLOCNO_CLASS (subloop_allocno) == rclass);
3726 ira_assert (bitmap_bit_p (subloop_node->all_allocnos,
3727 ALLOCNO_NUM (subloop_allocno)));
3728 if (ira_single_region_allocno_p (a, subloop_allocno)
3729 || !ira_subloop_allocnos_can_differ_p (a, hard_regno >= 0,
3730 false))
3732 gcc_assert (!ALLOCNO_MIGHT_CONFLICT_WITH_PARENT_P
3733 (subloop_allocno));
3734 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
3736 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3737 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3738 if (hard_regno >= 0)
3739 update_costs_from_copies (subloop_allocno, true, true);
3740 /* We don't need updated costs anymore. */
3741 ira_free_allocno_updated_costs (subloop_allocno);
3744 else if (hard_regno < 0)
3746 /* If we allocate a register to SUBLOOP_ALLOCNO, we'll need
3747 to load the register on entry to the subloop and store
3748 the register back on exit from the subloop. This incurs
3749 a fixed cost for all registers. Since UPDATED_MEMORY_COST
3750 is (and should only be) used relative to the register costs
3751 for the same allocno, we can subtract this shared register
3752 cost from the memory cost. */
3753 ira_loop_border_costs border_costs (subloop_allocno);
3754 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
3755 -= border_costs.spill_outside_loop_cost ();
3757 else
3759 ira_loop_border_costs border_costs (subloop_allocno);
3760 aclass = ALLOCNO_CLASS (subloop_allocno);
3761 ira_init_register_move_cost_if_necessary (mode);
3762 cost = border_costs.move_between_loops_cost ();
3763 ira_allocate_and_set_or_copy_costs
3764 (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno), aclass,
3765 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno),
3766 ALLOCNO_HARD_REG_COSTS (subloop_allocno));
3767 ira_allocate_and_set_or_copy_costs
3768 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno),
3769 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno));
3770 ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
3771 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
3772 -= cost;
3773 if (ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
3774 > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index])
3775 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
3776 = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index];
3777 /* If we spill SUBLOOP_ALLOCNO, we'll need to store HARD_REGNO
3778 on entry to the subloop and restore HARD_REGNO on exit from
3779 the subloop. */
3780 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
3781 += border_costs.spill_inside_loop_cost ();
3785 ira_free (allocno_color_data);
3786 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3788 a = ira_allocnos[j];
3789 ALLOCNO_ADD_DATA (a) = NULL;
3793 /* Initialize the common data for coloring and calls functions to do
3794 Chaitin-Briggs and regional coloring. */
3795 static void
3796 do_coloring (void)
3798 coloring_allocno_bitmap = ira_allocate_bitmap ();
3799 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3800 fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
3802 ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
3804 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3805 ira_print_disposition (ira_dump_file);
3807 ira_free_bitmap (coloring_allocno_bitmap);
3812 /* Move spill/restore code, which are to be generated in ira-emit.cc,
3813 to less frequent points (if it is profitable) by reassigning some
3814 allocnos (in loop with subloops containing in another loop) to
3815 memory which results in longer live-range where the corresponding
3816 pseudo-registers will be in memory. */
3817 static void
3818 move_spill_restore (void)
3820 int cost, regno, hard_regno, hard_regno2, index;
3821 bool changed_p;
3822 machine_mode mode;
3823 enum reg_class rclass;
3824 ira_allocno_t a, parent_allocno, subloop_allocno;
3825 ira_loop_tree_node_t parent, loop_node, subloop_node;
3826 ira_allocno_iterator ai;
3828 for (;;)
3830 changed_p = false;
3831 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3832 fprintf (ira_dump_file, "New iteration of spill/restore move\n");
3833 FOR_EACH_ALLOCNO (a, ai)
3835 regno = ALLOCNO_REGNO (a);
3836 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
3837 if (ALLOCNO_CAP_MEMBER (a) != NULL
3838 || ALLOCNO_CAP (a) != NULL
3839 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
3840 || loop_node->children == NULL
3841 /* don't do the optimization because it can create
3842 copies and the reload pass can spill the allocno set
3843 by copy although the allocno will not get memory
3844 slot. */
3845 || ira_equiv_no_lvalue_p (regno)
3846 || !bitmap_bit_p (loop_node->border_allocnos, ALLOCNO_NUM (a))
3847 /* Do not spill static chain pointer pseudo when
3848 non-local goto is used. */
3849 || non_spilled_static_chain_regno_p (regno))
3850 continue;
3851 mode = ALLOCNO_MODE (a);
3852 rclass = ALLOCNO_CLASS (a);
3853 index = ira_class_hard_reg_index[rclass][hard_regno];
3854 ira_assert (index >= 0);
3855 cost = (ALLOCNO_MEMORY_COST (a)
3856 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
3857 ? ALLOCNO_CLASS_COST (a)
3858 : ALLOCNO_HARD_REG_COSTS (a)[index]));
3859 ira_init_register_move_cost_if_necessary (mode);
3860 for (subloop_node = loop_node->subloops;
3861 subloop_node != NULL;
3862 subloop_node = subloop_node->subloop_next)
3864 ira_assert (subloop_node->bb == NULL);
3865 subloop_allocno = subloop_node->regno_allocno_map[regno];
3866 if (subloop_allocno == NULL)
3867 continue;
3868 ira_assert (rclass == ALLOCNO_CLASS (subloop_allocno));
3869 ira_loop_border_costs border_costs (subloop_allocno);
3871 /* We have accumulated cost. To get the real cost of
3872 allocno usage in the loop we should subtract the costs
3873 added by propagate_allocno_info for the subloop allocnos. */
3874 int reg_cost
3875 = (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
3876 ? ALLOCNO_CLASS_COST (subloop_allocno)
3877 : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]);
3879 int spill_cost
3880 = (border_costs.spill_inside_loop_cost ()
3881 + ALLOCNO_MEMORY_COST (subloop_allocno));
3883 /* If HARD_REGNO conflicts with SUBLOOP_A then
3884 propagate_allocno_info will have propagated
3885 the cost of spilling HARD_REGNO in SUBLOOP_NODE.
3886 (ira_subloop_allocnos_can_differ_p must be true
3887 in that case.) If HARD_REGNO is a caller-saved
3888 register, we might have modelled it in the same way.
3890 Otherwise, SPILL_COST acted as a cap on the propagated
3891 register cost, in cases where the allocations can differ. */
3892 auto conflicts = ira_total_conflict_hard_regs (subloop_allocno);
3893 if (TEST_HARD_REG_BIT (conflicts, hard_regno)
3894 || (ira_need_caller_save_p (subloop_allocno, hard_regno)
3895 && ira_caller_save_loop_spill_p (a, subloop_allocno,
3896 spill_cost)))
3897 reg_cost = spill_cost;
3898 else if (ira_subloop_allocnos_can_differ_p (a))
3899 reg_cost = MIN (reg_cost, spill_cost);
3901 cost -= ALLOCNO_MEMORY_COST (subloop_allocno) - reg_cost;
3903 if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
3904 /* The register was spilled in the subloop. If we spill
3905 it in the outer loop too then we'll no longer need to
3906 save the register on entry to the subloop and restore
3907 the register on exit from the subloop. */
3908 cost -= border_costs.spill_inside_loop_cost ();
3909 else
3911 /* The register was also allocated in the subloop. If we
3912 spill it in the outer loop then we'll need to load the
3913 register on entry to the subloop and store the register
3914 back on exit from the subloop. */
3915 cost += border_costs.spill_outside_loop_cost ();
3916 if (hard_regno2 != hard_regno)
3917 cost -= border_costs.move_between_loops_cost ();
3920 if ((parent = loop_node->parent) != NULL
3921 && (parent_allocno = parent->regno_allocno_map[regno]) != NULL)
3923 ira_assert (rclass == ALLOCNO_CLASS (parent_allocno));
3924 ira_loop_border_costs border_costs (a);
3925 if ((hard_regno2 = ALLOCNO_HARD_REGNO (parent_allocno)) < 0)
3926 /* The register was spilled in the parent loop. If we spill
3927 it in this loop too then we'll no longer need to load the
3928 register on entry to this loop and save the register back
3929 on exit from this loop. */
3930 cost -= border_costs.spill_outside_loop_cost ();
3931 else
3933 /* The register was also allocated in the parent loop.
3934 If we spill it in this loop then we'll need to save
3935 the register on entry to this loop and restore the
3936 register on exit from this loop. */
3937 cost += border_costs.spill_inside_loop_cost ();
3938 if (hard_regno2 != hard_regno)
3939 cost -= border_costs.move_between_loops_cost ();
3942 if (cost < 0)
3944 ALLOCNO_HARD_REGNO (a) = -1;
3945 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3947 fprintf
3948 (ira_dump_file,
3949 " Moving spill/restore for a%dr%d up from loop %d",
3950 ALLOCNO_NUM (a), regno, loop_node->loop_num);
3951 fprintf (ira_dump_file, " - profit %d\n", -cost);
3953 changed_p = true;
3956 if (! changed_p)
3957 break;
3963 /* Update current hard reg costs and current conflict hard reg costs
3964 for allocno A. It is done by processing its copies containing
3965 other allocnos already assigned. */
3966 static void
3967 update_curr_costs (ira_allocno_t a)
3969 int i, hard_regno, cost;
3970 machine_mode mode;
3971 enum reg_class aclass, rclass;
3972 ira_allocno_t another_a;
3973 ira_copy_t cp, next_cp;
3975 ira_free_allocno_updated_costs (a);
3976 ira_assert (! ALLOCNO_ASSIGNED_P (a));
3977 aclass = ALLOCNO_CLASS (a);
3978 if (aclass == NO_REGS)
3979 return;
3980 mode = ALLOCNO_MODE (a);
3981 ira_init_register_move_cost_if_necessary (mode);
3982 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3984 if (cp->first == a)
3986 next_cp = cp->next_first_allocno_copy;
3987 another_a = cp->second;
3989 else if (cp->second == a)
3991 next_cp = cp->next_second_allocno_copy;
3992 another_a = cp->first;
3994 else
3995 gcc_unreachable ();
3996 if (! ira_reg_classes_intersect_p[aclass][ALLOCNO_CLASS (another_a)]
3997 || ! ALLOCNO_ASSIGNED_P (another_a)
3998 || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
3999 continue;
4000 rclass = REGNO_REG_CLASS (hard_regno);
4001 i = ira_class_hard_reg_index[aclass][hard_regno];
4002 if (i < 0)
4003 continue;
4004 cost = (cp->first == a
4005 ? ira_register_move_cost[mode][rclass][aclass]
4006 : ira_register_move_cost[mode][aclass][rclass]);
4007 ira_allocate_and_set_or_copy_costs
4008 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a),
4009 ALLOCNO_HARD_REG_COSTS (a));
4010 ira_allocate_and_set_or_copy_costs
4011 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
4012 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
4013 ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
4014 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
4018 /* Try to assign hard registers to the unassigned allocnos and
4019 allocnos conflicting with them or conflicting with allocnos whose
4020 regno >= START_REGNO. The function is called after ira_flattening,
4021 so more allocnos (including ones created in ira-emit.cc) will have a
4022 chance to get a hard register. We use simple assignment algorithm
4023 based on priorities. */
4024 void
4025 ira_reassign_conflict_allocnos (int start_regno)
4027 int i, allocnos_to_color_num;
4028 ira_allocno_t a;
4029 enum reg_class aclass;
4030 bitmap allocnos_to_color;
4031 ira_allocno_iterator ai;
4033 allocnos_to_color = ira_allocate_bitmap ();
4034 allocnos_to_color_num = 0;
4035 FOR_EACH_ALLOCNO (a, ai)
4037 int n = ALLOCNO_NUM_OBJECTS (a);
4039 if (! ALLOCNO_ASSIGNED_P (a)
4040 && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
4042 if (ALLOCNO_CLASS (a) != NO_REGS)
4043 sorted_allocnos[allocnos_to_color_num++] = a;
4044 else
4046 ALLOCNO_ASSIGNED_P (a) = true;
4047 ALLOCNO_HARD_REGNO (a) = -1;
4048 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
4049 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
4051 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
4053 if (ALLOCNO_REGNO (a) < start_regno
4054 || (aclass = ALLOCNO_CLASS (a)) == NO_REGS)
4055 continue;
4056 for (i = 0; i < n; i++)
4058 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4059 ira_object_t conflict_obj;
4060 ira_object_conflict_iterator oci;
4062 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
4064 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
4066 ira_assert (ira_reg_classes_intersect_p
4067 [aclass][ALLOCNO_CLASS (conflict_a)]);
4068 if (!bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
4069 continue;
4070 sorted_allocnos[allocnos_to_color_num++] = conflict_a;
4074 ira_free_bitmap (allocnos_to_color);
4075 if (allocnos_to_color_num > 1)
4077 setup_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
4078 qsort (sorted_allocnos, allocnos_to_color_num, sizeof (ira_allocno_t),
4079 allocno_priority_compare_func);
4081 for (i = 0; i < allocnos_to_color_num; i++)
4083 a = sorted_allocnos[i];
4084 ALLOCNO_ASSIGNED_P (a) = false;
4085 update_curr_costs (a);
4087 for (i = 0; i < allocnos_to_color_num; i++)
4089 a = sorted_allocnos[i];
4090 if (assign_hard_reg (a, true))
4092 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4093 fprintf
4094 (ira_dump_file,
4095 " Secondary allocation: assign hard reg %d to reg %d\n",
4096 ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
4103 /* This page contains functions used to find conflicts using allocno
4104 live ranges. */
4106 #ifdef ENABLE_IRA_CHECKING
4108 /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
4109 intersect. This should be used when there is only one region.
4110 Currently this is used during reload. */
4111 static bool
4112 conflict_by_live_ranges_p (int regno1, int regno2)
4114 ira_allocno_t a1, a2;
4116 ira_assert (regno1 >= FIRST_PSEUDO_REGISTER
4117 && regno2 >= FIRST_PSEUDO_REGISTER);
4118 /* Reg info calculated by dataflow infrastructure can be different
4119 from one calculated by regclass. */
4120 if ((a1 = ira_loop_tree_root->regno_allocno_map[regno1]) == NULL
4121 || (a2 = ira_loop_tree_root->regno_allocno_map[regno2]) == NULL)
4122 return false;
4123 return allocnos_conflict_by_live_ranges_p (a1, a2);
4126 #endif
4130 /* This page contains code to coalesce memory stack slots used by
4131 spilled allocnos. This results in smaller stack frame, better data
4132 locality, and in smaller code for some architectures like
4133 x86/x86_64 where insn size depends on address displacement value.
4134 On the other hand, it can worsen insn scheduling after the RA but
4135 in practice it is less important than smaller stack frames. */
4137 /* TRUE if we coalesced some allocnos. In other words, if we got
4138 loops formed by members first_coalesced_allocno and
4139 next_coalesced_allocno containing more one allocno. */
4140 static bool allocno_coalesced_p;
4142 /* Bitmap used to prevent a repeated allocno processing because of
4143 coalescing. */
4144 static bitmap processed_coalesced_allocno_bitmap;
4146 /* See below. */
4147 typedef struct coalesce_data *coalesce_data_t;
4149 /* To decrease footprint of ira_allocno structure we store all data
4150 needed only for coalescing in the following structure. */
4151 struct coalesce_data
4153 /* Coalesced allocnos form a cyclic list. One allocno given by
4154 FIRST represents all coalesced allocnos. The
4155 list is chained by NEXT. */
4156 ira_allocno_t first;
4157 ira_allocno_t next;
4158 int temp;
4161 /* Container for storing allocno data concerning coalescing. */
4162 static coalesce_data_t allocno_coalesce_data;
4164 /* Macro to access the data concerning coalescing. */
4165 #define ALLOCNO_COALESCE_DATA(a) ((coalesce_data_t) ALLOCNO_ADD_DATA (a))
4167 /* Merge two sets of coalesced allocnos given correspondingly by
4168 allocnos A1 and A2 (more accurately merging A2 set into A1
4169 set). */
4170 static void
4171 merge_allocnos (ira_allocno_t a1, ira_allocno_t a2)
4173 ira_allocno_t a, first, last, next;
4175 first = ALLOCNO_COALESCE_DATA (a1)->first;
4176 a = ALLOCNO_COALESCE_DATA (a2)->first;
4177 if (first == a)
4178 return;
4179 for (last = a2, a = ALLOCNO_COALESCE_DATA (a2)->next;;
4180 a = ALLOCNO_COALESCE_DATA (a)->next)
4182 ALLOCNO_COALESCE_DATA (a)->first = first;
4183 if (a == a2)
4184 break;
4185 last = a;
4187 next = allocno_coalesce_data[ALLOCNO_NUM (first)].next;
4188 allocno_coalesce_data[ALLOCNO_NUM (first)].next = a2;
4189 allocno_coalesce_data[ALLOCNO_NUM (last)].next = next;
4192 /* Return TRUE if there are conflicting allocnos from two sets of
4193 coalesced allocnos given correspondingly by allocnos A1 and A2. We
4194 use live ranges to find conflicts because conflicts are represented
4195 only for allocnos of the same allocno class and during the reload
4196 pass we coalesce allocnos for sharing stack memory slots. */
4197 static bool
4198 coalesced_allocno_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
4200 ira_allocno_t a, conflict_a;
4202 if (allocno_coalesced_p)
4204 bitmap_clear (processed_coalesced_allocno_bitmap);
4205 for (a = ALLOCNO_COALESCE_DATA (a1)->next;;
4206 a = ALLOCNO_COALESCE_DATA (a)->next)
4208 bitmap_set_bit (processed_coalesced_allocno_bitmap, ALLOCNO_NUM (a));
4209 if (a == a1)
4210 break;
4213 for (a = ALLOCNO_COALESCE_DATA (a2)->next;;
4214 a = ALLOCNO_COALESCE_DATA (a)->next)
4216 for (conflict_a = ALLOCNO_COALESCE_DATA (a1)->next;;
4217 conflict_a = ALLOCNO_COALESCE_DATA (conflict_a)->next)
4219 if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
4220 return true;
4221 if (conflict_a == a1)
4222 break;
4224 if (a == a2)
4225 break;
4227 return false;
4230 /* The major function for aggressive allocno coalescing. We coalesce
4231 only spilled allocnos. If some allocnos have been coalesced, we
4232 set up flag allocno_coalesced_p. */
4233 static void
4234 coalesce_allocnos (void)
4236 ira_allocno_t a;
4237 ira_copy_t cp, next_cp;
4238 unsigned int j;
4239 int i, n, cp_num, regno;
4240 bitmap_iterator bi;
4242 cp_num = 0;
4243 /* Collect copies. */
4244 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
4246 a = ira_allocnos[j];
4247 regno = ALLOCNO_REGNO (a);
4248 if (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
4249 || ira_equiv_no_lvalue_p (regno))
4250 continue;
4251 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
4253 if (cp->first == a)
4255 next_cp = cp->next_first_allocno_copy;
4256 regno = ALLOCNO_REGNO (cp->second);
4257 /* For priority coloring we coalesce allocnos only with
4258 the same allocno class not with intersected allocno
4259 classes as it were possible. It is done for
4260 simplicity. */
4261 if ((cp->insn != NULL || cp->constraint_p)
4262 && ALLOCNO_ASSIGNED_P (cp->second)
4263 && ALLOCNO_HARD_REGNO (cp->second) < 0
4264 && ! ira_equiv_no_lvalue_p (regno))
4265 sorted_copies[cp_num++] = cp;
4267 else if (cp->second == a)
4268 next_cp = cp->next_second_allocno_copy;
4269 else
4270 gcc_unreachable ();
4273 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
4274 /* Coalesced copies, most frequently executed first. */
4275 for (; cp_num != 0;)
4277 for (i = 0; i < cp_num; i++)
4279 cp = sorted_copies[i];
4280 if (! coalesced_allocno_conflict_p (cp->first, cp->second))
4282 allocno_coalesced_p = true;
4283 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4284 fprintf
4285 (ira_dump_file,
4286 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
4287 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
4288 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
4289 cp->freq);
4290 merge_allocnos (cp->first, cp->second);
4291 i++;
4292 break;
4295 /* Collect the rest of copies. */
4296 for (n = 0; i < cp_num; i++)
4298 cp = sorted_copies[i];
4299 if (allocno_coalesce_data[ALLOCNO_NUM (cp->first)].first
4300 != allocno_coalesce_data[ALLOCNO_NUM (cp->second)].first)
4301 sorted_copies[n++] = cp;
4303 cp_num = n;
4307 /* Usage cost and order number of coalesced allocno set to which
4308 given pseudo register belongs to. */
4309 static int *regno_coalesced_allocno_cost;
4310 static int *regno_coalesced_allocno_num;
4312 /* Sort pseudos according frequencies of coalesced allocno sets they
4313 belong to (putting most frequently ones first), and according to
4314 coalesced allocno set order numbers. */
4315 static int
4316 coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
4318 const int regno1 = *(const int *) v1p;
4319 const int regno2 = *(const int *) v2p;
4320 int diff;
4322 if ((diff = (regno_coalesced_allocno_cost[regno2]
4323 - regno_coalesced_allocno_cost[regno1])) != 0)
4324 return diff;
4325 if ((diff = (regno_coalesced_allocno_num[regno1]
4326 - regno_coalesced_allocno_num[regno2])) != 0)
4327 return diff;
4328 return regno1 - regno2;
4331 /* Widest width in which each pseudo reg is referred to (via subreg).
4332 It is used for sorting pseudo registers. */
4333 static machine_mode *regno_max_ref_mode;
4335 /* Sort pseudos according their slot numbers (putting ones with
4336 smaller numbers first, or last when the frame pointer is not
4337 needed). */
4338 static int
4339 coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
4341 const int regno1 = *(const int *) v1p;
4342 const int regno2 = *(const int *) v2p;
4343 ira_allocno_t a1 = ira_regno_allocno_map[regno1];
4344 ira_allocno_t a2 = ira_regno_allocno_map[regno2];
4345 int diff, slot_num1, slot_num2;
4346 machine_mode mode1, mode2;
4348 if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
4350 if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
4351 return regno1 - regno2;
4352 return 1;
4354 else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
4355 return -1;
4356 slot_num1 = -ALLOCNO_HARD_REGNO (a1);
4357 slot_num2 = -ALLOCNO_HARD_REGNO (a2);
4358 if ((diff = slot_num1 - slot_num2) != 0)
4359 return (frame_pointer_needed
4360 || (!FRAME_GROWS_DOWNWARD) == STACK_GROWS_DOWNWARD ? diff : -diff);
4361 mode1 = wider_subreg_mode (PSEUDO_REGNO_MODE (regno1),
4362 regno_max_ref_mode[regno1]);
4363 mode2 = wider_subreg_mode (PSEUDO_REGNO_MODE (regno2),
4364 regno_max_ref_mode[regno2]);
4365 if ((diff = compare_sizes_for_sort (GET_MODE_SIZE (mode2),
4366 GET_MODE_SIZE (mode1))) != 0)
4367 return diff;
4368 return regno1 - regno2;
4371 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
4372 for coalesced allocno sets containing allocnos with their regnos
4373 given in array PSEUDO_REGNOS of length N. */
4374 static void
4375 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
4377 int i, num, regno, cost;
4378 ira_allocno_t allocno, a;
4380 for (num = i = 0; i < n; i++)
4382 regno = pseudo_regnos[i];
4383 allocno = ira_regno_allocno_map[regno];
4384 if (allocno == NULL)
4386 regno_coalesced_allocno_cost[regno] = 0;
4387 regno_coalesced_allocno_num[regno] = ++num;
4388 continue;
4390 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
4391 continue;
4392 num++;
4393 for (cost = 0, a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4394 a = ALLOCNO_COALESCE_DATA (a)->next)
4396 cost += ALLOCNO_FREQ (a);
4397 if (a == allocno)
4398 break;
4400 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4401 a = ALLOCNO_COALESCE_DATA (a)->next)
4403 regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
4404 regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
4405 if (a == allocno)
4406 break;
4411 /* Collect spilled allocnos representing coalesced allocno sets (the
4412 first coalesced allocno). The collected allocnos are returned
4413 through array SPILLED_COALESCED_ALLOCNOS. The function returns the
4414 number of the collected allocnos. The allocnos are given by their
4415 regnos in array PSEUDO_REGNOS of length N. */
4416 static int
4417 collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
4418 ira_allocno_t *spilled_coalesced_allocnos)
4420 int i, num, regno;
4421 ira_allocno_t allocno;
4423 for (num = i = 0; i < n; i++)
4425 regno = pseudo_regnos[i];
4426 allocno = ira_regno_allocno_map[regno];
4427 if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
4428 || ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
4429 continue;
4430 spilled_coalesced_allocnos[num++] = allocno;
4432 return num;
4435 /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
4436 given slot contains live ranges of coalesced allocnos assigned to
4437 given slot. */
4438 static live_range_t *slot_coalesced_allocnos_live_ranges;
4440 /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
4441 ranges intersected with live ranges of coalesced allocnos assigned
4442 to slot with number N. */
4443 static bool
4444 slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno, int n)
4446 ira_allocno_t a;
4448 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4449 a = ALLOCNO_COALESCE_DATA (a)->next)
4451 int i;
4452 int nr = ALLOCNO_NUM_OBJECTS (a);
4453 gcc_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
4454 for (i = 0; i < nr; i++)
4456 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4458 if (ira_live_ranges_intersect_p
4459 (slot_coalesced_allocnos_live_ranges[n],
4460 OBJECT_LIVE_RANGES (obj)))
4461 return true;
4463 if (a == allocno)
4464 break;
4466 return false;
4469 /* Update live ranges of slot to which coalesced allocnos represented
4470 by ALLOCNO were assigned. */
4471 static void
4472 setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno)
4474 int i, n;
4475 ira_allocno_t a;
4476 live_range_t r;
4478 n = ALLOCNO_COALESCE_DATA (allocno)->temp;
4479 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4480 a = ALLOCNO_COALESCE_DATA (a)->next)
4482 int nr = ALLOCNO_NUM_OBJECTS (a);
4483 gcc_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
4484 for (i = 0; i < nr; i++)
4486 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4488 r = ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj));
4489 slot_coalesced_allocnos_live_ranges[n]
4490 = ira_merge_live_ranges
4491 (slot_coalesced_allocnos_live_ranges[n], r);
4493 if (a == allocno)
4494 break;
4498 /* We have coalesced allocnos involving in copies. Coalesce allocnos
4499 further in order to share the same memory stack slot. Allocnos
4500 representing sets of allocnos coalesced before the call are given
4501 in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
4502 some allocnos were coalesced in the function. */
4503 static bool
4504 coalesce_spill_slots (ira_allocno_t *spilled_coalesced_allocnos, int num)
4506 int i, j, n, last_coalesced_allocno_num;
4507 ira_allocno_t allocno, a;
4508 bool merged_p = false;
4509 bitmap set_jump_crosses = regstat_get_setjmp_crosses ();
4511 slot_coalesced_allocnos_live_ranges
4512 = (live_range_t *) ira_allocate (sizeof (live_range_t) * ira_allocnos_num);
4513 memset (slot_coalesced_allocnos_live_ranges, 0,
4514 sizeof (live_range_t) * ira_allocnos_num);
4515 last_coalesced_allocno_num = 0;
4516 /* Coalesce non-conflicting spilled allocnos preferring most
4517 frequently used. */
4518 for (i = 0; i < num; i++)
4520 allocno = spilled_coalesced_allocnos[i];
4521 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
4522 || bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (allocno))
4523 || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
4524 continue;
4525 for (j = 0; j < i; j++)
4527 a = spilled_coalesced_allocnos[j];
4528 n = ALLOCNO_COALESCE_DATA (a)->temp;
4529 if (ALLOCNO_COALESCE_DATA (a)->first == a
4530 && ! bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (a))
4531 && ! ira_equiv_no_lvalue_p (ALLOCNO_REGNO (a))
4532 && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno, n))
4533 break;
4535 if (j >= i)
4537 /* No coalescing: set up number for coalesced allocnos
4538 represented by ALLOCNO. */
4539 ALLOCNO_COALESCE_DATA (allocno)->temp = last_coalesced_allocno_num++;
4540 setup_slot_coalesced_allocno_live_ranges (allocno);
4542 else
4544 allocno_coalesced_p = true;
4545 merged_p = true;
4546 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4547 fprintf (ira_dump_file,
4548 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
4549 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
4550 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
4551 ALLOCNO_COALESCE_DATA (allocno)->temp
4552 = ALLOCNO_COALESCE_DATA (a)->temp;
4553 setup_slot_coalesced_allocno_live_ranges (allocno);
4554 merge_allocnos (a, allocno);
4555 ira_assert (ALLOCNO_COALESCE_DATA (a)->first == a);
4558 for (i = 0; i < ira_allocnos_num; i++)
4559 ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges[i]);
4560 ira_free (slot_coalesced_allocnos_live_ranges);
4561 return merged_p;
4564 /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
4565 subsequent assigning stack slots to them in the reload pass. To do
4566 this we coalesce spilled allocnos first to decrease the number of
4567 memory-memory move insns. This function is called by the
4568 reload. */
4569 void
4570 ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
4571 machine_mode *reg_max_ref_mode)
4573 int max_regno = max_reg_num ();
4574 int i, regno, num, slot_num;
4575 ira_allocno_t allocno, a;
4576 ira_allocno_iterator ai;
4577 ira_allocno_t *spilled_coalesced_allocnos;
4579 ira_assert (! ira_use_lra_p);
4581 /* Set up allocnos can be coalesced. */
4582 coloring_allocno_bitmap = ira_allocate_bitmap ();
4583 for (i = 0; i < n; i++)
4585 regno = pseudo_regnos[i];
4586 allocno = ira_regno_allocno_map[regno];
4587 if (allocno != NULL)
4588 bitmap_set_bit (coloring_allocno_bitmap, ALLOCNO_NUM (allocno));
4590 allocno_coalesced_p = false;
4591 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
4592 allocno_coalesce_data
4593 = (coalesce_data_t) ira_allocate (sizeof (struct coalesce_data)
4594 * ira_allocnos_num);
4595 /* Initialize coalesce data for allocnos. */
4596 FOR_EACH_ALLOCNO (a, ai)
4598 ALLOCNO_ADD_DATA (a) = allocno_coalesce_data + ALLOCNO_NUM (a);
4599 ALLOCNO_COALESCE_DATA (a)->first = a;
4600 ALLOCNO_COALESCE_DATA (a)->next = a;
4602 coalesce_allocnos ();
4603 ira_free_bitmap (coloring_allocno_bitmap);
4604 regno_coalesced_allocno_cost
4605 = (int *) ira_allocate (max_regno * sizeof (int));
4606 regno_coalesced_allocno_num
4607 = (int *) ira_allocate (max_regno * sizeof (int));
4608 memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
4609 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
4610 /* Sort regnos according frequencies of the corresponding coalesced
4611 allocno sets. */
4612 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
4613 spilled_coalesced_allocnos
4614 = (ira_allocno_t *) ira_allocate (ira_allocnos_num
4615 * sizeof (ira_allocno_t));
4616 /* Collect allocnos representing the spilled coalesced allocno
4617 sets. */
4618 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
4619 spilled_coalesced_allocnos);
4620 if (flag_ira_share_spill_slots
4621 && coalesce_spill_slots (spilled_coalesced_allocnos, num))
4623 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
4624 qsort (pseudo_regnos, n, sizeof (int),
4625 coalesced_pseudo_reg_freq_compare);
4626 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
4627 spilled_coalesced_allocnos);
4629 ira_free_bitmap (processed_coalesced_allocno_bitmap);
4630 allocno_coalesced_p = false;
4631 /* Assign stack slot numbers to spilled allocno sets, use smaller
4632 numbers for most frequently used coalesced allocnos. -1 is
4633 reserved for dynamic search of stack slots for pseudos spilled by
4634 the reload. */
4635 slot_num = 1;
4636 for (i = 0; i < num; i++)
4638 allocno = spilled_coalesced_allocnos[i];
4639 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
4640 || ALLOCNO_HARD_REGNO (allocno) >= 0
4641 || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
4642 continue;
4643 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4644 fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
4645 slot_num++;
4646 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4647 a = ALLOCNO_COALESCE_DATA (a)->next)
4649 ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
4650 ALLOCNO_HARD_REGNO (a) = -slot_num;
4651 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4653 machine_mode mode = wider_subreg_mode
4654 (PSEUDO_REGNO_MODE (ALLOCNO_REGNO (a)),
4655 reg_max_ref_mode[ALLOCNO_REGNO (a)]);
4656 fprintf (ira_dump_file, " a%dr%d(%d,",
4657 ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a));
4658 print_dec (GET_MODE_SIZE (mode), ira_dump_file, SIGNED);
4659 fprintf (ira_dump_file, ")\n");
4662 if (a == allocno)
4663 break;
4665 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4666 fprintf (ira_dump_file, "\n");
4668 ira_spilled_reg_stack_slots_num = slot_num - 1;
4669 ira_free (spilled_coalesced_allocnos);
4670 /* Sort regnos according the slot numbers. */
4671 regno_max_ref_mode = reg_max_ref_mode;
4672 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
4673 FOR_EACH_ALLOCNO (a, ai)
4674 ALLOCNO_ADD_DATA (a) = NULL;
4675 ira_free (allocno_coalesce_data);
4676 ira_free (regno_coalesced_allocno_num);
4677 ira_free (regno_coalesced_allocno_cost);
4682 /* This page contains code used by the reload pass to improve the
4683 final code. */
4685 /* The function is called from reload to mark changes in the
4686 allocation of REGNO made by the reload. Remember that reg_renumber
4687 reflects the change result. */
4688 void
4689 ira_mark_allocation_change (int regno)
4691 ira_allocno_t a = ira_regno_allocno_map[regno];
4692 int old_hard_regno, hard_regno, cost;
4693 enum reg_class aclass = ALLOCNO_CLASS (a);
4695 ira_assert (a != NULL);
4696 hard_regno = reg_renumber[regno];
4697 if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
4698 return;
4699 if (old_hard_regno < 0)
4700 cost = -ALLOCNO_MEMORY_COST (a);
4701 else
4703 ira_assert (ira_class_hard_reg_index[aclass][old_hard_regno] >= 0);
4704 cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
4705 ? ALLOCNO_CLASS_COST (a)
4706 : ALLOCNO_HARD_REG_COSTS (a)
4707 [ira_class_hard_reg_index[aclass][old_hard_regno]]);
4708 update_costs_from_copies (a, false, false);
4710 ira_overall_cost -= cost;
4711 ALLOCNO_HARD_REGNO (a) = hard_regno;
4712 if (hard_regno < 0)
4714 ALLOCNO_HARD_REGNO (a) = -1;
4715 cost += ALLOCNO_MEMORY_COST (a);
4717 else if (ira_class_hard_reg_index[aclass][hard_regno] >= 0)
4719 cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
4720 ? ALLOCNO_CLASS_COST (a)
4721 : ALLOCNO_HARD_REG_COSTS (a)
4722 [ira_class_hard_reg_index[aclass][hard_regno]]);
4723 update_costs_from_copies (a, true, false);
4725 else
4726 /* Reload changed class of the allocno. */
4727 cost = 0;
4728 ira_overall_cost += cost;
4731 /* This function is called when reload deletes memory-memory move. In
4732 this case we marks that the allocation of the corresponding
4733 allocnos should be not changed in future. Otherwise we risk to get
4734 a wrong code. */
4735 void
4736 ira_mark_memory_move_deletion (int dst_regno, int src_regno)
4738 ira_allocno_t dst = ira_regno_allocno_map[dst_regno];
4739 ira_allocno_t src = ira_regno_allocno_map[src_regno];
4741 ira_assert (dst != NULL && src != NULL
4742 && ALLOCNO_HARD_REGNO (dst) < 0
4743 && ALLOCNO_HARD_REGNO (src) < 0);
4744 ALLOCNO_DONT_REASSIGN_P (dst) = true;
4745 ALLOCNO_DONT_REASSIGN_P (src) = true;
4748 /* Try to assign a hard register (except for FORBIDDEN_REGS) to
4749 allocno A and return TRUE in the case of success. */
4750 static bool
4751 allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
4753 int hard_regno;
4754 enum reg_class aclass;
4755 int regno = ALLOCNO_REGNO (a);
4756 HARD_REG_SET saved[2];
4757 int i, n;
4759 n = ALLOCNO_NUM_OBJECTS (a);
4760 for (i = 0; i < n; i++)
4762 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4763 saved[i] = OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
4764 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) |= forbidden_regs;
4765 if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
4766 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) |= ira_need_caller_save_regs (a);
4768 ALLOCNO_ASSIGNED_P (a) = false;
4769 aclass = ALLOCNO_CLASS (a);
4770 update_curr_costs (a);
4771 assign_hard_reg (a, true);
4772 hard_regno = ALLOCNO_HARD_REGNO (a);
4773 reg_renumber[regno] = hard_regno;
4774 if (hard_regno < 0)
4775 ALLOCNO_HARD_REGNO (a) = -1;
4776 else
4778 ira_assert (ira_class_hard_reg_index[aclass][hard_regno] >= 0);
4779 ira_overall_cost
4780 -= (ALLOCNO_MEMORY_COST (a)
4781 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
4782 ? ALLOCNO_CLASS_COST (a)
4783 : ALLOCNO_HARD_REG_COSTS (a)[ira_class_hard_reg_index
4784 [aclass][hard_regno]]));
4785 if (ira_need_caller_save_p (a, hard_regno))
4787 ira_assert (flag_caller_saves);
4788 caller_save_needed = 1;
4792 /* If we found a hard register, modify the RTL for the pseudo
4793 register to show the hard register, and mark the pseudo register
4794 live. */
4795 if (reg_renumber[regno] >= 0)
4797 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4798 fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
4799 SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
4800 mark_home_live (regno);
4802 else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4803 fprintf (ira_dump_file, "\n");
4804 for (i = 0; i < n; i++)
4806 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4807 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) = saved[i];
4809 return reg_renumber[regno] >= 0;
4812 /* Sort pseudos according their usage frequencies (putting most
4813 frequently ones first). */
4814 static int
4815 pseudo_reg_compare (const void *v1p, const void *v2p)
4817 int regno1 = *(const int *) v1p;
4818 int regno2 = *(const int *) v2p;
4819 int diff;
4821 if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
4822 return diff;
4823 return regno1 - regno2;
4826 /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
4827 NUM of them) or spilled pseudos conflicting with pseudos in
4828 SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
4829 allocation has been changed. The function doesn't use
4830 BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
4831 PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
4832 is called by the reload pass at the end of each reload
4833 iteration. */
4834 bool
4835 ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
4836 HARD_REG_SET bad_spill_regs,
4837 HARD_REG_SET *pseudo_forbidden_regs,
4838 HARD_REG_SET *pseudo_previous_regs,
4839 bitmap spilled)
4841 int i, n, regno;
4842 bool changed_p;
4843 ira_allocno_t a;
4844 HARD_REG_SET forbidden_regs;
4845 bitmap temp = BITMAP_ALLOC (NULL);
4847 /* Add pseudos which conflict with pseudos already in
4848 SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
4849 to allocating in two steps as some of the conflicts might have
4850 a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
4851 for (i = 0; i < num; i++)
4852 bitmap_set_bit (temp, spilled_pseudo_regs[i]);
4854 for (i = 0, n = num; i < n; i++)
4856 int nr, j;
4857 int regno = spilled_pseudo_regs[i];
4858 bitmap_set_bit (temp, regno);
4860 a = ira_regno_allocno_map[regno];
4861 nr = ALLOCNO_NUM_OBJECTS (a);
4862 for (j = 0; j < nr; j++)
4864 ira_object_t conflict_obj;
4865 ira_object_t obj = ALLOCNO_OBJECT (a, j);
4866 ira_object_conflict_iterator oci;
4868 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
4870 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
4871 if (ALLOCNO_HARD_REGNO (conflict_a) < 0
4872 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
4873 && bitmap_set_bit (temp, ALLOCNO_REGNO (conflict_a)))
4875 spilled_pseudo_regs[num++] = ALLOCNO_REGNO (conflict_a);
4876 /* ?!? This seems wrong. */
4877 bitmap_set_bit (consideration_allocno_bitmap,
4878 ALLOCNO_NUM (conflict_a));
4884 if (num > 1)
4885 qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
4886 changed_p = false;
4887 /* Try to assign hard registers to pseudos from
4888 SPILLED_PSEUDO_REGS. */
4889 for (i = 0; i < num; i++)
4891 regno = spilled_pseudo_regs[i];
4892 forbidden_regs = (bad_spill_regs
4893 | pseudo_forbidden_regs[regno]
4894 | pseudo_previous_regs[regno]);
4895 gcc_assert (reg_renumber[regno] < 0);
4896 a = ira_regno_allocno_map[regno];
4897 ira_mark_allocation_change (regno);
4898 ira_assert (reg_renumber[regno] < 0);
4899 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4900 fprintf (ira_dump_file,
4901 " Try Assign %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
4902 ALLOCNO_MEMORY_COST (a)
4903 - ALLOCNO_CLASS_COST (a));
4904 allocno_reload_assign (a, forbidden_regs);
4905 if (reg_renumber[regno] >= 0)
4907 CLEAR_REGNO_REG_SET (spilled, regno);
4908 changed_p = true;
4911 BITMAP_FREE (temp);
4912 return changed_p;
4915 /* The function is called by reload and returns already allocated
4916 stack slot (if any) for REGNO with given INHERENT_SIZE and
4917 TOTAL_SIZE. In the case of failure to find a slot which can be
4918 used for REGNO, the function returns NULL. */
4920 ira_reuse_stack_slot (int regno, poly_uint64 inherent_size,
4921 poly_uint64 total_size)
4923 unsigned int i;
4924 int slot_num, best_slot_num;
4925 int cost, best_cost;
4926 ira_copy_t cp, next_cp;
4927 ira_allocno_t another_allocno, allocno = ira_regno_allocno_map[regno];
4928 rtx x;
4929 bitmap_iterator bi;
4930 class ira_spilled_reg_stack_slot *slot = NULL;
4932 ira_assert (! ira_use_lra_p);
4934 ira_assert (known_eq (inherent_size, PSEUDO_REGNO_BYTES (regno))
4935 && known_le (inherent_size, total_size)
4936 && ALLOCNO_HARD_REGNO (allocno) < 0);
4937 if (! flag_ira_share_spill_slots)
4938 return NULL_RTX;
4939 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4940 if (slot_num != -1)
4942 slot = &ira_spilled_reg_stack_slots[slot_num];
4943 x = slot->mem;
4945 else
4947 best_cost = best_slot_num = -1;
4948 x = NULL_RTX;
4949 /* It means that the pseudo was spilled in the reload pass, try
4950 to reuse a slot. */
4951 for (slot_num = 0;
4952 slot_num < ira_spilled_reg_stack_slots_num;
4953 slot_num++)
4955 slot = &ira_spilled_reg_stack_slots[slot_num];
4956 if (slot->mem == NULL_RTX)
4957 continue;
4958 if (maybe_lt (slot->width, total_size)
4959 || maybe_lt (GET_MODE_SIZE (GET_MODE (slot->mem)), inherent_size))
4960 continue;
4962 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4963 FIRST_PSEUDO_REGISTER, i, bi)
4965 another_allocno = ira_regno_allocno_map[i];
4966 if (allocnos_conflict_by_live_ranges_p (allocno,
4967 another_allocno))
4968 goto cont;
4970 for (cost = 0, cp = ALLOCNO_COPIES (allocno);
4971 cp != NULL;
4972 cp = next_cp)
4974 if (cp->first == allocno)
4976 next_cp = cp->next_first_allocno_copy;
4977 another_allocno = cp->second;
4979 else if (cp->second == allocno)
4981 next_cp = cp->next_second_allocno_copy;
4982 another_allocno = cp->first;
4984 else
4985 gcc_unreachable ();
4986 if (cp->insn == NULL_RTX)
4987 continue;
4988 if (bitmap_bit_p (&slot->spilled_regs,
4989 ALLOCNO_REGNO (another_allocno)))
4990 cost += cp->freq;
4992 if (cost > best_cost)
4994 best_cost = cost;
4995 best_slot_num = slot_num;
4997 cont:
5000 if (best_cost >= 0)
5002 slot_num = best_slot_num;
5003 slot = &ira_spilled_reg_stack_slots[slot_num];
5004 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
5005 x = slot->mem;
5006 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
5009 if (x != NULL_RTX)
5011 ira_assert (known_ge (slot->width, total_size));
5012 #ifdef ENABLE_IRA_CHECKING
5013 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
5014 FIRST_PSEUDO_REGISTER, i, bi)
5016 ira_assert (! conflict_by_live_ranges_p (regno, i));
5018 #endif
5019 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
5020 if (internal_flag_ira_verbose > 3 && ira_dump_file)
5022 fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
5023 regno, REG_FREQ (regno), slot_num);
5024 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
5025 FIRST_PSEUDO_REGISTER, i, bi)
5027 if ((unsigned) regno != i)
5028 fprintf (ira_dump_file, " %d", i);
5030 fprintf (ira_dump_file, "\n");
5033 return x;
5036 /* This is called by reload every time a new stack slot X with
5037 TOTAL_SIZE was allocated for REGNO. We store this info for
5038 subsequent ira_reuse_stack_slot calls. */
5039 void
5040 ira_mark_new_stack_slot (rtx x, int regno, poly_uint64 total_size)
5042 class ira_spilled_reg_stack_slot *slot;
5043 int slot_num;
5044 ira_allocno_t allocno;
5046 ira_assert (! ira_use_lra_p);
5048 ira_assert (known_le (PSEUDO_REGNO_BYTES (regno), total_size));
5049 allocno = ira_regno_allocno_map[regno];
5050 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
5051 if (slot_num == -1)
5053 slot_num = ira_spilled_reg_stack_slots_num++;
5054 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
5056 slot = &ira_spilled_reg_stack_slots[slot_num];
5057 INIT_REG_SET (&slot->spilled_regs);
5058 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
5059 slot->mem = x;
5060 slot->width = total_size;
5061 if (internal_flag_ira_verbose > 3 && ira_dump_file)
5062 fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
5063 regno, REG_FREQ (regno), slot_num);
5067 /* Return spill cost for pseudo-registers whose numbers are in array
5068 REGNOS (with a negative number as an end marker) for reload with
5069 given IN and OUT for INSN. Return also number points (through
5070 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
5071 the register pressure is high, number of references of the
5072 pseudo-registers (through NREFS), the number of psuedo registers
5073 whose allocated register wouldn't need saving in the prologue
5074 (through CALL_USED_COUNT), and the first hard regno occupied by the
5075 pseudo-registers (through FIRST_HARD_REGNO). */
5076 static int
5077 calculate_spill_cost (int *regnos, rtx in, rtx out, rtx_insn *insn,
5078 int *excess_pressure_live_length,
5079 int *nrefs, int *call_used_count, int *first_hard_regno)
5081 int i, cost, regno, hard_regno, count, saved_cost;
5082 bool in_p, out_p;
5083 int length;
5084 ira_allocno_t a;
5086 *nrefs = 0;
5087 for (length = count = cost = i = 0;; i++)
5089 regno = regnos[i];
5090 if (regno < 0)
5091 break;
5092 *nrefs += REG_N_REFS (regno);
5093 hard_regno = reg_renumber[regno];
5094 ira_assert (hard_regno >= 0);
5095 a = ira_regno_allocno_map[regno];
5096 length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) / ALLOCNO_NUM_OBJECTS (a);
5097 cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
5098 if (in_hard_reg_set_p (crtl->abi->full_reg_clobbers (),
5099 ALLOCNO_MODE (a), hard_regno))
5100 count++;
5101 in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
5102 out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
5103 if ((in_p || out_p)
5104 && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
5106 saved_cost = 0;
5107 if (in_p)
5108 saved_cost += ira_memory_move_cost
5109 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][1];
5110 if (out_p)
5111 saved_cost
5112 += ira_memory_move_cost
5113 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][0];
5114 cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
5117 *excess_pressure_live_length = length;
5118 *call_used_count = count;
5119 hard_regno = -1;
5120 if (regnos[0] >= 0)
5122 hard_regno = reg_renumber[regnos[0]];
5124 *first_hard_regno = hard_regno;
5125 return cost;
5128 /* Return TRUE if spilling pseudo-registers whose numbers are in array
5129 REGNOS is better than spilling pseudo-registers with numbers in
5130 OTHER_REGNOS for reload with given IN and OUT for INSN. The
5131 function used by the reload pass to make better register spilling
5132 decisions. */
5133 bool
5134 ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
5135 rtx in, rtx out, rtx_insn *insn)
5137 int cost, other_cost;
5138 int length, other_length;
5139 int nrefs, other_nrefs;
5140 int call_used_count, other_call_used_count;
5141 int hard_regno, other_hard_regno;
5143 cost = calculate_spill_cost (regnos, in, out, insn,
5144 &length, &nrefs, &call_used_count, &hard_regno);
5145 other_cost = calculate_spill_cost (other_regnos, in, out, insn,
5146 &other_length, &other_nrefs,
5147 &other_call_used_count,
5148 &other_hard_regno);
5149 if (nrefs == 0 && other_nrefs != 0)
5150 return true;
5151 if (nrefs != 0 && other_nrefs == 0)
5152 return false;
5153 if (cost != other_cost)
5154 return cost < other_cost;
5155 if (length != other_length)
5156 return length > other_length;
5157 #ifdef REG_ALLOC_ORDER
5158 if (hard_regno >= 0 && other_hard_regno >= 0)
5159 return (inv_reg_alloc_order[hard_regno]
5160 < inv_reg_alloc_order[other_hard_regno]);
5161 #else
5162 if (call_used_count != other_call_used_count)
5163 return call_used_count > other_call_used_count;
5164 #endif
5165 return false;
5170 /* Allocate and initialize data necessary for assign_hard_reg. */
5171 void
5172 ira_initiate_assign (void)
5174 sorted_allocnos
5175 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
5176 * ira_allocnos_num);
5177 consideration_allocno_bitmap = ira_allocate_bitmap ();
5178 initiate_cost_update ();
5179 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
5180 sorted_copies = (ira_copy_t *) ira_allocate (ira_copies_num
5181 * sizeof (ira_copy_t));
5184 /* Deallocate data used by assign_hard_reg. */
5185 void
5186 ira_finish_assign (void)
5188 ira_free (sorted_allocnos);
5189 ira_free_bitmap (consideration_allocno_bitmap);
5190 finish_cost_update ();
5191 ira_free (allocno_priorities);
5192 ira_free (sorted_copies);
5197 /* Entry function doing color-based register allocation. */
5198 static void
5199 color (void)
5201 allocno_stack_vec.create (ira_allocnos_num);
5202 memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
5203 ira_initiate_assign ();
5204 do_coloring ();
5205 ira_finish_assign ();
5206 allocno_stack_vec.release ();
5207 move_spill_restore ();
5212 /* This page contains a simple register allocator without usage of
5213 allocno conflicts. This is used for fast allocation for -O0. */
5215 /* Do register allocation by not using allocno conflicts. It uses
5216 only allocno live ranges. The algorithm is close to Chow's
5217 priority coloring. */
5218 static void
5219 fast_allocation (void)
5221 int i, j, k, num, class_size, hard_regno, best_hard_regno, cost, min_cost;
5222 int *costs;
5223 #ifdef STACK_REGS
5224 bool no_stack_reg_p;
5225 #endif
5226 enum reg_class aclass;
5227 machine_mode mode;
5228 ira_allocno_t a;
5229 ira_allocno_iterator ai;
5230 live_range_t r;
5231 HARD_REG_SET conflict_hard_regs, *used_hard_regs;
5233 sorted_allocnos = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
5234 * ira_allocnos_num);
5235 num = 0;
5236 FOR_EACH_ALLOCNO (a, ai)
5237 sorted_allocnos[num++] = a;
5238 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
5239 setup_allocno_priorities (sorted_allocnos, num);
5240 used_hard_regs = (HARD_REG_SET *) ira_allocate (sizeof (HARD_REG_SET)
5241 * ira_max_point);
5242 for (i = 0; i < ira_max_point; i++)
5243 CLEAR_HARD_REG_SET (used_hard_regs[i]);
5244 qsort (sorted_allocnos, num, sizeof (ira_allocno_t),
5245 allocno_priority_compare_func);
5246 for (i = 0; i < num; i++)
5248 int nr, l;
5250 a = sorted_allocnos[i];
5251 nr = ALLOCNO_NUM_OBJECTS (a);
5252 CLEAR_HARD_REG_SET (conflict_hard_regs);
5253 for (l = 0; l < nr; l++)
5255 ira_object_t obj = ALLOCNO_OBJECT (a, l);
5256 conflict_hard_regs |= OBJECT_CONFLICT_HARD_REGS (obj);
5257 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
5258 for (j = r->start; j <= r->finish; j++)
5259 conflict_hard_regs |= used_hard_regs[j];
5261 aclass = ALLOCNO_CLASS (a);
5262 ALLOCNO_ASSIGNED_P (a) = true;
5263 ALLOCNO_HARD_REGNO (a) = -1;
5264 if (hard_reg_set_subset_p (reg_class_contents[aclass],
5265 conflict_hard_regs))
5266 continue;
5267 mode = ALLOCNO_MODE (a);
5268 #ifdef STACK_REGS
5269 no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
5270 #endif
5271 class_size = ira_class_hard_regs_num[aclass];
5272 costs = ALLOCNO_HARD_REG_COSTS (a);
5273 min_cost = INT_MAX;
5274 best_hard_regno = -1;
5275 for (j = 0; j < class_size; j++)
5277 hard_regno = ira_class_hard_regs[aclass][j];
5278 #ifdef STACK_REGS
5279 if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
5280 && hard_regno <= LAST_STACK_REG)
5281 continue;
5282 #endif
5283 if (ira_hard_reg_set_intersection_p (hard_regno, mode, conflict_hard_regs)
5284 || (TEST_HARD_REG_BIT
5285 (ira_prohibited_class_mode_regs[aclass][mode], hard_regno)))
5286 continue;
5287 if (NUM_REGISTER_FILTERS
5288 && !test_register_filters (ALLOCNO_REGISTER_FILTERS (a),
5289 hard_regno))
5290 continue;
5291 if (costs == NULL)
5293 best_hard_regno = hard_regno;
5294 break;
5296 cost = costs[j];
5297 if (min_cost > cost)
5299 min_cost = cost;
5300 best_hard_regno = hard_regno;
5303 if (best_hard_regno < 0)
5304 continue;
5305 ALLOCNO_HARD_REGNO (a) = hard_regno = best_hard_regno;
5306 for (l = 0; l < nr; l++)
5308 ira_object_t obj = ALLOCNO_OBJECT (a, l);
5309 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
5310 for (k = r->start; k <= r->finish; k++)
5311 used_hard_regs[k] |= ira_reg_mode_hard_regset[hard_regno][mode];
5314 ira_free (sorted_allocnos);
5315 ira_free (used_hard_regs);
5316 ira_free (allocno_priorities);
5317 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
5318 ira_print_disposition (ira_dump_file);
5323 /* Entry function doing coloring. */
5324 void
5325 ira_color (void)
5327 ira_allocno_t a;
5328 ira_allocno_iterator ai;
5330 /* Setup updated costs. */
5331 FOR_EACH_ALLOCNO (a, ai)
5333 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
5334 ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
5336 if (ira_conflicts_p)
5337 color ();
5338 else
5339 fast_allocation ();