Fix PR 93568 (thinko)
[official-gcc.git] / gcc / ira-color.c
blob444cb1e827972df73960a2dd031d9941dd82d1df
1 /* IRA allocation based on graph coloring.
2 Copyright (C) 2006-2020 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 typedef struct allocno_hard_regs *allocno_hard_regs_t;
41 /* The structure contains information about hard registers can be
42 assigned to allocnos. Usually it is allocno profitable hard
43 registers but in some cases this set can be a bit different. Major
44 reason of the difference is a requirement to use hard register sets
45 that form a tree or a forest (set of trees), i.e. hard register set
46 of a node should contain hard register sets of its subnodes. */
47 struct allocno_hard_regs
49 /* Hard registers can be assigned to an allocno. */
50 HARD_REG_SET set;
51 /* Overall (spilling) cost of all allocnos with given register
52 set. */
53 int64_t cost;
56 typedef struct allocno_hard_regs_node *allocno_hard_regs_node_t;
58 /* A node representing allocno hard registers. Such nodes form a
59 forest (set of trees). Each subnode of given node in the forest
60 refers for hard register set (usually allocno profitable hard
61 register set) which is a subset of one referred from given
62 node. */
63 struct allocno_hard_regs_node
65 /* Set up number of the node in preorder traversing of the forest. */
66 int preorder_num;
67 /* Used for different calculation like finding conflict size of an
68 allocno. */
69 int check;
70 /* Used for calculation of conflict size of an allocno. The
71 conflict size of the allocno is maximal number of given allocno
72 hard registers needed for allocation of the conflicting allocnos.
73 Given allocno is trivially colored if this number plus the number
74 of hard registers needed for given allocno is not greater than
75 the number of given allocno hard register set. */
76 int conflict_size;
77 /* The number of hard registers given by member hard_regs. */
78 int hard_regs_num;
79 /* The following member is used to form the final forest. */
80 bool used_p;
81 /* Pointer to the corresponding profitable hard registers. */
82 allocno_hard_regs_t hard_regs;
83 /* Parent, first subnode, previous and next node with the same
84 parent in the forest. */
85 allocno_hard_regs_node_t parent, first, prev, next;
88 /* Info about changing hard reg costs of an allocno. */
89 struct update_cost_record
91 /* Hard regno for which we changed the cost. */
92 int hard_regno;
93 /* Divisor used when we changed the cost of HARD_REGNO. */
94 int divisor;
95 /* Next record for given allocno. */
96 struct update_cost_record *next;
99 /* To decrease footprint of ira_allocno structure we store all data
100 needed only for coloring in the following structure. */
101 struct allocno_color_data
103 /* TRUE value means that the allocno was not removed yet from the
104 conflicting graph during coloring. */
105 unsigned int in_graph_p : 1;
106 /* TRUE if it is put on the stack to make other allocnos
107 colorable. */
108 unsigned int may_be_spilled_p : 1;
109 /* TRUE if the allocno is trivially colorable. */
110 unsigned int colorable_p : 1;
111 /* Number of hard registers of the allocno class really
112 available for the allocno allocation. It is number of the
113 profitable hard regs. */
114 int available_regs_num;
115 /* Sum of frequencies of hard register preferences of all
116 conflicting allocnos which are not the coloring stack yet. */
117 int conflict_allocno_hard_prefs;
118 /* Allocnos in a bucket (used in coloring) chained by the following
119 two members. */
120 ira_allocno_t next_bucket_allocno;
121 ira_allocno_t prev_bucket_allocno;
122 /* Used for temporary purposes. */
123 int temp;
124 /* Used to exclude repeated processing. */
125 int last_process;
126 /* Profitable hard regs available for this pseudo allocation. It
127 means that the set excludes unavailable hard regs and hard regs
128 conflicting with given pseudo. They should be of the allocno
129 class. */
130 HARD_REG_SET profitable_hard_regs;
131 /* The allocno hard registers node. */
132 allocno_hard_regs_node_t hard_regs_node;
133 /* Array of structures allocno_hard_regs_subnode representing
134 given allocno hard registers node (the 1st element in the array)
135 and all its subnodes in the tree (forest) of allocno hard
136 register nodes (see comments above). */
137 int hard_regs_subnodes_start;
138 /* The length of the previous array. */
139 int hard_regs_subnodes_num;
140 /* Records about updating allocno hard reg costs from copies. If
141 the allocno did not get expected hard register, these records are
142 used to restore original hard reg costs of allocnos connected to
143 this allocno by copies. */
144 struct update_cost_record *update_cost_records;
145 /* Threads. We collect allocnos connected by copies into threads
146 and try to assign hard regs to allocnos by threads. */
147 /* Allocno representing all thread. */
148 ira_allocno_t first_thread_allocno;
149 /* Allocnos in thread forms a cycle list through the following
150 member. */
151 ira_allocno_t next_thread_allocno;
152 /* All thread frequency. Defined only for first thread allocno. */
153 int thread_freq;
154 /* Sum of frequencies of hard register preferences of the allocno. */
155 int hard_reg_prefs;
158 /* See above. */
159 typedef struct allocno_color_data *allocno_color_data_t;
161 /* Container for storing allocno data concerning coloring. */
162 static allocno_color_data_t allocno_color_data;
164 /* Macro to access the data concerning coloring. */
165 #define ALLOCNO_COLOR_DATA(a) ((allocno_color_data_t) ALLOCNO_ADD_DATA (a))
167 /* Used for finding allocno colorability to exclude repeated allocno
168 processing and for updating preferencing to exclude repeated
169 allocno processing during assignment. */
170 static int curr_allocno_process;
172 /* This file contains code for regional graph coloring, spill/restore
173 code placement optimization, and code helping the reload pass to do
174 a better job. */
176 /* Bitmap of allocnos which should be colored. */
177 static bitmap coloring_allocno_bitmap;
179 /* Bitmap of allocnos which should be taken into account during
180 coloring. In general case it contains allocnos from
181 coloring_allocno_bitmap plus other already colored conflicting
182 allocnos. */
183 static bitmap consideration_allocno_bitmap;
185 /* All allocnos sorted according their priorities. */
186 static ira_allocno_t *sorted_allocnos;
188 /* Vec representing the stack of allocnos used during coloring. */
189 static vec<ira_allocno_t> allocno_stack_vec;
191 /* Helper for qsort comparison callbacks - return a positive integer if
192 X > Y, or a negative value otherwise. Use a conditional expression
193 instead of a difference computation to insulate from possible overflow
194 issues, e.g. X - Y < 0 for some X > 0 and Y < 0. */
195 #define SORTGT(x,y) (((x) > (y)) ? 1 : -1)
199 /* Definition of vector of allocno hard registers. */
201 /* Vector of unique allocno hard registers. */
202 static vec<allocno_hard_regs_t> allocno_hard_regs_vec;
204 struct allocno_hard_regs_hasher : nofree_ptr_hash <allocno_hard_regs>
206 static inline hashval_t hash (const allocno_hard_regs *);
207 static inline bool equal (const allocno_hard_regs *,
208 const allocno_hard_regs *);
211 /* Returns hash value for allocno hard registers V. */
212 inline hashval_t
213 allocno_hard_regs_hasher::hash (const allocno_hard_regs *hv)
215 return iterative_hash (&hv->set, sizeof (HARD_REG_SET), 0);
218 /* Compares allocno hard registers V1 and V2. */
219 inline bool
220 allocno_hard_regs_hasher::equal (const allocno_hard_regs *hv1,
221 const allocno_hard_regs *hv2)
223 return hv1->set == hv2->set;
226 /* Hash table of unique allocno hard registers. */
227 static hash_table<allocno_hard_regs_hasher> *allocno_hard_regs_htab;
229 /* Return allocno hard registers in the hash table equal to HV. */
230 static allocno_hard_regs_t
231 find_hard_regs (allocno_hard_regs_t hv)
233 return allocno_hard_regs_htab->find (hv);
236 /* Insert allocno hard registers HV in the hash table (if it is not
237 there yet) and return the value which in the table. */
238 static allocno_hard_regs_t
239 insert_hard_regs (allocno_hard_regs_t hv)
241 allocno_hard_regs **slot = allocno_hard_regs_htab->find_slot (hv, INSERT);
243 if (*slot == NULL)
244 *slot = hv;
245 return *slot;
248 /* Initialize data concerning allocno hard registers. */
249 static void
250 init_allocno_hard_regs (void)
252 allocno_hard_regs_vec.create (200);
253 allocno_hard_regs_htab
254 = new hash_table<allocno_hard_regs_hasher> (200);
257 /* Add (or update info about) allocno hard registers with SET and
258 COST. */
259 static allocno_hard_regs_t
260 add_allocno_hard_regs (HARD_REG_SET set, int64_t cost)
262 struct allocno_hard_regs temp;
263 allocno_hard_regs_t hv;
265 gcc_assert (! hard_reg_set_empty_p (set));
266 temp.set = set;
267 if ((hv = find_hard_regs (&temp)) != NULL)
268 hv->cost += cost;
269 else
271 hv = ((struct allocno_hard_regs *)
272 ira_allocate (sizeof (struct allocno_hard_regs)));
273 hv->set = set;
274 hv->cost = cost;
275 allocno_hard_regs_vec.safe_push (hv);
276 insert_hard_regs (hv);
278 return hv;
281 /* Finalize data concerning allocno hard registers. */
282 static void
283 finish_allocno_hard_regs (void)
285 int i;
286 allocno_hard_regs_t hv;
288 for (i = 0;
289 allocno_hard_regs_vec.iterate (i, &hv);
290 i++)
291 ira_free (hv);
292 delete allocno_hard_regs_htab;
293 allocno_hard_regs_htab = NULL;
294 allocno_hard_regs_vec.release ();
297 /* Sort hard regs according to their frequency of usage. */
298 static int
299 allocno_hard_regs_compare (const void *v1p, const void *v2p)
301 allocno_hard_regs_t hv1 = *(const allocno_hard_regs_t *) v1p;
302 allocno_hard_regs_t hv2 = *(const allocno_hard_regs_t *) v2p;
304 if (hv2->cost > hv1->cost)
305 return 1;
306 else if (hv2->cost < hv1->cost)
307 return -1;
308 return SORTGT (allocno_hard_regs_hasher::hash(hv2), allocno_hard_regs_hasher::hash(hv1));
313 /* Used for finding a common ancestor of two allocno hard registers
314 nodes in the forest. We use the current value of
315 'node_check_tick' to mark all nodes from one node to the top and
316 then walking up from another node until we find a marked node.
318 It is also used to figure out allocno colorability as a mark that
319 we already reset value of member 'conflict_size' for the forest
320 node corresponding to the processed allocno. */
321 static int node_check_tick;
323 /* Roots of the forest containing hard register sets can be assigned
324 to allocnos. */
325 static allocno_hard_regs_node_t hard_regs_roots;
327 /* Definition of vector of allocno hard register nodes. */
329 /* Vector used to create the forest. */
330 static vec<allocno_hard_regs_node_t> hard_regs_node_vec;
332 /* Create and return allocno hard registers node containing allocno
333 hard registers HV. */
334 static allocno_hard_regs_node_t
335 create_new_allocno_hard_regs_node (allocno_hard_regs_t hv)
337 allocno_hard_regs_node_t new_node;
339 new_node = ((struct allocno_hard_regs_node *)
340 ira_allocate (sizeof (struct allocno_hard_regs_node)));
341 new_node->check = 0;
342 new_node->hard_regs = hv;
343 new_node->hard_regs_num = hard_reg_set_size (hv->set);
344 new_node->first = NULL;
345 new_node->used_p = false;
346 return new_node;
349 /* Add allocno hard registers node NEW_NODE to the forest on its level
350 given by ROOTS. */
351 static void
352 add_new_allocno_hard_regs_node_to_forest (allocno_hard_regs_node_t *roots,
353 allocno_hard_regs_node_t new_node)
355 new_node->next = *roots;
356 if (new_node->next != NULL)
357 new_node->next->prev = new_node;
358 new_node->prev = NULL;
359 *roots = new_node;
362 /* Add allocno hard registers HV (or its best approximation if it is
363 not possible) to the forest on its level given by ROOTS. */
364 static void
365 add_allocno_hard_regs_to_forest (allocno_hard_regs_node_t *roots,
366 allocno_hard_regs_t hv)
368 unsigned int i, start;
369 allocno_hard_regs_node_t node, prev, new_node;
370 HARD_REG_SET temp_set;
371 allocno_hard_regs_t hv2;
373 start = hard_regs_node_vec.length ();
374 for (node = *roots; node != NULL; node = node->next)
376 if (hv->set == node->hard_regs->set)
377 return;
378 if (hard_reg_set_subset_p (hv->set, node->hard_regs->set))
380 add_allocno_hard_regs_to_forest (&node->first, hv);
381 return;
383 if (hard_reg_set_subset_p (node->hard_regs->set, hv->set))
384 hard_regs_node_vec.safe_push (node);
385 else if (hard_reg_set_intersect_p (hv->set, node->hard_regs->set))
387 temp_set = hv->set & node->hard_regs->set;
388 hv2 = add_allocno_hard_regs (temp_set, hv->cost);
389 add_allocno_hard_regs_to_forest (&node->first, hv2);
392 if (hard_regs_node_vec.length ()
393 > start + 1)
395 /* Create a new node which contains nodes in hard_regs_node_vec. */
396 CLEAR_HARD_REG_SET (temp_set);
397 for (i = start;
398 i < hard_regs_node_vec.length ();
399 i++)
401 node = hard_regs_node_vec[i];
402 temp_set |= node->hard_regs->set;
404 hv = add_allocno_hard_regs (temp_set, hv->cost);
405 new_node = create_new_allocno_hard_regs_node (hv);
406 prev = NULL;
407 for (i = start;
408 i < hard_regs_node_vec.length ();
409 i++)
411 node = hard_regs_node_vec[i];
412 if (node->prev == NULL)
413 *roots = node->next;
414 else
415 node->prev->next = node->next;
416 if (node->next != NULL)
417 node->next->prev = node->prev;
418 if (prev == NULL)
419 new_node->first = node;
420 else
421 prev->next = node;
422 node->prev = prev;
423 node->next = NULL;
424 prev = node;
426 add_new_allocno_hard_regs_node_to_forest (roots, new_node);
428 hard_regs_node_vec.truncate (start);
431 /* Add allocno hard registers nodes starting with the forest level
432 given by FIRST which contains biggest set inside SET. */
433 static void
434 collect_allocno_hard_regs_cover (allocno_hard_regs_node_t first,
435 HARD_REG_SET set)
437 allocno_hard_regs_node_t node;
439 ira_assert (first != NULL);
440 for (node = first; node != NULL; node = node->next)
441 if (hard_reg_set_subset_p (node->hard_regs->set, set))
442 hard_regs_node_vec.safe_push (node);
443 else if (hard_reg_set_intersect_p (set, node->hard_regs->set))
444 collect_allocno_hard_regs_cover (node->first, set);
447 /* Set up field parent as PARENT in all allocno hard registers nodes
448 in forest given by FIRST. */
449 static void
450 setup_allocno_hard_regs_nodes_parent (allocno_hard_regs_node_t first,
451 allocno_hard_regs_node_t parent)
453 allocno_hard_regs_node_t node;
455 for (node = first; node != NULL; node = node->next)
457 node->parent = parent;
458 setup_allocno_hard_regs_nodes_parent (node->first, node);
462 /* Return allocno hard registers node which is a first common ancestor
463 node of FIRST and SECOND in the forest. */
464 static allocno_hard_regs_node_t
465 first_common_ancestor_node (allocno_hard_regs_node_t first,
466 allocno_hard_regs_node_t second)
468 allocno_hard_regs_node_t node;
470 node_check_tick++;
471 for (node = first; node != NULL; node = node->parent)
472 node->check = node_check_tick;
473 for (node = second; node != NULL; node = node->parent)
474 if (node->check == node_check_tick)
475 return node;
476 return first_common_ancestor_node (second, first);
479 /* Print hard reg set SET to F. */
480 static void
481 print_hard_reg_set (FILE *f, HARD_REG_SET set, bool new_line_p)
483 int i, start;
485 for (start = -1, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
487 if (TEST_HARD_REG_BIT (set, i))
489 if (i == 0 || ! TEST_HARD_REG_BIT (set, i - 1))
490 start = i;
492 if (start >= 0
493 && (i == FIRST_PSEUDO_REGISTER - 1 || ! TEST_HARD_REG_BIT (set, i)))
495 if (start == i - 1)
496 fprintf (f, " %d", start);
497 else if (start == i - 2)
498 fprintf (f, " %d %d", start, start + 1);
499 else
500 fprintf (f, " %d-%d", start, i - 1);
501 start = -1;
504 if (new_line_p)
505 fprintf (f, "\n");
508 /* Print allocno hard register subforest given by ROOTS and its LEVEL
509 to F. */
510 static void
511 print_hard_regs_subforest (FILE *f, allocno_hard_regs_node_t roots,
512 int level)
514 int i;
515 allocno_hard_regs_node_t node;
517 for (node = roots; node != NULL; node = node->next)
519 fprintf (f, " ");
520 for (i = 0; i < level * 2; i++)
521 fprintf (f, " ");
522 fprintf (f, "%d:(", node->preorder_num);
523 print_hard_reg_set (f, node->hard_regs->set, false);
524 fprintf (f, ")@%" PRId64"\n", node->hard_regs->cost);
525 print_hard_regs_subforest (f, node->first, level + 1);
529 /* Print the allocno hard register forest to F. */
530 static void
531 print_hard_regs_forest (FILE *f)
533 fprintf (f, " Hard reg set forest:\n");
534 print_hard_regs_subforest (f, hard_regs_roots, 1);
537 /* Print the allocno hard register forest to stderr. */
538 void
539 ira_debug_hard_regs_forest (void)
541 print_hard_regs_forest (stderr);
544 /* Remove unused allocno hard registers nodes from forest given by its
545 *ROOTS. */
546 static void
547 remove_unused_allocno_hard_regs_nodes (allocno_hard_regs_node_t *roots)
549 allocno_hard_regs_node_t node, prev, next, last;
551 for (prev = NULL, node = *roots; node != NULL; node = next)
553 next = node->next;
554 if (node->used_p)
556 remove_unused_allocno_hard_regs_nodes (&node->first);
557 prev = node;
559 else
561 for (last = node->first;
562 last != NULL && last->next != NULL;
563 last = last->next)
565 if (last != NULL)
567 if (prev == NULL)
568 *roots = node->first;
569 else
570 prev->next = node->first;
571 if (next != NULL)
572 next->prev = last;
573 last->next = next;
574 next = node->first;
576 else
578 if (prev == NULL)
579 *roots = next;
580 else
581 prev->next = next;
582 if (next != NULL)
583 next->prev = prev;
585 ira_free (node);
590 /* Set up fields preorder_num starting with START_NUM in all allocno
591 hard registers nodes in forest given by FIRST. Return biggest set
592 PREORDER_NUM increased by 1. */
593 static int
594 enumerate_allocno_hard_regs_nodes (allocno_hard_regs_node_t first,
595 allocno_hard_regs_node_t parent,
596 int start_num)
598 allocno_hard_regs_node_t node;
600 for (node = first; node != NULL; node = node->next)
602 node->preorder_num = start_num++;
603 node->parent = parent;
604 start_num = enumerate_allocno_hard_regs_nodes (node->first, node,
605 start_num);
607 return start_num;
610 /* Number of allocno hard registers nodes in the forest. */
611 static int allocno_hard_regs_nodes_num;
613 /* Table preorder number of allocno hard registers node in the forest
614 -> the allocno hard registers node. */
615 static allocno_hard_regs_node_t *allocno_hard_regs_nodes;
617 /* See below. */
618 typedef struct allocno_hard_regs_subnode *allocno_hard_regs_subnode_t;
620 /* The structure is used to describes all subnodes (not only immediate
621 ones) in the mentioned above tree for given allocno hard register
622 node. The usage of such data accelerates calculation of
623 colorability of given allocno. */
624 struct allocno_hard_regs_subnode
626 /* The conflict size of conflicting allocnos whose hard register
627 sets are equal sets (plus supersets if given node is given
628 allocno hard registers node) of one in the given node. */
629 int left_conflict_size;
630 /* The summary conflict size of conflicting allocnos whose hard
631 register sets are strict subsets of one in the given node.
632 Overall conflict size is
633 left_conflict_subnodes_size
634 + MIN (max_node_impact - left_conflict_subnodes_size,
635 left_conflict_size)
637 short left_conflict_subnodes_size;
638 short max_node_impact;
641 /* Container for hard regs subnodes of all allocnos. */
642 static allocno_hard_regs_subnode_t allocno_hard_regs_subnodes;
644 /* Table (preorder number of allocno hard registers node in the
645 forest, preorder number of allocno hard registers subnode) -> index
646 of the subnode relative to the node. -1 if it is not a
647 subnode. */
648 static int *allocno_hard_regs_subnode_index;
650 /* Setup arrays ALLOCNO_HARD_REGS_NODES and
651 ALLOCNO_HARD_REGS_SUBNODE_INDEX. */
652 static void
653 setup_allocno_hard_regs_subnode_index (allocno_hard_regs_node_t first)
655 allocno_hard_regs_node_t node, parent;
656 int index;
658 for (node = first; node != NULL; node = node->next)
660 allocno_hard_regs_nodes[node->preorder_num] = node;
661 for (parent = node; parent != NULL; parent = parent->parent)
663 index = parent->preorder_num * allocno_hard_regs_nodes_num;
664 allocno_hard_regs_subnode_index[index + node->preorder_num]
665 = node->preorder_num - parent->preorder_num;
667 setup_allocno_hard_regs_subnode_index (node->first);
671 /* Count all allocno hard registers nodes in tree ROOT. */
672 static int
673 get_allocno_hard_regs_subnodes_num (allocno_hard_regs_node_t root)
675 int len = 1;
677 for (root = root->first; root != NULL; root = root->next)
678 len += get_allocno_hard_regs_subnodes_num (root);
679 return len;
682 /* Build the forest of allocno hard registers nodes and assign each
683 allocno a node from the forest. */
684 static void
685 form_allocno_hard_regs_nodes_forest (void)
687 unsigned int i, j, size, len;
688 int start;
689 ira_allocno_t a;
690 allocno_hard_regs_t hv;
691 bitmap_iterator bi;
692 HARD_REG_SET temp;
693 allocno_hard_regs_node_t node, allocno_hard_regs_node;
694 allocno_color_data_t allocno_data;
696 node_check_tick = 0;
697 init_allocno_hard_regs ();
698 hard_regs_roots = NULL;
699 hard_regs_node_vec.create (100);
700 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
701 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
703 CLEAR_HARD_REG_SET (temp);
704 SET_HARD_REG_BIT (temp, i);
705 hv = add_allocno_hard_regs (temp, 0);
706 node = create_new_allocno_hard_regs_node (hv);
707 add_new_allocno_hard_regs_node_to_forest (&hard_regs_roots, node);
709 start = allocno_hard_regs_vec.length ();
710 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
712 a = ira_allocnos[i];
713 allocno_data = ALLOCNO_COLOR_DATA (a);
715 if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
716 continue;
717 hv = (add_allocno_hard_regs
718 (allocno_data->profitable_hard_regs,
719 ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a)));
721 temp = ~ira_no_alloc_regs;
722 add_allocno_hard_regs (temp, 0);
723 qsort (allocno_hard_regs_vec.address () + start,
724 allocno_hard_regs_vec.length () - start,
725 sizeof (allocno_hard_regs_t), allocno_hard_regs_compare);
726 for (i = start;
727 allocno_hard_regs_vec.iterate (i, &hv);
728 i++)
730 add_allocno_hard_regs_to_forest (&hard_regs_roots, hv);
731 ira_assert (hard_regs_node_vec.length () == 0);
733 /* We need to set up parent fields for right work of
734 first_common_ancestor_node. */
735 setup_allocno_hard_regs_nodes_parent (hard_regs_roots, NULL);
736 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
738 a = ira_allocnos[i];
739 allocno_data = ALLOCNO_COLOR_DATA (a);
740 if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
741 continue;
742 hard_regs_node_vec.truncate (0);
743 collect_allocno_hard_regs_cover (hard_regs_roots,
744 allocno_data->profitable_hard_regs);
745 allocno_hard_regs_node = NULL;
746 for (j = 0; hard_regs_node_vec.iterate (j, &node); j++)
747 allocno_hard_regs_node
748 = (j == 0
749 ? node
750 : first_common_ancestor_node (node, allocno_hard_regs_node));
751 /* That is a temporary storage. */
752 allocno_hard_regs_node->used_p = true;
753 allocno_data->hard_regs_node = allocno_hard_regs_node;
755 ira_assert (hard_regs_roots->next == NULL);
756 hard_regs_roots->used_p = true;
757 remove_unused_allocno_hard_regs_nodes (&hard_regs_roots);
758 allocno_hard_regs_nodes_num
759 = enumerate_allocno_hard_regs_nodes (hard_regs_roots, NULL, 0);
760 allocno_hard_regs_nodes
761 = ((allocno_hard_regs_node_t *)
762 ira_allocate (allocno_hard_regs_nodes_num
763 * sizeof (allocno_hard_regs_node_t)));
764 size = allocno_hard_regs_nodes_num * allocno_hard_regs_nodes_num;
765 allocno_hard_regs_subnode_index
766 = (int *) ira_allocate (size * sizeof (int));
767 for (i = 0; i < size; i++)
768 allocno_hard_regs_subnode_index[i] = -1;
769 setup_allocno_hard_regs_subnode_index (hard_regs_roots);
770 start = 0;
771 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
773 a = ira_allocnos[i];
774 allocno_data = ALLOCNO_COLOR_DATA (a);
775 if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
776 continue;
777 len = get_allocno_hard_regs_subnodes_num (allocno_data->hard_regs_node);
778 allocno_data->hard_regs_subnodes_start = start;
779 allocno_data->hard_regs_subnodes_num = len;
780 start += len;
782 allocno_hard_regs_subnodes
783 = ((allocno_hard_regs_subnode_t)
784 ira_allocate (sizeof (struct allocno_hard_regs_subnode) * start));
785 hard_regs_node_vec.release ();
788 /* Free tree of allocno hard registers nodes given by its ROOT. */
789 static void
790 finish_allocno_hard_regs_nodes_tree (allocno_hard_regs_node_t root)
792 allocno_hard_regs_node_t child, next;
794 for (child = root->first; child != NULL; child = next)
796 next = child->next;
797 finish_allocno_hard_regs_nodes_tree (child);
799 ira_free (root);
802 /* Finish work with the forest of allocno hard registers nodes. */
803 static void
804 finish_allocno_hard_regs_nodes_forest (void)
806 allocno_hard_regs_node_t node, next;
808 ira_free (allocno_hard_regs_subnodes);
809 for (node = hard_regs_roots; node != NULL; node = next)
811 next = node->next;
812 finish_allocno_hard_regs_nodes_tree (node);
814 ira_free (allocno_hard_regs_nodes);
815 ira_free (allocno_hard_regs_subnode_index);
816 finish_allocno_hard_regs ();
819 /* Set up left conflict sizes and left conflict subnodes sizes of hard
820 registers subnodes of allocno A. Return TRUE if allocno A is
821 trivially colorable. */
822 static bool
823 setup_left_conflict_sizes_p (ira_allocno_t a)
825 int i, k, nobj, start;
826 int conflict_size, left_conflict_subnodes_size, node_preorder_num;
827 allocno_color_data_t data;
828 HARD_REG_SET profitable_hard_regs;
829 allocno_hard_regs_subnode_t subnodes;
830 allocno_hard_regs_node_t node;
831 HARD_REG_SET node_set;
833 nobj = ALLOCNO_NUM_OBJECTS (a);
834 data = ALLOCNO_COLOR_DATA (a);
835 subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
836 profitable_hard_regs = data->profitable_hard_regs;
837 node = data->hard_regs_node;
838 node_preorder_num = node->preorder_num;
839 node_set = node->hard_regs->set;
840 node_check_tick++;
841 for (k = 0; k < nobj; k++)
843 ira_object_t obj = ALLOCNO_OBJECT (a, k);
844 ira_object_t conflict_obj;
845 ira_object_conflict_iterator oci;
847 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
849 int size;
850 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
851 allocno_hard_regs_node_t conflict_node, temp_node;
852 HARD_REG_SET conflict_node_set;
853 allocno_color_data_t conflict_data;
855 conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
856 if (! ALLOCNO_COLOR_DATA (conflict_a)->in_graph_p
857 || ! hard_reg_set_intersect_p (profitable_hard_regs,
858 conflict_data
859 ->profitable_hard_regs))
860 continue;
861 conflict_node = conflict_data->hard_regs_node;
862 conflict_node_set = conflict_node->hard_regs->set;
863 if (hard_reg_set_subset_p (node_set, conflict_node_set))
864 temp_node = node;
865 else
867 ira_assert (hard_reg_set_subset_p (conflict_node_set, node_set));
868 temp_node = conflict_node;
870 if (temp_node->check != node_check_tick)
872 temp_node->check = node_check_tick;
873 temp_node->conflict_size = 0;
875 size = (ira_reg_class_max_nregs
876 [ALLOCNO_CLASS (conflict_a)][ALLOCNO_MODE (conflict_a)]);
877 if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1)
878 /* We will deal with the subwords individually. */
879 size = 1;
880 temp_node->conflict_size += size;
883 for (i = 0; i < data->hard_regs_subnodes_num; i++)
885 allocno_hard_regs_node_t temp_node;
887 temp_node = allocno_hard_regs_nodes[i + node_preorder_num];
888 ira_assert (temp_node->preorder_num == i + node_preorder_num);
889 subnodes[i].left_conflict_size = (temp_node->check != node_check_tick
890 ? 0 : temp_node->conflict_size);
891 if (hard_reg_set_subset_p (temp_node->hard_regs->set,
892 profitable_hard_regs))
893 subnodes[i].max_node_impact = temp_node->hard_regs_num;
894 else
896 HARD_REG_SET temp_set;
897 int j, n, hard_regno;
898 enum reg_class aclass;
900 temp_set = temp_node->hard_regs->set & profitable_hard_regs;
901 aclass = ALLOCNO_CLASS (a);
902 for (n = 0, j = ira_class_hard_regs_num[aclass] - 1; j >= 0; j--)
904 hard_regno = ira_class_hard_regs[aclass][j];
905 if (TEST_HARD_REG_BIT (temp_set, hard_regno))
906 n++;
908 subnodes[i].max_node_impact = n;
910 subnodes[i].left_conflict_subnodes_size = 0;
912 start = node_preorder_num * allocno_hard_regs_nodes_num;
913 for (i = data->hard_regs_subnodes_num - 1; i > 0; i--)
915 int size, parent_i;
916 allocno_hard_regs_node_t parent;
918 size = (subnodes[i].left_conflict_subnodes_size
919 + MIN (subnodes[i].max_node_impact
920 - subnodes[i].left_conflict_subnodes_size,
921 subnodes[i].left_conflict_size));
922 parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
923 gcc_checking_assert(parent);
924 parent_i
925 = allocno_hard_regs_subnode_index[start + parent->preorder_num];
926 gcc_checking_assert(parent_i >= 0);
927 subnodes[parent_i].left_conflict_subnodes_size += size;
929 left_conflict_subnodes_size = subnodes[0].left_conflict_subnodes_size;
930 conflict_size
931 = (left_conflict_subnodes_size
932 + MIN (subnodes[0].max_node_impact - left_conflict_subnodes_size,
933 subnodes[0].left_conflict_size));
934 conflict_size += ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
935 data->colorable_p = conflict_size <= data->available_regs_num;
936 return data->colorable_p;
939 /* Update left conflict sizes of hard registers subnodes of allocno A
940 after removing allocno REMOVED_A with SIZE from the conflict graph.
941 Return TRUE if A is trivially colorable. */
942 static bool
943 update_left_conflict_sizes_p (ira_allocno_t a,
944 ira_allocno_t removed_a, int size)
946 int i, conflict_size, before_conflict_size, diff, start;
947 int node_preorder_num, parent_i;
948 allocno_hard_regs_node_t node, removed_node, parent;
949 allocno_hard_regs_subnode_t subnodes;
950 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
952 ira_assert (! data->colorable_p);
953 node = data->hard_regs_node;
954 node_preorder_num = node->preorder_num;
955 removed_node = ALLOCNO_COLOR_DATA (removed_a)->hard_regs_node;
956 ira_assert (hard_reg_set_subset_p (removed_node->hard_regs->set,
957 node->hard_regs->set)
958 || hard_reg_set_subset_p (node->hard_regs->set,
959 removed_node->hard_regs->set));
960 start = node_preorder_num * allocno_hard_regs_nodes_num;
961 i = allocno_hard_regs_subnode_index[start + removed_node->preorder_num];
962 if (i < 0)
963 i = 0;
964 subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
965 before_conflict_size
966 = (subnodes[i].left_conflict_subnodes_size
967 + MIN (subnodes[i].max_node_impact
968 - subnodes[i].left_conflict_subnodes_size,
969 subnodes[i].left_conflict_size));
970 subnodes[i].left_conflict_size -= size;
971 for (;;)
973 conflict_size
974 = (subnodes[i].left_conflict_subnodes_size
975 + MIN (subnodes[i].max_node_impact
976 - subnodes[i].left_conflict_subnodes_size,
977 subnodes[i].left_conflict_size));
978 if ((diff = before_conflict_size - conflict_size) == 0)
979 break;
980 ira_assert (conflict_size < before_conflict_size);
981 parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
982 if (parent == NULL)
983 break;
984 parent_i
985 = allocno_hard_regs_subnode_index[start + parent->preorder_num];
986 if (parent_i < 0)
987 break;
988 i = parent_i;
989 before_conflict_size
990 = (subnodes[i].left_conflict_subnodes_size
991 + MIN (subnodes[i].max_node_impact
992 - subnodes[i].left_conflict_subnodes_size,
993 subnodes[i].left_conflict_size));
994 subnodes[i].left_conflict_subnodes_size -= diff;
996 if (i != 0
997 || (conflict_size
998 + ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
999 > data->available_regs_num))
1000 return false;
1001 data->colorable_p = true;
1002 return true;
1005 /* Return true if allocno A has empty profitable hard regs. */
1006 static bool
1007 empty_profitable_hard_regs (ira_allocno_t a)
1009 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
1011 return hard_reg_set_empty_p (data->profitable_hard_regs);
1014 /* Set up profitable hard registers for each allocno being
1015 colored. */
1016 static void
1017 setup_profitable_hard_regs (void)
1019 unsigned int i;
1020 int j, k, nobj, hard_regno, nregs, class_size;
1021 ira_allocno_t a;
1022 bitmap_iterator bi;
1023 enum reg_class aclass;
1024 machine_mode mode;
1025 allocno_color_data_t data;
1027 /* Initial set up from allocno classes and explicitly conflicting
1028 hard regs. */
1029 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1031 a = ira_allocnos[i];
1032 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS)
1033 continue;
1034 data = ALLOCNO_COLOR_DATA (a);
1035 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL
1036 && ALLOCNO_CLASS_COST (a) > ALLOCNO_MEMORY_COST (a)
1037 /* Do not empty profitable regs for static chain pointer
1038 pseudo when non-local goto is used. */
1039 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1040 CLEAR_HARD_REG_SET (data->profitable_hard_regs);
1041 else
1043 mode = ALLOCNO_MODE (a);
1044 data->profitable_hard_regs
1045 = ira_useful_class_mode_regs[aclass][mode];
1046 nobj = ALLOCNO_NUM_OBJECTS (a);
1047 for (k = 0; k < nobj; k++)
1049 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1051 data->profitable_hard_regs
1052 &= ~OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
1056 /* Exclude hard regs already assigned for conflicting objects. */
1057 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, i, bi)
1059 a = ira_allocnos[i];
1060 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1061 || ! ALLOCNO_ASSIGNED_P (a)
1062 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1063 continue;
1064 mode = ALLOCNO_MODE (a);
1065 nregs = hard_regno_nregs (hard_regno, mode);
1066 nobj = ALLOCNO_NUM_OBJECTS (a);
1067 for (k = 0; k < nobj; k++)
1069 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1070 ira_object_t conflict_obj;
1071 ira_object_conflict_iterator oci;
1073 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1075 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1077 /* We can process the conflict allocno repeatedly with
1078 the same result. */
1079 if (nregs == nobj && nregs > 1)
1081 int num = OBJECT_SUBWORD (conflict_obj);
1083 if (REG_WORDS_BIG_ENDIAN)
1084 CLEAR_HARD_REG_BIT
1085 (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1086 hard_regno + nobj - num - 1);
1087 else
1088 CLEAR_HARD_REG_BIT
1089 (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1090 hard_regno + num);
1092 else
1093 ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs
1094 &= ~ira_reg_mode_hard_regset[hard_regno][mode];
1098 /* Exclude too costly hard regs. */
1099 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1101 int min_cost = INT_MAX;
1102 int *costs;
1104 a = ira_allocnos[i];
1105 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1106 || empty_profitable_hard_regs (a))
1107 continue;
1108 data = ALLOCNO_COLOR_DATA (a);
1109 if ((costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a)) != NULL
1110 || (costs = ALLOCNO_HARD_REG_COSTS (a)) != NULL)
1112 class_size = ira_class_hard_regs_num[aclass];
1113 for (j = 0; j < class_size; j++)
1115 hard_regno = ira_class_hard_regs[aclass][j];
1116 if (! TEST_HARD_REG_BIT (data->profitable_hard_regs,
1117 hard_regno))
1118 continue;
1119 if (ALLOCNO_UPDATED_MEMORY_COST (a) < costs[j]
1120 /* Do not remove HARD_REGNO for static chain pointer
1121 pseudo when non-local goto is used. */
1122 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1123 CLEAR_HARD_REG_BIT (data->profitable_hard_regs,
1124 hard_regno);
1125 else if (min_cost > costs[j])
1126 min_cost = costs[j];
1129 else if (ALLOCNO_UPDATED_MEMORY_COST (a)
1130 < ALLOCNO_UPDATED_CLASS_COST (a)
1131 /* Do not empty profitable regs for static chain
1132 pointer pseudo when non-local goto is used. */
1133 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1134 CLEAR_HARD_REG_SET (data->profitable_hard_regs);
1135 if (ALLOCNO_UPDATED_CLASS_COST (a) > min_cost)
1136 ALLOCNO_UPDATED_CLASS_COST (a) = min_cost;
1142 /* This page contains functions used to choose hard registers for
1143 allocnos. */
1145 /* Pool for update cost records. */
1146 static object_allocator<update_cost_record> update_cost_record_pool
1147 ("update cost records");
1149 /* Return new update cost record with given params. */
1150 static struct update_cost_record *
1151 get_update_cost_record (int hard_regno, int divisor,
1152 struct update_cost_record *next)
1154 struct update_cost_record *record;
1156 record = update_cost_record_pool.allocate ();
1157 record->hard_regno = hard_regno;
1158 record->divisor = divisor;
1159 record->next = next;
1160 return record;
1163 /* Free memory for all records in LIST. */
1164 static void
1165 free_update_cost_record_list (struct update_cost_record *list)
1167 struct update_cost_record *next;
1169 while (list != NULL)
1171 next = list->next;
1172 update_cost_record_pool.remove (list);
1173 list = next;
1177 /* Free memory allocated for all update cost records. */
1178 static void
1179 finish_update_cost_records (void)
1181 update_cost_record_pool.release ();
1184 /* Array whose element value is TRUE if the corresponding hard
1185 register was already allocated for an allocno. */
1186 static bool allocated_hardreg_p[FIRST_PSEUDO_REGISTER];
1188 /* Describes one element in a queue of allocnos whose costs need to be
1189 updated. Each allocno in the queue is known to have an allocno
1190 class. */
1191 struct update_cost_queue_elem
1193 /* This element is in the queue iff CHECK == update_cost_check. */
1194 int check;
1196 /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
1197 connecting this allocno to the one being allocated. */
1198 int divisor;
1200 /* Allocno from which we are chaining costs of connected allocnos.
1201 It is used not go back in graph of allocnos connected by
1202 copies. */
1203 ira_allocno_t from;
1205 /* The next allocno in the queue, or null if this is the last element. */
1206 ira_allocno_t next;
1209 /* The first element in a queue of allocnos whose copy costs need to be
1210 updated. Null if the queue is empty. */
1211 static ira_allocno_t update_cost_queue;
1213 /* The last element in the queue described by update_cost_queue.
1214 Not valid if update_cost_queue is null. */
1215 static struct update_cost_queue_elem *update_cost_queue_tail;
1217 /* A pool of elements in the queue described by update_cost_queue.
1218 Elements are indexed by ALLOCNO_NUM. */
1219 static struct update_cost_queue_elem *update_cost_queue_elems;
1221 /* The current value of update_costs_from_copies call count. */
1222 static int update_cost_check;
1224 /* Allocate and initialize data necessary for function
1225 update_costs_from_copies. */
1226 static void
1227 initiate_cost_update (void)
1229 size_t size;
1231 size = ira_allocnos_num * sizeof (struct update_cost_queue_elem);
1232 update_cost_queue_elems
1233 = (struct update_cost_queue_elem *) ira_allocate (size);
1234 memset (update_cost_queue_elems, 0, size);
1235 update_cost_check = 0;
1238 /* Deallocate data used by function update_costs_from_copies. */
1239 static void
1240 finish_cost_update (void)
1242 ira_free (update_cost_queue_elems);
1243 finish_update_cost_records ();
1246 /* When we traverse allocnos to update hard register costs, the cost
1247 divisor will be multiplied by the following macro value for each
1248 hop from given allocno to directly connected allocnos. */
1249 #define COST_HOP_DIVISOR 4
1251 /* Start a new cost-updating pass. */
1252 static void
1253 start_update_cost (void)
1255 update_cost_check++;
1256 update_cost_queue = NULL;
1259 /* Add (ALLOCNO, FROM, DIVISOR) to the end of update_cost_queue, unless
1260 ALLOCNO is already in the queue, or has NO_REGS class. */
1261 static inline void
1262 queue_update_cost (ira_allocno_t allocno, ira_allocno_t from, int divisor)
1264 struct update_cost_queue_elem *elem;
1266 elem = &update_cost_queue_elems[ALLOCNO_NUM (allocno)];
1267 if (elem->check != update_cost_check
1268 && ALLOCNO_CLASS (allocno) != NO_REGS)
1270 elem->check = update_cost_check;
1271 elem->from = from;
1272 elem->divisor = divisor;
1273 elem->next = NULL;
1274 if (update_cost_queue == NULL)
1275 update_cost_queue = allocno;
1276 else
1277 update_cost_queue_tail->next = allocno;
1278 update_cost_queue_tail = elem;
1282 /* Try to remove the first element from update_cost_queue. Return
1283 false if the queue was empty, otherwise make (*ALLOCNO, *FROM,
1284 *DIVISOR) describe the removed element. */
1285 static inline bool
1286 get_next_update_cost (ira_allocno_t *allocno, ira_allocno_t *from, int *divisor)
1288 struct update_cost_queue_elem *elem;
1290 if (update_cost_queue == NULL)
1291 return false;
1293 *allocno = update_cost_queue;
1294 elem = &update_cost_queue_elems[ALLOCNO_NUM (*allocno)];
1295 *from = elem->from;
1296 *divisor = elem->divisor;
1297 update_cost_queue = elem->next;
1298 return true;
1301 /* Increase costs of HARD_REGNO by UPDATE_COST and conflict cost by
1302 UPDATE_CONFLICT_COST for ALLOCNO. Return true if we really
1303 modified the cost. */
1304 static bool
1305 update_allocno_cost (ira_allocno_t allocno, int hard_regno,
1306 int update_cost, int update_conflict_cost)
1308 int i;
1309 enum reg_class aclass = ALLOCNO_CLASS (allocno);
1311 i = ira_class_hard_reg_index[aclass][hard_regno];
1312 if (i < 0)
1313 return false;
1314 ira_allocate_and_set_or_copy_costs
1315 (&ALLOCNO_UPDATED_HARD_REG_COSTS (allocno), aclass,
1316 ALLOCNO_UPDATED_CLASS_COST (allocno),
1317 ALLOCNO_HARD_REG_COSTS (allocno));
1318 ira_allocate_and_set_or_copy_costs
1319 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno),
1320 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (allocno));
1321 ALLOCNO_UPDATED_HARD_REG_COSTS (allocno)[i] += update_cost;
1322 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno)[i] += update_conflict_cost;
1323 return true;
1326 /* Update (decrease if DECR_P) HARD_REGNO cost of allocnos connected
1327 by copies to ALLOCNO to increase chances to remove some copies as
1328 the result of subsequent assignment. Record cost updates if
1329 RECORD_P is true. */
1330 static void
1331 update_costs_from_allocno (ira_allocno_t allocno, int hard_regno,
1332 int divisor, bool decr_p, bool record_p)
1334 int cost, update_cost, update_conflict_cost;
1335 machine_mode mode;
1336 enum reg_class rclass, aclass;
1337 ira_allocno_t another_allocno, from = NULL;
1338 ira_copy_t cp, next_cp;
1340 rclass = REGNO_REG_CLASS (hard_regno);
1343 mode = ALLOCNO_MODE (allocno);
1344 ira_init_register_move_cost_if_necessary (mode);
1345 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1347 if (cp->first == allocno)
1349 next_cp = cp->next_first_allocno_copy;
1350 another_allocno = cp->second;
1352 else if (cp->second == allocno)
1354 next_cp = cp->next_second_allocno_copy;
1355 another_allocno = cp->first;
1357 else
1358 gcc_unreachable ();
1360 if (another_allocno == from)
1361 continue;
1363 aclass = ALLOCNO_CLASS (another_allocno);
1364 if (! TEST_HARD_REG_BIT (reg_class_contents[aclass],
1365 hard_regno)
1366 || ALLOCNO_ASSIGNED_P (another_allocno))
1367 continue;
1369 /* If we have different modes use the smallest one. It is
1370 a sub-register move. It is hard to predict what LRA
1371 will reload (the pseudo or its sub-register) but LRA
1372 will try to minimize the data movement. Also for some
1373 register classes bigger modes might be invalid,
1374 e.g. DImode for AREG on x86. For such cases the
1375 register move cost will be maximal. */
1376 mode = narrower_subreg_mode (mode, ALLOCNO_MODE (cp->second));
1377 ira_init_register_move_cost_if_necessary (mode);
1379 cost = (cp->second == allocno
1380 ? ira_register_move_cost[mode][rclass][aclass]
1381 : ira_register_move_cost[mode][aclass][rclass]);
1382 if (decr_p)
1383 cost = -cost;
1385 update_conflict_cost = update_cost = cp->freq * cost / divisor;
1387 if (ALLOCNO_COLOR_DATA (another_allocno) != NULL
1388 && (ALLOCNO_COLOR_DATA (allocno)->first_thread_allocno
1389 != ALLOCNO_COLOR_DATA (another_allocno)->first_thread_allocno))
1390 /* Decrease conflict cost of ANOTHER_ALLOCNO if it is not
1391 in the same allocation thread. */
1392 update_conflict_cost /= COST_HOP_DIVISOR;
1394 if (update_cost == 0)
1395 continue;
1397 if (! update_allocno_cost (another_allocno, hard_regno,
1398 update_cost, update_conflict_cost))
1399 continue;
1400 queue_update_cost (another_allocno, allocno, divisor * COST_HOP_DIVISOR);
1401 if (record_p && ALLOCNO_COLOR_DATA (another_allocno) != NULL)
1402 ALLOCNO_COLOR_DATA (another_allocno)->update_cost_records
1403 = get_update_cost_record (hard_regno, divisor,
1404 ALLOCNO_COLOR_DATA (another_allocno)
1405 ->update_cost_records);
1408 while (get_next_update_cost (&allocno, &from, &divisor));
1411 /* Decrease preferred ALLOCNO hard register costs and costs of
1412 allocnos connected to ALLOCNO through copy. */
1413 static void
1414 update_costs_from_prefs (ira_allocno_t allocno)
1416 ira_pref_t pref;
1418 start_update_cost ();
1419 for (pref = ALLOCNO_PREFS (allocno); pref != NULL; pref = pref->next_pref)
1420 update_costs_from_allocno (allocno, pref->hard_regno,
1421 COST_HOP_DIVISOR, true, true);
1424 /* Update (decrease if DECR_P) the cost of allocnos connected to
1425 ALLOCNO through copies to increase chances to remove some copies as
1426 the result of subsequent assignment. ALLOCNO was just assigned to
1427 a hard register. Record cost updates if RECORD_P is true. */
1428 static void
1429 update_costs_from_copies (ira_allocno_t allocno, bool decr_p, bool record_p)
1431 int hard_regno;
1433 hard_regno = ALLOCNO_HARD_REGNO (allocno);
1434 ira_assert (hard_regno >= 0 && ALLOCNO_CLASS (allocno) != NO_REGS);
1435 start_update_cost ();
1436 update_costs_from_allocno (allocno, hard_regno, 1, decr_p, record_p);
1439 /* Update conflict_allocno_hard_prefs of allocnos conflicting with
1440 ALLOCNO. */
1441 static void
1442 update_conflict_allocno_hard_prefs (ira_allocno_t allocno)
1444 int l, nr = ALLOCNO_NUM_OBJECTS (allocno);
1446 for (l = 0; l < nr; l++)
1448 ira_object_t conflict_obj, obj = ALLOCNO_OBJECT (allocno, l);
1449 ira_object_conflict_iterator oci;
1451 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1453 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1454 allocno_color_data_t conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
1455 ira_pref_t pref;
1457 if (!(hard_reg_set_intersect_p
1458 (ALLOCNO_COLOR_DATA (allocno)->profitable_hard_regs,
1459 conflict_data->profitable_hard_regs)))
1460 continue;
1461 for (pref = ALLOCNO_PREFS (allocno);
1462 pref != NULL;
1463 pref = pref->next_pref)
1464 conflict_data->conflict_allocno_hard_prefs += pref->freq;
1469 /* Restore costs of allocnos connected to ALLOCNO by copies as it was
1470 before updating costs of these allocnos from given allocno. This
1471 is a wise thing to do as if given allocno did not get an expected
1472 hard reg, using smaller cost of the hard reg for allocnos connected
1473 by copies to given allocno becomes actually misleading. Free all
1474 update cost records for ALLOCNO as we don't need them anymore. */
1475 static void
1476 restore_costs_from_copies (ira_allocno_t allocno)
1478 struct update_cost_record *records, *curr;
1480 if (ALLOCNO_COLOR_DATA (allocno) == NULL)
1481 return;
1482 records = ALLOCNO_COLOR_DATA (allocno)->update_cost_records;
1483 start_update_cost ();
1484 for (curr = records; curr != NULL; curr = curr->next)
1485 update_costs_from_allocno (allocno, curr->hard_regno,
1486 curr->divisor, true, false);
1487 free_update_cost_record_list (records);
1488 ALLOCNO_COLOR_DATA (allocno)->update_cost_records = NULL;
1491 /* This function updates COSTS (decrease if DECR_P) for hard_registers
1492 of ACLASS by conflict costs of the unassigned allocnos
1493 connected by copies with allocnos in update_cost_queue. This
1494 update increases chances to remove some copies. */
1495 static void
1496 update_conflict_hard_regno_costs (int *costs, enum reg_class aclass,
1497 bool decr_p)
1499 int i, cost, class_size, freq, mult, div, divisor;
1500 int index, hard_regno;
1501 int *conflict_costs;
1502 bool cont_p;
1503 enum reg_class another_aclass;
1504 ira_allocno_t allocno, another_allocno, from;
1505 ira_copy_t cp, next_cp;
1507 while (get_next_update_cost (&allocno, &from, &divisor))
1508 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1510 if (cp->first == allocno)
1512 next_cp = cp->next_first_allocno_copy;
1513 another_allocno = cp->second;
1515 else if (cp->second == allocno)
1517 next_cp = cp->next_second_allocno_copy;
1518 another_allocno = cp->first;
1520 else
1521 gcc_unreachable ();
1523 if (another_allocno == from)
1524 continue;
1526 another_aclass = ALLOCNO_CLASS (another_allocno);
1527 if (! ira_reg_classes_intersect_p[aclass][another_aclass]
1528 || ALLOCNO_ASSIGNED_P (another_allocno)
1529 || ALLOCNO_COLOR_DATA (another_allocno)->may_be_spilled_p)
1530 continue;
1531 class_size = ira_class_hard_regs_num[another_aclass];
1532 ira_allocate_and_copy_costs
1533 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
1534 another_aclass, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
1535 conflict_costs
1536 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno);
1537 if (conflict_costs == NULL)
1538 cont_p = true;
1539 else
1541 mult = cp->freq;
1542 freq = ALLOCNO_FREQ (another_allocno);
1543 if (freq == 0)
1544 freq = 1;
1545 div = freq * divisor;
1546 cont_p = false;
1547 for (i = class_size - 1; i >= 0; i--)
1549 hard_regno = ira_class_hard_regs[another_aclass][i];
1550 ira_assert (hard_regno >= 0);
1551 index = ira_class_hard_reg_index[aclass][hard_regno];
1552 if (index < 0)
1553 continue;
1554 cost = (int) (((int64_t) conflict_costs [i] * mult) / div);
1555 if (cost == 0)
1556 continue;
1557 cont_p = true;
1558 if (decr_p)
1559 cost = -cost;
1560 costs[index] += cost;
1563 /* Probably 5 hops will be enough. */
1564 if (cont_p
1565 && divisor <= (COST_HOP_DIVISOR
1566 * COST_HOP_DIVISOR
1567 * COST_HOP_DIVISOR
1568 * COST_HOP_DIVISOR))
1569 queue_update_cost (another_allocno, allocno, divisor * COST_HOP_DIVISOR);
1573 /* Set up conflicting (through CONFLICT_REGS) for each object of
1574 allocno A and the start allocno profitable regs (through
1575 START_PROFITABLE_REGS). Remember that the start profitable regs
1576 exclude hard regs which cannot hold value of mode of allocno A.
1577 This covers mostly cases when multi-register value should be
1578 aligned. */
1579 static inline void
1580 get_conflict_and_start_profitable_regs (ira_allocno_t a, bool retry_p,
1581 HARD_REG_SET *conflict_regs,
1582 HARD_REG_SET *start_profitable_regs)
1584 int i, nwords;
1585 ira_object_t obj;
1587 nwords = ALLOCNO_NUM_OBJECTS (a);
1588 for (i = 0; i < nwords; i++)
1590 obj = ALLOCNO_OBJECT (a, i);
1591 conflict_regs[i] = OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
1593 if (retry_p)
1594 *start_profitable_regs
1595 = (reg_class_contents[ALLOCNO_CLASS (a)]
1596 &~ (ira_prohibited_class_mode_regs
1597 [ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]));
1598 else
1599 *start_profitable_regs = ALLOCNO_COLOR_DATA (a)->profitable_hard_regs;
1602 /* Return true if HARD_REGNO is ok for assigning to allocno A with
1603 PROFITABLE_REGS and whose objects have CONFLICT_REGS. */
1604 static inline bool
1605 check_hard_reg_p (ira_allocno_t a, int hard_regno,
1606 HARD_REG_SET *conflict_regs, HARD_REG_SET profitable_regs)
1608 int j, nwords, nregs;
1609 enum reg_class aclass;
1610 machine_mode mode;
1612 aclass = ALLOCNO_CLASS (a);
1613 mode = ALLOCNO_MODE (a);
1614 if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[aclass][mode],
1615 hard_regno))
1616 return false;
1617 /* Checking only profitable hard regs. */
1618 if (! TEST_HARD_REG_BIT (profitable_regs, hard_regno))
1619 return false;
1620 nregs = hard_regno_nregs (hard_regno, mode);
1621 nwords = ALLOCNO_NUM_OBJECTS (a);
1622 for (j = 0; j < nregs; j++)
1624 int k;
1625 int set_to_test_start = 0, set_to_test_end = nwords;
1627 if (nregs == nwords)
1629 if (REG_WORDS_BIG_ENDIAN)
1630 set_to_test_start = nwords - j - 1;
1631 else
1632 set_to_test_start = j;
1633 set_to_test_end = set_to_test_start + 1;
1635 for (k = set_to_test_start; k < set_to_test_end; k++)
1636 if (TEST_HARD_REG_BIT (conflict_regs[k], hard_regno + j))
1637 break;
1638 if (k != set_to_test_end)
1639 break;
1641 return j == nregs;
1644 /* Return number of registers needed to be saved and restored at
1645 function prologue/epilogue if we allocate HARD_REGNO to hold value
1646 of MODE. */
1647 static int
1648 calculate_saved_nregs (int hard_regno, machine_mode mode)
1650 int i;
1651 int nregs = 0;
1653 ira_assert (hard_regno >= 0);
1654 for (i = hard_regno_nregs (hard_regno, mode) - 1; i >= 0; i--)
1655 if (!allocated_hardreg_p[hard_regno + i]
1656 && !crtl->abi->clobbers_full_reg_p (hard_regno + i)
1657 && !LOCAL_REGNO (hard_regno + i))
1658 nregs++;
1659 return nregs;
1662 /* Choose a hard register for allocno A. If RETRY_P is TRUE, it means
1663 that the function called from function
1664 `ira_reassign_conflict_allocnos' and `allocno_reload_assign'. In
1665 this case some allocno data are not defined or updated and we
1666 should not touch these data. The function returns true if we
1667 managed to assign a hard register to the allocno.
1669 To assign a hard register, first of all we calculate all conflict
1670 hard registers which can come from conflicting allocnos with
1671 already assigned hard registers. After that we find first free
1672 hard register with the minimal cost. During hard register cost
1673 calculation we take conflict hard register costs into account to
1674 give a chance for conflicting allocnos to get a better hard
1675 register in the future.
1677 If the best hard register cost is bigger than cost of memory usage
1678 for the allocno, we don't assign a hard register to given allocno
1679 at all.
1681 If we assign a hard register to the allocno, we update costs of the
1682 hard register for allocnos connected by copies to improve a chance
1683 to coalesce insns represented by the copies when we assign hard
1684 registers to the allocnos connected by the copies. */
1685 static bool
1686 assign_hard_reg (ira_allocno_t a, bool retry_p)
1688 HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
1689 int i, j, hard_regno, best_hard_regno, class_size;
1690 int cost, mem_cost, min_cost, full_cost, min_full_cost, nwords, word;
1691 int *a_costs;
1692 enum reg_class aclass;
1693 machine_mode mode;
1694 static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
1695 int saved_nregs;
1696 enum reg_class rclass;
1697 int add_cost;
1698 #ifdef STACK_REGS
1699 bool no_stack_reg_p;
1700 #endif
1702 ira_assert (! ALLOCNO_ASSIGNED_P (a));
1703 get_conflict_and_start_profitable_regs (a, retry_p,
1704 conflicting_regs,
1705 &profitable_hard_regs);
1706 aclass = ALLOCNO_CLASS (a);
1707 class_size = ira_class_hard_regs_num[aclass];
1708 best_hard_regno = -1;
1709 memset (full_costs, 0, sizeof (int) * class_size);
1710 mem_cost = 0;
1711 memset (costs, 0, sizeof (int) * class_size);
1712 memset (full_costs, 0, sizeof (int) * class_size);
1713 #ifdef STACK_REGS
1714 no_stack_reg_p = false;
1715 #endif
1716 if (! retry_p)
1717 start_update_cost ();
1718 mem_cost += ALLOCNO_UPDATED_MEMORY_COST (a);
1720 ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
1721 aclass, ALLOCNO_HARD_REG_COSTS (a));
1722 a_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
1723 #ifdef STACK_REGS
1724 no_stack_reg_p = no_stack_reg_p || ALLOCNO_TOTAL_NO_STACK_REG_P (a);
1725 #endif
1726 cost = ALLOCNO_UPDATED_CLASS_COST (a);
1727 for (i = 0; i < class_size; i++)
1728 if (a_costs != NULL)
1730 costs[i] += a_costs[i];
1731 full_costs[i] += a_costs[i];
1733 else
1735 costs[i] += cost;
1736 full_costs[i] += cost;
1738 nwords = ALLOCNO_NUM_OBJECTS (a);
1739 curr_allocno_process++;
1740 for (word = 0; word < nwords; word++)
1742 ira_object_t conflict_obj;
1743 ira_object_t obj = ALLOCNO_OBJECT (a, word);
1744 ira_object_conflict_iterator oci;
1746 /* Take preferences of conflicting allocnos into account. */
1747 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1749 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1750 enum reg_class conflict_aclass;
1751 allocno_color_data_t data = ALLOCNO_COLOR_DATA (conflict_a);
1753 /* Reload can give another class so we need to check all
1754 allocnos. */
1755 if (!retry_p
1756 && ((!ALLOCNO_ASSIGNED_P (conflict_a)
1757 || ALLOCNO_HARD_REGNO (conflict_a) < 0)
1758 && !(hard_reg_set_intersect_p
1759 (profitable_hard_regs,
1760 ALLOCNO_COLOR_DATA
1761 (conflict_a)->profitable_hard_regs))))
1763 /* All conflict allocnos are in consideration bitmap
1764 when retry_p is false. It might change in future and
1765 if it happens the assert will be broken. It means
1766 the code should be modified for the new
1767 assumptions. */
1768 ira_assert (bitmap_bit_p (consideration_allocno_bitmap,
1769 ALLOCNO_NUM (conflict_a)));
1770 continue;
1772 conflict_aclass = ALLOCNO_CLASS (conflict_a);
1773 ira_assert (ira_reg_classes_intersect_p
1774 [aclass][conflict_aclass]);
1775 if (ALLOCNO_ASSIGNED_P (conflict_a))
1777 hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
1778 if (hard_regno >= 0
1779 && (ira_hard_reg_set_intersection_p
1780 (hard_regno, ALLOCNO_MODE (conflict_a),
1781 reg_class_contents[aclass])))
1783 int n_objects = ALLOCNO_NUM_OBJECTS (conflict_a);
1784 int conflict_nregs;
1786 mode = ALLOCNO_MODE (conflict_a);
1787 conflict_nregs = hard_regno_nregs (hard_regno, mode);
1788 if (conflict_nregs == n_objects && conflict_nregs > 1)
1790 int num = OBJECT_SUBWORD (conflict_obj);
1792 if (REG_WORDS_BIG_ENDIAN)
1793 SET_HARD_REG_BIT (conflicting_regs[word],
1794 hard_regno + n_objects - num - 1);
1795 else
1796 SET_HARD_REG_BIT (conflicting_regs[word],
1797 hard_regno + num);
1799 else
1800 conflicting_regs[word]
1801 |= ira_reg_mode_hard_regset[hard_regno][mode];
1802 if (hard_reg_set_subset_p (profitable_hard_regs,
1803 conflicting_regs[word]))
1804 goto fail;
1807 else if (! retry_p
1808 && ! ALLOCNO_COLOR_DATA (conflict_a)->may_be_spilled_p
1809 /* Don't process the conflict allocno twice. */
1810 && (ALLOCNO_COLOR_DATA (conflict_a)->last_process
1811 != curr_allocno_process))
1813 int k, *conflict_costs;
1815 ALLOCNO_COLOR_DATA (conflict_a)->last_process
1816 = curr_allocno_process;
1817 ira_allocate_and_copy_costs
1818 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a),
1819 conflict_aclass,
1820 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_a));
1821 conflict_costs
1822 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a);
1823 if (conflict_costs != NULL)
1824 for (j = class_size - 1; j >= 0; j--)
1826 hard_regno = ira_class_hard_regs[aclass][j];
1827 ira_assert (hard_regno >= 0);
1828 k = ira_class_hard_reg_index[conflict_aclass][hard_regno];
1829 if (k < 0
1830 /* If HARD_REGNO is not available for CONFLICT_A,
1831 the conflict would be ignored, since HARD_REGNO
1832 will never be assigned to CONFLICT_A. */
1833 || !TEST_HARD_REG_BIT (data->profitable_hard_regs,
1834 hard_regno))
1835 continue;
1836 full_costs[j] -= conflict_costs[k];
1838 queue_update_cost (conflict_a, NULL, COST_HOP_DIVISOR);
1843 if (! retry_p)
1844 /* Take into account preferences of allocnos connected by copies to
1845 the conflict allocnos. */
1846 update_conflict_hard_regno_costs (full_costs, aclass, true);
1848 /* Take preferences of allocnos connected by copies into
1849 account. */
1850 if (! retry_p)
1852 start_update_cost ();
1853 queue_update_cost (a, NULL, COST_HOP_DIVISOR);
1854 update_conflict_hard_regno_costs (full_costs, aclass, false);
1856 min_cost = min_full_cost = INT_MAX;
1857 /* We don't care about giving callee saved registers to allocnos no
1858 living through calls because call clobbered registers are
1859 allocated first (it is usual practice to put them first in
1860 REG_ALLOC_ORDER). */
1861 mode = ALLOCNO_MODE (a);
1862 for (i = 0; i < class_size; i++)
1864 hard_regno = ira_class_hard_regs[aclass][i];
1865 #ifdef STACK_REGS
1866 if (no_stack_reg_p
1867 && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
1868 continue;
1869 #endif
1870 if (! check_hard_reg_p (a, hard_regno,
1871 conflicting_regs, profitable_hard_regs))
1872 continue;
1873 cost = costs[i];
1874 full_cost = full_costs[i];
1875 if (!HONOR_REG_ALLOC_ORDER)
1877 if ((saved_nregs = calculate_saved_nregs (hard_regno, mode)) != 0)
1878 /* We need to save/restore the hard register in
1879 epilogue/prologue. Therefore we increase the cost. */
1881 rclass = REGNO_REG_CLASS (hard_regno);
1882 add_cost = ((ira_memory_move_cost[mode][rclass][0]
1883 + ira_memory_move_cost[mode][rclass][1])
1884 * saved_nregs / hard_regno_nregs (hard_regno,
1885 mode) - 1);
1886 cost += add_cost;
1887 full_cost += add_cost;
1890 if (min_cost > cost)
1891 min_cost = cost;
1892 if (min_full_cost > full_cost)
1894 min_full_cost = full_cost;
1895 best_hard_regno = hard_regno;
1896 ira_assert (hard_regno >= 0);
1899 if (min_full_cost > mem_cost
1900 /* Do not spill static chain pointer pseudo when non-local goto
1901 is used. */
1902 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1904 if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1905 fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
1906 mem_cost, min_full_cost);
1907 best_hard_regno = -1;
1909 fail:
1910 if (best_hard_regno >= 0)
1912 for (i = hard_regno_nregs (best_hard_regno, mode) - 1; i >= 0; i--)
1913 allocated_hardreg_p[best_hard_regno + i] = true;
1915 if (! retry_p)
1916 restore_costs_from_copies (a);
1917 ALLOCNO_HARD_REGNO (a) = best_hard_regno;
1918 ALLOCNO_ASSIGNED_P (a) = true;
1919 if (best_hard_regno >= 0)
1920 update_costs_from_copies (a, true, ! retry_p);
1921 ira_assert (ALLOCNO_CLASS (a) == aclass);
1922 /* We don't need updated costs anymore. */
1923 ira_free_allocno_updated_costs (a);
1924 return best_hard_regno >= 0;
1929 /* An array used to sort copies. */
1930 static ira_copy_t *sorted_copies;
1932 /* If allocno A is a cap, return non-cap allocno from which A is
1933 created. Otherwise, return A. */
1934 static ira_allocno_t
1935 get_cap_member (ira_allocno_t a)
1937 ira_allocno_t member;
1939 while ((member = ALLOCNO_CAP_MEMBER (a)) != NULL)
1940 a = member;
1941 return a;
1944 /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
1945 used to find a conflict for new allocnos or allocnos with the
1946 different allocno classes. */
1947 static bool
1948 allocnos_conflict_by_live_ranges_p (ira_allocno_t a1, ira_allocno_t a2)
1950 rtx reg1, reg2;
1951 int i, j;
1952 int n1 = ALLOCNO_NUM_OBJECTS (a1);
1953 int n2 = ALLOCNO_NUM_OBJECTS (a2);
1955 if (a1 == a2)
1956 return false;
1957 reg1 = regno_reg_rtx[ALLOCNO_REGNO (a1)];
1958 reg2 = regno_reg_rtx[ALLOCNO_REGNO (a2)];
1959 if (reg1 != NULL && reg2 != NULL
1960 && ORIGINAL_REGNO (reg1) == ORIGINAL_REGNO (reg2))
1961 return false;
1963 /* We don't keep live ranges for caps because they can be quite big.
1964 Use ranges of non-cap allocno from which caps are created. */
1965 a1 = get_cap_member (a1);
1966 a2 = get_cap_member (a2);
1967 for (i = 0; i < n1; i++)
1969 ira_object_t c1 = ALLOCNO_OBJECT (a1, i);
1971 for (j = 0; j < n2; j++)
1973 ira_object_t c2 = ALLOCNO_OBJECT (a2, j);
1975 if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1),
1976 OBJECT_LIVE_RANGES (c2)))
1977 return true;
1980 return false;
1983 /* The function is used to sort copies according to their execution
1984 frequencies. */
1985 static int
1986 copy_freq_compare_func (const void *v1p, const void *v2p)
1988 ira_copy_t cp1 = *(const ira_copy_t *) v1p, cp2 = *(const ira_copy_t *) v2p;
1989 int pri1, pri2;
1991 pri1 = cp1->freq;
1992 pri2 = cp2->freq;
1993 if (pri2 - pri1)
1994 return pri2 - pri1;
1996 /* If frequencies are equal, sort by copies, so that the results of
1997 qsort leave nothing to chance. */
1998 return cp1->num - cp2->num;
2003 /* Return true if any allocno from thread of A1 conflicts with any
2004 allocno from thread A2. */
2005 static bool
2006 allocno_thread_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
2008 ira_allocno_t a, conflict_a;
2010 for (a = ALLOCNO_COLOR_DATA (a2)->next_thread_allocno;;
2011 a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2013 for (conflict_a = ALLOCNO_COLOR_DATA (a1)->next_thread_allocno;;
2014 conflict_a = ALLOCNO_COLOR_DATA (conflict_a)->next_thread_allocno)
2016 if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
2017 return true;
2018 if (conflict_a == a1)
2019 break;
2021 if (a == a2)
2022 break;
2024 return false;
2027 /* Merge two threads given correspondingly by their first allocnos T1
2028 and T2 (more accurately merging T2 into T1). */
2029 static void
2030 merge_threads (ira_allocno_t t1, ira_allocno_t t2)
2032 ira_allocno_t a, next, last;
2034 gcc_assert (t1 != t2
2035 && ALLOCNO_COLOR_DATA (t1)->first_thread_allocno == t1
2036 && ALLOCNO_COLOR_DATA (t2)->first_thread_allocno == t2);
2037 for (last = t2, a = ALLOCNO_COLOR_DATA (t2)->next_thread_allocno;;
2038 a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2040 ALLOCNO_COLOR_DATA (a)->first_thread_allocno = t1;
2041 if (a == t2)
2042 break;
2043 last = a;
2045 next = ALLOCNO_COLOR_DATA (t1)->next_thread_allocno;
2046 ALLOCNO_COLOR_DATA (t1)->next_thread_allocno = t2;
2047 ALLOCNO_COLOR_DATA (last)->next_thread_allocno = next;
2048 ALLOCNO_COLOR_DATA (t1)->thread_freq += ALLOCNO_COLOR_DATA (t2)->thread_freq;
2051 /* Create threads by processing CP_NUM copies from sorted copies. We
2052 process the most expensive copies first. */
2053 static void
2054 form_threads_from_copies (int cp_num)
2056 ira_allocno_t a, thread1, thread2;
2057 ira_copy_t cp;
2058 int i, n;
2060 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
2061 /* Form threads processing copies, most frequently executed
2062 first. */
2063 for (; cp_num != 0;)
2065 for (i = 0; i < cp_num; i++)
2067 cp = sorted_copies[i];
2068 thread1 = ALLOCNO_COLOR_DATA (cp->first)->first_thread_allocno;
2069 thread2 = ALLOCNO_COLOR_DATA (cp->second)->first_thread_allocno;
2070 if (thread1 == thread2)
2071 continue;
2072 if (! allocno_thread_conflict_p (thread1, thread2))
2074 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2075 fprintf
2076 (ira_dump_file,
2077 " Forming thread by copy %d:a%dr%d-a%dr%d (freq=%d):\n",
2078 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
2079 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
2080 cp->freq);
2081 merge_threads (thread1, thread2);
2082 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2084 thread1 = ALLOCNO_COLOR_DATA (thread1)->first_thread_allocno;
2085 fprintf (ira_dump_file, " Result (freq=%d): a%dr%d(%d)",
2086 ALLOCNO_COLOR_DATA (thread1)->thread_freq,
2087 ALLOCNO_NUM (thread1), ALLOCNO_REGNO (thread1),
2088 ALLOCNO_FREQ (thread1));
2089 for (a = ALLOCNO_COLOR_DATA (thread1)->next_thread_allocno;
2090 a != thread1;
2091 a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2092 fprintf (ira_dump_file, " a%dr%d(%d)",
2093 ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2094 ALLOCNO_FREQ (a));
2095 fprintf (ira_dump_file, "\n");
2097 i++;
2098 break;
2101 /* Collect the rest of copies. */
2102 for (n = 0; i < cp_num; i++)
2104 cp = sorted_copies[i];
2105 if (ALLOCNO_COLOR_DATA (cp->first)->first_thread_allocno
2106 != ALLOCNO_COLOR_DATA (cp->second)->first_thread_allocno)
2107 sorted_copies[n++] = cp;
2109 cp_num = n;
2113 /* Create threads by processing copies of all alocnos from BUCKET. We
2114 process the most expensive copies first. */
2115 static void
2116 form_threads_from_bucket (ira_allocno_t bucket)
2118 ira_allocno_t a;
2119 ira_copy_t cp, next_cp;
2120 int cp_num = 0;
2122 for (a = bucket; a != NULL; a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2124 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2126 if (cp->first == a)
2128 next_cp = cp->next_first_allocno_copy;
2129 sorted_copies[cp_num++] = cp;
2131 else if (cp->second == a)
2132 next_cp = cp->next_second_allocno_copy;
2133 else
2134 gcc_unreachable ();
2137 form_threads_from_copies (cp_num);
2140 /* Create threads by processing copies of colorable allocno A. We
2141 process most expensive copies first. */
2142 static void
2143 form_threads_from_colorable_allocno (ira_allocno_t a)
2145 ira_allocno_t another_a;
2146 ira_copy_t cp, next_cp;
2147 int cp_num = 0;
2149 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2151 if (cp->first == a)
2153 next_cp = cp->next_first_allocno_copy;
2154 another_a = cp->second;
2156 else if (cp->second == a)
2158 next_cp = cp->next_second_allocno_copy;
2159 another_a = cp->first;
2161 else
2162 gcc_unreachable ();
2163 if ((! ALLOCNO_COLOR_DATA (another_a)->in_graph_p
2164 && !ALLOCNO_COLOR_DATA (another_a)->may_be_spilled_p)
2165 || ALLOCNO_COLOR_DATA (another_a)->colorable_p)
2166 sorted_copies[cp_num++] = cp;
2168 form_threads_from_copies (cp_num);
2171 /* Form initial threads which contain only one allocno. */
2172 static void
2173 init_allocno_threads (void)
2175 ira_allocno_t a;
2176 unsigned int j;
2177 bitmap_iterator bi;
2178 ira_pref_t pref;
2180 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2182 a = ira_allocnos[j];
2183 /* Set up initial thread data: */
2184 ALLOCNO_COLOR_DATA (a)->first_thread_allocno
2185 = ALLOCNO_COLOR_DATA (a)->next_thread_allocno = a;
2186 ALLOCNO_COLOR_DATA (a)->thread_freq = ALLOCNO_FREQ (a);
2187 ALLOCNO_COLOR_DATA (a)->hard_reg_prefs = 0;
2188 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
2189 ALLOCNO_COLOR_DATA (a)->hard_reg_prefs += pref->freq;
2195 /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
2197 /* Bucket of allocnos that can colored currently without spilling. */
2198 static ira_allocno_t colorable_allocno_bucket;
2200 /* Bucket of allocnos that might be not colored currently without
2201 spilling. */
2202 static ira_allocno_t uncolorable_allocno_bucket;
2204 /* The current number of allocnos in the uncolorable_bucket. */
2205 static int uncolorable_allocnos_num;
2207 /* Return the current spill priority of allocno A. The less the
2208 number, the more preferable the allocno for spilling. */
2209 static inline int
2210 allocno_spill_priority (ira_allocno_t a)
2212 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
2214 return (data->temp
2215 / (ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
2216 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
2217 + 1));
2220 /* Add allocno A to bucket *BUCKET_PTR. A should be not in a bucket
2221 before the call. */
2222 static void
2223 add_allocno_to_bucket (ira_allocno_t a, ira_allocno_t *bucket_ptr)
2225 ira_allocno_t first_a;
2226 allocno_color_data_t data;
2228 if (bucket_ptr == &uncolorable_allocno_bucket
2229 && ALLOCNO_CLASS (a) != NO_REGS)
2231 uncolorable_allocnos_num++;
2232 ira_assert (uncolorable_allocnos_num > 0);
2234 first_a = *bucket_ptr;
2235 data = ALLOCNO_COLOR_DATA (a);
2236 data->next_bucket_allocno = first_a;
2237 data->prev_bucket_allocno = NULL;
2238 if (first_a != NULL)
2239 ALLOCNO_COLOR_DATA (first_a)->prev_bucket_allocno = a;
2240 *bucket_ptr = a;
2243 /* Compare two allocnos to define which allocno should be pushed first
2244 into the coloring stack. If the return is a negative number, the
2245 allocno given by the first parameter will be pushed first. In this
2246 case such allocno has less priority than the second one and the
2247 hard register will be assigned to it after assignment to the second
2248 one. As the result of such assignment order, the second allocno
2249 has a better chance to get the best hard register. */
2250 static int
2251 bucket_allocno_compare_func (const void *v1p, const void *v2p)
2253 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2254 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2255 int diff, freq1, freq2, a1_num, a2_num, pref1, pref2;
2256 ira_allocno_t t1 = ALLOCNO_COLOR_DATA (a1)->first_thread_allocno;
2257 ira_allocno_t t2 = ALLOCNO_COLOR_DATA (a2)->first_thread_allocno;
2258 int cl1 = ALLOCNO_CLASS (a1), cl2 = ALLOCNO_CLASS (a2);
2260 /* Push allocnos with minimal hard_reg_prefs first. */
2261 pref1 = ALLOCNO_COLOR_DATA (a1)->hard_reg_prefs;
2262 pref2 = ALLOCNO_COLOR_DATA (a2)->hard_reg_prefs;
2263 if ((diff = pref1 - pref2) != 0)
2264 return diff;
2265 /* Push allocnos with minimal conflict_allocno_hard_prefs first. */
2266 pref1 = ALLOCNO_COLOR_DATA (a1)->conflict_allocno_hard_prefs;
2267 pref2 = ALLOCNO_COLOR_DATA (a2)->conflict_allocno_hard_prefs;
2268 if ((diff = pref1 - pref2) != 0)
2269 return diff;
2270 freq1 = ALLOCNO_COLOR_DATA (t1)->thread_freq;
2271 freq2 = ALLOCNO_COLOR_DATA (t2)->thread_freq;
2272 if ((diff = freq1 - freq2) != 0)
2273 return diff;
2275 if ((diff = ALLOCNO_NUM (t2) - ALLOCNO_NUM (t1)) != 0)
2276 return diff;
2278 /* Push pseudos requiring less hard registers first. It means that
2279 we will assign pseudos requiring more hard registers first
2280 avoiding creation small holes in free hard register file into
2281 which the pseudos requiring more hard registers cannot fit. */
2282 if ((diff = (ira_reg_class_max_nregs[cl1][ALLOCNO_MODE (a1)]
2283 - ira_reg_class_max_nregs[cl2][ALLOCNO_MODE (a2)])) != 0)
2284 return diff;
2286 freq1 = ALLOCNO_FREQ (a1);
2287 freq2 = ALLOCNO_FREQ (a2);
2288 if ((diff = freq1 - freq2) != 0)
2289 return diff;
2291 a1_num = ALLOCNO_COLOR_DATA (a1)->available_regs_num;
2292 a2_num = ALLOCNO_COLOR_DATA (a2)->available_regs_num;
2293 if ((diff = a2_num - a1_num) != 0)
2294 return diff;
2295 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
2298 /* Sort bucket *BUCKET_PTR and return the result through
2299 BUCKET_PTR. */
2300 static void
2301 sort_bucket (ira_allocno_t *bucket_ptr,
2302 int (*compare_func) (const void *, const void *))
2304 ira_allocno_t a, head;
2305 int n;
2307 for (n = 0, a = *bucket_ptr;
2308 a != NULL;
2309 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2310 sorted_allocnos[n++] = a;
2311 if (n <= 1)
2312 return;
2313 qsort (sorted_allocnos, n, sizeof (ira_allocno_t), compare_func);
2314 head = NULL;
2315 for (n--; n >= 0; n--)
2317 a = sorted_allocnos[n];
2318 ALLOCNO_COLOR_DATA (a)->next_bucket_allocno = head;
2319 ALLOCNO_COLOR_DATA (a)->prev_bucket_allocno = NULL;
2320 if (head != NULL)
2321 ALLOCNO_COLOR_DATA (head)->prev_bucket_allocno = a;
2322 head = a;
2324 *bucket_ptr = head;
2327 /* Add ALLOCNO to colorable bucket maintaining the order according
2328 their priority. ALLOCNO should be not in a bucket before the
2329 call. */
2330 static void
2331 add_allocno_to_ordered_colorable_bucket (ira_allocno_t allocno)
2333 ira_allocno_t before, after;
2335 form_threads_from_colorable_allocno (allocno);
2336 for (before = colorable_allocno_bucket, after = NULL;
2337 before != NULL;
2338 after = before,
2339 before = ALLOCNO_COLOR_DATA (before)->next_bucket_allocno)
2340 if (bucket_allocno_compare_func (&allocno, &before) < 0)
2341 break;
2342 ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno = before;
2343 ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno = after;
2344 if (after == NULL)
2345 colorable_allocno_bucket = allocno;
2346 else
2347 ALLOCNO_COLOR_DATA (after)->next_bucket_allocno = allocno;
2348 if (before != NULL)
2349 ALLOCNO_COLOR_DATA (before)->prev_bucket_allocno = allocno;
2352 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
2353 the call. */
2354 static void
2355 delete_allocno_from_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
2357 ira_allocno_t prev_allocno, next_allocno;
2359 if (bucket_ptr == &uncolorable_allocno_bucket
2360 && ALLOCNO_CLASS (allocno) != NO_REGS)
2362 uncolorable_allocnos_num--;
2363 ira_assert (uncolorable_allocnos_num >= 0);
2365 prev_allocno = ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno;
2366 next_allocno = ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno;
2367 if (prev_allocno != NULL)
2368 ALLOCNO_COLOR_DATA (prev_allocno)->next_bucket_allocno = next_allocno;
2369 else
2371 ira_assert (*bucket_ptr == allocno);
2372 *bucket_ptr = next_allocno;
2374 if (next_allocno != NULL)
2375 ALLOCNO_COLOR_DATA (next_allocno)->prev_bucket_allocno = prev_allocno;
2378 /* Put allocno A onto the coloring stack without removing it from its
2379 bucket. Pushing allocno to the coloring stack can result in moving
2380 conflicting allocnos from the uncolorable bucket to the colorable
2381 one. Update conflict_allocno_hard_prefs of the conflicting
2382 allocnos which are not on stack yet. */
2383 static void
2384 push_allocno_to_stack (ira_allocno_t a)
2386 enum reg_class aclass;
2387 allocno_color_data_t data, conflict_data;
2388 int size, i, n = ALLOCNO_NUM_OBJECTS (a);
2390 data = ALLOCNO_COLOR_DATA (a);
2391 data->in_graph_p = false;
2392 allocno_stack_vec.safe_push (a);
2393 aclass = ALLOCNO_CLASS (a);
2394 if (aclass == NO_REGS)
2395 return;
2396 size = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
2397 if (n > 1)
2399 /* We will deal with the subwords individually. */
2400 gcc_assert (size == ALLOCNO_NUM_OBJECTS (a));
2401 size = 1;
2403 for (i = 0; i < n; i++)
2405 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2406 ira_object_t conflict_obj;
2407 ira_object_conflict_iterator oci;
2409 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2411 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2412 ira_pref_t pref;
2414 conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
2415 if (! conflict_data->in_graph_p
2416 || ALLOCNO_ASSIGNED_P (conflict_a)
2417 || !(hard_reg_set_intersect_p
2418 (ALLOCNO_COLOR_DATA (a)->profitable_hard_regs,
2419 conflict_data->profitable_hard_regs)))
2420 continue;
2421 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
2422 conflict_data->conflict_allocno_hard_prefs -= pref->freq;
2423 if (conflict_data->colorable_p)
2424 continue;
2425 ira_assert (bitmap_bit_p (coloring_allocno_bitmap,
2426 ALLOCNO_NUM (conflict_a)));
2427 if (update_left_conflict_sizes_p (conflict_a, a, size))
2429 delete_allocno_from_bucket
2430 (conflict_a, &uncolorable_allocno_bucket);
2431 add_allocno_to_ordered_colorable_bucket (conflict_a);
2432 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2434 fprintf (ira_dump_file, " Making");
2435 ira_print_expanded_allocno (conflict_a);
2436 fprintf (ira_dump_file, " colorable\n");
2444 /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
2445 The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
2446 static void
2447 remove_allocno_from_bucket_and_push (ira_allocno_t allocno, bool colorable_p)
2449 if (colorable_p)
2450 delete_allocno_from_bucket (allocno, &colorable_allocno_bucket);
2451 else
2452 delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
2453 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2455 fprintf (ira_dump_file, " Pushing");
2456 ira_print_expanded_allocno (allocno);
2457 if (colorable_p)
2458 fprintf (ira_dump_file, "(cost %d)\n",
2459 ALLOCNO_COLOR_DATA (allocno)->temp);
2460 else
2461 fprintf (ira_dump_file, "(potential spill: %spri=%d, cost=%d)\n",
2462 ALLOCNO_BAD_SPILL_P (allocno) ? "bad spill, " : "",
2463 allocno_spill_priority (allocno),
2464 ALLOCNO_COLOR_DATA (allocno)->temp);
2466 if (! colorable_p)
2467 ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p = true;
2468 push_allocno_to_stack (allocno);
2471 /* Put all allocnos from colorable bucket onto the coloring stack. */
2472 static void
2473 push_only_colorable (void)
2475 form_threads_from_bucket (colorable_allocno_bucket);
2476 sort_bucket (&colorable_allocno_bucket, bucket_allocno_compare_func);
2477 for (;colorable_allocno_bucket != NULL;)
2478 remove_allocno_from_bucket_and_push (colorable_allocno_bucket, true);
2481 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
2482 loop given by its LOOP_NODE. */
2484 ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
2486 int freq, i;
2487 edge_iterator ei;
2488 edge e;
2489 vec<edge> edges;
2491 ira_assert (current_loops != NULL && loop_node->loop != NULL
2492 && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
2493 freq = 0;
2494 if (! exit_p)
2496 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
2497 if (e->src != loop_node->loop->latch
2498 && (regno < 0
2499 || (bitmap_bit_p (df_get_live_out (e->src), regno)
2500 && bitmap_bit_p (df_get_live_in (e->dest), regno))))
2501 freq += EDGE_FREQUENCY (e);
2503 else
2505 edges = get_loop_exit_edges (loop_node->loop);
2506 FOR_EACH_VEC_ELT (edges, i, e)
2507 if (regno < 0
2508 || (bitmap_bit_p (df_get_live_out (e->src), regno)
2509 && bitmap_bit_p (df_get_live_in (e->dest), regno)))
2510 freq += EDGE_FREQUENCY (e);
2511 edges.release ();
2514 return REG_FREQ_FROM_EDGE_FREQ (freq);
2517 /* Calculate and return the cost of putting allocno A into memory. */
2518 static int
2519 calculate_allocno_spill_cost (ira_allocno_t a)
2521 int regno, cost;
2522 machine_mode mode;
2523 enum reg_class rclass;
2524 ira_allocno_t parent_allocno;
2525 ira_loop_tree_node_t parent_node, loop_node;
2527 regno = ALLOCNO_REGNO (a);
2528 cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_UPDATED_CLASS_COST (a);
2529 if (ALLOCNO_CAP (a) != NULL)
2530 return cost;
2531 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
2532 if ((parent_node = loop_node->parent) == NULL)
2533 return cost;
2534 if ((parent_allocno = parent_node->regno_allocno_map[regno]) == NULL)
2535 return cost;
2536 mode = ALLOCNO_MODE (a);
2537 rclass = ALLOCNO_CLASS (a);
2538 if (ALLOCNO_HARD_REGNO (parent_allocno) < 0)
2539 cost -= (ira_memory_move_cost[mode][rclass][0]
2540 * ira_loop_edge_freq (loop_node, regno, true)
2541 + ira_memory_move_cost[mode][rclass][1]
2542 * ira_loop_edge_freq (loop_node, regno, false));
2543 else
2545 ira_init_register_move_cost_if_necessary (mode);
2546 cost += ((ira_memory_move_cost[mode][rclass][1]
2547 * ira_loop_edge_freq (loop_node, regno, true)
2548 + ira_memory_move_cost[mode][rclass][0]
2549 * ira_loop_edge_freq (loop_node, regno, false))
2550 - (ira_register_move_cost[mode][rclass][rclass]
2551 * (ira_loop_edge_freq (loop_node, regno, false)
2552 + ira_loop_edge_freq (loop_node, regno, true))));
2554 return cost;
2557 /* Used for sorting allocnos for spilling. */
2558 static inline int
2559 allocno_spill_priority_compare (ira_allocno_t a1, ira_allocno_t a2)
2561 int pri1, pri2, diff;
2563 /* Avoid spilling static chain pointer pseudo when non-local goto is
2564 used. */
2565 if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))
2566 return 1;
2567 else if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2)))
2568 return -1;
2569 if (ALLOCNO_BAD_SPILL_P (a1) && ! ALLOCNO_BAD_SPILL_P (a2))
2570 return 1;
2571 if (ALLOCNO_BAD_SPILL_P (a2) && ! ALLOCNO_BAD_SPILL_P (a1))
2572 return -1;
2573 pri1 = allocno_spill_priority (a1);
2574 pri2 = allocno_spill_priority (a2);
2575 if ((diff = pri1 - pri2) != 0)
2576 return diff;
2577 if ((diff
2578 = ALLOCNO_COLOR_DATA (a1)->temp - ALLOCNO_COLOR_DATA (a2)->temp) != 0)
2579 return diff;
2580 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2583 /* Used for sorting allocnos for spilling. */
2584 static int
2585 allocno_spill_sort_compare (const void *v1p, const void *v2p)
2587 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2588 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2590 return allocno_spill_priority_compare (p1, p2);
2593 /* Push allocnos to the coloring stack. The order of allocnos in the
2594 stack defines the order for the subsequent coloring. */
2595 static void
2596 push_allocnos_to_stack (void)
2598 ira_allocno_t a;
2599 int cost;
2601 /* Calculate uncolorable allocno spill costs. */
2602 for (a = uncolorable_allocno_bucket;
2603 a != NULL;
2604 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2605 if (ALLOCNO_CLASS (a) != NO_REGS)
2607 cost = calculate_allocno_spill_cost (a);
2608 /* ??? Remove cost of copies between the coalesced
2609 allocnos. */
2610 ALLOCNO_COLOR_DATA (a)->temp = cost;
2612 sort_bucket (&uncolorable_allocno_bucket, allocno_spill_sort_compare);
2613 for (;;)
2615 push_only_colorable ();
2616 a = uncolorable_allocno_bucket;
2617 if (a == NULL)
2618 break;
2619 remove_allocno_from_bucket_and_push (a, false);
2621 ira_assert (colorable_allocno_bucket == NULL
2622 && uncolorable_allocno_bucket == NULL);
2623 ira_assert (uncolorable_allocnos_num == 0);
2626 /* Pop the coloring stack and assign hard registers to the popped
2627 allocnos. */
2628 static void
2629 pop_allocnos_from_stack (void)
2631 ira_allocno_t allocno;
2632 enum reg_class aclass;
2634 for (;allocno_stack_vec.length () != 0;)
2636 allocno = allocno_stack_vec.pop ();
2637 aclass = ALLOCNO_CLASS (allocno);
2638 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2640 fprintf (ira_dump_file, " Popping");
2641 ira_print_expanded_allocno (allocno);
2642 fprintf (ira_dump_file, " -- ");
2644 if (aclass == NO_REGS)
2646 ALLOCNO_HARD_REGNO (allocno) = -1;
2647 ALLOCNO_ASSIGNED_P (allocno) = true;
2648 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
2649 ira_assert
2650 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
2651 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2652 fprintf (ira_dump_file, "assign memory\n");
2654 else if (assign_hard_reg (allocno, false))
2656 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2657 fprintf (ira_dump_file, "assign reg %d\n",
2658 ALLOCNO_HARD_REGNO (allocno));
2660 else if (ALLOCNO_ASSIGNED_P (allocno))
2662 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2663 fprintf (ira_dump_file, "spill%s\n",
2664 ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p
2665 ? "" : "!");
2667 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2671 /* Set up number of available hard registers for allocno A. */
2672 static void
2673 setup_allocno_available_regs_num (ira_allocno_t a)
2675 int i, n, hard_regno, hard_regs_num, nwords;
2676 enum reg_class aclass;
2677 allocno_color_data_t data;
2679 aclass = ALLOCNO_CLASS (a);
2680 data = ALLOCNO_COLOR_DATA (a);
2681 data->available_regs_num = 0;
2682 if (aclass == NO_REGS)
2683 return;
2684 hard_regs_num = ira_class_hard_regs_num[aclass];
2685 nwords = ALLOCNO_NUM_OBJECTS (a);
2686 for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
2688 hard_regno = ira_class_hard_regs[aclass][i];
2689 /* Checking only profitable hard regs. */
2690 if (TEST_HARD_REG_BIT (data->profitable_hard_regs, hard_regno))
2691 n++;
2693 data->available_regs_num = n;
2694 if (internal_flag_ira_verbose <= 2 || ira_dump_file == NULL)
2695 return;
2696 fprintf
2697 (ira_dump_file,
2698 " Allocno a%dr%d of %s(%d) has %d avail. regs ",
2699 ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2700 reg_class_names[aclass], ira_class_hard_regs_num[aclass], n);
2701 print_hard_reg_set (ira_dump_file, data->profitable_hard_regs, false);
2702 fprintf (ira_dump_file, ", %snode: ",
2703 data->profitable_hard_regs == data->hard_regs_node->hard_regs->set
2704 ? "" : "^");
2705 print_hard_reg_set (ira_dump_file,
2706 data->hard_regs_node->hard_regs->set, false);
2707 for (i = 0; i < nwords; i++)
2709 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2711 if (nwords != 1)
2713 if (i != 0)
2714 fprintf (ira_dump_file, ", ");
2715 fprintf (ira_dump_file, " obj %d", i);
2717 fprintf (ira_dump_file, " (confl regs = ");
2718 print_hard_reg_set (ira_dump_file, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2719 false);
2720 fprintf (ira_dump_file, ")");
2722 fprintf (ira_dump_file, "\n");
2725 /* Put ALLOCNO in a bucket corresponding to its number and size of its
2726 conflicting allocnos and hard registers. */
2727 static void
2728 put_allocno_into_bucket (ira_allocno_t allocno)
2730 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2731 setup_allocno_available_regs_num (allocno);
2732 if (setup_left_conflict_sizes_p (allocno))
2733 add_allocno_to_bucket (allocno, &colorable_allocno_bucket);
2734 else
2735 add_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
2738 /* Map: allocno number -> allocno priority. */
2739 static int *allocno_priorities;
2741 /* Set up priorities for N allocnos in array
2742 CONSIDERATION_ALLOCNOS. */
2743 static void
2744 setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
2746 int i, length, nrefs, priority, max_priority, mult;
2747 ira_allocno_t a;
2749 max_priority = 0;
2750 for (i = 0; i < n; i++)
2752 a = consideration_allocnos[i];
2753 nrefs = ALLOCNO_NREFS (a);
2754 ira_assert (nrefs >= 0);
2755 mult = floor_log2 (ALLOCNO_NREFS (a)) + 1;
2756 ira_assert (mult >= 0);
2757 allocno_priorities[ALLOCNO_NUM (a)]
2758 = priority
2759 = (mult
2760 * (ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a))
2761 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
2762 if (priority < 0)
2763 priority = -priority;
2764 if (max_priority < priority)
2765 max_priority = priority;
2767 mult = max_priority == 0 ? 1 : INT_MAX / max_priority;
2768 for (i = 0; i < n; i++)
2770 a = consideration_allocnos[i];
2771 length = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2772 if (ALLOCNO_NUM_OBJECTS (a) > 1)
2773 length /= ALLOCNO_NUM_OBJECTS (a);
2774 if (length <= 0)
2775 length = 1;
2776 allocno_priorities[ALLOCNO_NUM (a)]
2777 = allocno_priorities[ALLOCNO_NUM (a)] * mult / length;
2781 /* Sort allocnos according to the profit of usage of a hard register
2782 instead of memory for them. */
2783 static int
2784 allocno_cost_compare_func (const void *v1p, const void *v2p)
2786 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2787 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2788 int c1, c2;
2790 c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_UPDATED_CLASS_COST (p1);
2791 c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_UPDATED_CLASS_COST (p2);
2792 if (c1 - c2)
2793 return c1 - c2;
2795 /* If regs are equally good, sort by allocno numbers, so that the
2796 results of qsort leave nothing to chance. */
2797 return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
2800 /* Return savings on removed copies when ALLOCNO is assigned to
2801 HARD_REGNO. */
2802 static int
2803 allocno_copy_cost_saving (ira_allocno_t allocno, int hard_regno)
2805 int cost = 0;
2806 machine_mode allocno_mode = ALLOCNO_MODE (allocno);
2807 enum reg_class rclass;
2808 ira_copy_t cp, next_cp;
2810 rclass = REGNO_REG_CLASS (hard_regno);
2811 if (ira_reg_class_max_nregs[rclass][allocno_mode]
2812 > ira_class_hard_regs_num[rclass])
2813 /* For the above condition the cost can be wrong. Use the allocno
2814 class in this case. */
2815 rclass = ALLOCNO_CLASS (allocno);
2816 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
2818 if (cp->first == allocno)
2820 next_cp = cp->next_first_allocno_copy;
2821 if (ALLOCNO_HARD_REGNO (cp->second) != hard_regno)
2822 continue;
2824 else if (cp->second == allocno)
2826 next_cp = cp->next_second_allocno_copy;
2827 if (ALLOCNO_HARD_REGNO (cp->first) != hard_regno)
2828 continue;
2830 else
2831 gcc_unreachable ();
2832 ira_init_register_move_cost_if_necessary (allocno_mode);
2833 cost += cp->freq * ira_register_move_cost[allocno_mode][rclass][rclass];
2835 return cost;
2838 /* We used Chaitin-Briggs coloring to assign as many pseudos as
2839 possible to hard registers. Let us try to improve allocation with
2840 cost point of view. This function improves the allocation by
2841 spilling some allocnos and assigning the freed hard registers to
2842 other allocnos if it decreases the overall allocation cost. */
2843 static void
2844 improve_allocation (void)
2846 unsigned int i;
2847 int j, k, n, hregno, conflict_hregno, base_cost, class_size, word, nwords;
2848 int check, spill_cost, min_cost, nregs, conflict_nregs, r, best;
2849 bool try_p;
2850 enum reg_class aclass;
2851 machine_mode mode;
2852 int *allocno_costs;
2853 int costs[FIRST_PSEUDO_REGISTER];
2854 HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
2855 ira_allocno_t a;
2856 bitmap_iterator bi;
2858 /* Don't bother to optimize the code with static chain pointer and
2859 non-local goto in order not to spill the chain pointer
2860 pseudo. */
2861 if (cfun->static_chain_decl && crtl->has_nonlocal_goto)
2862 return;
2863 /* Clear counts used to process conflicting allocnos only once for
2864 each allocno. */
2865 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2866 ALLOCNO_COLOR_DATA (ira_allocnos[i])->temp = 0;
2867 check = n = 0;
2868 /* Process each allocno and try to assign a hard register to it by
2869 spilling some its conflicting allocnos. */
2870 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2872 a = ira_allocnos[i];
2873 ALLOCNO_COLOR_DATA (a)->temp = 0;
2874 if (empty_profitable_hard_regs (a))
2875 continue;
2876 check++;
2877 aclass = ALLOCNO_CLASS (a);
2878 allocno_costs = ALLOCNO_HARD_REG_COSTS (a);
2879 if ((hregno = ALLOCNO_HARD_REGNO (a)) < 0)
2880 base_cost = ALLOCNO_UPDATED_MEMORY_COST (a);
2881 else if (allocno_costs == NULL)
2882 /* It means that assigning a hard register is not profitable
2883 (we don't waste memory for hard register costs in this
2884 case). */
2885 continue;
2886 else
2887 base_cost = (allocno_costs[ira_class_hard_reg_index[aclass][hregno]]
2888 - allocno_copy_cost_saving (a, hregno));
2889 try_p = false;
2890 get_conflict_and_start_profitable_regs (a, false,
2891 conflicting_regs,
2892 &profitable_hard_regs);
2893 class_size = ira_class_hard_regs_num[aclass];
2894 /* Set up cost improvement for usage of each profitable hard
2895 register for allocno A. */
2896 for (j = 0; j < class_size; j++)
2898 hregno = ira_class_hard_regs[aclass][j];
2899 if (! check_hard_reg_p (a, hregno,
2900 conflicting_regs, profitable_hard_regs))
2901 continue;
2902 ira_assert (ira_class_hard_reg_index[aclass][hregno] == j);
2903 k = allocno_costs == NULL ? 0 : j;
2904 costs[hregno] = (allocno_costs == NULL
2905 ? ALLOCNO_UPDATED_CLASS_COST (a) : allocno_costs[k]);
2906 costs[hregno] -= allocno_copy_cost_saving (a, hregno);
2907 costs[hregno] -= base_cost;
2908 if (costs[hregno] < 0)
2909 try_p = true;
2911 if (! try_p)
2912 /* There is no chance to improve the allocation cost by
2913 assigning hard register to allocno A even without spilling
2914 conflicting allocnos. */
2915 continue;
2916 mode = ALLOCNO_MODE (a);
2917 nwords = ALLOCNO_NUM_OBJECTS (a);
2918 /* Process each allocno conflicting with A and update the cost
2919 improvement for profitable hard registers of A. To use a
2920 hard register for A we need to spill some conflicting
2921 allocnos and that creates penalty for the cost
2922 improvement. */
2923 for (word = 0; word < nwords; word++)
2925 ira_object_t conflict_obj;
2926 ira_object_t obj = ALLOCNO_OBJECT (a, word);
2927 ira_object_conflict_iterator oci;
2929 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2931 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2933 if (ALLOCNO_COLOR_DATA (conflict_a)->temp == check)
2934 /* We already processed this conflicting allocno
2935 because we processed earlier another object of the
2936 conflicting allocno. */
2937 continue;
2938 ALLOCNO_COLOR_DATA (conflict_a)->temp = check;
2939 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
2940 continue;
2941 spill_cost = ALLOCNO_UPDATED_MEMORY_COST (conflict_a);
2942 k = (ira_class_hard_reg_index
2943 [ALLOCNO_CLASS (conflict_a)][conflict_hregno]);
2944 ira_assert (k >= 0);
2945 if ((allocno_costs = ALLOCNO_HARD_REG_COSTS (conflict_a))
2946 != NULL)
2947 spill_cost -= allocno_costs[k];
2948 else
2949 spill_cost -= ALLOCNO_UPDATED_CLASS_COST (conflict_a);
2950 spill_cost
2951 += allocno_copy_cost_saving (conflict_a, conflict_hregno);
2952 conflict_nregs = hard_regno_nregs (conflict_hregno,
2953 ALLOCNO_MODE (conflict_a));
2954 for (r = conflict_hregno;
2955 r >= 0 && (int) end_hard_regno (mode, r) > conflict_hregno;
2956 r--)
2957 if (check_hard_reg_p (a, r,
2958 conflicting_regs, profitable_hard_regs))
2959 costs[r] += spill_cost;
2960 for (r = conflict_hregno + 1;
2961 r < conflict_hregno + conflict_nregs;
2962 r++)
2963 if (check_hard_reg_p (a, r,
2964 conflicting_regs, profitable_hard_regs))
2965 costs[r] += spill_cost;
2968 min_cost = INT_MAX;
2969 best = -1;
2970 /* Now we choose hard register for A which results in highest
2971 allocation cost improvement. */
2972 for (j = 0; j < class_size; j++)
2974 hregno = ira_class_hard_regs[aclass][j];
2975 if (check_hard_reg_p (a, hregno,
2976 conflicting_regs, profitable_hard_regs)
2977 && min_cost > costs[hregno])
2979 best = hregno;
2980 min_cost = costs[hregno];
2983 if (min_cost >= 0)
2984 /* We are in a situation when assigning any hard register to A
2985 by spilling some conflicting allocnos does not improve the
2986 allocation cost. */
2987 continue;
2988 nregs = hard_regno_nregs (best, mode);
2989 /* Now spill conflicting allocnos which contain a hard register
2990 of A when we assign the best chosen hard register to it. */
2991 for (word = 0; word < nwords; word++)
2993 ira_object_t conflict_obj;
2994 ira_object_t obj = ALLOCNO_OBJECT (a, word);
2995 ira_object_conflict_iterator oci;
2997 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2999 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3001 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
3002 continue;
3003 conflict_nregs = hard_regno_nregs (conflict_hregno,
3004 ALLOCNO_MODE (conflict_a));
3005 if (best + nregs <= conflict_hregno
3006 || conflict_hregno + conflict_nregs <= best)
3007 /* No intersection. */
3008 continue;
3009 ALLOCNO_HARD_REGNO (conflict_a) = -1;
3010 sorted_allocnos[n++] = conflict_a;
3011 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3012 fprintf (ira_dump_file, "Spilling a%dr%d for a%dr%d\n",
3013 ALLOCNO_NUM (conflict_a), ALLOCNO_REGNO (conflict_a),
3014 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3017 /* Assign the best chosen hard register to A. */
3018 ALLOCNO_HARD_REGNO (a) = best;
3019 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3020 fprintf (ira_dump_file, "Assigning %d to a%dr%d\n",
3021 best, ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3023 if (n == 0)
3024 return;
3025 /* We spilled some allocnos to assign their hard registers to other
3026 allocnos. The spilled allocnos are now in array
3027 'sorted_allocnos'. There is still a possibility that some of the
3028 spilled allocnos can get hard registers. So let us try assign
3029 them hard registers again (just a reminder -- function
3030 'assign_hard_reg' assigns hard registers only if it is possible
3031 and profitable). We process the spilled allocnos with biggest
3032 benefit to get hard register first -- see function
3033 'allocno_cost_compare_func'. */
3034 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
3035 allocno_cost_compare_func);
3036 for (j = 0; j < n; j++)
3038 a = sorted_allocnos[j];
3039 ALLOCNO_ASSIGNED_P (a) = false;
3040 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3042 fprintf (ira_dump_file, " ");
3043 ira_print_expanded_allocno (a);
3044 fprintf (ira_dump_file, " -- ");
3046 if (assign_hard_reg (a, false))
3048 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3049 fprintf (ira_dump_file, "assign hard reg %d\n",
3050 ALLOCNO_HARD_REGNO (a));
3052 else
3054 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3055 fprintf (ira_dump_file, "assign memory\n");
3060 /* Sort allocnos according to their priorities. */
3061 static int
3062 allocno_priority_compare_func (const void *v1p, const void *v2p)
3064 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
3065 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
3066 int pri1, pri2, diff;
3068 /* Assign hard reg to static chain pointer pseudo first when
3069 non-local goto is used. */
3070 if ((diff = (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2))
3071 - non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))) != 0)
3072 return diff;
3073 pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
3074 pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
3075 if (pri2 != pri1)
3076 return SORTGT (pri2, pri1);
3078 /* If regs are equally good, sort by allocnos, so that the results of
3079 qsort leave nothing to chance. */
3080 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
3083 /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
3084 taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
3085 static void
3086 color_allocnos (void)
3088 unsigned int i, n;
3089 bitmap_iterator bi;
3090 ira_allocno_t a;
3092 setup_profitable_hard_regs ();
3093 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3095 allocno_color_data_t data;
3096 ira_pref_t pref, next_pref;
3098 a = ira_allocnos[i];
3099 data = ALLOCNO_COLOR_DATA (a);
3100 data->conflict_allocno_hard_prefs = 0;
3101 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = next_pref)
3103 next_pref = pref->next_pref;
3104 if (! ira_hard_reg_in_set_p (pref->hard_regno,
3105 ALLOCNO_MODE (a),
3106 data->profitable_hard_regs))
3107 ira_remove_pref (pref);
3111 if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
3113 n = 0;
3114 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3116 a = ira_allocnos[i];
3117 if (ALLOCNO_CLASS (a) == NO_REGS)
3119 ALLOCNO_HARD_REGNO (a) = -1;
3120 ALLOCNO_ASSIGNED_P (a) = true;
3121 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3122 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3123 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3125 fprintf (ira_dump_file, " Spill");
3126 ira_print_expanded_allocno (a);
3127 fprintf (ira_dump_file, "\n");
3129 continue;
3131 sorted_allocnos[n++] = a;
3133 if (n != 0)
3135 setup_allocno_priorities (sorted_allocnos, n);
3136 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
3137 allocno_priority_compare_func);
3138 for (i = 0; i < n; i++)
3140 a = sorted_allocnos[i];
3141 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3143 fprintf (ira_dump_file, " ");
3144 ira_print_expanded_allocno (a);
3145 fprintf (ira_dump_file, " -- ");
3147 if (assign_hard_reg (a, false))
3149 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3150 fprintf (ira_dump_file, "assign hard reg %d\n",
3151 ALLOCNO_HARD_REGNO (a));
3153 else
3155 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3156 fprintf (ira_dump_file, "assign memory\n");
3161 else
3163 form_allocno_hard_regs_nodes_forest ();
3164 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3165 print_hard_regs_forest (ira_dump_file);
3166 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3168 a = ira_allocnos[i];
3169 if (ALLOCNO_CLASS (a) != NO_REGS && ! empty_profitable_hard_regs (a))
3171 ALLOCNO_COLOR_DATA (a)->in_graph_p = true;
3172 update_costs_from_prefs (a);
3173 update_conflict_allocno_hard_prefs (a);
3175 else
3177 ALLOCNO_HARD_REGNO (a) = -1;
3178 ALLOCNO_ASSIGNED_P (a) = true;
3179 /* We don't need updated costs anymore. */
3180 ira_free_allocno_updated_costs (a);
3181 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3183 fprintf (ira_dump_file, " Spill");
3184 ira_print_expanded_allocno (a);
3185 fprintf (ira_dump_file, "\n");
3189 /* Put the allocnos into the corresponding buckets. */
3190 colorable_allocno_bucket = NULL;
3191 uncolorable_allocno_bucket = NULL;
3192 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3194 a = ira_allocnos[i];
3195 if (ALLOCNO_COLOR_DATA (a)->in_graph_p)
3196 put_allocno_into_bucket (a);
3198 push_allocnos_to_stack ();
3199 pop_allocnos_from_stack ();
3200 finish_allocno_hard_regs_nodes_forest ();
3202 improve_allocation ();
3207 /* Output information about the loop given by its LOOP_TREE_NODE. */
3208 static void
3209 print_loop_title (ira_loop_tree_node_t loop_tree_node)
3211 unsigned int j;
3212 bitmap_iterator bi;
3213 ira_loop_tree_node_t subloop_node, dest_loop_node;
3214 edge e;
3215 edge_iterator ei;
3217 if (loop_tree_node->parent == NULL)
3218 fprintf (ira_dump_file,
3219 "\n Loop 0 (parent -1, header bb%d, depth 0)\n bbs:",
3220 NUM_FIXED_BLOCKS);
3221 else
3223 ira_assert (current_loops != NULL && loop_tree_node->loop != NULL);
3224 fprintf (ira_dump_file,
3225 "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
3226 loop_tree_node->loop_num, loop_tree_node->parent->loop_num,
3227 loop_tree_node->loop->header->index,
3228 loop_depth (loop_tree_node->loop));
3230 for (subloop_node = loop_tree_node->children;
3231 subloop_node != NULL;
3232 subloop_node = subloop_node->next)
3233 if (subloop_node->bb != NULL)
3235 fprintf (ira_dump_file, " %d", subloop_node->bb->index);
3236 FOR_EACH_EDGE (e, ei, subloop_node->bb->succs)
3237 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
3238 && ((dest_loop_node = IRA_BB_NODE (e->dest)->parent)
3239 != loop_tree_node))
3240 fprintf (ira_dump_file, "(->%d:l%d)",
3241 e->dest->index, dest_loop_node->loop_num);
3243 fprintf (ira_dump_file, "\n all:");
3244 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
3245 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
3246 fprintf (ira_dump_file, "\n modified regnos:");
3247 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
3248 fprintf (ira_dump_file, " %d", j);
3249 fprintf (ira_dump_file, "\n border:");
3250 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
3251 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
3252 fprintf (ira_dump_file, "\n Pressure:");
3253 for (j = 0; (int) j < ira_pressure_classes_num; j++)
3255 enum reg_class pclass;
3257 pclass = ira_pressure_classes[j];
3258 if (loop_tree_node->reg_pressure[pclass] == 0)
3259 continue;
3260 fprintf (ira_dump_file, " %s=%d", reg_class_names[pclass],
3261 loop_tree_node->reg_pressure[pclass]);
3263 fprintf (ira_dump_file, "\n");
3266 /* Color the allocnos inside loop (in the extreme case it can be all
3267 of the function) given the corresponding LOOP_TREE_NODE. The
3268 function is called for each loop during top-down traverse of the
3269 loop tree. */
3270 static void
3271 color_pass (ira_loop_tree_node_t loop_tree_node)
3273 int regno, hard_regno, index = -1, n;
3274 int cost, exit_freq, enter_freq;
3275 unsigned int j;
3276 bitmap_iterator bi;
3277 machine_mode mode;
3278 enum reg_class rclass, aclass, pclass;
3279 ira_allocno_t a, subloop_allocno;
3280 ira_loop_tree_node_t subloop_node;
3282 ira_assert (loop_tree_node->bb == NULL);
3283 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3284 print_loop_title (loop_tree_node);
3286 bitmap_copy (coloring_allocno_bitmap, loop_tree_node->all_allocnos);
3287 bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
3288 n = 0;
3289 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3291 a = ira_allocnos[j];
3292 n++;
3293 if (! ALLOCNO_ASSIGNED_P (a))
3294 continue;
3295 bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
3297 allocno_color_data
3298 = (allocno_color_data_t) ira_allocate (sizeof (struct allocno_color_data)
3299 * n);
3300 memset (allocno_color_data, 0, sizeof (struct allocno_color_data) * n);
3301 curr_allocno_process = 0;
3302 n = 0;
3303 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3305 a = ira_allocnos[j];
3306 ALLOCNO_ADD_DATA (a) = allocno_color_data + n;
3307 n++;
3309 init_allocno_threads ();
3310 /* Color all mentioned allocnos including transparent ones. */
3311 color_allocnos ();
3312 /* Process caps. They are processed just once. */
3313 if (flag_ira_region == IRA_REGION_MIXED
3314 || flag_ira_region == IRA_REGION_ALL)
3315 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
3317 a = ira_allocnos[j];
3318 if (ALLOCNO_CAP_MEMBER (a) == NULL)
3319 continue;
3320 /* Remove from processing in the next loop. */
3321 bitmap_clear_bit (consideration_allocno_bitmap, j);
3322 rclass = ALLOCNO_CLASS (a);
3323 pclass = ira_pressure_class_translate[rclass];
3324 if (flag_ira_region == IRA_REGION_MIXED
3325 && (loop_tree_node->reg_pressure[pclass]
3326 <= ira_class_hard_regs_num[pclass]))
3328 mode = ALLOCNO_MODE (a);
3329 hard_regno = ALLOCNO_HARD_REGNO (a);
3330 if (hard_regno >= 0)
3332 index = ira_class_hard_reg_index[rclass][hard_regno];
3333 ira_assert (index >= 0);
3335 regno = ALLOCNO_REGNO (a);
3336 subloop_allocno = ALLOCNO_CAP_MEMBER (a);
3337 subloop_node = ALLOCNO_LOOP_TREE_NODE (subloop_allocno);
3338 ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno));
3339 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3340 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3341 if (hard_regno >= 0)
3342 update_costs_from_copies (subloop_allocno, true, true);
3343 /* We don't need updated costs anymore. */
3344 ira_free_allocno_updated_costs (subloop_allocno);
3347 /* Update costs of the corresponding allocnos (not caps) in the
3348 subloops. */
3349 for (subloop_node = loop_tree_node->subloops;
3350 subloop_node != NULL;
3351 subloop_node = subloop_node->subloop_next)
3353 ira_assert (subloop_node->bb == NULL);
3354 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3356 a = ira_allocnos[j];
3357 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3358 mode = ALLOCNO_MODE (a);
3359 rclass = ALLOCNO_CLASS (a);
3360 pclass = ira_pressure_class_translate[rclass];
3361 hard_regno = ALLOCNO_HARD_REGNO (a);
3362 /* Use hard register class here. ??? */
3363 if (hard_regno >= 0)
3365 index = ira_class_hard_reg_index[rclass][hard_regno];
3366 ira_assert (index >= 0);
3368 regno = ALLOCNO_REGNO (a);
3369 /* ??? conflict costs */
3370 subloop_allocno = subloop_node->regno_allocno_map[regno];
3371 if (subloop_allocno == NULL
3372 || ALLOCNO_CAP (subloop_allocno) != NULL)
3373 continue;
3374 ira_assert (ALLOCNO_CLASS (subloop_allocno) == rclass);
3375 ira_assert (bitmap_bit_p (subloop_node->all_allocnos,
3376 ALLOCNO_NUM (subloop_allocno)));
3377 if ((flag_ira_region == IRA_REGION_MIXED
3378 && (loop_tree_node->reg_pressure[pclass]
3379 <= ira_class_hard_regs_num[pclass]))
3380 || (pic_offset_table_rtx != NULL
3381 && regno == (int) REGNO (pic_offset_table_rtx))
3382 /* Avoid overlapped multi-registers. Moves between them
3383 might result in wrong code generation. */
3384 || (hard_regno >= 0
3385 && ira_reg_class_max_nregs[pclass][mode] > 1))
3387 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
3389 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3390 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3391 if (hard_regno >= 0)
3392 update_costs_from_copies (subloop_allocno, true, true);
3393 /* We don't need updated costs anymore. */
3394 ira_free_allocno_updated_costs (subloop_allocno);
3396 continue;
3398 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
3399 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
3400 ira_assert (regno < ira_reg_equiv_len);
3401 if (ira_equiv_no_lvalue_p (regno))
3403 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
3405 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3406 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3407 if (hard_regno >= 0)
3408 update_costs_from_copies (subloop_allocno, true, true);
3409 /* We don't need updated costs anymore. */
3410 ira_free_allocno_updated_costs (subloop_allocno);
3413 else if (hard_regno < 0)
3415 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
3416 -= ((ira_memory_move_cost[mode][rclass][1] * enter_freq)
3417 + (ira_memory_move_cost[mode][rclass][0] * exit_freq));
3419 else
3421 aclass = ALLOCNO_CLASS (subloop_allocno);
3422 ira_init_register_move_cost_if_necessary (mode);
3423 cost = (ira_register_move_cost[mode][rclass][rclass]
3424 * (exit_freq + enter_freq));
3425 ira_allocate_and_set_or_copy_costs
3426 (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno), aclass,
3427 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno),
3428 ALLOCNO_HARD_REG_COSTS (subloop_allocno));
3429 ira_allocate_and_set_or_copy_costs
3430 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno),
3431 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno));
3432 ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
3433 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
3434 -= cost;
3435 if (ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
3436 > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index])
3437 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
3438 = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index];
3439 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
3440 += (ira_memory_move_cost[mode][rclass][0] * enter_freq
3441 + ira_memory_move_cost[mode][rclass][1] * exit_freq);
3445 ira_free (allocno_color_data);
3446 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3448 a = ira_allocnos[j];
3449 ALLOCNO_ADD_DATA (a) = NULL;
3453 /* Initialize the common data for coloring and calls functions to do
3454 Chaitin-Briggs and regional coloring. */
3455 static void
3456 do_coloring (void)
3458 coloring_allocno_bitmap = ira_allocate_bitmap ();
3459 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3460 fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
3462 ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
3464 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3465 ira_print_disposition (ira_dump_file);
3467 ira_free_bitmap (coloring_allocno_bitmap);
3472 /* Move spill/restore code, which are to be generated in ira-emit.c,
3473 to less frequent points (if it is profitable) by reassigning some
3474 allocnos (in loop with subloops containing in another loop) to
3475 memory which results in longer live-range where the corresponding
3476 pseudo-registers will be in memory. */
3477 static void
3478 move_spill_restore (void)
3480 int cost, regno, hard_regno, hard_regno2, index;
3481 bool changed_p;
3482 int enter_freq, exit_freq;
3483 machine_mode mode;
3484 enum reg_class rclass;
3485 ira_allocno_t a, parent_allocno, subloop_allocno;
3486 ira_loop_tree_node_t parent, loop_node, subloop_node;
3487 ira_allocno_iterator ai;
3489 for (;;)
3491 changed_p = false;
3492 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3493 fprintf (ira_dump_file, "New iteration of spill/restore move\n");
3494 FOR_EACH_ALLOCNO (a, ai)
3496 regno = ALLOCNO_REGNO (a);
3497 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
3498 if (ALLOCNO_CAP_MEMBER (a) != NULL
3499 || ALLOCNO_CAP (a) != NULL
3500 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
3501 || loop_node->children == NULL
3502 /* don't do the optimization because it can create
3503 copies and the reload pass can spill the allocno set
3504 by copy although the allocno will not get memory
3505 slot. */
3506 || ira_equiv_no_lvalue_p (regno)
3507 || !bitmap_bit_p (loop_node->border_allocnos, ALLOCNO_NUM (a))
3508 /* Do not spill static chain pointer pseudo when
3509 non-local goto is used. */
3510 || non_spilled_static_chain_regno_p (regno))
3511 continue;
3512 mode = ALLOCNO_MODE (a);
3513 rclass = ALLOCNO_CLASS (a);
3514 index = ira_class_hard_reg_index[rclass][hard_regno];
3515 ira_assert (index >= 0);
3516 cost = (ALLOCNO_MEMORY_COST (a)
3517 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
3518 ? ALLOCNO_CLASS_COST (a)
3519 : ALLOCNO_HARD_REG_COSTS (a)[index]));
3520 ira_init_register_move_cost_if_necessary (mode);
3521 for (subloop_node = loop_node->subloops;
3522 subloop_node != NULL;
3523 subloop_node = subloop_node->subloop_next)
3525 ira_assert (subloop_node->bb == NULL);
3526 subloop_allocno = subloop_node->regno_allocno_map[regno];
3527 if (subloop_allocno == NULL)
3528 continue;
3529 ira_assert (rclass == ALLOCNO_CLASS (subloop_allocno));
3530 /* We have accumulated cost. To get the real cost of
3531 allocno usage in the loop we should subtract costs of
3532 the subloop allocnos. */
3533 cost -= (ALLOCNO_MEMORY_COST (subloop_allocno)
3534 - (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
3535 ? ALLOCNO_CLASS_COST (subloop_allocno)
3536 : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]));
3537 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
3538 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
3539 if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
3540 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3541 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3542 else
3544 cost
3545 += (ira_memory_move_cost[mode][rclass][0] * exit_freq
3546 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3547 if (hard_regno2 != hard_regno)
3548 cost -= (ira_register_move_cost[mode][rclass][rclass]
3549 * (exit_freq + enter_freq));
3552 if ((parent = loop_node->parent) != NULL
3553 && (parent_allocno = parent->regno_allocno_map[regno]) != NULL)
3555 ira_assert (rclass == ALLOCNO_CLASS (parent_allocno));
3556 exit_freq = ira_loop_edge_freq (loop_node, regno, true);
3557 enter_freq = ira_loop_edge_freq (loop_node, regno, false);
3558 if ((hard_regno2 = ALLOCNO_HARD_REGNO (parent_allocno)) < 0)
3559 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3560 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3561 else
3563 cost
3564 += (ira_memory_move_cost[mode][rclass][1] * exit_freq
3565 + ira_memory_move_cost[mode][rclass][0] * enter_freq);
3566 if (hard_regno2 != hard_regno)
3567 cost -= (ira_register_move_cost[mode][rclass][rclass]
3568 * (exit_freq + enter_freq));
3571 if (cost < 0)
3573 ALLOCNO_HARD_REGNO (a) = -1;
3574 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3576 fprintf
3577 (ira_dump_file,
3578 " Moving spill/restore for a%dr%d up from loop %d",
3579 ALLOCNO_NUM (a), regno, loop_node->loop_num);
3580 fprintf (ira_dump_file, " - profit %d\n", -cost);
3582 changed_p = true;
3585 if (! changed_p)
3586 break;
3592 /* Update current hard reg costs and current conflict hard reg costs
3593 for allocno A. It is done by processing its copies containing
3594 other allocnos already assigned. */
3595 static void
3596 update_curr_costs (ira_allocno_t a)
3598 int i, hard_regno, cost;
3599 machine_mode mode;
3600 enum reg_class aclass, rclass;
3601 ira_allocno_t another_a;
3602 ira_copy_t cp, next_cp;
3604 ira_free_allocno_updated_costs (a);
3605 ira_assert (! ALLOCNO_ASSIGNED_P (a));
3606 aclass = ALLOCNO_CLASS (a);
3607 if (aclass == NO_REGS)
3608 return;
3609 mode = ALLOCNO_MODE (a);
3610 ira_init_register_move_cost_if_necessary (mode);
3611 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3613 if (cp->first == a)
3615 next_cp = cp->next_first_allocno_copy;
3616 another_a = cp->second;
3618 else if (cp->second == a)
3620 next_cp = cp->next_second_allocno_copy;
3621 another_a = cp->first;
3623 else
3624 gcc_unreachable ();
3625 if (! ira_reg_classes_intersect_p[aclass][ALLOCNO_CLASS (another_a)]
3626 || ! ALLOCNO_ASSIGNED_P (another_a)
3627 || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
3628 continue;
3629 rclass = REGNO_REG_CLASS (hard_regno);
3630 i = ira_class_hard_reg_index[aclass][hard_regno];
3631 if (i < 0)
3632 continue;
3633 cost = (cp->first == a
3634 ? ira_register_move_cost[mode][rclass][aclass]
3635 : ira_register_move_cost[mode][aclass][rclass]);
3636 ira_allocate_and_set_or_copy_costs
3637 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a),
3638 ALLOCNO_HARD_REG_COSTS (a));
3639 ira_allocate_and_set_or_copy_costs
3640 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
3641 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
3642 ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3643 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3647 /* Try to assign hard registers to the unassigned allocnos and
3648 allocnos conflicting with them or conflicting with allocnos whose
3649 regno >= START_REGNO. The function is called after ira_flattening,
3650 so more allocnos (including ones created in ira-emit.c) will have a
3651 chance to get a hard register. We use simple assignment algorithm
3652 based on priorities. */
3653 void
3654 ira_reassign_conflict_allocnos (int start_regno)
3656 int i, allocnos_to_color_num;
3657 ira_allocno_t a;
3658 enum reg_class aclass;
3659 bitmap allocnos_to_color;
3660 ira_allocno_iterator ai;
3662 allocnos_to_color = ira_allocate_bitmap ();
3663 allocnos_to_color_num = 0;
3664 FOR_EACH_ALLOCNO (a, ai)
3666 int n = ALLOCNO_NUM_OBJECTS (a);
3668 if (! ALLOCNO_ASSIGNED_P (a)
3669 && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
3671 if (ALLOCNO_CLASS (a) != NO_REGS)
3672 sorted_allocnos[allocnos_to_color_num++] = a;
3673 else
3675 ALLOCNO_ASSIGNED_P (a) = true;
3676 ALLOCNO_HARD_REGNO (a) = -1;
3677 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3678 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3680 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
3682 if (ALLOCNO_REGNO (a) < start_regno
3683 || (aclass = ALLOCNO_CLASS (a)) == NO_REGS)
3684 continue;
3685 for (i = 0; i < n; i++)
3687 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3688 ira_object_t conflict_obj;
3689 ira_object_conflict_iterator oci;
3691 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3693 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3695 ira_assert (ira_reg_classes_intersect_p
3696 [aclass][ALLOCNO_CLASS (conflict_a)]);
3697 if (!bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
3698 continue;
3699 sorted_allocnos[allocnos_to_color_num++] = conflict_a;
3703 ira_free_bitmap (allocnos_to_color);
3704 if (allocnos_to_color_num > 1)
3706 setup_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
3707 qsort (sorted_allocnos, allocnos_to_color_num, sizeof (ira_allocno_t),
3708 allocno_priority_compare_func);
3710 for (i = 0; i < allocnos_to_color_num; i++)
3712 a = sorted_allocnos[i];
3713 ALLOCNO_ASSIGNED_P (a) = false;
3714 update_curr_costs (a);
3716 for (i = 0; i < allocnos_to_color_num; i++)
3718 a = sorted_allocnos[i];
3719 if (assign_hard_reg (a, true))
3721 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3722 fprintf
3723 (ira_dump_file,
3724 " Secondary allocation: assign hard reg %d to reg %d\n",
3725 ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
3732 /* This page contains functions used to find conflicts using allocno
3733 live ranges. */
3735 #ifdef ENABLE_IRA_CHECKING
3737 /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
3738 intersect. This should be used when there is only one region.
3739 Currently this is used during reload. */
3740 static bool
3741 conflict_by_live_ranges_p (int regno1, int regno2)
3743 ira_allocno_t a1, a2;
3745 ira_assert (regno1 >= FIRST_PSEUDO_REGISTER
3746 && regno2 >= FIRST_PSEUDO_REGISTER);
3747 /* Reg info calculated by dataflow infrastructure can be different
3748 from one calculated by regclass. */
3749 if ((a1 = ira_loop_tree_root->regno_allocno_map[regno1]) == NULL
3750 || (a2 = ira_loop_tree_root->regno_allocno_map[regno2]) == NULL)
3751 return false;
3752 return allocnos_conflict_by_live_ranges_p (a1, a2);
3755 #endif
3759 /* This page contains code to coalesce memory stack slots used by
3760 spilled allocnos. This results in smaller stack frame, better data
3761 locality, and in smaller code for some architectures like
3762 x86/x86_64 where insn size depends on address displacement value.
3763 On the other hand, it can worsen insn scheduling after the RA but
3764 in practice it is less important than smaller stack frames. */
3766 /* TRUE if we coalesced some allocnos. In other words, if we got
3767 loops formed by members first_coalesced_allocno and
3768 next_coalesced_allocno containing more one allocno. */
3769 static bool allocno_coalesced_p;
3771 /* Bitmap used to prevent a repeated allocno processing because of
3772 coalescing. */
3773 static bitmap processed_coalesced_allocno_bitmap;
3775 /* See below. */
3776 typedef struct coalesce_data *coalesce_data_t;
3778 /* To decrease footprint of ira_allocno structure we store all data
3779 needed only for coalescing in the following structure. */
3780 struct coalesce_data
3782 /* Coalesced allocnos form a cyclic list. One allocno given by
3783 FIRST represents all coalesced allocnos. The
3784 list is chained by NEXT. */
3785 ira_allocno_t first;
3786 ira_allocno_t next;
3787 int temp;
3790 /* Container for storing allocno data concerning coalescing. */
3791 static coalesce_data_t allocno_coalesce_data;
3793 /* Macro to access the data concerning coalescing. */
3794 #define ALLOCNO_COALESCE_DATA(a) ((coalesce_data_t) ALLOCNO_ADD_DATA (a))
3796 /* Merge two sets of coalesced allocnos given correspondingly by
3797 allocnos A1 and A2 (more accurately merging A2 set into A1
3798 set). */
3799 static void
3800 merge_allocnos (ira_allocno_t a1, ira_allocno_t a2)
3802 ira_allocno_t a, first, last, next;
3804 first = ALLOCNO_COALESCE_DATA (a1)->first;
3805 a = ALLOCNO_COALESCE_DATA (a2)->first;
3806 if (first == a)
3807 return;
3808 for (last = a2, a = ALLOCNO_COALESCE_DATA (a2)->next;;
3809 a = ALLOCNO_COALESCE_DATA (a)->next)
3811 ALLOCNO_COALESCE_DATA (a)->first = first;
3812 if (a == a2)
3813 break;
3814 last = a;
3816 next = allocno_coalesce_data[ALLOCNO_NUM (first)].next;
3817 allocno_coalesce_data[ALLOCNO_NUM (first)].next = a2;
3818 allocno_coalesce_data[ALLOCNO_NUM (last)].next = next;
3821 /* Return TRUE if there are conflicting allocnos from two sets of
3822 coalesced allocnos given correspondingly by allocnos A1 and A2. We
3823 use live ranges to find conflicts because conflicts are represented
3824 only for allocnos of the same allocno class and during the reload
3825 pass we coalesce allocnos for sharing stack memory slots. */
3826 static bool
3827 coalesced_allocno_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
3829 ira_allocno_t a, conflict_a;
3831 if (allocno_coalesced_p)
3833 bitmap_clear (processed_coalesced_allocno_bitmap);
3834 for (a = ALLOCNO_COALESCE_DATA (a1)->next;;
3835 a = ALLOCNO_COALESCE_DATA (a)->next)
3837 bitmap_set_bit (processed_coalesced_allocno_bitmap, ALLOCNO_NUM (a));
3838 if (a == a1)
3839 break;
3842 for (a = ALLOCNO_COALESCE_DATA (a2)->next;;
3843 a = ALLOCNO_COALESCE_DATA (a)->next)
3845 for (conflict_a = ALLOCNO_COALESCE_DATA (a1)->next;;
3846 conflict_a = ALLOCNO_COALESCE_DATA (conflict_a)->next)
3848 if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
3849 return true;
3850 if (conflict_a == a1)
3851 break;
3853 if (a == a2)
3854 break;
3856 return false;
3859 /* The major function for aggressive allocno coalescing. We coalesce
3860 only spilled allocnos. If some allocnos have been coalesced, we
3861 set up flag allocno_coalesced_p. */
3862 static void
3863 coalesce_allocnos (void)
3865 ira_allocno_t a;
3866 ira_copy_t cp, next_cp;
3867 unsigned int j;
3868 int i, n, cp_num, regno;
3869 bitmap_iterator bi;
3871 cp_num = 0;
3872 /* Collect copies. */
3873 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
3875 a = ira_allocnos[j];
3876 regno = ALLOCNO_REGNO (a);
3877 if (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
3878 || ira_equiv_no_lvalue_p (regno))
3879 continue;
3880 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3882 if (cp->first == a)
3884 next_cp = cp->next_first_allocno_copy;
3885 regno = ALLOCNO_REGNO (cp->second);
3886 /* For priority coloring we coalesce allocnos only with
3887 the same allocno class not with intersected allocno
3888 classes as it were possible. It is done for
3889 simplicity. */
3890 if ((cp->insn != NULL || cp->constraint_p)
3891 && ALLOCNO_ASSIGNED_P (cp->second)
3892 && ALLOCNO_HARD_REGNO (cp->second) < 0
3893 && ! ira_equiv_no_lvalue_p (regno))
3894 sorted_copies[cp_num++] = cp;
3896 else if (cp->second == a)
3897 next_cp = cp->next_second_allocno_copy;
3898 else
3899 gcc_unreachable ();
3902 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
3903 /* Coalesced copies, most frequently executed first. */
3904 for (; cp_num != 0;)
3906 for (i = 0; i < cp_num; i++)
3908 cp = sorted_copies[i];
3909 if (! coalesced_allocno_conflict_p (cp->first, cp->second))
3911 allocno_coalesced_p = true;
3912 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3913 fprintf
3914 (ira_dump_file,
3915 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
3916 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
3917 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
3918 cp->freq);
3919 merge_allocnos (cp->first, cp->second);
3920 i++;
3921 break;
3924 /* Collect the rest of copies. */
3925 for (n = 0; i < cp_num; i++)
3927 cp = sorted_copies[i];
3928 if (allocno_coalesce_data[ALLOCNO_NUM (cp->first)].first
3929 != allocno_coalesce_data[ALLOCNO_NUM (cp->second)].first)
3930 sorted_copies[n++] = cp;
3932 cp_num = n;
3936 /* Usage cost and order number of coalesced allocno set to which
3937 given pseudo register belongs to. */
3938 static int *regno_coalesced_allocno_cost;
3939 static int *regno_coalesced_allocno_num;
3941 /* Sort pseudos according frequencies of coalesced allocno sets they
3942 belong to (putting most frequently ones first), and according to
3943 coalesced allocno set order numbers. */
3944 static int
3945 coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
3947 const int regno1 = *(const int *) v1p;
3948 const int regno2 = *(const int *) v2p;
3949 int diff;
3951 if ((diff = (regno_coalesced_allocno_cost[regno2]
3952 - regno_coalesced_allocno_cost[regno1])) != 0)
3953 return diff;
3954 if ((diff = (regno_coalesced_allocno_num[regno1]
3955 - regno_coalesced_allocno_num[regno2])) != 0)
3956 return diff;
3957 return regno1 - regno2;
3960 /* Widest width in which each pseudo reg is referred to (via subreg).
3961 It is used for sorting pseudo registers. */
3962 static machine_mode *regno_max_ref_mode;
3964 /* Sort pseudos according their slot numbers (putting ones with
3965 smaller numbers first, or last when the frame pointer is not
3966 needed). */
3967 static int
3968 coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
3970 const int regno1 = *(const int *) v1p;
3971 const int regno2 = *(const int *) v2p;
3972 ira_allocno_t a1 = ira_regno_allocno_map[regno1];
3973 ira_allocno_t a2 = ira_regno_allocno_map[regno2];
3974 int diff, slot_num1, slot_num2;
3975 machine_mode mode1, mode2;
3977 if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
3979 if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
3980 return regno1 - regno2;
3981 return 1;
3983 else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
3984 return -1;
3985 slot_num1 = -ALLOCNO_HARD_REGNO (a1);
3986 slot_num2 = -ALLOCNO_HARD_REGNO (a2);
3987 if ((diff = slot_num1 - slot_num2) != 0)
3988 return (frame_pointer_needed
3989 || (!FRAME_GROWS_DOWNWARD) == STACK_GROWS_DOWNWARD ? diff : -diff);
3990 mode1 = wider_subreg_mode (PSEUDO_REGNO_MODE (regno1),
3991 regno_max_ref_mode[regno1]);
3992 mode2 = wider_subreg_mode (PSEUDO_REGNO_MODE (regno2),
3993 regno_max_ref_mode[regno2]);
3994 if ((diff = compare_sizes_for_sort (GET_MODE_SIZE (mode2),
3995 GET_MODE_SIZE (mode1))) != 0)
3996 return diff;
3997 return regno1 - regno2;
4000 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
4001 for coalesced allocno sets containing allocnos with their regnos
4002 given in array PSEUDO_REGNOS of length N. */
4003 static void
4004 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
4006 int i, num, regno, cost;
4007 ira_allocno_t allocno, a;
4009 for (num = i = 0; i < n; i++)
4011 regno = pseudo_regnos[i];
4012 allocno = ira_regno_allocno_map[regno];
4013 if (allocno == NULL)
4015 regno_coalesced_allocno_cost[regno] = 0;
4016 regno_coalesced_allocno_num[regno] = ++num;
4017 continue;
4019 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
4020 continue;
4021 num++;
4022 for (cost = 0, a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4023 a = ALLOCNO_COALESCE_DATA (a)->next)
4025 cost += ALLOCNO_FREQ (a);
4026 if (a == allocno)
4027 break;
4029 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4030 a = ALLOCNO_COALESCE_DATA (a)->next)
4032 regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
4033 regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
4034 if (a == allocno)
4035 break;
4040 /* Collect spilled allocnos representing coalesced allocno sets (the
4041 first coalesced allocno). The collected allocnos are returned
4042 through array SPILLED_COALESCED_ALLOCNOS. The function returns the
4043 number of the collected allocnos. The allocnos are given by their
4044 regnos in array PSEUDO_REGNOS of length N. */
4045 static int
4046 collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
4047 ira_allocno_t *spilled_coalesced_allocnos)
4049 int i, num, regno;
4050 ira_allocno_t allocno;
4052 for (num = i = 0; i < n; i++)
4054 regno = pseudo_regnos[i];
4055 allocno = ira_regno_allocno_map[regno];
4056 if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
4057 || ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
4058 continue;
4059 spilled_coalesced_allocnos[num++] = allocno;
4061 return num;
4064 /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
4065 given slot contains live ranges of coalesced allocnos assigned to
4066 given slot. */
4067 static live_range_t *slot_coalesced_allocnos_live_ranges;
4069 /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
4070 ranges intersected with live ranges of coalesced allocnos assigned
4071 to slot with number N. */
4072 static bool
4073 slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno, int n)
4075 ira_allocno_t a;
4077 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4078 a = ALLOCNO_COALESCE_DATA (a)->next)
4080 int i;
4081 int nr = ALLOCNO_NUM_OBJECTS (a);
4082 gcc_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
4083 for (i = 0; i < nr; i++)
4085 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4087 if (ira_live_ranges_intersect_p
4088 (slot_coalesced_allocnos_live_ranges[n],
4089 OBJECT_LIVE_RANGES (obj)))
4090 return true;
4092 if (a == allocno)
4093 break;
4095 return false;
4098 /* Update live ranges of slot to which coalesced allocnos represented
4099 by ALLOCNO were assigned. */
4100 static void
4101 setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno)
4103 int i, n;
4104 ira_allocno_t a;
4105 live_range_t r;
4107 n = ALLOCNO_COALESCE_DATA (allocno)->temp;
4108 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4109 a = ALLOCNO_COALESCE_DATA (a)->next)
4111 int nr = ALLOCNO_NUM_OBJECTS (a);
4112 gcc_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
4113 for (i = 0; i < nr; i++)
4115 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4117 r = ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj));
4118 slot_coalesced_allocnos_live_ranges[n]
4119 = ira_merge_live_ranges
4120 (slot_coalesced_allocnos_live_ranges[n], r);
4122 if (a == allocno)
4123 break;
4127 /* We have coalesced allocnos involving in copies. Coalesce allocnos
4128 further in order to share the same memory stack slot. Allocnos
4129 representing sets of allocnos coalesced before the call are given
4130 in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
4131 some allocnos were coalesced in the function. */
4132 static bool
4133 coalesce_spill_slots (ira_allocno_t *spilled_coalesced_allocnos, int num)
4135 int i, j, n, last_coalesced_allocno_num;
4136 ira_allocno_t allocno, a;
4137 bool merged_p = false;
4138 bitmap set_jump_crosses = regstat_get_setjmp_crosses ();
4140 slot_coalesced_allocnos_live_ranges
4141 = (live_range_t *) ira_allocate (sizeof (live_range_t) * ira_allocnos_num);
4142 memset (slot_coalesced_allocnos_live_ranges, 0,
4143 sizeof (live_range_t) * ira_allocnos_num);
4144 last_coalesced_allocno_num = 0;
4145 /* Coalesce non-conflicting spilled allocnos preferring most
4146 frequently used. */
4147 for (i = 0; i < num; i++)
4149 allocno = spilled_coalesced_allocnos[i];
4150 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
4151 || bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (allocno))
4152 || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
4153 continue;
4154 for (j = 0; j < i; j++)
4156 a = spilled_coalesced_allocnos[j];
4157 n = ALLOCNO_COALESCE_DATA (a)->temp;
4158 if (ALLOCNO_COALESCE_DATA (a)->first == a
4159 && ! bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (a))
4160 && ! ira_equiv_no_lvalue_p (ALLOCNO_REGNO (a))
4161 && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno, n))
4162 break;
4164 if (j >= i)
4166 /* No coalescing: set up number for coalesced allocnos
4167 represented by ALLOCNO. */
4168 ALLOCNO_COALESCE_DATA (allocno)->temp = last_coalesced_allocno_num++;
4169 setup_slot_coalesced_allocno_live_ranges (allocno);
4171 else
4173 allocno_coalesced_p = true;
4174 merged_p = true;
4175 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4176 fprintf (ira_dump_file,
4177 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
4178 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
4179 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
4180 ALLOCNO_COALESCE_DATA (allocno)->temp
4181 = ALLOCNO_COALESCE_DATA (a)->temp;
4182 setup_slot_coalesced_allocno_live_ranges (allocno);
4183 merge_allocnos (a, allocno);
4184 ira_assert (ALLOCNO_COALESCE_DATA (a)->first == a);
4187 for (i = 0; i < ira_allocnos_num; i++)
4188 ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges[i]);
4189 ira_free (slot_coalesced_allocnos_live_ranges);
4190 return merged_p;
4193 /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
4194 subsequent assigning stack slots to them in the reload pass. To do
4195 this we coalesce spilled allocnos first to decrease the number of
4196 memory-memory move insns. This function is called by the
4197 reload. */
4198 void
4199 ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
4200 machine_mode *reg_max_ref_mode)
4202 int max_regno = max_reg_num ();
4203 int i, regno, num, slot_num;
4204 ira_allocno_t allocno, a;
4205 ira_allocno_iterator ai;
4206 ira_allocno_t *spilled_coalesced_allocnos;
4208 ira_assert (! ira_use_lra_p);
4210 /* Set up allocnos can be coalesced. */
4211 coloring_allocno_bitmap = ira_allocate_bitmap ();
4212 for (i = 0; i < n; i++)
4214 regno = pseudo_regnos[i];
4215 allocno = ira_regno_allocno_map[regno];
4216 if (allocno != NULL)
4217 bitmap_set_bit (coloring_allocno_bitmap, ALLOCNO_NUM (allocno));
4219 allocno_coalesced_p = false;
4220 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
4221 allocno_coalesce_data
4222 = (coalesce_data_t) ira_allocate (sizeof (struct coalesce_data)
4223 * ira_allocnos_num);
4224 /* Initialize coalesce data for allocnos. */
4225 FOR_EACH_ALLOCNO (a, ai)
4227 ALLOCNO_ADD_DATA (a) = allocno_coalesce_data + ALLOCNO_NUM (a);
4228 ALLOCNO_COALESCE_DATA (a)->first = a;
4229 ALLOCNO_COALESCE_DATA (a)->next = a;
4231 coalesce_allocnos ();
4232 ira_free_bitmap (coloring_allocno_bitmap);
4233 regno_coalesced_allocno_cost
4234 = (int *) ira_allocate (max_regno * sizeof (int));
4235 regno_coalesced_allocno_num
4236 = (int *) ira_allocate (max_regno * sizeof (int));
4237 memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
4238 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
4239 /* Sort regnos according frequencies of the corresponding coalesced
4240 allocno sets. */
4241 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
4242 spilled_coalesced_allocnos
4243 = (ira_allocno_t *) ira_allocate (ira_allocnos_num
4244 * sizeof (ira_allocno_t));
4245 /* Collect allocnos representing the spilled coalesced allocno
4246 sets. */
4247 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
4248 spilled_coalesced_allocnos);
4249 if (flag_ira_share_spill_slots
4250 && coalesce_spill_slots (spilled_coalesced_allocnos, num))
4252 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
4253 qsort (pseudo_regnos, n, sizeof (int),
4254 coalesced_pseudo_reg_freq_compare);
4255 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
4256 spilled_coalesced_allocnos);
4258 ira_free_bitmap (processed_coalesced_allocno_bitmap);
4259 allocno_coalesced_p = false;
4260 /* Assign stack slot numbers to spilled allocno sets, use smaller
4261 numbers for most frequently used coalesced allocnos. -1 is
4262 reserved for dynamic search of stack slots for pseudos spilled by
4263 the reload. */
4264 slot_num = 1;
4265 for (i = 0; i < num; i++)
4267 allocno = spilled_coalesced_allocnos[i];
4268 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
4269 || ALLOCNO_HARD_REGNO (allocno) >= 0
4270 || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
4271 continue;
4272 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4273 fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
4274 slot_num++;
4275 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4276 a = ALLOCNO_COALESCE_DATA (a)->next)
4278 ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
4279 ALLOCNO_HARD_REGNO (a) = -slot_num;
4280 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4282 machine_mode mode = wider_subreg_mode
4283 (PSEUDO_REGNO_MODE (ALLOCNO_REGNO (a)),
4284 reg_max_ref_mode[ALLOCNO_REGNO (a)]);
4285 fprintf (ira_dump_file, " a%dr%d(%d,",
4286 ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a));
4287 print_dec (GET_MODE_SIZE (mode), ira_dump_file, SIGNED);
4288 fprintf (ira_dump_file, ")\n");
4291 if (a == allocno)
4292 break;
4294 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4295 fprintf (ira_dump_file, "\n");
4297 ira_spilled_reg_stack_slots_num = slot_num - 1;
4298 ira_free (spilled_coalesced_allocnos);
4299 /* Sort regnos according the slot numbers. */
4300 regno_max_ref_mode = reg_max_ref_mode;
4301 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
4302 FOR_EACH_ALLOCNO (a, ai)
4303 ALLOCNO_ADD_DATA (a) = NULL;
4304 ira_free (allocno_coalesce_data);
4305 ira_free (regno_coalesced_allocno_num);
4306 ira_free (regno_coalesced_allocno_cost);
4311 /* This page contains code used by the reload pass to improve the
4312 final code. */
4314 /* The function is called from reload to mark changes in the
4315 allocation of REGNO made by the reload. Remember that reg_renumber
4316 reflects the change result. */
4317 void
4318 ira_mark_allocation_change (int regno)
4320 ira_allocno_t a = ira_regno_allocno_map[regno];
4321 int old_hard_regno, hard_regno, cost;
4322 enum reg_class aclass = ALLOCNO_CLASS (a);
4324 ira_assert (a != NULL);
4325 hard_regno = reg_renumber[regno];
4326 if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
4327 return;
4328 if (old_hard_regno < 0)
4329 cost = -ALLOCNO_MEMORY_COST (a);
4330 else
4332 ira_assert (ira_class_hard_reg_index[aclass][old_hard_regno] >= 0);
4333 cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
4334 ? ALLOCNO_CLASS_COST (a)
4335 : ALLOCNO_HARD_REG_COSTS (a)
4336 [ira_class_hard_reg_index[aclass][old_hard_regno]]);
4337 update_costs_from_copies (a, false, false);
4339 ira_overall_cost -= cost;
4340 ALLOCNO_HARD_REGNO (a) = hard_regno;
4341 if (hard_regno < 0)
4343 ALLOCNO_HARD_REGNO (a) = -1;
4344 cost += ALLOCNO_MEMORY_COST (a);
4346 else if (ira_class_hard_reg_index[aclass][hard_regno] >= 0)
4348 cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
4349 ? ALLOCNO_CLASS_COST (a)
4350 : ALLOCNO_HARD_REG_COSTS (a)
4351 [ira_class_hard_reg_index[aclass][hard_regno]]);
4352 update_costs_from_copies (a, true, false);
4354 else
4355 /* Reload changed class of the allocno. */
4356 cost = 0;
4357 ira_overall_cost += cost;
4360 /* This function is called when reload deletes memory-memory move. In
4361 this case we marks that the allocation of the corresponding
4362 allocnos should be not changed in future. Otherwise we risk to get
4363 a wrong code. */
4364 void
4365 ira_mark_memory_move_deletion (int dst_regno, int src_regno)
4367 ira_allocno_t dst = ira_regno_allocno_map[dst_regno];
4368 ira_allocno_t src = ira_regno_allocno_map[src_regno];
4370 ira_assert (dst != NULL && src != NULL
4371 && ALLOCNO_HARD_REGNO (dst) < 0
4372 && ALLOCNO_HARD_REGNO (src) < 0);
4373 ALLOCNO_DONT_REASSIGN_P (dst) = true;
4374 ALLOCNO_DONT_REASSIGN_P (src) = true;
4377 /* Try to assign a hard register (except for FORBIDDEN_REGS) to
4378 allocno A and return TRUE in the case of success. */
4379 static bool
4380 allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
4382 int hard_regno;
4383 enum reg_class aclass;
4384 int regno = ALLOCNO_REGNO (a);
4385 HARD_REG_SET saved[2];
4386 int i, n;
4388 n = ALLOCNO_NUM_OBJECTS (a);
4389 for (i = 0; i < n; i++)
4391 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4392 saved[i] = OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
4393 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) |= forbidden_regs;
4394 if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
4395 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) |= ira_need_caller_save_regs (a);
4397 ALLOCNO_ASSIGNED_P (a) = false;
4398 aclass = ALLOCNO_CLASS (a);
4399 update_curr_costs (a);
4400 assign_hard_reg (a, true);
4401 hard_regno = ALLOCNO_HARD_REGNO (a);
4402 reg_renumber[regno] = hard_regno;
4403 if (hard_regno < 0)
4404 ALLOCNO_HARD_REGNO (a) = -1;
4405 else
4407 ira_assert (ira_class_hard_reg_index[aclass][hard_regno] >= 0);
4408 ira_overall_cost
4409 -= (ALLOCNO_MEMORY_COST (a)
4410 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
4411 ? ALLOCNO_CLASS_COST (a)
4412 : ALLOCNO_HARD_REG_COSTS (a)[ira_class_hard_reg_index
4413 [aclass][hard_regno]]));
4414 if (ira_need_caller_save_p (a, hard_regno))
4416 ira_assert (flag_caller_saves);
4417 caller_save_needed = 1;
4421 /* If we found a hard register, modify the RTL for the pseudo
4422 register to show the hard register, and mark the pseudo register
4423 live. */
4424 if (reg_renumber[regno] >= 0)
4426 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4427 fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
4428 SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
4429 mark_home_live (regno);
4431 else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4432 fprintf (ira_dump_file, "\n");
4433 for (i = 0; i < n; i++)
4435 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4436 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) = saved[i];
4438 return reg_renumber[regno] >= 0;
4441 /* Sort pseudos according their usage frequencies (putting most
4442 frequently ones first). */
4443 static int
4444 pseudo_reg_compare (const void *v1p, const void *v2p)
4446 int regno1 = *(const int *) v1p;
4447 int regno2 = *(const int *) v2p;
4448 int diff;
4450 if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
4451 return diff;
4452 return regno1 - regno2;
4455 /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
4456 NUM of them) or spilled pseudos conflicting with pseudos in
4457 SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
4458 allocation has been changed. The function doesn't use
4459 BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
4460 PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
4461 is called by the reload pass at the end of each reload
4462 iteration. */
4463 bool
4464 ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
4465 HARD_REG_SET bad_spill_regs,
4466 HARD_REG_SET *pseudo_forbidden_regs,
4467 HARD_REG_SET *pseudo_previous_regs,
4468 bitmap spilled)
4470 int i, n, regno;
4471 bool changed_p;
4472 ira_allocno_t a;
4473 HARD_REG_SET forbidden_regs;
4474 bitmap temp = BITMAP_ALLOC (NULL);
4476 /* Add pseudos which conflict with pseudos already in
4477 SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
4478 to allocating in two steps as some of the conflicts might have
4479 a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
4480 for (i = 0; i < num; i++)
4481 bitmap_set_bit (temp, spilled_pseudo_regs[i]);
4483 for (i = 0, n = num; i < n; i++)
4485 int nr, j;
4486 int regno = spilled_pseudo_regs[i];
4487 bitmap_set_bit (temp, regno);
4489 a = ira_regno_allocno_map[regno];
4490 nr = ALLOCNO_NUM_OBJECTS (a);
4491 for (j = 0; j < nr; j++)
4493 ira_object_t conflict_obj;
4494 ira_object_t obj = ALLOCNO_OBJECT (a, j);
4495 ira_object_conflict_iterator oci;
4497 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
4499 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
4500 if (ALLOCNO_HARD_REGNO (conflict_a) < 0
4501 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
4502 && bitmap_set_bit (temp, ALLOCNO_REGNO (conflict_a)))
4504 spilled_pseudo_regs[num++] = ALLOCNO_REGNO (conflict_a);
4505 /* ?!? This seems wrong. */
4506 bitmap_set_bit (consideration_allocno_bitmap,
4507 ALLOCNO_NUM (conflict_a));
4513 if (num > 1)
4514 qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
4515 changed_p = false;
4516 /* Try to assign hard registers to pseudos from
4517 SPILLED_PSEUDO_REGS. */
4518 for (i = 0; i < num; i++)
4520 regno = spilled_pseudo_regs[i];
4521 forbidden_regs = (bad_spill_regs
4522 | pseudo_forbidden_regs[regno]
4523 | pseudo_previous_regs[regno]);
4524 gcc_assert (reg_renumber[regno] < 0);
4525 a = ira_regno_allocno_map[regno];
4526 ira_mark_allocation_change (regno);
4527 ira_assert (reg_renumber[regno] < 0);
4528 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4529 fprintf (ira_dump_file,
4530 " Try Assign %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
4531 ALLOCNO_MEMORY_COST (a)
4532 - ALLOCNO_CLASS_COST (a));
4533 allocno_reload_assign (a, forbidden_regs);
4534 if (reg_renumber[regno] >= 0)
4536 CLEAR_REGNO_REG_SET (spilled, regno);
4537 changed_p = true;
4540 BITMAP_FREE (temp);
4541 return changed_p;
4544 /* The function is called by reload and returns already allocated
4545 stack slot (if any) for REGNO with given INHERENT_SIZE and
4546 TOTAL_SIZE. In the case of failure to find a slot which can be
4547 used for REGNO, the function returns NULL. */
4549 ira_reuse_stack_slot (int regno, poly_uint64 inherent_size,
4550 poly_uint64 total_size)
4552 unsigned int i;
4553 int slot_num, best_slot_num;
4554 int cost, best_cost;
4555 ira_copy_t cp, next_cp;
4556 ira_allocno_t another_allocno, allocno = ira_regno_allocno_map[regno];
4557 rtx x;
4558 bitmap_iterator bi;
4559 class ira_spilled_reg_stack_slot *slot = NULL;
4561 ira_assert (! ira_use_lra_p);
4563 ira_assert (known_eq (inherent_size, PSEUDO_REGNO_BYTES (regno))
4564 && known_le (inherent_size, total_size)
4565 && ALLOCNO_HARD_REGNO (allocno) < 0);
4566 if (! flag_ira_share_spill_slots)
4567 return NULL_RTX;
4568 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4569 if (slot_num != -1)
4571 slot = &ira_spilled_reg_stack_slots[slot_num];
4572 x = slot->mem;
4574 else
4576 best_cost = best_slot_num = -1;
4577 x = NULL_RTX;
4578 /* It means that the pseudo was spilled in the reload pass, try
4579 to reuse a slot. */
4580 for (slot_num = 0;
4581 slot_num < ira_spilled_reg_stack_slots_num;
4582 slot_num++)
4584 slot = &ira_spilled_reg_stack_slots[slot_num];
4585 if (slot->mem == NULL_RTX)
4586 continue;
4587 if (maybe_lt (slot->width, total_size)
4588 || maybe_lt (GET_MODE_SIZE (GET_MODE (slot->mem)), inherent_size))
4589 continue;
4591 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4592 FIRST_PSEUDO_REGISTER, i, bi)
4594 another_allocno = ira_regno_allocno_map[i];
4595 if (allocnos_conflict_by_live_ranges_p (allocno,
4596 another_allocno))
4597 goto cont;
4599 for (cost = 0, cp = ALLOCNO_COPIES (allocno);
4600 cp != NULL;
4601 cp = next_cp)
4603 if (cp->first == allocno)
4605 next_cp = cp->next_first_allocno_copy;
4606 another_allocno = cp->second;
4608 else if (cp->second == allocno)
4610 next_cp = cp->next_second_allocno_copy;
4611 another_allocno = cp->first;
4613 else
4614 gcc_unreachable ();
4615 if (cp->insn == NULL_RTX)
4616 continue;
4617 if (bitmap_bit_p (&slot->spilled_regs,
4618 ALLOCNO_REGNO (another_allocno)))
4619 cost += cp->freq;
4621 if (cost > best_cost)
4623 best_cost = cost;
4624 best_slot_num = slot_num;
4626 cont:
4629 if (best_cost >= 0)
4631 slot_num = best_slot_num;
4632 slot = &ira_spilled_reg_stack_slots[slot_num];
4633 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4634 x = slot->mem;
4635 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4638 if (x != NULL_RTX)
4640 ira_assert (known_ge (slot->width, total_size));
4641 #ifdef ENABLE_IRA_CHECKING
4642 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4643 FIRST_PSEUDO_REGISTER, i, bi)
4645 ira_assert (! conflict_by_live_ranges_p (regno, i));
4647 #endif
4648 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4649 if (internal_flag_ira_verbose > 3 && ira_dump_file)
4651 fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
4652 regno, REG_FREQ (regno), slot_num);
4653 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4654 FIRST_PSEUDO_REGISTER, i, bi)
4656 if ((unsigned) regno != i)
4657 fprintf (ira_dump_file, " %d", i);
4659 fprintf (ira_dump_file, "\n");
4662 return x;
4665 /* This is called by reload every time a new stack slot X with
4666 TOTAL_SIZE was allocated for REGNO. We store this info for
4667 subsequent ira_reuse_stack_slot calls. */
4668 void
4669 ira_mark_new_stack_slot (rtx x, int regno, poly_uint64 total_size)
4671 class ira_spilled_reg_stack_slot *slot;
4672 int slot_num;
4673 ira_allocno_t allocno;
4675 ira_assert (! ira_use_lra_p);
4677 ira_assert (known_le (PSEUDO_REGNO_BYTES (regno), total_size));
4678 allocno = ira_regno_allocno_map[regno];
4679 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4680 if (slot_num == -1)
4682 slot_num = ira_spilled_reg_stack_slots_num++;
4683 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4685 slot = &ira_spilled_reg_stack_slots[slot_num];
4686 INIT_REG_SET (&slot->spilled_regs);
4687 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4688 slot->mem = x;
4689 slot->width = total_size;
4690 if (internal_flag_ira_verbose > 3 && ira_dump_file)
4691 fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
4692 regno, REG_FREQ (regno), slot_num);
4696 /* Return spill cost for pseudo-registers whose numbers are in array
4697 REGNOS (with a negative number as an end marker) for reload with
4698 given IN and OUT for INSN. Return also number points (through
4699 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
4700 the register pressure is high, number of references of the
4701 pseudo-registers (through NREFS), the number of psuedo registers
4702 whose allocated register wouldn't need saving in the prologue
4703 (through CALL_USED_COUNT), and the first hard regno occupied by the
4704 pseudo-registers (through FIRST_HARD_REGNO). */
4705 static int
4706 calculate_spill_cost (int *regnos, rtx in, rtx out, rtx_insn *insn,
4707 int *excess_pressure_live_length,
4708 int *nrefs, int *call_used_count, int *first_hard_regno)
4710 int i, cost, regno, hard_regno, count, saved_cost;
4711 bool in_p, out_p;
4712 int length;
4713 ira_allocno_t a;
4715 *nrefs = 0;
4716 for (length = count = cost = i = 0;; i++)
4718 regno = regnos[i];
4719 if (regno < 0)
4720 break;
4721 *nrefs += REG_N_REFS (regno);
4722 hard_regno = reg_renumber[regno];
4723 ira_assert (hard_regno >= 0);
4724 a = ira_regno_allocno_map[regno];
4725 length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) / ALLOCNO_NUM_OBJECTS (a);
4726 cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
4727 if (in_hard_reg_set_p (crtl->abi->full_reg_clobbers (),
4728 ALLOCNO_MODE (a), hard_regno))
4729 count++;
4730 in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
4731 out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
4732 if ((in_p || out_p)
4733 && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
4735 saved_cost = 0;
4736 if (in_p)
4737 saved_cost += ira_memory_move_cost
4738 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][1];
4739 if (out_p)
4740 saved_cost
4741 += ira_memory_move_cost
4742 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][0];
4743 cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
4746 *excess_pressure_live_length = length;
4747 *call_used_count = count;
4748 hard_regno = -1;
4749 if (regnos[0] >= 0)
4751 hard_regno = reg_renumber[regnos[0]];
4753 *first_hard_regno = hard_regno;
4754 return cost;
4757 /* Return TRUE if spilling pseudo-registers whose numbers are in array
4758 REGNOS is better than spilling pseudo-registers with numbers in
4759 OTHER_REGNOS for reload with given IN and OUT for INSN. The
4760 function used by the reload pass to make better register spilling
4761 decisions. */
4762 bool
4763 ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
4764 rtx in, rtx out, rtx_insn *insn)
4766 int cost, other_cost;
4767 int length, other_length;
4768 int nrefs, other_nrefs;
4769 int call_used_count, other_call_used_count;
4770 int hard_regno, other_hard_regno;
4772 cost = calculate_spill_cost (regnos, in, out, insn,
4773 &length, &nrefs, &call_used_count, &hard_regno);
4774 other_cost = calculate_spill_cost (other_regnos, in, out, insn,
4775 &other_length, &other_nrefs,
4776 &other_call_used_count,
4777 &other_hard_regno);
4778 if (nrefs == 0 && other_nrefs != 0)
4779 return true;
4780 if (nrefs != 0 && other_nrefs == 0)
4781 return false;
4782 if (cost != other_cost)
4783 return cost < other_cost;
4784 if (length != other_length)
4785 return length > other_length;
4786 #ifdef REG_ALLOC_ORDER
4787 if (hard_regno >= 0 && other_hard_regno >= 0)
4788 return (inv_reg_alloc_order[hard_regno]
4789 < inv_reg_alloc_order[other_hard_regno]);
4790 #else
4791 if (call_used_count != other_call_used_count)
4792 return call_used_count > other_call_used_count;
4793 #endif
4794 return false;
4799 /* Allocate and initialize data necessary for assign_hard_reg. */
4800 void
4801 ira_initiate_assign (void)
4803 sorted_allocnos
4804 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4805 * ira_allocnos_num);
4806 consideration_allocno_bitmap = ira_allocate_bitmap ();
4807 initiate_cost_update ();
4808 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4809 sorted_copies = (ira_copy_t *) ira_allocate (ira_copies_num
4810 * sizeof (ira_copy_t));
4813 /* Deallocate data used by assign_hard_reg. */
4814 void
4815 ira_finish_assign (void)
4817 ira_free (sorted_allocnos);
4818 ira_free_bitmap (consideration_allocno_bitmap);
4819 finish_cost_update ();
4820 ira_free (allocno_priorities);
4821 ira_free (sorted_copies);
4826 /* Entry function doing color-based register allocation. */
4827 static void
4828 color (void)
4830 allocno_stack_vec.create (ira_allocnos_num);
4831 memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
4832 ira_initiate_assign ();
4833 do_coloring ();
4834 ira_finish_assign ();
4835 allocno_stack_vec.release ();
4836 move_spill_restore ();
4841 /* This page contains a simple register allocator without usage of
4842 allocno conflicts. This is used for fast allocation for -O0. */
4844 /* Do register allocation by not using allocno conflicts. It uses
4845 only allocno live ranges. The algorithm is close to Chow's
4846 priority coloring. */
4847 static void
4848 fast_allocation (void)
4850 int i, j, k, num, class_size, hard_regno, best_hard_regno, cost, min_cost;
4851 int *costs;
4852 #ifdef STACK_REGS
4853 bool no_stack_reg_p;
4854 #endif
4855 enum reg_class aclass;
4856 machine_mode mode;
4857 ira_allocno_t a;
4858 ira_allocno_iterator ai;
4859 live_range_t r;
4860 HARD_REG_SET conflict_hard_regs, *used_hard_regs;
4862 sorted_allocnos = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4863 * ira_allocnos_num);
4864 num = 0;
4865 FOR_EACH_ALLOCNO (a, ai)
4866 sorted_allocnos[num++] = a;
4867 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4868 setup_allocno_priorities (sorted_allocnos, num);
4869 used_hard_regs = (HARD_REG_SET *) ira_allocate (sizeof (HARD_REG_SET)
4870 * ira_max_point);
4871 for (i = 0; i < ira_max_point; i++)
4872 CLEAR_HARD_REG_SET (used_hard_regs[i]);
4873 qsort (sorted_allocnos, num, sizeof (ira_allocno_t),
4874 allocno_priority_compare_func);
4875 for (i = 0; i < num; i++)
4877 int nr, l;
4879 a = sorted_allocnos[i];
4880 nr = ALLOCNO_NUM_OBJECTS (a);
4881 CLEAR_HARD_REG_SET (conflict_hard_regs);
4882 for (l = 0; l < nr; l++)
4884 ira_object_t obj = ALLOCNO_OBJECT (a, l);
4885 conflict_hard_regs |= OBJECT_CONFLICT_HARD_REGS (obj);
4886 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4887 for (j = r->start; j <= r->finish; j++)
4888 conflict_hard_regs |= used_hard_regs[j];
4890 aclass = ALLOCNO_CLASS (a);
4891 ALLOCNO_ASSIGNED_P (a) = true;
4892 ALLOCNO_HARD_REGNO (a) = -1;
4893 if (hard_reg_set_subset_p (reg_class_contents[aclass],
4894 conflict_hard_regs))
4895 continue;
4896 mode = ALLOCNO_MODE (a);
4897 #ifdef STACK_REGS
4898 no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
4899 #endif
4900 class_size = ira_class_hard_regs_num[aclass];
4901 costs = ALLOCNO_HARD_REG_COSTS (a);
4902 min_cost = INT_MAX;
4903 best_hard_regno = -1;
4904 for (j = 0; j < class_size; j++)
4906 hard_regno = ira_class_hard_regs[aclass][j];
4907 #ifdef STACK_REGS
4908 if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
4909 && hard_regno <= LAST_STACK_REG)
4910 continue;
4911 #endif
4912 if (ira_hard_reg_set_intersection_p (hard_regno, mode, conflict_hard_regs)
4913 || (TEST_HARD_REG_BIT
4914 (ira_prohibited_class_mode_regs[aclass][mode], hard_regno)))
4915 continue;
4916 if (costs == NULL)
4918 best_hard_regno = hard_regno;
4919 break;
4921 cost = costs[j];
4922 if (min_cost > cost)
4924 min_cost = cost;
4925 best_hard_regno = hard_regno;
4928 if (best_hard_regno < 0)
4929 continue;
4930 ALLOCNO_HARD_REGNO (a) = hard_regno = best_hard_regno;
4931 for (l = 0; l < nr; l++)
4933 ira_object_t obj = ALLOCNO_OBJECT (a, l);
4934 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4935 for (k = r->start; k <= r->finish; k++)
4936 used_hard_regs[k] |= ira_reg_mode_hard_regset[hard_regno][mode];
4939 ira_free (sorted_allocnos);
4940 ira_free (used_hard_regs);
4941 ira_free (allocno_priorities);
4942 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
4943 ira_print_disposition (ira_dump_file);
4948 /* Entry function doing coloring. */
4949 void
4950 ira_color (void)
4952 ira_allocno_t a;
4953 ira_allocno_iterator ai;
4955 /* Setup updated costs. */
4956 FOR_EACH_ALLOCNO (a, ai)
4958 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
4959 ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
4961 if (ira_conflicts_p)
4962 color ();
4963 else
4964 fast_allocation ();