Remove extra newline
[official-gcc.git] / gcc / ira-color.c
blob0ffdd1920204ce6bc4c41497a5bb25a4040ecb21
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, end;
485 for (start = end = -1, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
487 bool reg_included = TEST_HARD_REG_BIT (set, i);
489 if (reg_included)
491 if (start == -1)
492 start = i;
493 end = i;
495 if (start >= 0 && (!reg_included || i == FIRST_PSEUDO_REGISTER - 1))
497 if (start == end)
498 fprintf (f, " %d", start);
499 else if (start == end + 1)
500 fprintf (f, " %d %d", start, end);
501 else
502 fprintf (f, " %d-%d", start, end);
503 start = -1;
506 if (new_line_p)
507 fprintf (f, "\n");
510 /* Print allocno hard register subforest given by ROOTS and its LEVEL
511 to F. */
512 static void
513 print_hard_regs_subforest (FILE *f, allocno_hard_regs_node_t roots,
514 int level)
516 int i;
517 allocno_hard_regs_node_t node;
519 for (node = roots; node != NULL; node = node->next)
521 fprintf (f, " ");
522 for (i = 0; i < level * 2; i++)
523 fprintf (f, " ");
524 fprintf (f, "%d:(", node->preorder_num);
525 print_hard_reg_set (f, node->hard_regs->set, false);
526 fprintf (f, ")@%" PRId64"\n", node->hard_regs->cost);
527 print_hard_regs_subforest (f, node->first, level + 1);
531 /* Print the allocno hard register forest to F. */
532 static void
533 print_hard_regs_forest (FILE *f)
535 fprintf (f, " Hard reg set forest:\n");
536 print_hard_regs_subforest (f, hard_regs_roots, 1);
539 /* Print the allocno hard register forest to stderr. */
540 void
541 ira_debug_hard_regs_forest (void)
543 print_hard_regs_forest (stderr);
546 /* Remove unused allocno hard registers nodes from forest given by its
547 *ROOTS. */
548 static void
549 remove_unused_allocno_hard_regs_nodes (allocno_hard_regs_node_t *roots)
551 allocno_hard_regs_node_t node, prev, next, last;
553 for (prev = NULL, node = *roots; node != NULL; node = next)
555 next = node->next;
556 if (node->used_p)
558 remove_unused_allocno_hard_regs_nodes (&node->first);
559 prev = node;
561 else
563 for (last = node->first;
564 last != NULL && last->next != NULL;
565 last = last->next)
567 if (last != NULL)
569 if (prev == NULL)
570 *roots = node->first;
571 else
572 prev->next = node->first;
573 if (next != NULL)
574 next->prev = last;
575 last->next = next;
576 next = node->first;
578 else
580 if (prev == NULL)
581 *roots = next;
582 else
583 prev->next = next;
584 if (next != NULL)
585 next->prev = prev;
587 ira_free (node);
592 /* Set up fields preorder_num starting with START_NUM in all allocno
593 hard registers nodes in forest given by FIRST. Return biggest set
594 PREORDER_NUM increased by 1. */
595 static int
596 enumerate_allocno_hard_regs_nodes (allocno_hard_regs_node_t first,
597 allocno_hard_regs_node_t parent,
598 int start_num)
600 allocno_hard_regs_node_t node;
602 for (node = first; node != NULL; node = node->next)
604 node->preorder_num = start_num++;
605 node->parent = parent;
606 start_num = enumerate_allocno_hard_regs_nodes (node->first, node,
607 start_num);
609 return start_num;
612 /* Number of allocno hard registers nodes in the forest. */
613 static int allocno_hard_regs_nodes_num;
615 /* Table preorder number of allocno hard registers node in the forest
616 -> the allocno hard registers node. */
617 static allocno_hard_regs_node_t *allocno_hard_regs_nodes;
619 /* See below. */
620 typedef struct allocno_hard_regs_subnode *allocno_hard_regs_subnode_t;
622 /* The structure is used to describes all subnodes (not only immediate
623 ones) in the mentioned above tree for given allocno hard register
624 node. The usage of such data accelerates calculation of
625 colorability of given allocno. */
626 struct allocno_hard_regs_subnode
628 /* The conflict size of conflicting allocnos whose hard register
629 sets are equal sets (plus supersets if given node is given
630 allocno hard registers node) of one in the given node. */
631 int left_conflict_size;
632 /* The summary conflict size of conflicting allocnos whose hard
633 register sets are strict subsets of one in the given node.
634 Overall conflict size is
635 left_conflict_subnodes_size
636 + MIN (max_node_impact - left_conflict_subnodes_size,
637 left_conflict_size)
639 short left_conflict_subnodes_size;
640 short max_node_impact;
643 /* Container for hard regs subnodes of all allocnos. */
644 static allocno_hard_regs_subnode_t allocno_hard_regs_subnodes;
646 /* Table (preorder number of allocno hard registers node in the
647 forest, preorder number of allocno hard registers subnode) -> index
648 of the subnode relative to the node. -1 if it is not a
649 subnode. */
650 static int *allocno_hard_regs_subnode_index;
652 /* Setup arrays ALLOCNO_HARD_REGS_NODES and
653 ALLOCNO_HARD_REGS_SUBNODE_INDEX. */
654 static void
655 setup_allocno_hard_regs_subnode_index (allocno_hard_regs_node_t first)
657 allocno_hard_regs_node_t node, parent;
658 int index;
660 for (node = first; node != NULL; node = node->next)
662 allocno_hard_regs_nodes[node->preorder_num] = node;
663 for (parent = node; parent != NULL; parent = parent->parent)
665 index = parent->preorder_num * allocno_hard_regs_nodes_num;
666 allocno_hard_regs_subnode_index[index + node->preorder_num]
667 = node->preorder_num - parent->preorder_num;
669 setup_allocno_hard_regs_subnode_index (node->first);
673 /* Count all allocno hard registers nodes in tree ROOT. */
674 static int
675 get_allocno_hard_regs_subnodes_num (allocno_hard_regs_node_t root)
677 int len = 1;
679 for (root = root->first; root != NULL; root = root->next)
680 len += get_allocno_hard_regs_subnodes_num (root);
681 return len;
684 /* Build the forest of allocno hard registers nodes and assign each
685 allocno a node from the forest. */
686 static void
687 form_allocno_hard_regs_nodes_forest (void)
689 unsigned int i, j, size, len;
690 int start;
691 ira_allocno_t a;
692 allocno_hard_regs_t hv;
693 bitmap_iterator bi;
694 HARD_REG_SET temp;
695 allocno_hard_regs_node_t node, allocno_hard_regs_node;
696 allocno_color_data_t allocno_data;
698 node_check_tick = 0;
699 init_allocno_hard_regs ();
700 hard_regs_roots = NULL;
701 hard_regs_node_vec.create (100);
702 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
703 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
705 CLEAR_HARD_REG_SET (temp);
706 SET_HARD_REG_BIT (temp, i);
707 hv = add_allocno_hard_regs (temp, 0);
708 node = create_new_allocno_hard_regs_node (hv);
709 add_new_allocno_hard_regs_node_to_forest (&hard_regs_roots, node);
711 start = allocno_hard_regs_vec.length ();
712 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
714 a = ira_allocnos[i];
715 allocno_data = ALLOCNO_COLOR_DATA (a);
717 if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
718 continue;
719 hv = (add_allocno_hard_regs
720 (allocno_data->profitable_hard_regs,
721 ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a)));
723 temp = ~ira_no_alloc_regs;
724 add_allocno_hard_regs (temp, 0);
725 qsort (allocno_hard_regs_vec.address () + start,
726 allocno_hard_regs_vec.length () - start,
727 sizeof (allocno_hard_regs_t), allocno_hard_regs_compare);
728 for (i = start;
729 allocno_hard_regs_vec.iterate (i, &hv);
730 i++)
732 add_allocno_hard_regs_to_forest (&hard_regs_roots, hv);
733 ira_assert (hard_regs_node_vec.length () == 0);
735 /* We need to set up parent fields for right work of
736 first_common_ancestor_node. */
737 setup_allocno_hard_regs_nodes_parent (hard_regs_roots, NULL);
738 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
740 a = ira_allocnos[i];
741 allocno_data = ALLOCNO_COLOR_DATA (a);
742 if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
743 continue;
744 hard_regs_node_vec.truncate (0);
745 collect_allocno_hard_regs_cover (hard_regs_roots,
746 allocno_data->profitable_hard_regs);
747 allocno_hard_regs_node = NULL;
748 for (j = 0; hard_regs_node_vec.iterate (j, &node); j++)
749 allocno_hard_regs_node
750 = (j == 0
751 ? node
752 : first_common_ancestor_node (node, allocno_hard_regs_node));
753 /* That is a temporary storage. */
754 allocno_hard_regs_node->used_p = true;
755 allocno_data->hard_regs_node = allocno_hard_regs_node;
757 ira_assert (hard_regs_roots->next == NULL);
758 hard_regs_roots->used_p = true;
759 remove_unused_allocno_hard_regs_nodes (&hard_regs_roots);
760 allocno_hard_regs_nodes_num
761 = enumerate_allocno_hard_regs_nodes (hard_regs_roots, NULL, 0);
762 allocno_hard_regs_nodes
763 = ((allocno_hard_regs_node_t *)
764 ira_allocate (allocno_hard_regs_nodes_num
765 * sizeof (allocno_hard_regs_node_t)));
766 size = allocno_hard_regs_nodes_num * allocno_hard_regs_nodes_num;
767 allocno_hard_regs_subnode_index
768 = (int *) ira_allocate (size * sizeof (int));
769 for (i = 0; i < size; i++)
770 allocno_hard_regs_subnode_index[i] = -1;
771 setup_allocno_hard_regs_subnode_index (hard_regs_roots);
772 start = 0;
773 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
775 a = ira_allocnos[i];
776 allocno_data = ALLOCNO_COLOR_DATA (a);
777 if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
778 continue;
779 len = get_allocno_hard_regs_subnodes_num (allocno_data->hard_regs_node);
780 allocno_data->hard_regs_subnodes_start = start;
781 allocno_data->hard_regs_subnodes_num = len;
782 start += len;
784 allocno_hard_regs_subnodes
785 = ((allocno_hard_regs_subnode_t)
786 ira_allocate (sizeof (struct allocno_hard_regs_subnode) * start));
787 hard_regs_node_vec.release ();
790 /* Free tree of allocno hard registers nodes given by its ROOT. */
791 static void
792 finish_allocno_hard_regs_nodes_tree (allocno_hard_regs_node_t root)
794 allocno_hard_regs_node_t child, next;
796 for (child = root->first; child != NULL; child = next)
798 next = child->next;
799 finish_allocno_hard_regs_nodes_tree (child);
801 ira_free (root);
804 /* Finish work with the forest of allocno hard registers nodes. */
805 static void
806 finish_allocno_hard_regs_nodes_forest (void)
808 allocno_hard_regs_node_t node, next;
810 ira_free (allocno_hard_regs_subnodes);
811 for (node = hard_regs_roots; node != NULL; node = next)
813 next = node->next;
814 finish_allocno_hard_regs_nodes_tree (node);
816 ira_free (allocno_hard_regs_nodes);
817 ira_free (allocno_hard_regs_subnode_index);
818 finish_allocno_hard_regs ();
821 /* Set up left conflict sizes and left conflict subnodes sizes of hard
822 registers subnodes of allocno A. Return TRUE if allocno A is
823 trivially colorable. */
824 static bool
825 setup_left_conflict_sizes_p (ira_allocno_t a)
827 int i, k, nobj, start;
828 int conflict_size, left_conflict_subnodes_size, node_preorder_num;
829 allocno_color_data_t data;
830 HARD_REG_SET profitable_hard_regs;
831 allocno_hard_regs_subnode_t subnodes;
832 allocno_hard_regs_node_t node;
833 HARD_REG_SET node_set;
835 nobj = ALLOCNO_NUM_OBJECTS (a);
836 data = ALLOCNO_COLOR_DATA (a);
837 subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
838 profitable_hard_regs = data->profitable_hard_regs;
839 node = data->hard_regs_node;
840 node_preorder_num = node->preorder_num;
841 node_set = node->hard_regs->set;
842 node_check_tick++;
843 for (k = 0; k < nobj; k++)
845 ira_object_t obj = ALLOCNO_OBJECT (a, k);
846 ira_object_t conflict_obj;
847 ira_object_conflict_iterator oci;
849 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
851 int size;
852 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
853 allocno_hard_regs_node_t conflict_node, temp_node;
854 HARD_REG_SET conflict_node_set;
855 allocno_color_data_t conflict_data;
857 conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
858 if (! ALLOCNO_COLOR_DATA (conflict_a)->in_graph_p
859 || ! hard_reg_set_intersect_p (profitable_hard_regs,
860 conflict_data
861 ->profitable_hard_regs))
862 continue;
863 conflict_node = conflict_data->hard_regs_node;
864 conflict_node_set = conflict_node->hard_regs->set;
865 if (hard_reg_set_subset_p (node_set, conflict_node_set))
866 temp_node = node;
867 else
869 ira_assert (hard_reg_set_subset_p (conflict_node_set, node_set));
870 temp_node = conflict_node;
872 if (temp_node->check != node_check_tick)
874 temp_node->check = node_check_tick;
875 temp_node->conflict_size = 0;
877 size = (ira_reg_class_max_nregs
878 [ALLOCNO_CLASS (conflict_a)][ALLOCNO_MODE (conflict_a)]);
879 if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1)
880 /* We will deal with the subwords individually. */
881 size = 1;
882 temp_node->conflict_size += size;
885 for (i = 0; i < data->hard_regs_subnodes_num; i++)
887 allocno_hard_regs_node_t temp_node;
889 temp_node = allocno_hard_regs_nodes[i + node_preorder_num];
890 ira_assert (temp_node->preorder_num == i + node_preorder_num);
891 subnodes[i].left_conflict_size = (temp_node->check != node_check_tick
892 ? 0 : temp_node->conflict_size);
893 if (hard_reg_set_subset_p (temp_node->hard_regs->set,
894 profitable_hard_regs))
895 subnodes[i].max_node_impact = temp_node->hard_regs_num;
896 else
898 HARD_REG_SET temp_set;
899 int j, n, hard_regno;
900 enum reg_class aclass;
902 temp_set = temp_node->hard_regs->set & profitable_hard_regs;
903 aclass = ALLOCNO_CLASS (a);
904 for (n = 0, j = ira_class_hard_regs_num[aclass] - 1; j >= 0; j--)
906 hard_regno = ira_class_hard_regs[aclass][j];
907 if (TEST_HARD_REG_BIT (temp_set, hard_regno))
908 n++;
910 subnodes[i].max_node_impact = n;
912 subnodes[i].left_conflict_subnodes_size = 0;
914 start = node_preorder_num * allocno_hard_regs_nodes_num;
915 for (i = data->hard_regs_subnodes_num - 1; i > 0; i--)
917 int size, parent_i;
918 allocno_hard_regs_node_t parent;
920 size = (subnodes[i].left_conflict_subnodes_size
921 + MIN (subnodes[i].max_node_impact
922 - subnodes[i].left_conflict_subnodes_size,
923 subnodes[i].left_conflict_size));
924 parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
925 gcc_checking_assert(parent);
926 parent_i
927 = allocno_hard_regs_subnode_index[start + parent->preorder_num];
928 gcc_checking_assert(parent_i >= 0);
929 subnodes[parent_i].left_conflict_subnodes_size += size;
931 left_conflict_subnodes_size = subnodes[0].left_conflict_subnodes_size;
932 conflict_size
933 = (left_conflict_subnodes_size
934 + MIN (subnodes[0].max_node_impact - left_conflict_subnodes_size,
935 subnodes[0].left_conflict_size));
936 conflict_size += ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
937 data->colorable_p = conflict_size <= data->available_regs_num;
938 return data->colorable_p;
941 /* Update left conflict sizes of hard registers subnodes of allocno A
942 after removing allocno REMOVED_A with SIZE from the conflict graph.
943 Return TRUE if A is trivially colorable. */
944 static bool
945 update_left_conflict_sizes_p (ira_allocno_t a,
946 ira_allocno_t removed_a, int size)
948 int i, conflict_size, before_conflict_size, diff, start;
949 int node_preorder_num, parent_i;
950 allocno_hard_regs_node_t node, removed_node, parent;
951 allocno_hard_regs_subnode_t subnodes;
952 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
954 ira_assert (! data->colorable_p);
955 node = data->hard_regs_node;
956 node_preorder_num = node->preorder_num;
957 removed_node = ALLOCNO_COLOR_DATA (removed_a)->hard_regs_node;
958 ira_assert (hard_reg_set_subset_p (removed_node->hard_regs->set,
959 node->hard_regs->set)
960 || hard_reg_set_subset_p (node->hard_regs->set,
961 removed_node->hard_regs->set));
962 start = node_preorder_num * allocno_hard_regs_nodes_num;
963 i = allocno_hard_regs_subnode_index[start + removed_node->preorder_num];
964 if (i < 0)
965 i = 0;
966 subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
967 before_conflict_size
968 = (subnodes[i].left_conflict_subnodes_size
969 + MIN (subnodes[i].max_node_impact
970 - subnodes[i].left_conflict_subnodes_size,
971 subnodes[i].left_conflict_size));
972 subnodes[i].left_conflict_size -= size;
973 for (;;)
975 conflict_size
976 = (subnodes[i].left_conflict_subnodes_size
977 + MIN (subnodes[i].max_node_impact
978 - subnodes[i].left_conflict_subnodes_size,
979 subnodes[i].left_conflict_size));
980 if ((diff = before_conflict_size - conflict_size) == 0)
981 break;
982 ira_assert (conflict_size < before_conflict_size);
983 parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
984 if (parent == NULL)
985 break;
986 parent_i
987 = allocno_hard_regs_subnode_index[start + parent->preorder_num];
988 if (parent_i < 0)
989 break;
990 i = parent_i;
991 before_conflict_size
992 = (subnodes[i].left_conflict_subnodes_size
993 + MIN (subnodes[i].max_node_impact
994 - subnodes[i].left_conflict_subnodes_size,
995 subnodes[i].left_conflict_size));
996 subnodes[i].left_conflict_subnodes_size -= diff;
998 if (i != 0
999 || (conflict_size
1000 + ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
1001 > data->available_regs_num))
1002 return false;
1003 data->colorable_p = true;
1004 return true;
1007 /* Return true if allocno A has empty profitable hard regs. */
1008 static bool
1009 empty_profitable_hard_regs (ira_allocno_t a)
1011 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
1013 return hard_reg_set_empty_p (data->profitable_hard_regs);
1016 /* Set up profitable hard registers for each allocno being
1017 colored. */
1018 static void
1019 setup_profitable_hard_regs (void)
1021 unsigned int i;
1022 int j, k, nobj, hard_regno, nregs, class_size;
1023 ira_allocno_t a;
1024 bitmap_iterator bi;
1025 enum reg_class aclass;
1026 machine_mode mode;
1027 allocno_color_data_t data;
1029 /* Initial set up from allocno classes and explicitly conflicting
1030 hard regs. */
1031 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1033 a = ira_allocnos[i];
1034 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS)
1035 continue;
1036 data = ALLOCNO_COLOR_DATA (a);
1037 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL
1038 && ALLOCNO_CLASS_COST (a) > ALLOCNO_MEMORY_COST (a)
1039 /* Do not empty profitable regs for static chain pointer
1040 pseudo when non-local goto is used. */
1041 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1042 CLEAR_HARD_REG_SET (data->profitable_hard_regs);
1043 else
1045 mode = ALLOCNO_MODE (a);
1046 data->profitable_hard_regs
1047 = ira_useful_class_mode_regs[aclass][mode];
1048 nobj = ALLOCNO_NUM_OBJECTS (a);
1049 for (k = 0; k < nobj; k++)
1051 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1053 data->profitable_hard_regs
1054 &= ~OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
1058 /* Exclude hard regs already assigned for conflicting objects. */
1059 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, i, bi)
1061 a = ira_allocnos[i];
1062 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1063 || ! ALLOCNO_ASSIGNED_P (a)
1064 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1065 continue;
1066 mode = ALLOCNO_MODE (a);
1067 nregs = hard_regno_nregs (hard_regno, mode);
1068 nobj = ALLOCNO_NUM_OBJECTS (a);
1069 for (k = 0; k < nobj; k++)
1071 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1072 ira_object_t conflict_obj;
1073 ira_object_conflict_iterator oci;
1075 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1077 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1079 /* We can process the conflict allocno repeatedly with
1080 the same result. */
1081 if (nregs == nobj && nregs > 1)
1083 int num = OBJECT_SUBWORD (conflict_obj);
1085 if (REG_WORDS_BIG_ENDIAN)
1086 CLEAR_HARD_REG_BIT
1087 (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1088 hard_regno + nobj - num - 1);
1089 else
1090 CLEAR_HARD_REG_BIT
1091 (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1092 hard_regno + num);
1094 else
1095 ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs
1096 &= ~ira_reg_mode_hard_regset[hard_regno][mode];
1100 /* Exclude too costly hard regs. */
1101 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1103 int min_cost = INT_MAX;
1104 int *costs;
1106 a = ira_allocnos[i];
1107 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1108 || empty_profitable_hard_regs (a))
1109 continue;
1110 data = ALLOCNO_COLOR_DATA (a);
1111 if ((costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a)) != NULL
1112 || (costs = ALLOCNO_HARD_REG_COSTS (a)) != NULL)
1114 class_size = ira_class_hard_regs_num[aclass];
1115 for (j = 0; j < class_size; j++)
1117 hard_regno = ira_class_hard_regs[aclass][j];
1118 if (! TEST_HARD_REG_BIT (data->profitable_hard_regs,
1119 hard_regno))
1120 continue;
1121 if (ALLOCNO_UPDATED_MEMORY_COST (a) < costs[j]
1122 /* Do not remove HARD_REGNO for static chain pointer
1123 pseudo when non-local goto is used. */
1124 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1125 CLEAR_HARD_REG_BIT (data->profitable_hard_regs,
1126 hard_regno);
1127 else if (min_cost > costs[j])
1128 min_cost = costs[j];
1131 else if (ALLOCNO_UPDATED_MEMORY_COST (a)
1132 < ALLOCNO_UPDATED_CLASS_COST (a)
1133 /* Do not empty profitable regs for static chain
1134 pointer pseudo when non-local goto is used. */
1135 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1136 CLEAR_HARD_REG_SET (data->profitable_hard_regs);
1137 if (ALLOCNO_UPDATED_CLASS_COST (a) > min_cost)
1138 ALLOCNO_UPDATED_CLASS_COST (a) = min_cost;
1144 /* This page contains functions used to choose hard registers for
1145 allocnos. */
1147 /* Pool for update cost records. */
1148 static object_allocator<update_cost_record> update_cost_record_pool
1149 ("update cost records");
1151 /* Return new update cost record with given params. */
1152 static struct update_cost_record *
1153 get_update_cost_record (int hard_regno, int divisor,
1154 struct update_cost_record *next)
1156 struct update_cost_record *record;
1158 record = update_cost_record_pool.allocate ();
1159 record->hard_regno = hard_regno;
1160 record->divisor = divisor;
1161 record->next = next;
1162 return record;
1165 /* Free memory for all records in LIST. */
1166 static void
1167 free_update_cost_record_list (struct update_cost_record *list)
1169 struct update_cost_record *next;
1171 while (list != NULL)
1173 next = list->next;
1174 update_cost_record_pool.remove (list);
1175 list = next;
1179 /* Free memory allocated for all update cost records. */
1180 static void
1181 finish_update_cost_records (void)
1183 update_cost_record_pool.release ();
1186 /* Array whose element value is TRUE if the corresponding hard
1187 register was already allocated for an allocno. */
1188 static bool allocated_hardreg_p[FIRST_PSEUDO_REGISTER];
1190 /* Describes one element in a queue of allocnos whose costs need to be
1191 updated. Each allocno in the queue is known to have an allocno
1192 class. */
1193 struct update_cost_queue_elem
1195 /* This element is in the queue iff CHECK == update_cost_check. */
1196 int check;
1198 /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
1199 connecting this allocno to the one being allocated. */
1200 int divisor;
1202 /* Allocno from which we started chaining costs of connected
1203 allocnos. */
1204 ira_allocno_t start;
1206 /* Allocno from which we are chaining costs of connected allocnos.
1207 It is used not go back in graph of allocnos connected by
1208 copies. */
1209 ira_allocno_t from;
1211 /* The next allocno in the queue, or null if this is the last element. */
1212 ira_allocno_t next;
1215 /* The first element in a queue of allocnos whose copy costs need to be
1216 updated. Null if the queue is empty. */
1217 static ira_allocno_t update_cost_queue;
1219 /* The last element in the queue described by update_cost_queue.
1220 Not valid if update_cost_queue is null. */
1221 static struct update_cost_queue_elem *update_cost_queue_tail;
1223 /* A pool of elements in the queue described by update_cost_queue.
1224 Elements are indexed by ALLOCNO_NUM. */
1225 static struct update_cost_queue_elem *update_cost_queue_elems;
1227 /* The current value of update_costs_from_copies call count. */
1228 static int update_cost_check;
1230 /* Allocate and initialize data necessary for function
1231 update_costs_from_copies. */
1232 static void
1233 initiate_cost_update (void)
1235 size_t size;
1237 size = ira_allocnos_num * sizeof (struct update_cost_queue_elem);
1238 update_cost_queue_elems
1239 = (struct update_cost_queue_elem *) ira_allocate (size);
1240 memset (update_cost_queue_elems, 0, size);
1241 update_cost_check = 0;
1244 /* Deallocate data used by function update_costs_from_copies. */
1245 static void
1246 finish_cost_update (void)
1248 ira_free (update_cost_queue_elems);
1249 finish_update_cost_records ();
1252 /* When we traverse allocnos to update hard register costs, the cost
1253 divisor will be multiplied by the following macro value for each
1254 hop from given allocno to directly connected allocnos. */
1255 #define COST_HOP_DIVISOR 4
1257 /* Start a new cost-updating pass. */
1258 static void
1259 start_update_cost (void)
1261 update_cost_check++;
1262 update_cost_queue = NULL;
1265 /* Add (ALLOCNO, START, FROM, DIVISOR) to the end of update_cost_queue, unless
1266 ALLOCNO is already in the queue, or has NO_REGS class. */
1267 static inline void
1268 queue_update_cost (ira_allocno_t allocno, ira_allocno_t start,
1269 ira_allocno_t from, int divisor)
1271 struct update_cost_queue_elem *elem;
1273 elem = &update_cost_queue_elems[ALLOCNO_NUM (allocno)];
1274 if (elem->check != update_cost_check
1275 && ALLOCNO_CLASS (allocno) != NO_REGS)
1277 elem->check = update_cost_check;
1278 elem->start = start;
1279 elem->from = from;
1280 elem->divisor = divisor;
1281 elem->next = NULL;
1282 if (update_cost_queue == NULL)
1283 update_cost_queue = allocno;
1284 else
1285 update_cost_queue_tail->next = allocno;
1286 update_cost_queue_tail = elem;
1290 /* Try to remove the first element from update_cost_queue. Return
1291 false if the queue was empty, otherwise make (*ALLOCNO, *START,
1292 *FROM, *DIVISOR) describe the removed element. */
1293 static inline bool
1294 get_next_update_cost (ira_allocno_t *allocno, ira_allocno_t *start,
1295 ira_allocno_t *from, int *divisor)
1297 struct update_cost_queue_elem *elem;
1299 if (update_cost_queue == NULL)
1300 return false;
1302 *allocno = update_cost_queue;
1303 elem = &update_cost_queue_elems[ALLOCNO_NUM (*allocno)];
1304 *start = elem->start;
1305 *from = elem->from;
1306 *divisor = elem->divisor;
1307 update_cost_queue = elem->next;
1308 return true;
1311 /* Increase costs of HARD_REGNO by UPDATE_COST and conflict cost by
1312 UPDATE_CONFLICT_COST for ALLOCNO. Return true if we really
1313 modified the cost. */
1314 static bool
1315 update_allocno_cost (ira_allocno_t allocno, int hard_regno,
1316 int update_cost, int update_conflict_cost)
1318 int i;
1319 enum reg_class aclass = ALLOCNO_CLASS (allocno);
1321 i = ira_class_hard_reg_index[aclass][hard_regno];
1322 if (i < 0)
1323 return false;
1324 ira_allocate_and_set_or_copy_costs
1325 (&ALLOCNO_UPDATED_HARD_REG_COSTS (allocno), aclass,
1326 ALLOCNO_UPDATED_CLASS_COST (allocno),
1327 ALLOCNO_HARD_REG_COSTS (allocno));
1328 ira_allocate_and_set_or_copy_costs
1329 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno),
1330 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (allocno));
1331 ALLOCNO_UPDATED_HARD_REG_COSTS (allocno)[i] += update_cost;
1332 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno)[i] += update_conflict_cost;
1333 return true;
1336 /* Return TRUE if allocnos A1 and A2 conflicts. Here we are
1337 interesting only in conflicts of allocnos with intersected allocno
1338 classes. */
1339 static bool
1340 allocnos_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
1342 ira_object_t obj, conflict_obj;
1343 ira_object_conflict_iterator oci;
1344 int word, nwords = ALLOCNO_NUM_OBJECTS (a1);
1346 for (word = 0; word < nwords; word++)
1348 obj = ALLOCNO_OBJECT (a1, word);
1349 /* Take preferences of conflicting allocnos into account. */
1350 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1351 if (OBJECT_ALLOCNO (conflict_obj) == a2)
1352 return true;
1354 return false;
1357 /* Update (decrease if DECR_P) HARD_REGNO cost of allocnos connected
1358 by copies to ALLOCNO to increase chances to remove some copies as
1359 the result of subsequent assignment. Update conflict costs only
1360 for true CONFLICT_COST_UPDATE_P. Record cost updates if RECORD_P is
1361 true. */
1362 static void
1363 update_costs_from_allocno (ira_allocno_t allocno, int hard_regno,
1364 int divisor, bool decr_p, bool record_p,
1365 bool conflict_cost_update_p)
1367 int cost, update_cost, update_conflict_cost;
1368 machine_mode mode;
1369 enum reg_class rclass, aclass;
1370 ira_allocno_t another_allocno, start = allocno, from = NULL;
1371 ira_copy_t cp, next_cp;
1373 rclass = REGNO_REG_CLASS (hard_regno);
1376 mode = ALLOCNO_MODE (allocno);
1377 ira_init_register_move_cost_if_necessary (mode);
1378 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1380 if (cp->first == allocno)
1382 next_cp = cp->next_first_allocno_copy;
1383 another_allocno = cp->second;
1385 else if (cp->second == allocno)
1387 next_cp = cp->next_second_allocno_copy;
1388 another_allocno = cp->first;
1390 else
1391 gcc_unreachable ();
1393 if (another_allocno == from
1394 || allocnos_conflict_p (another_allocno, start))
1395 continue;
1397 aclass = ALLOCNO_CLASS (another_allocno);
1398 if (! TEST_HARD_REG_BIT (reg_class_contents[aclass],
1399 hard_regno)
1400 || ALLOCNO_ASSIGNED_P (another_allocno))
1401 continue;
1403 /* If we have different modes use the smallest one. It is
1404 a sub-register move. It is hard to predict what LRA
1405 will reload (the pseudo or its sub-register) but LRA
1406 will try to minimize the data movement. Also for some
1407 register classes bigger modes might be invalid,
1408 e.g. DImode for AREG on x86. For such cases the
1409 register move cost will be maximal. */
1410 mode = narrower_subreg_mode (mode, ALLOCNO_MODE (cp->second));
1411 ira_init_register_move_cost_if_necessary (mode);
1413 cost = (cp->second == allocno
1414 ? ira_register_move_cost[mode][rclass][aclass]
1415 : ira_register_move_cost[mode][aclass][rclass]);
1416 if (decr_p)
1417 cost = -cost;
1419 update_cost = cp->freq * cost / divisor;
1420 update_conflict_cost = conflict_cost_update_p ? update_cost : 0;
1422 if (ALLOCNO_COLOR_DATA (another_allocno) != NULL
1423 && (ALLOCNO_COLOR_DATA (allocno)->first_thread_allocno
1424 != ALLOCNO_COLOR_DATA (another_allocno)->first_thread_allocno))
1425 /* Decrease conflict cost of ANOTHER_ALLOCNO if it is not
1426 in the same allocation thread. */
1427 update_conflict_cost /= COST_HOP_DIVISOR;
1429 if (update_cost == 0)
1430 continue;
1432 if (! update_allocno_cost (another_allocno, hard_regno,
1433 update_cost, update_conflict_cost))
1434 continue;
1435 queue_update_cost (another_allocno, start, allocno,
1436 divisor * COST_HOP_DIVISOR);
1437 if (record_p && ALLOCNO_COLOR_DATA (another_allocno) != NULL)
1438 ALLOCNO_COLOR_DATA (another_allocno)->update_cost_records
1439 = get_update_cost_record (hard_regno, divisor,
1440 ALLOCNO_COLOR_DATA (another_allocno)
1441 ->update_cost_records);
1444 while (get_next_update_cost (&allocno, &start, &from, &divisor));
1447 /* Decrease preferred ALLOCNO hard register costs and costs of
1448 allocnos connected to ALLOCNO through copy. */
1449 static void
1450 update_costs_from_prefs (ira_allocno_t allocno)
1452 ira_pref_t pref;
1454 start_update_cost ();
1455 for (pref = ALLOCNO_PREFS (allocno); pref != NULL; pref = pref->next_pref)
1456 update_costs_from_allocno (allocno, pref->hard_regno,
1457 COST_HOP_DIVISOR, true, true, false);
1460 /* Update (decrease if DECR_P) the cost of allocnos connected to
1461 ALLOCNO through copies to increase chances to remove some copies as
1462 the result of subsequent assignment. ALLOCNO was just assigned to
1463 a hard register. Record cost updates if RECORD_P is true. */
1464 static void
1465 update_costs_from_copies (ira_allocno_t allocno, bool decr_p, bool record_p)
1467 int hard_regno;
1469 hard_regno = ALLOCNO_HARD_REGNO (allocno);
1470 ira_assert (hard_regno >= 0 && ALLOCNO_CLASS (allocno) != NO_REGS);
1471 start_update_cost ();
1472 update_costs_from_allocno (allocno, hard_regno, 1, decr_p, record_p, true);
1475 /* Update conflict_allocno_hard_prefs of allocnos conflicting with
1476 ALLOCNO. */
1477 static void
1478 update_conflict_allocno_hard_prefs (ira_allocno_t allocno)
1480 int l, nr = ALLOCNO_NUM_OBJECTS (allocno);
1482 for (l = 0; l < nr; l++)
1484 ira_object_t conflict_obj, obj = ALLOCNO_OBJECT (allocno, l);
1485 ira_object_conflict_iterator oci;
1487 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1489 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1490 allocno_color_data_t conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
1491 ira_pref_t pref;
1493 if (!(hard_reg_set_intersect_p
1494 (ALLOCNO_COLOR_DATA (allocno)->profitable_hard_regs,
1495 conflict_data->profitable_hard_regs)))
1496 continue;
1497 for (pref = ALLOCNO_PREFS (allocno);
1498 pref != NULL;
1499 pref = pref->next_pref)
1500 conflict_data->conflict_allocno_hard_prefs += pref->freq;
1505 /* Restore costs of allocnos connected to ALLOCNO by copies as it was
1506 before updating costs of these allocnos from given allocno. This
1507 is a wise thing to do as if given allocno did not get an expected
1508 hard reg, using smaller cost of the hard reg for allocnos connected
1509 by copies to given allocno becomes actually misleading. Free all
1510 update cost records for ALLOCNO as we don't need them anymore. */
1511 static void
1512 restore_costs_from_copies (ira_allocno_t allocno)
1514 struct update_cost_record *records, *curr;
1516 if (ALLOCNO_COLOR_DATA (allocno) == NULL)
1517 return;
1518 records = ALLOCNO_COLOR_DATA (allocno)->update_cost_records;
1519 start_update_cost ();
1520 for (curr = records; curr != NULL; curr = curr->next)
1521 update_costs_from_allocno (allocno, curr->hard_regno,
1522 curr->divisor, true, false, true);
1523 free_update_cost_record_list (records);
1524 ALLOCNO_COLOR_DATA (allocno)->update_cost_records = NULL;
1527 /* This function updates COSTS (decrease if DECR_P) for hard_registers
1528 of ACLASS by conflict costs of the unassigned allocnos
1529 connected by copies with allocnos in update_cost_queue. This
1530 update increases chances to remove some copies. */
1531 static void
1532 update_conflict_hard_regno_costs (int *costs, enum reg_class aclass,
1533 bool decr_p)
1535 int i, cost, class_size, freq, mult, div, divisor;
1536 int index, hard_regno;
1537 int *conflict_costs;
1538 bool cont_p;
1539 enum reg_class another_aclass;
1540 ira_allocno_t allocno, another_allocno, start, from;
1541 ira_copy_t cp, next_cp;
1543 while (get_next_update_cost (&allocno, &start, &from, &divisor))
1544 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1546 if (cp->first == allocno)
1548 next_cp = cp->next_first_allocno_copy;
1549 another_allocno = cp->second;
1551 else if (cp->second == allocno)
1553 next_cp = cp->next_second_allocno_copy;
1554 another_allocno = cp->first;
1556 else
1557 gcc_unreachable ();
1559 if (another_allocno == from
1560 || allocnos_conflict_p (another_allocno, start))
1561 continue;
1563 another_aclass = ALLOCNO_CLASS (another_allocno);
1564 if (! ira_reg_classes_intersect_p[aclass][another_aclass]
1565 || ALLOCNO_ASSIGNED_P (another_allocno)
1566 || ALLOCNO_COLOR_DATA (another_allocno)->may_be_spilled_p)
1567 continue;
1568 class_size = ira_class_hard_regs_num[another_aclass];
1569 ira_allocate_and_copy_costs
1570 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
1571 another_aclass, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
1572 conflict_costs
1573 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno);
1574 if (conflict_costs == NULL)
1575 cont_p = true;
1576 else
1578 mult = cp->freq;
1579 freq = ALLOCNO_FREQ (another_allocno);
1580 if (freq == 0)
1581 freq = 1;
1582 div = freq * divisor;
1583 cont_p = false;
1584 for (i = class_size - 1; i >= 0; i--)
1586 hard_regno = ira_class_hard_regs[another_aclass][i];
1587 ira_assert (hard_regno >= 0);
1588 index = ira_class_hard_reg_index[aclass][hard_regno];
1589 if (index < 0)
1590 continue;
1591 cost = (int) (((int64_t) conflict_costs [i] * mult) / div);
1592 if (cost == 0)
1593 continue;
1594 cont_p = true;
1595 if (decr_p)
1596 cost = -cost;
1597 costs[index] += cost;
1600 /* Probably 5 hops will be enough. */
1601 if (cont_p
1602 && divisor <= (COST_HOP_DIVISOR
1603 * COST_HOP_DIVISOR
1604 * COST_HOP_DIVISOR
1605 * COST_HOP_DIVISOR))
1606 queue_update_cost (another_allocno, start, from, divisor * COST_HOP_DIVISOR);
1610 /* Set up conflicting (through CONFLICT_REGS) for each object of
1611 allocno A and the start allocno profitable regs (through
1612 START_PROFITABLE_REGS). Remember that the start profitable regs
1613 exclude hard regs which cannot hold value of mode of allocno A.
1614 This covers mostly cases when multi-register value should be
1615 aligned. */
1616 static inline void
1617 get_conflict_and_start_profitable_regs (ira_allocno_t a, bool retry_p,
1618 HARD_REG_SET *conflict_regs,
1619 HARD_REG_SET *start_profitable_regs)
1621 int i, nwords;
1622 ira_object_t obj;
1624 nwords = ALLOCNO_NUM_OBJECTS (a);
1625 for (i = 0; i < nwords; i++)
1627 obj = ALLOCNO_OBJECT (a, i);
1628 conflict_regs[i] = OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
1630 if (retry_p)
1631 *start_profitable_regs
1632 = (reg_class_contents[ALLOCNO_CLASS (a)]
1633 &~ (ira_prohibited_class_mode_regs
1634 [ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]));
1635 else
1636 *start_profitable_regs = ALLOCNO_COLOR_DATA (a)->profitable_hard_regs;
1639 /* Return true if HARD_REGNO is ok for assigning to allocno A with
1640 PROFITABLE_REGS and whose objects have CONFLICT_REGS. */
1641 static inline bool
1642 check_hard_reg_p (ira_allocno_t a, int hard_regno,
1643 HARD_REG_SET *conflict_regs, HARD_REG_SET profitable_regs)
1645 int j, nwords, nregs;
1646 enum reg_class aclass;
1647 machine_mode mode;
1649 aclass = ALLOCNO_CLASS (a);
1650 mode = ALLOCNO_MODE (a);
1651 if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[aclass][mode],
1652 hard_regno))
1653 return false;
1654 /* Checking only profitable hard regs. */
1655 if (! TEST_HARD_REG_BIT (profitable_regs, hard_regno))
1656 return false;
1657 nregs = hard_regno_nregs (hard_regno, mode);
1658 nwords = ALLOCNO_NUM_OBJECTS (a);
1659 for (j = 0; j < nregs; j++)
1661 int k;
1662 int set_to_test_start = 0, set_to_test_end = nwords;
1664 if (nregs == nwords)
1666 if (REG_WORDS_BIG_ENDIAN)
1667 set_to_test_start = nwords - j - 1;
1668 else
1669 set_to_test_start = j;
1670 set_to_test_end = set_to_test_start + 1;
1672 for (k = set_to_test_start; k < set_to_test_end; k++)
1673 if (TEST_HARD_REG_BIT (conflict_regs[k], hard_regno + j))
1674 break;
1675 if (k != set_to_test_end)
1676 break;
1678 return j == nregs;
1681 /* Return number of registers needed to be saved and restored at
1682 function prologue/epilogue if we allocate HARD_REGNO to hold value
1683 of MODE. */
1684 static int
1685 calculate_saved_nregs (int hard_regno, machine_mode mode)
1687 int i;
1688 int nregs = 0;
1690 ira_assert (hard_regno >= 0);
1691 for (i = hard_regno_nregs (hard_regno, mode) - 1; i >= 0; i--)
1692 if (!allocated_hardreg_p[hard_regno + i]
1693 && !crtl->abi->clobbers_full_reg_p (hard_regno + i)
1694 && !LOCAL_REGNO (hard_regno + i))
1695 nregs++;
1696 return nregs;
1699 /* Choose a hard register for allocno A. If RETRY_P is TRUE, it means
1700 that the function called from function
1701 `ira_reassign_conflict_allocnos' and `allocno_reload_assign'. In
1702 this case some allocno data are not defined or updated and we
1703 should not touch these data. The function returns true if we
1704 managed to assign a hard register to the allocno.
1706 To assign a hard register, first of all we calculate all conflict
1707 hard registers which can come from conflicting allocnos with
1708 already assigned hard registers. After that we find first free
1709 hard register with the minimal cost. During hard register cost
1710 calculation we take conflict hard register costs into account to
1711 give a chance for conflicting allocnos to get a better hard
1712 register in the future.
1714 If the best hard register cost is bigger than cost of memory usage
1715 for the allocno, we don't assign a hard register to given allocno
1716 at all.
1718 If we assign a hard register to the allocno, we update costs of the
1719 hard register for allocnos connected by copies to improve a chance
1720 to coalesce insns represented by the copies when we assign hard
1721 registers to the allocnos connected by the copies. */
1722 static bool
1723 assign_hard_reg (ira_allocno_t a, bool retry_p)
1725 HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
1726 int i, j, hard_regno, best_hard_regno, class_size;
1727 int cost, mem_cost, min_cost, full_cost, min_full_cost, nwords, word;
1728 int *a_costs;
1729 enum reg_class aclass;
1730 machine_mode mode;
1731 static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
1732 int saved_nregs;
1733 enum reg_class rclass;
1734 int add_cost;
1735 #ifdef STACK_REGS
1736 bool no_stack_reg_p;
1737 #endif
1739 ira_assert (! ALLOCNO_ASSIGNED_P (a));
1740 get_conflict_and_start_profitable_regs (a, retry_p,
1741 conflicting_regs,
1742 &profitable_hard_regs);
1743 aclass = ALLOCNO_CLASS (a);
1744 class_size = ira_class_hard_regs_num[aclass];
1745 best_hard_regno = -1;
1746 memset (full_costs, 0, sizeof (int) * class_size);
1747 mem_cost = 0;
1748 memset (costs, 0, sizeof (int) * class_size);
1749 memset (full_costs, 0, sizeof (int) * class_size);
1750 #ifdef STACK_REGS
1751 no_stack_reg_p = false;
1752 #endif
1753 if (! retry_p)
1754 start_update_cost ();
1755 mem_cost += ALLOCNO_UPDATED_MEMORY_COST (a);
1757 ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
1758 aclass, ALLOCNO_HARD_REG_COSTS (a));
1759 a_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
1760 #ifdef STACK_REGS
1761 no_stack_reg_p = no_stack_reg_p || ALLOCNO_TOTAL_NO_STACK_REG_P (a);
1762 #endif
1763 cost = ALLOCNO_UPDATED_CLASS_COST (a);
1764 for (i = 0; i < class_size; i++)
1765 if (a_costs != NULL)
1767 costs[i] += a_costs[i];
1768 full_costs[i] += a_costs[i];
1770 else
1772 costs[i] += cost;
1773 full_costs[i] += cost;
1775 nwords = ALLOCNO_NUM_OBJECTS (a);
1776 curr_allocno_process++;
1777 for (word = 0; word < nwords; word++)
1779 ira_object_t conflict_obj;
1780 ira_object_t obj = ALLOCNO_OBJECT (a, word);
1781 ira_object_conflict_iterator oci;
1783 /* Take preferences of conflicting allocnos into account. */
1784 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1786 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1787 enum reg_class conflict_aclass;
1788 allocno_color_data_t data = ALLOCNO_COLOR_DATA (conflict_a);
1790 /* Reload can give another class so we need to check all
1791 allocnos. */
1792 if (!retry_p
1793 && ((!ALLOCNO_ASSIGNED_P (conflict_a)
1794 || ALLOCNO_HARD_REGNO (conflict_a) < 0)
1795 && !(hard_reg_set_intersect_p
1796 (profitable_hard_regs,
1797 ALLOCNO_COLOR_DATA
1798 (conflict_a)->profitable_hard_regs))))
1800 /* All conflict allocnos are in consideration bitmap
1801 when retry_p is false. It might change in future and
1802 if it happens the assert will be broken. It means
1803 the code should be modified for the new
1804 assumptions. */
1805 ira_assert (bitmap_bit_p (consideration_allocno_bitmap,
1806 ALLOCNO_NUM (conflict_a)));
1807 continue;
1809 conflict_aclass = ALLOCNO_CLASS (conflict_a);
1810 ira_assert (ira_reg_classes_intersect_p
1811 [aclass][conflict_aclass]);
1812 if (ALLOCNO_ASSIGNED_P (conflict_a))
1814 hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
1815 if (hard_regno >= 0
1816 && (ira_hard_reg_set_intersection_p
1817 (hard_regno, ALLOCNO_MODE (conflict_a),
1818 reg_class_contents[aclass])))
1820 int n_objects = ALLOCNO_NUM_OBJECTS (conflict_a);
1821 int conflict_nregs;
1823 mode = ALLOCNO_MODE (conflict_a);
1824 conflict_nregs = hard_regno_nregs (hard_regno, mode);
1825 if (conflict_nregs == n_objects && conflict_nregs > 1)
1827 int num = OBJECT_SUBWORD (conflict_obj);
1829 if (REG_WORDS_BIG_ENDIAN)
1830 SET_HARD_REG_BIT (conflicting_regs[word],
1831 hard_regno + n_objects - num - 1);
1832 else
1833 SET_HARD_REG_BIT (conflicting_regs[word],
1834 hard_regno + num);
1836 else
1837 conflicting_regs[word]
1838 |= ira_reg_mode_hard_regset[hard_regno][mode];
1839 if (hard_reg_set_subset_p (profitable_hard_regs,
1840 conflicting_regs[word]))
1841 goto fail;
1844 else if (! retry_p
1845 && ! ALLOCNO_COLOR_DATA (conflict_a)->may_be_spilled_p
1846 /* Don't process the conflict allocno twice. */
1847 && (ALLOCNO_COLOR_DATA (conflict_a)->last_process
1848 != curr_allocno_process))
1850 int k, *conflict_costs;
1852 ALLOCNO_COLOR_DATA (conflict_a)->last_process
1853 = curr_allocno_process;
1854 ira_allocate_and_copy_costs
1855 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a),
1856 conflict_aclass,
1857 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_a));
1858 conflict_costs
1859 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a);
1860 if (conflict_costs != NULL)
1861 for (j = class_size - 1; j >= 0; j--)
1863 hard_regno = ira_class_hard_regs[aclass][j];
1864 ira_assert (hard_regno >= 0);
1865 k = ira_class_hard_reg_index[conflict_aclass][hard_regno];
1866 if (k < 0
1867 /* If HARD_REGNO is not available for CONFLICT_A,
1868 the conflict would be ignored, since HARD_REGNO
1869 will never be assigned to CONFLICT_A. */
1870 || !TEST_HARD_REG_BIT (data->profitable_hard_regs,
1871 hard_regno))
1872 continue;
1873 full_costs[j] -= conflict_costs[k];
1875 queue_update_cost (conflict_a, conflict_a, NULL, COST_HOP_DIVISOR);
1879 if (! retry_p)
1880 /* Take into account preferences of allocnos connected by copies to
1881 the conflict allocnos. */
1882 update_conflict_hard_regno_costs (full_costs, aclass, true);
1884 /* Take preferences of allocnos connected by copies into
1885 account. */
1886 if (! retry_p)
1888 start_update_cost ();
1889 queue_update_cost (a, a, NULL, COST_HOP_DIVISOR);
1890 update_conflict_hard_regno_costs (full_costs, aclass, false);
1892 min_cost = min_full_cost = INT_MAX;
1893 /* We don't care about giving callee saved registers to allocnos no
1894 living through calls because call clobbered registers are
1895 allocated first (it is usual practice to put them first in
1896 REG_ALLOC_ORDER). */
1897 mode = ALLOCNO_MODE (a);
1898 for (i = 0; i < class_size; i++)
1900 hard_regno = ira_class_hard_regs[aclass][i];
1901 #ifdef STACK_REGS
1902 if (no_stack_reg_p
1903 && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
1904 continue;
1905 #endif
1906 if (! check_hard_reg_p (a, hard_regno,
1907 conflicting_regs, profitable_hard_regs))
1908 continue;
1909 cost = costs[i];
1910 full_cost = full_costs[i];
1911 if (!HONOR_REG_ALLOC_ORDER)
1913 if ((saved_nregs = calculate_saved_nregs (hard_regno, mode)) != 0)
1914 /* We need to save/restore the hard register in
1915 epilogue/prologue. Therefore we increase the cost. */
1917 rclass = REGNO_REG_CLASS (hard_regno);
1918 add_cost = ((ira_memory_move_cost[mode][rclass][0]
1919 + ira_memory_move_cost[mode][rclass][1])
1920 * saved_nregs / hard_regno_nregs (hard_regno,
1921 mode) - 1);
1922 cost += add_cost;
1923 full_cost += add_cost;
1926 if (min_cost > cost)
1927 min_cost = cost;
1928 if (min_full_cost > full_cost)
1930 min_full_cost = full_cost;
1931 best_hard_regno = hard_regno;
1932 ira_assert (hard_regno >= 0);
1934 if (internal_flag_ira_verbose > 5 && ira_dump_file != NULL)
1935 fprintf (ira_dump_file, "(%d=%d,%d) ", hard_regno, cost, full_cost);
1937 if (min_full_cost > mem_cost
1938 /* Do not spill static chain pointer pseudo when non-local goto
1939 is used. */
1940 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1942 if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1943 fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
1944 mem_cost, min_full_cost);
1945 best_hard_regno = -1;
1947 fail:
1948 if (best_hard_regno >= 0)
1950 for (i = hard_regno_nregs (best_hard_regno, mode) - 1; i >= 0; i--)
1951 allocated_hardreg_p[best_hard_regno + i] = true;
1953 if (! retry_p)
1954 restore_costs_from_copies (a);
1955 ALLOCNO_HARD_REGNO (a) = best_hard_regno;
1956 ALLOCNO_ASSIGNED_P (a) = true;
1957 if (best_hard_regno >= 0)
1958 update_costs_from_copies (a, true, ! retry_p);
1959 ira_assert (ALLOCNO_CLASS (a) == aclass);
1960 /* We don't need updated costs anymore. */
1961 ira_free_allocno_updated_costs (a);
1962 return best_hard_regno >= 0;
1967 /* An array used to sort copies. */
1968 static ira_copy_t *sorted_copies;
1970 /* If allocno A is a cap, return non-cap allocno from which A is
1971 created. Otherwise, return A. */
1972 static ira_allocno_t
1973 get_cap_member (ira_allocno_t a)
1975 ira_allocno_t member;
1977 while ((member = ALLOCNO_CAP_MEMBER (a)) != NULL)
1978 a = member;
1979 return a;
1982 /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
1983 used to find a conflict for new allocnos or allocnos with the
1984 different allocno classes. */
1985 static bool
1986 allocnos_conflict_by_live_ranges_p (ira_allocno_t a1, ira_allocno_t a2)
1988 rtx reg1, reg2;
1989 int i, j;
1990 int n1 = ALLOCNO_NUM_OBJECTS (a1);
1991 int n2 = ALLOCNO_NUM_OBJECTS (a2);
1993 if (a1 == a2)
1994 return false;
1995 reg1 = regno_reg_rtx[ALLOCNO_REGNO (a1)];
1996 reg2 = regno_reg_rtx[ALLOCNO_REGNO (a2)];
1997 if (reg1 != NULL && reg2 != NULL
1998 && ORIGINAL_REGNO (reg1) == ORIGINAL_REGNO (reg2))
1999 return false;
2001 /* We don't keep live ranges for caps because they can be quite big.
2002 Use ranges of non-cap allocno from which caps are created. */
2003 a1 = get_cap_member (a1);
2004 a2 = get_cap_member (a2);
2005 for (i = 0; i < n1; i++)
2007 ira_object_t c1 = ALLOCNO_OBJECT (a1, i);
2009 for (j = 0; j < n2; j++)
2011 ira_object_t c2 = ALLOCNO_OBJECT (a2, j);
2013 if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1),
2014 OBJECT_LIVE_RANGES (c2)))
2015 return true;
2018 return false;
2021 /* The function is used to sort copies according to their execution
2022 frequencies. */
2023 static int
2024 copy_freq_compare_func (const void *v1p, const void *v2p)
2026 ira_copy_t cp1 = *(const ira_copy_t *) v1p, cp2 = *(const ira_copy_t *) v2p;
2027 int pri1, pri2;
2029 pri1 = cp1->freq;
2030 pri2 = cp2->freq;
2031 if (pri2 - pri1)
2032 return pri2 - pri1;
2034 /* If frequencies are equal, sort by copies, so that the results of
2035 qsort leave nothing to chance. */
2036 return cp1->num - cp2->num;
2041 /* Return true if any allocno from thread of A1 conflicts with any
2042 allocno from thread A2. */
2043 static bool
2044 allocno_thread_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
2046 ira_allocno_t a, conflict_a;
2048 for (a = ALLOCNO_COLOR_DATA (a2)->next_thread_allocno;;
2049 a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2051 for (conflict_a = ALLOCNO_COLOR_DATA (a1)->next_thread_allocno;;
2052 conflict_a = ALLOCNO_COLOR_DATA (conflict_a)->next_thread_allocno)
2054 if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
2055 return true;
2056 if (conflict_a == a1)
2057 break;
2059 if (a == a2)
2060 break;
2062 return false;
2065 /* Merge two threads given correspondingly by their first allocnos T1
2066 and T2 (more accurately merging T2 into T1). */
2067 static void
2068 merge_threads (ira_allocno_t t1, ira_allocno_t t2)
2070 ira_allocno_t a, next, last;
2072 gcc_assert (t1 != t2
2073 && ALLOCNO_COLOR_DATA (t1)->first_thread_allocno == t1
2074 && ALLOCNO_COLOR_DATA (t2)->first_thread_allocno == t2);
2075 for (last = t2, a = ALLOCNO_COLOR_DATA (t2)->next_thread_allocno;;
2076 a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2078 ALLOCNO_COLOR_DATA (a)->first_thread_allocno = t1;
2079 if (a == t2)
2080 break;
2081 last = a;
2083 next = ALLOCNO_COLOR_DATA (t1)->next_thread_allocno;
2084 ALLOCNO_COLOR_DATA (t1)->next_thread_allocno = t2;
2085 ALLOCNO_COLOR_DATA (last)->next_thread_allocno = next;
2086 ALLOCNO_COLOR_DATA (t1)->thread_freq += ALLOCNO_COLOR_DATA (t2)->thread_freq;
2089 /* Create threads by processing CP_NUM copies from sorted copies. We
2090 process the most expensive copies first. */
2091 static void
2092 form_threads_from_copies (int cp_num)
2094 ira_allocno_t a, thread1, thread2;
2095 ira_copy_t cp;
2096 int i, n;
2098 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
2099 /* Form threads processing copies, most frequently executed
2100 first. */
2101 for (; cp_num != 0;)
2103 for (i = 0; i < cp_num; i++)
2105 cp = sorted_copies[i];
2106 thread1 = ALLOCNO_COLOR_DATA (cp->first)->first_thread_allocno;
2107 thread2 = ALLOCNO_COLOR_DATA (cp->second)->first_thread_allocno;
2108 if (thread1 == thread2)
2109 continue;
2110 if (! allocno_thread_conflict_p (thread1, thread2))
2112 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2113 fprintf
2114 (ira_dump_file,
2115 " Forming thread by copy %d:a%dr%d-a%dr%d (freq=%d):\n",
2116 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
2117 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
2118 cp->freq);
2119 merge_threads (thread1, thread2);
2120 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2122 thread1 = ALLOCNO_COLOR_DATA (thread1)->first_thread_allocno;
2123 fprintf (ira_dump_file, " Result (freq=%d): a%dr%d(%d)",
2124 ALLOCNO_COLOR_DATA (thread1)->thread_freq,
2125 ALLOCNO_NUM (thread1), ALLOCNO_REGNO (thread1),
2126 ALLOCNO_FREQ (thread1));
2127 for (a = ALLOCNO_COLOR_DATA (thread1)->next_thread_allocno;
2128 a != thread1;
2129 a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2130 fprintf (ira_dump_file, " a%dr%d(%d)",
2131 ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2132 ALLOCNO_FREQ (a));
2133 fprintf (ira_dump_file, "\n");
2135 i++;
2136 break;
2139 /* Collect the rest of copies. */
2140 for (n = 0; i < cp_num; i++)
2142 cp = sorted_copies[i];
2143 if (ALLOCNO_COLOR_DATA (cp->first)->first_thread_allocno
2144 != ALLOCNO_COLOR_DATA (cp->second)->first_thread_allocno)
2145 sorted_copies[n++] = cp;
2147 cp_num = n;
2151 /* Create threads by processing copies of all alocnos from BUCKET. We
2152 process the most expensive copies first. */
2153 static void
2154 form_threads_from_bucket (ira_allocno_t bucket)
2156 ira_allocno_t a;
2157 ira_copy_t cp, next_cp;
2158 int cp_num = 0;
2160 for (a = bucket; a != NULL; a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2162 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2164 if (cp->first == a)
2166 next_cp = cp->next_first_allocno_copy;
2167 sorted_copies[cp_num++] = cp;
2169 else if (cp->second == a)
2170 next_cp = cp->next_second_allocno_copy;
2171 else
2172 gcc_unreachable ();
2175 form_threads_from_copies (cp_num);
2178 /* Create threads by processing copies of colorable allocno A. We
2179 process most expensive copies first. */
2180 static void
2181 form_threads_from_colorable_allocno (ira_allocno_t a)
2183 ira_allocno_t another_a;
2184 ira_copy_t cp, next_cp;
2185 int cp_num = 0;
2187 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2189 if (cp->first == a)
2191 next_cp = cp->next_first_allocno_copy;
2192 another_a = cp->second;
2194 else if (cp->second == a)
2196 next_cp = cp->next_second_allocno_copy;
2197 another_a = cp->first;
2199 else
2200 gcc_unreachable ();
2201 if ((! ALLOCNO_COLOR_DATA (another_a)->in_graph_p
2202 && !ALLOCNO_COLOR_DATA (another_a)->may_be_spilled_p)
2203 || ALLOCNO_COLOR_DATA (another_a)->colorable_p)
2204 sorted_copies[cp_num++] = cp;
2206 form_threads_from_copies (cp_num);
2209 /* Form initial threads which contain only one allocno. */
2210 static void
2211 init_allocno_threads (void)
2213 ira_allocno_t a;
2214 unsigned int j;
2215 bitmap_iterator bi;
2216 ira_pref_t pref;
2218 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2220 a = ira_allocnos[j];
2221 /* Set up initial thread data: */
2222 ALLOCNO_COLOR_DATA (a)->first_thread_allocno
2223 = ALLOCNO_COLOR_DATA (a)->next_thread_allocno = a;
2224 ALLOCNO_COLOR_DATA (a)->thread_freq = ALLOCNO_FREQ (a);
2225 ALLOCNO_COLOR_DATA (a)->hard_reg_prefs = 0;
2226 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
2227 ALLOCNO_COLOR_DATA (a)->hard_reg_prefs += pref->freq;
2233 /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
2235 /* Bucket of allocnos that can colored currently without spilling. */
2236 static ira_allocno_t colorable_allocno_bucket;
2238 /* Bucket of allocnos that might be not colored currently without
2239 spilling. */
2240 static ira_allocno_t uncolorable_allocno_bucket;
2242 /* The current number of allocnos in the uncolorable_bucket. */
2243 static int uncolorable_allocnos_num;
2245 /* Return the current spill priority of allocno A. The less the
2246 number, the more preferable the allocno for spilling. */
2247 static inline int
2248 allocno_spill_priority (ira_allocno_t a)
2250 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
2252 return (data->temp
2253 / (ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
2254 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
2255 + 1));
2258 /* Add allocno A to bucket *BUCKET_PTR. A should be not in a bucket
2259 before the call. */
2260 static void
2261 add_allocno_to_bucket (ira_allocno_t a, ira_allocno_t *bucket_ptr)
2263 ira_allocno_t first_a;
2264 allocno_color_data_t data;
2266 if (bucket_ptr == &uncolorable_allocno_bucket
2267 && ALLOCNO_CLASS (a) != NO_REGS)
2269 uncolorable_allocnos_num++;
2270 ira_assert (uncolorable_allocnos_num > 0);
2272 first_a = *bucket_ptr;
2273 data = ALLOCNO_COLOR_DATA (a);
2274 data->next_bucket_allocno = first_a;
2275 data->prev_bucket_allocno = NULL;
2276 if (first_a != NULL)
2277 ALLOCNO_COLOR_DATA (first_a)->prev_bucket_allocno = a;
2278 *bucket_ptr = a;
2281 /* Compare two allocnos to define which allocno should be pushed first
2282 into the coloring stack. If the return is a negative number, the
2283 allocno given by the first parameter will be pushed first. In this
2284 case such allocno has less priority than the second one and the
2285 hard register will be assigned to it after assignment to the second
2286 one. As the result of such assignment order, the second allocno
2287 has a better chance to get the best hard register. */
2288 static int
2289 bucket_allocno_compare_func (const void *v1p, const void *v2p)
2291 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2292 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2293 int diff, freq1, freq2, a1_num, a2_num, pref1, pref2;
2294 ira_allocno_t t1 = ALLOCNO_COLOR_DATA (a1)->first_thread_allocno;
2295 ira_allocno_t t2 = ALLOCNO_COLOR_DATA (a2)->first_thread_allocno;
2296 int cl1 = ALLOCNO_CLASS (a1), cl2 = ALLOCNO_CLASS (a2);
2298 freq1 = ALLOCNO_COLOR_DATA (t1)->thread_freq;
2299 freq2 = ALLOCNO_COLOR_DATA (t2)->thread_freq;
2300 if ((diff = freq1 - freq2) != 0)
2301 return diff;
2303 if ((diff = ALLOCNO_NUM (t2) - ALLOCNO_NUM (t1)) != 0)
2304 return diff;
2306 /* Push pseudos requiring less hard registers first. It means that
2307 we will assign pseudos requiring more hard registers first
2308 avoiding creation small holes in free hard register file into
2309 which the pseudos requiring more hard registers cannot fit. */
2310 if ((diff = (ira_reg_class_max_nregs[cl1][ALLOCNO_MODE (a1)]
2311 - ira_reg_class_max_nregs[cl2][ALLOCNO_MODE (a2)])) != 0)
2312 return diff;
2314 freq1 = ALLOCNO_FREQ (a1);
2315 freq2 = ALLOCNO_FREQ (a2);
2316 if ((diff = freq1 - freq2) != 0)
2317 return diff;
2319 a1_num = ALLOCNO_COLOR_DATA (a1)->available_regs_num;
2320 a2_num = ALLOCNO_COLOR_DATA (a2)->available_regs_num;
2321 if ((diff = a2_num - a1_num) != 0)
2322 return diff;
2323 /* Push allocnos with minimal conflict_allocno_hard_prefs first. */
2324 pref1 = ALLOCNO_COLOR_DATA (a1)->conflict_allocno_hard_prefs;
2325 pref2 = ALLOCNO_COLOR_DATA (a2)->conflict_allocno_hard_prefs;
2326 if ((diff = pref1 - pref2) != 0)
2327 return diff;
2328 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
2331 /* Sort bucket *BUCKET_PTR and return the result through
2332 BUCKET_PTR. */
2333 static void
2334 sort_bucket (ira_allocno_t *bucket_ptr,
2335 int (*compare_func) (const void *, const void *))
2337 ira_allocno_t a, head;
2338 int n;
2340 for (n = 0, a = *bucket_ptr;
2341 a != NULL;
2342 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2343 sorted_allocnos[n++] = a;
2344 if (n <= 1)
2345 return;
2346 qsort (sorted_allocnos, n, sizeof (ira_allocno_t), compare_func);
2347 head = NULL;
2348 for (n--; n >= 0; n--)
2350 a = sorted_allocnos[n];
2351 ALLOCNO_COLOR_DATA (a)->next_bucket_allocno = head;
2352 ALLOCNO_COLOR_DATA (a)->prev_bucket_allocno = NULL;
2353 if (head != NULL)
2354 ALLOCNO_COLOR_DATA (head)->prev_bucket_allocno = a;
2355 head = a;
2357 *bucket_ptr = head;
2360 /* Add ALLOCNO to colorable bucket maintaining the order according
2361 their priority. ALLOCNO should be not in a bucket before the
2362 call. */
2363 static void
2364 add_allocno_to_ordered_colorable_bucket (ira_allocno_t allocno)
2366 ira_allocno_t before, after;
2368 form_threads_from_colorable_allocno (allocno);
2369 for (before = colorable_allocno_bucket, after = NULL;
2370 before != NULL;
2371 after = before,
2372 before = ALLOCNO_COLOR_DATA (before)->next_bucket_allocno)
2373 if (bucket_allocno_compare_func (&allocno, &before) < 0)
2374 break;
2375 ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno = before;
2376 ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno = after;
2377 if (after == NULL)
2378 colorable_allocno_bucket = allocno;
2379 else
2380 ALLOCNO_COLOR_DATA (after)->next_bucket_allocno = allocno;
2381 if (before != NULL)
2382 ALLOCNO_COLOR_DATA (before)->prev_bucket_allocno = allocno;
2385 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
2386 the call. */
2387 static void
2388 delete_allocno_from_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
2390 ira_allocno_t prev_allocno, next_allocno;
2392 if (bucket_ptr == &uncolorable_allocno_bucket
2393 && ALLOCNO_CLASS (allocno) != NO_REGS)
2395 uncolorable_allocnos_num--;
2396 ira_assert (uncolorable_allocnos_num >= 0);
2398 prev_allocno = ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno;
2399 next_allocno = ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno;
2400 if (prev_allocno != NULL)
2401 ALLOCNO_COLOR_DATA (prev_allocno)->next_bucket_allocno = next_allocno;
2402 else
2404 ira_assert (*bucket_ptr == allocno);
2405 *bucket_ptr = next_allocno;
2407 if (next_allocno != NULL)
2408 ALLOCNO_COLOR_DATA (next_allocno)->prev_bucket_allocno = prev_allocno;
2411 /* Put allocno A onto the coloring stack without removing it from its
2412 bucket. Pushing allocno to the coloring stack can result in moving
2413 conflicting allocnos from the uncolorable bucket to the colorable
2414 one. Update conflict_allocno_hard_prefs of the conflicting
2415 allocnos which are not on stack yet. */
2416 static void
2417 push_allocno_to_stack (ira_allocno_t a)
2419 enum reg_class aclass;
2420 allocno_color_data_t data, conflict_data;
2421 int size, i, n = ALLOCNO_NUM_OBJECTS (a);
2423 data = ALLOCNO_COLOR_DATA (a);
2424 data->in_graph_p = false;
2425 allocno_stack_vec.safe_push (a);
2426 aclass = ALLOCNO_CLASS (a);
2427 if (aclass == NO_REGS)
2428 return;
2429 size = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
2430 if (n > 1)
2432 /* We will deal with the subwords individually. */
2433 gcc_assert (size == ALLOCNO_NUM_OBJECTS (a));
2434 size = 1;
2436 for (i = 0; i < n; i++)
2438 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2439 ira_object_t conflict_obj;
2440 ira_object_conflict_iterator oci;
2442 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2444 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2445 ira_pref_t pref;
2447 conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
2448 if (! conflict_data->in_graph_p
2449 || ALLOCNO_ASSIGNED_P (conflict_a)
2450 || !(hard_reg_set_intersect_p
2451 (ALLOCNO_COLOR_DATA (a)->profitable_hard_regs,
2452 conflict_data->profitable_hard_regs)))
2453 continue;
2454 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
2455 conflict_data->conflict_allocno_hard_prefs -= pref->freq;
2456 if (conflict_data->colorable_p)
2457 continue;
2458 ira_assert (bitmap_bit_p (coloring_allocno_bitmap,
2459 ALLOCNO_NUM (conflict_a)));
2460 if (update_left_conflict_sizes_p (conflict_a, a, size))
2462 delete_allocno_from_bucket
2463 (conflict_a, &uncolorable_allocno_bucket);
2464 add_allocno_to_ordered_colorable_bucket (conflict_a);
2465 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2467 fprintf (ira_dump_file, " Making");
2468 ira_print_expanded_allocno (conflict_a);
2469 fprintf (ira_dump_file, " colorable\n");
2477 /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
2478 The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
2479 static void
2480 remove_allocno_from_bucket_and_push (ira_allocno_t allocno, bool colorable_p)
2482 if (colorable_p)
2483 delete_allocno_from_bucket (allocno, &colorable_allocno_bucket);
2484 else
2485 delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
2486 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2488 fprintf (ira_dump_file, " Pushing");
2489 ira_print_expanded_allocno (allocno);
2490 if (colorable_p)
2491 fprintf (ira_dump_file, "(cost %d)\n",
2492 ALLOCNO_COLOR_DATA (allocno)->temp);
2493 else
2494 fprintf (ira_dump_file, "(potential spill: %spri=%d, cost=%d)\n",
2495 ALLOCNO_BAD_SPILL_P (allocno) ? "bad spill, " : "",
2496 allocno_spill_priority (allocno),
2497 ALLOCNO_COLOR_DATA (allocno)->temp);
2499 if (! colorable_p)
2500 ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p = true;
2501 push_allocno_to_stack (allocno);
2504 /* Put all allocnos from colorable bucket onto the coloring stack. */
2505 static void
2506 push_only_colorable (void)
2508 form_threads_from_bucket (colorable_allocno_bucket);
2509 sort_bucket (&colorable_allocno_bucket, bucket_allocno_compare_func);
2510 for (;colorable_allocno_bucket != NULL;)
2511 remove_allocno_from_bucket_and_push (colorable_allocno_bucket, true);
2514 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
2515 loop given by its LOOP_NODE. */
2517 ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
2519 int freq, i;
2520 edge_iterator ei;
2521 edge e;
2522 vec<edge> edges;
2524 ira_assert (current_loops != NULL && loop_node->loop != NULL
2525 && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
2526 freq = 0;
2527 if (! exit_p)
2529 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
2530 if (e->src != loop_node->loop->latch
2531 && (regno < 0
2532 || (bitmap_bit_p (df_get_live_out (e->src), regno)
2533 && bitmap_bit_p (df_get_live_in (e->dest), regno))))
2534 freq += EDGE_FREQUENCY (e);
2536 else
2538 edges = get_loop_exit_edges (loop_node->loop);
2539 FOR_EACH_VEC_ELT (edges, i, e)
2540 if (regno < 0
2541 || (bitmap_bit_p (df_get_live_out (e->src), regno)
2542 && bitmap_bit_p (df_get_live_in (e->dest), regno)))
2543 freq += EDGE_FREQUENCY (e);
2544 edges.release ();
2547 return REG_FREQ_FROM_EDGE_FREQ (freq);
2550 /* Calculate and return the cost of putting allocno A into memory. */
2551 static int
2552 calculate_allocno_spill_cost (ira_allocno_t a)
2554 int regno, cost;
2555 machine_mode mode;
2556 enum reg_class rclass;
2557 ira_allocno_t parent_allocno;
2558 ira_loop_tree_node_t parent_node, loop_node;
2560 regno = ALLOCNO_REGNO (a);
2561 cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_UPDATED_CLASS_COST (a);
2562 if (ALLOCNO_CAP (a) != NULL)
2563 return cost;
2564 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
2565 if ((parent_node = loop_node->parent) == NULL)
2566 return cost;
2567 if ((parent_allocno = parent_node->regno_allocno_map[regno]) == NULL)
2568 return cost;
2569 mode = ALLOCNO_MODE (a);
2570 rclass = ALLOCNO_CLASS (a);
2571 if (ALLOCNO_HARD_REGNO (parent_allocno) < 0)
2572 cost -= (ira_memory_move_cost[mode][rclass][0]
2573 * ira_loop_edge_freq (loop_node, regno, true)
2574 + ira_memory_move_cost[mode][rclass][1]
2575 * ira_loop_edge_freq (loop_node, regno, false));
2576 else
2578 ira_init_register_move_cost_if_necessary (mode);
2579 cost += ((ira_memory_move_cost[mode][rclass][1]
2580 * ira_loop_edge_freq (loop_node, regno, true)
2581 + ira_memory_move_cost[mode][rclass][0]
2582 * ira_loop_edge_freq (loop_node, regno, false))
2583 - (ira_register_move_cost[mode][rclass][rclass]
2584 * (ira_loop_edge_freq (loop_node, regno, false)
2585 + ira_loop_edge_freq (loop_node, regno, true))));
2587 return cost;
2590 /* Used for sorting allocnos for spilling. */
2591 static inline int
2592 allocno_spill_priority_compare (ira_allocno_t a1, ira_allocno_t a2)
2594 int pri1, pri2, diff;
2596 /* Avoid spilling static chain pointer pseudo when non-local goto is
2597 used. */
2598 if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))
2599 return 1;
2600 else if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2)))
2601 return -1;
2602 if (ALLOCNO_BAD_SPILL_P (a1) && ! ALLOCNO_BAD_SPILL_P (a2))
2603 return 1;
2604 if (ALLOCNO_BAD_SPILL_P (a2) && ! ALLOCNO_BAD_SPILL_P (a1))
2605 return -1;
2606 pri1 = allocno_spill_priority (a1);
2607 pri2 = allocno_spill_priority (a2);
2608 if ((diff = pri1 - pri2) != 0)
2609 return diff;
2610 if ((diff
2611 = ALLOCNO_COLOR_DATA (a1)->temp - ALLOCNO_COLOR_DATA (a2)->temp) != 0)
2612 return diff;
2613 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2616 /* Used for sorting allocnos for spilling. */
2617 static int
2618 allocno_spill_sort_compare (const void *v1p, const void *v2p)
2620 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2621 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2623 return allocno_spill_priority_compare (p1, p2);
2626 /* Push allocnos to the coloring stack. The order of allocnos in the
2627 stack defines the order for the subsequent coloring. */
2628 static void
2629 push_allocnos_to_stack (void)
2631 ira_allocno_t a;
2632 int cost;
2634 /* Calculate uncolorable allocno spill costs. */
2635 for (a = uncolorable_allocno_bucket;
2636 a != NULL;
2637 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2638 if (ALLOCNO_CLASS (a) != NO_REGS)
2640 cost = calculate_allocno_spill_cost (a);
2641 /* ??? Remove cost of copies between the coalesced
2642 allocnos. */
2643 ALLOCNO_COLOR_DATA (a)->temp = cost;
2645 sort_bucket (&uncolorable_allocno_bucket, allocno_spill_sort_compare);
2646 for (;;)
2648 push_only_colorable ();
2649 a = uncolorable_allocno_bucket;
2650 if (a == NULL)
2651 break;
2652 remove_allocno_from_bucket_and_push (a, false);
2654 ira_assert (colorable_allocno_bucket == NULL
2655 && uncolorable_allocno_bucket == NULL);
2656 ira_assert (uncolorable_allocnos_num == 0);
2659 /* Pop the coloring stack and assign hard registers to the popped
2660 allocnos. */
2661 static void
2662 pop_allocnos_from_stack (void)
2664 ira_allocno_t allocno;
2665 enum reg_class aclass;
2667 for (;allocno_stack_vec.length () != 0;)
2669 allocno = allocno_stack_vec.pop ();
2670 aclass = ALLOCNO_CLASS (allocno);
2671 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2673 fprintf (ira_dump_file, " Popping");
2674 ira_print_expanded_allocno (allocno);
2675 fprintf (ira_dump_file, " -- ");
2677 if (aclass == NO_REGS)
2679 ALLOCNO_HARD_REGNO (allocno) = -1;
2680 ALLOCNO_ASSIGNED_P (allocno) = true;
2681 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
2682 ira_assert
2683 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
2684 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2685 fprintf (ira_dump_file, "assign memory\n");
2687 else if (assign_hard_reg (allocno, false))
2689 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2690 fprintf (ira_dump_file, "assign reg %d\n",
2691 ALLOCNO_HARD_REGNO (allocno));
2693 else if (ALLOCNO_ASSIGNED_P (allocno))
2695 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2696 fprintf (ira_dump_file, "spill%s\n",
2697 ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p
2698 ? "" : "!");
2700 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2704 /* Set up number of available hard registers for allocno A. */
2705 static void
2706 setup_allocno_available_regs_num (ira_allocno_t a)
2708 int i, n, hard_regno, hard_regs_num, nwords;
2709 enum reg_class aclass;
2710 allocno_color_data_t data;
2712 aclass = ALLOCNO_CLASS (a);
2713 data = ALLOCNO_COLOR_DATA (a);
2714 data->available_regs_num = 0;
2715 if (aclass == NO_REGS)
2716 return;
2717 hard_regs_num = ira_class_hard_regs_num[aclass];
2718 nwords = ALLOCNO_NUM_OBJECTS (a);
2719 for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
2721 hard_regno = ira_class_hard_regs[aclass][i];
2722 /* Checking only profitable hard regs. */
2723 if (TEST_HARD_REG_BIT (data->profitable_hard_regs, hard_regno))
2724 n++;
2726 data->available_regs_num = n;
2727 if (internal_flag_ira_verbose <= 2 || ira_dump_file == NULL)
2728 return;
2729 fprintf
2730 (ira_dump_file,
2731 " Allocno a%dr%d of %s(%d) has %d avail. regs ",
2732 ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2733 reg_class_names[aclass], ira_class_hard_regs_num[aclass], n);
2734 print_hard_reg_set (ira_dump_file, data->profitable_hard_regs, false);
2735 fprintf (ira_dump_file, ", %snode: ",
2736 data->profitable_hard_regs == data->hard_regs_node->hard_regs->set
2737 ? "" : "^");
2738 print_hard_reg_set (ira_dump_file,
2739 data->hard_regs_node->hard_regs->set, false);
2740 for (i = 0; i < nwords; i++)
2742 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2744 if (nwords != 1)
2746 if (i != 0)
2747 fprintf (ira_dump_file, ", ");
2748 fprintf (ira_dump_file, " obj %d", i);
2750 fprintf (ira_dump_file, " (confl regs = ");
2751 print_hard_reg_set (ira_dump_file, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2752 false);
2753 fprintf (ira_dump_file, ")");
2755 fprintf (ira_dump_file, "\n");
2758 /* Put ALLOCNO in a bucket corresponding to its number and size of its
2759 conflicting allocnos and hard registers. */
2760 static void
2761 put_allocno_into_bucket (ira_allocno_t allocno)
2763 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2764 setup_allocno_available_regs_num (allocno);
2765 if (setup_left_conflict_sizes_p (allocno))
2766 add_allocno_to_bucket (allocno, &colorable_allocno_bucket);
2767 else
2768 add_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
2771 /* Map: allocno number -> allocno priority. */
2772 static int *allocno_priorities;
2774 /* Set up priorities for N allocnos in array
2775 CONSIDERATION_ALLOCNOS. */
2776 static void
2777 setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
2779 int i, length, nrefs, priority, max_priority, mult;
2780 ira_allocno_t a;
2782 max_priority = 0;
2783 for (i = 0; i < n; i++)
2785 a = consideration_allocnos[i];
2786 nrefs = ALLOCNO_NREFS (a);
2787 ira_assert (nrefs >= 0);
2788 mult = floor_log2 (ALLOCNO_NREFS (a)) + 1;
2789 ira_assert (mult >= 0);
2790 allocno_priorities[ALLOCNO_NUM (a)]
2791 = priority
2792 = (mult
2793 * (ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a))
2794 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
2795 if (priority < 0)
2796 priority = -priority;
2797 if (max_priority < priority)
2798 max_priority = priority;
2800 mult = max_priority == 0 ? 1 : INT_MAX / max_priority;
2801 for (i = 0; i < n; i++)
2803 a = consideration_allocnos[i];
2804 length = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2805 if (ALLOCNO_NUM_OBJECTS (a) > 1)
2806 length /= ALLOCNO_NUM_OBJECTS (a);
2807 if (length <= 0)
2808 length = 1;
2809 allocno_priorities[ALLOCNO_NUM (a)]
2810 = allocno_priorities[ALLOCNO_NUM (a)] * mult / length;
2814 /* Sort allocnos according to the profit of usage of a hard register
2815 instead of memory for them. */
2816 static int
2817 allocno_cost_compare_func (const void *v1p, const void *v2p)
2819 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2820 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2821 int c1, c2;
2823 c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_UPDATED_CLASS_COST (p1);
2824 c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_UPDATED_CLASS_COST (p2);
2825 if (c1 - c2)
2826 return c1 - c2;
2828 /* If regs are equally good, sort by allocno numbers, so that the
2829 results of qsort leave nothing to chance. */
2830 return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
2833 /* Return savings on removed copies when ALLOCNO is assigned to
2834 HARD_REGNO. */
2835 static int
2836 allocno_copy_cost_saving (ira_allocno_t allocno, int hard_regno)
2838 int cost = 0;
2839 machine_mode allocno_mode = ALLOCNO_MODE (allocno);
2840 enum reg_class rclass;
2841 ira_copy_t cp, next_cp;
2843 rclass = REGNO_REG_CLASS (hard_regno);
2844 if (ira_reg_class_max_nregs[rclass][allocno_mode]
2845 > ira_class_hard_regs_num[rclass])
2846 /* For the above condition the cost can be wrong. Use the allocno
2847 class in this case. */
2848 rclass = ALLOCNO_CLASS (allocno);
2849 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
2851 if (cp->first == allocno)
2853 next_cp = cp->next_first_allocno_copy;
2854 if (ALLOCNO_HARD_REGNO (cp->second) != hard_regno)
2855 continue;
2857 else if (cp->second == allocno)
2859 next_cp = cp->next_second_allocno_copy;
2860 if (ALLOCNO_HARD_REGNO (cp->first) != hard_regno)
2861 continue;
2863 else
2864 gcc_unreachable ();
2865 ira_init_register_move_cost_if_necessary (allocno_mode);
2866 cost += cp->freq * ira_register_move_cost[allocno_mode][rclass][rclass];
2868 return cost;
2871 /* We used Chaitin-Briggs coloring to assign as many pseudos as
2872 possible to hard registers. Let us try to improve allocation with
2873 cost point of view. This function improves the allocation by
2874 spilling some allocnos and assigning the freed hard registers to
2875 other allocnos if it decreases the overall allocation cost. */
2876 static void
2877 improve_allocation (void)
2879 unsigned int i;
2880 int j, k, n, hregno, conflict_hregno, base_cost, class_size, word, nwords;
2881 int check, spill_cost, min_cost, nregs, conflict_nregs, r, best;
2882 bool try_p;
2883 enum reg_class aclass;
2884 machine_mode mode;
2885 int *allocno_costs;
2886 int costs[FIRST_PSEUDO_REGISTER];
2887 HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
2888 ira_allocno_t a;
2889 bitmap_iterator bi;
2891 /* Don't bother to optimize the code with static chain pointer and
2892 non-local goto in order not to spill the chain pointer
2893 pseudo. */
2894 if (cfun->static_chain_decl && crtl->has_nonlocal_goto)
2895 return;
2896 /* Clear counts used to process conflicting allocnos only once for
2897 each allocno. */
2898 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2899 ALLOCNO_COLOR_DATA (ira_allocnos[i])->temp = 0;
2900 check = n = 0;
2901 /* Process each allocno and try to assign a hard register to it by
2902 spilling some its conflicting allocnos. */
2903 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2905 a = ira_allocnos[i];
2906 ALLOCNO_COLOR_DATA (a)->temp = 0;
2907 if (empty_profitable_hard_regs (a))
2908 continue;
2909 check++;
2910 aclass = ALLOCNO_CLASS (a);
2911 allocno_costs = ALLOCNO_HARD_REG_COSTS (a);
2912 if ((hregno = ALLOCNO_HARD_REGNO (a)) < 0)
2913 base_cost = ALLOCNO_UPDATED_MEMORY_COST (a);
2914 else if (allocno_costs == NULL)
2915 /* It means that assigning a hard register is not profitable
2916 (we don't waste memory for hard register costs in this
2917 case). */
2918 continue;
2919 else
2920 base_cost = (allocno_costs[ira_class_hard_reg_index[aclass][hregno]]
2921 - allocno_copy_cost_saving (a, hregno));
2922 try_p = false;
2923 get_conflict_and_start_profitable_regs (a, false,
2924 conflicting_regs,
2925 &profitable_hard_regs);
2926 class_size = ira_class_hard_regs_num[aclass];
2927 /* Set up cost improvement for usage of each profitable hard
2928 register for allocno A. */
2929 for (j = 0; j < class_size; j++)
2931 hregno = ira_class_hard_regs[aclass][j];
2932 if (! check_hard_reg_p (a, hregno,
2933 conflicting_regs, profitable_hard_regs))
2934 continue;
2935 ira_assert (ira_class_hard_reg_index[aclass][hregno] == j);
2936 k = allocno_costs == NULL ? 0 : j;
2937 costs[hregno] = (allocno_costs == NULL
2938 ? ALLOCNO_UPDATED_CLASS_COST (a) : allocno_costs[k]);
2939 costs[hregno] -= allocno_copy_cost_saving (a, hregno);
2940 costs[hregno] -= base_cost;
2941 if (costs[hregno] < 0)
2942 try_p = true;
2944 if (! try_p)
2945 /* There is no chance to improve the allocation cost by
2946 assigning hard register to allocno A even without spilling
2947 conflicting allocnos. */
2948 continue;
2949 mode = ALLOCNO_MODE (a);
2950 nwords = ALLOCNO_NUM_OBJECTS (a);
2951 /* Process each allocno conflicting with A and update the cost
2952 improvement for profitable hard registers of A. To use a
2953 hard register for A we need to spill some conflicting
2954 allocnos and that creates penalty for the cost
2955 improvement. */
2956 for (word = 0; word < nwords; word++)
2958 ira_object_t conflict_obj;
2959 ira_object_t obj = ALLOCNO_OBJECT (a, word);
2960 ira_object_conflict_iterator oci;
2962 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2964 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2966 if (ALLOCNO_COLOR_DATA (conflict_a)->temp == check)
2967 /* We already processed this conflicting allocno
2968 because we processed earlier another object of the
2969 conflicting allocno. */
2970 continue;
2971 ALLOCNO_COLOR_DATA (conflict_a)->temp = check;
2972 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
2973 continue;
2974 spill_cost = ALLOCNO_UPDATED_MEMORY_COST (conflict_a);
2975 k = (ira_class_hard_reg_index
2976 [ALLOCNO_CLASS (conflict_a)][conflict_hregno]);
2977 ira_assert (k >= 0);
2978 if ((allocno_costs = ALLOCNO_HARD_REG_COSTS (conflict_a))
2979 != NULL)
2980 spill_cost -= allocno_costs[k];
2981 else
2982 spill_cost -= ALLOCNO_UPDATED_CLASS_COST (conflict_a);
2983 spill_cost
2984 += allocno_copy_cost_saving (conflict_a, conflict_hregno);
2985 conflict_nregs = hard_regno_nregs (conflict_hregno,
2986 ALLOCNO_MODE (conflict_a));
2987 for (r = conflict_hregno;
2988 r >= 0 && (int) end_hard_regno (mode, r) > conflict_hregno;
2989 r--)
2990 if (check_hard_reg_p (a, r,
2991 conflicting_regs, profitable_hard_regs))
2992 costs[r] += spill_cost;
2993 for (r = conflict_hregno + 1;
2994 r < conflict_hregno + conflict_nregs;
2995 r++)
2996 if (check_hard_reg_p (a, r,
2997 conflicting_regs, profitable_hard_regs))
2998 costs[r] += spill_cost;
3001 min_cost = INT_MAX;
3002 best = -1;
3003 /* Now we choose hard register for A which results in highest
3004 allocation cost improvement. */
3005 for (j = 0; j < class_size; j++)
3007 hregno = ira_class_hard_regs[aclass][j];
3008 if (check_hard_reg_p (a, hregno,
3009 conflicting_regs, profitable_hard_regs)
3010 && min_cost > costs[hregno])
3012 best = hregno;
3013 min_cost = costs[hregno];
3016 if (min_cost >= 0)
3017 /* We are in a situation when assigning any hard register to A
3018 by spilling some conflicting allocnos does not improve the
3019 allocation cost. */
3020 continue;
3021 nregs = hard_regno_nregs (best, mode);
3022 /* Now spill conflicting allocnos which contain a hard register
3023 of A when we assign the best chosen hard register to it. */
3024 for (word = 0; word < nwords; word++)
3026 ira_object_t conflict_obj;
3027 ira_object_t obj = ALLOCNO_OBJECT (a, word);
3028 ira_object_conflict_iterator oci;
3030 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3032 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3034 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
3035 continue;
3036 conflict_nregs = hard_regno_nregs (conflict_hregno,
3037 ALLOCNO_MODE (conflict_a));
3038 if (best + nregs <= conflict_hregno
3039 || conflict_hregno + conflict_nregs <= best)
3040 /* No intersection. */
3041 continue;
3042 ALLOCNO_HARD_REGNO (conflict_a) = -1;
3043 sorted_allocnos[n++] = conflict_a;
3044 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3045 fprintf (ira_dump_file, "Spilling a%dr%d for a%dr%d\n",
3046 ALLOCNO_NUM (conflict_a), ALLOCNO_REGNO (conflict_a),
3047 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3050 /* Assign the best chosen hard register to A. */
3051 ALLOCNO_HARD_REGNO (a) = best;
3052 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3053 fprintf (ira_dump_file, "Assigning %d to a%dr%d\n",
3054 best, ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3056 if (n == 0)
3057 return;
3058 /* We spilled some allocnos to assign their hard registers to other
3059 allocnos. The spilled allocnos are now in array
3060 'sorted_allocnos'. There is still a possibility that some of the
3061 spilled allocnos can get hard registers. So let us try assign
3062 them hard registers again (just a reminder -- function
3063 'assign_hard_reg' assigns hard registers only if it is possible
3064 and profitable). We process the spilled allocnos with biggest
3065 benefit to get hard register first -- see function
3066 'allocno_cost_compare_func'. */
3067 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
3068 allocno_cost_compare_func);
3069 for (j = 0; j < n; j++)
3071 a = sorted_allocnos[j];
3072 ALLOCNO_ASSIGNED_P (a) = false;
3073 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3075 fprintf (ira_dump_file, " ");
3076 ira_print_expanded_allocno (a);
3077 fprintf (ira_dump_file, " -- ");
3079 if (assign_hard_reg (a, false))
3081 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3082 fprintf (ira_dump_file, "assign hard reg %d\n",
3083 ALLOCNO_HARD_REGNO (a));
3085 else
3087 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3088 fprintf (ira_dump_file, "assign memory\n");
3093 /* Sort allocnos according to their priorities. */
3094 static int
3095 allocno_priority_compare_func (const void *v1p, const void *v2p)
3097 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
3098 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
3099 int pri1, pri2, diff;
3101 /* Assign hard reg to static chain pointer pseudo first when
3102 non-local goto is used. */
3103 if ((diff = (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2))
3104 - non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))) != 0)
3105 return diff;
3106 pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
3107 pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
3108 if (pri2 != pri1)
3109 return SORTGT (pri2, pri1);
3111 /* If regs are equally good, sort by allocnos, so that the results of
3112 qsort leave nothing to chance. */
3113 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
3116 /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
3117 taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
3118 static void
3119 color_allocnos (void)
3121 unsigned int i, n;
3122 bitmap_iterator bi;
3123 ira_allocno_t a;
3125 setup_profitable_hard_regs ();
3126 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3128 allocno_color_data_t data;
3129 ira_pref_t pref, next_pref;
3131 a = ira_allocnos[i];
3132 data = ALLOCNO_COLOR_DATA (a);
3133 data->conflict_allocno_hard_prefs = 0;
3134 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = next_pref)
3136 next_pref = pref->next_pref;
3137 if (! ira_hard_reg_in_set_p (pref->hard_regno,
3138 ALLOCNO_MODE (a),
3139 data->profitable_hard_regs))
3140 ira_remove_pref (pref);
3144 if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
3146 n = 0;
3147 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3149 a = ira_allocnos[i];
3150 if (ALLOCNO_CLASS (a) == NO_REGS)
3152 ALLOCNO_HARD_REGNO (a) = -1;
3153 ALLOCNO_ASSIGNED_P (a) = true;
3154 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3155 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3156 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3158 fprintf (ira_dump_file, " Spill");
3159 ira_print_expanded_allocno (a);
3160 fprintf (ira_dump_file, "\n");
3162 continue;
3164 sorted_allocnos[n++] = a;
3166 if (n != 0)
3168 setup_allocno_priorities (sorted_allocnos, n);
3169 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
3170 allocno_priority_compare_func);
3171 for (i = 0; i < n; i++)
3173 a = sorted_allocnos[i];
3174 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3176 fprintf (ira_dump_file, " ");
3177 ira_print_expanded_allocno (a);
3178 fprintf (ira_dump_file, " -- ");
3180 if (assign_hard_reg (a, false))
3182 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3183 fprintf (ira_dump_file, "assign hard reg %d\n",
3184 ALLOCNO_HARD_REGNO (a));
3186 else
3188 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3189 fprintf (ira_dump_file, "assign memory\n");
3194 else
3196 form_allocno_hard_regs_nodes_forest ();
3197 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3198 print_hard_regs_forest (ira_dump_file);
3199 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3201 a = ira_allocnos[i];
3202 if (ALLOCNO_CLASS (a) != NO_REGS && ! empty_profitable_hard_regs (a))
3204 ALLOCNO_COLOR_DATA (a)->in_graph_p = true;
3205 update_costs_from_prefs (a);
3206 update_conflict_allocno_hard_prefs (a);
3208 else
3210 ALLOCNO_HARD_REGNO (a) = -1;
3211 ALLOCNO_ASSIGNED_P (a) = true;
3212 /* We don't need updated costs anymore. */
3213 ira_free_allocno_updated_costs (a);
3214 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3216 fprintf (ira_dump_file, " Spill");
3217 ira_print_expanded_allocno (a);
3218 fprintf (ira_dump_file, "\n");
3222 /* Put the allocnos into the corresponding buckets. */
3223 colorable_allocno_bucket = NULL;
3224 uncolorable_allocno_bucket = NULL;
3225 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3227 a = ira_allocnos[i];
3228 if (ALLOCNO_COLOR_DATA (a)->in_graph_p)
3229 put_allocno_into_bucket (a);
3231 push_allocnos_to_stack ();
3232 pop_allocnos_from_stack ();
3233 finish_allocno_hard_regs_nodes_forest ();
3235 improve_allocation ();
3240 /* Output information about the loop given by its LOOP_TREE_NODE. */
3241 static void
3242 print_loop_title (ira_loop_tree_node_t loop_tree_node)
3244 unsigned int j;
3245 bitmap_iterator bi;
3246 ira_loop_tree_node_t subloop_node, dest_loop_node;
3247 edge e;
3248 edge_iterator ei;
3250 if (loop_tree_node->parent == NULL)
3251 fprintf (ira_dump_file,
3252 "\n Loop 0 (parent -1, header bb%d, depth 0)\n bbs:",
3253 NUM_FIXED_BLOCKS);
3254 else
3256 ira_assert (current_loops != NULL && loop_tree_node->loop != NULL);
3257 fprintf (ira_dump_file,
3258 "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
3259 loop_tree_node->loop_num, loop_tree_node->parent->loop_num,
3260 loop_tree_node->loop->header->index,
3261 loop_depth (loop_tree_node->loop));
3263 for (subloop_node = loop_tree_node->children;
3264 subloop_node != NULL;
3265 subloop_node = subloop_node->next)
3266 if (subloop_node->bb != NULL)
3268 fprintf (ira_dump_file, " %d", subloop_node->bb->index);
3269 FOR_EACH_EDGE (e, ei, subloop_node->bb->succs)
3270 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
3271 && ((dest_loop_node = IRA_BB_NODE (e->dest)->parent)
3272 != loop_tree_node))
3273 fprintf (ira_dump_file, "(->%d:l%d)",
3274 e->dest->index, dest_loop_node->loop_num);
3276 fprintf (ira_dump_file, "\n all:");
3277 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
3278 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
3279 fprintf (ira_dump_file, "\n modified regnos:");
3280 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
3281 fprintf (ira_dump_file, " %d", j);
3282 fprintf (ira_dump_file, "\n border:");
3283 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
3284 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
3285 fprintf (ira_dump_file, "\n Pressure:");
3286 for (j = 0; (int) j < ira_pressure_classes_num; j++)
3288 enum reg_class pclass;
3290 pclass = ira_pressure_classes[j];
3291 if (loop_tree_node->reg_pressure[pclass] == 0)
3292 continue;
3293 fprintf (ira_dump_file, " %s=%d", reg_class_names[pclass],
3294 loop_tree_node->reg_pressure[pclass]);
3296 fprintf (ira_dump_file, "\n");
3299 /* Color the allocnos inside loop (in the extreme case it can be all
3300 of the function) given the corresponding LOOP_TREE_NODE. The
3301 function is called for each loop during top-down traverse of the
3302 loop tree. */
3303 static void
3304 color_pass (ira_loop_tree_node_t loop_tree_node)
3306 int regno, hard_regno, index = -1, n;
3307 int cost, exit_freq, enter_freq;
3308 unsigned int j;
3309 bitmap_iterator bi;
3310 machine_mode mode;
3311 enum reg_class rclass, aclass, pclass;
3312 ira_allocno_t a, subloop_allocno;
3313 ira_loop_tree_node_t subloop_node;
3315 ira_assert (loop_tree_node->bb == NULL);
3316 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3317 print_loop_title (loop_tree_node);
3319 bitmap_copy (coloring_allocno_bitmap, loop_tree_node->all_allocnos);
3320 bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
3321 n = 0;
3322 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3324 a = ira_allocnos[j];
3325 n++;
3326 if (! ALLOCNO_ASSIGNED_P (a))
3327 continue;
3328 bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
3330 allocno_color_data
3331 = (allocno_color_data_t) ira_allocate (sizeof (struct allocno_color_data)
3332 * n);
3333 memset (allocno_color_data, 0, sizeof (struct allocno_color_data) * n);
3334 curr_allocno_process = 0;
3335 n = 0;
3336 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3338 a = ira_allocnos[j];
3339 ALLOCNO_ADD_DATA (a) = allocno_color_data + n;
3340 n++;
3342 init_allocno_threads ();
3343 /* Color all mentioned allocnos including transparent ones. */
3344 color_allocnos ();
3345 /* Process caps. They are processed just once. */
3346 if (flag_ira_region == IRA_REGION_MIXED
3347 || flag_ira_region == IRA_REGION_ALL)
3348 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
3350 a = ira_allocnos[j];
3351 if (ALLOCNO_CAP_MEMBER (a) == NULL)
3352 continue;
3353 /* Remove from processing in the next loop. */
3354 bitmap_clear_bit (consideration_allocno_bitmap, j);
3355 rclass = ALLOCNO_CLASS (a);
3356 pclass = ira_pressure_class_translate[rclass];
3357 if (flag_ira_region == IRA_REGION_MIXED
3358 && (loop_tree_node->reg_pressure[pclass]
3359 <= ira_class_hard_regs_num[pclass]))
3361 mode = ALLOCNO_MODE (a);
3362 hard_regno = ALLOCNO_HARD_REGNO (a);
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 subloop_allocno = ALLOCNO_CAP_MEMBER (a);
3370 subloop_node = ALLOCNO_LOOP_TREE_NODE (subloop_allocno);
3371 ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno));
3372 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3373 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3374 if (hard_regno >= 0)
3375 update_costs_from_copies (subloop_allocno, true, true);
3376 /* We don't need updated costs anymore. */
3377 ira_free_allocno_updated_costs (subloop_allocno);
3380 /* Update costs of the corresponding allocnos (not caps) in the
3381 subloops. */
3382 for (subloop_node = loop_tree_node->subloops;
3383 subloop_node != NULL;
3384 subloop_node = subloop_node->subloop_next)
3386 ira_assert (subloop_node->bb == NULL);
3387 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3389 a = ira_allocnos[j];
3390 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3391 mode = ALLOCNO_MODE (a);
3392 rclass = ALLOCNO_CLASS (a);
3393 pclass = ira_pressure_class_translate[rclass];
3394 hard_regno = ALLOCNO_HARD_REGNO (a);
3395 /* Use hard register class here. ??? */
3396 if (hard_regno >= 0)
3398 index = ira_class_hard_reg_index[rclass][hard_regno];
3399 ira_assert (index >= 0);
3401 regno = ALLOCNO_REGNO (a);
3402 /* ??? conflict costs */
3403 subloop_allocno = subloop_node->regno_allocno_map[regno];
3404 if (subloop_allocno == NULL
3405 || ALLOCNO_CAP (subloop_allocno) != NULL)
3406 continue;
3407 ira_assert (ALLOCNO_CLASS (subloop_allocno) == rclass);
3408 ira_assert (bitmap_bit_p (subloop_node->all_allocnos,
3409 ALLOCNO_NUM (subloop_allocno)));
3410 if ((flag_ira_region == IRA_REGION_MIXED
3411 && (loop_tree_node->reg_pressure[pclass]
3412 <= ira_class_hard_regs_num[pclass]))
3413 || (pic_offset_table_rtx != NULL
3414 && regno == (int) REGNO (pic_offset_table_rtx))
3415 /* Avoid overlapped multi-registers. Moves between them
3416 might result in wrong code generation. */
3417 || (hard_regno >= 0
3418 && ira_reg_class_max_nregs[pclass][mode] > 1))
3420 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
3422 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3423 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3424 if (hard_regno >= 0)
3425 update_costs_from_copies (subloop_allocno, true, true);
3426 /* We don't need updated costs anymore. */
3427 ira_free_allocno_updated_costs (subloop_allocno);
3429 continue;
3431 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
3432 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
3433 ira_assert (regno < ira_reg_equiv_len);
3434 if (ira_equiv_no_lvalue_p (regno))
3436 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
3438 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3439 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3440 if (hard_regno >= 0)
3441 update_costs_from_copies (subloop_allocno, true, true);
3442 /* We don't need updated costs anymore. */
3443 ira_free_allocno_updated_costs (subloop_allocno);
3446 else if (hard_regno < 0)
3448 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
3449 -= ((ira_memory_move_cost[mode][rclass][1] * enter_freq)
3450 + (ira_memory_move_cost[mode][rclass][0] * exit_freq));
3452 else
3454 aclass = ALLOCNO_CLASS (subloop_allocno);
3455 ira_init_register_move_cost_if_necessary (mode);
3456 cost = (ira_register_move_cost[mode][rclass][rclass]
3457 * (exit_freq + enter_freq));
3458 ira_allocate_and_set_or_copy_costs
3459 (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno), aclass,
3460 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno),
3461 ALLOCNO_HARD_REG_COSTS (subloop_allocno));
3462 ira_allocate_and_set_or_copy_costs
3463 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno),
3464 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno));
3465 ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
3466 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
3467 -= cost;
3468 if (ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
3469 > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index])
3470 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
3471 = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index];
3472 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
3473 += (ira_memory_move_cost[mode][rclass][0] * enter_freq
3474 + ira_memory_move_cost[mode][rclass][1] * exit_freq);
3478 ira_free (allocno_color_data);
3479 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3481 a = ira_allocnos[j];
3482 ALLOCNO_ADD_DATA (a) = NULL;
3486 /* Initialize the common data for coloring and calls functions to do
3487 Chaitin-Briggs and regional coloring. */
3488 static void
3489 do_coloring (void)
3491 coloring_allocno_bitmap = ira_allocate_bitmap ();
3492 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3493 fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
3495 ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
3497 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3498 ira_print_disposition (ira_dump_file);
3500 ira_free_bitmap (coloring_allocno_bitmap);
3505 /* Move spill/restore code, which are to be generated in ira-emit.c,
3506 to less frequent points (if it is profitable) by reassigning some
3507 allocnos (in loop with subloops containing in another loop) to
3508 memory which results in longer live-range where the corresponding
3509 pseudo-registers will be in memory. */
3510 static void
3511 move_spill_restore (void)
3513 int cost, regno, hard_regno, hard_regno2, index;
3514 bool changed_p;
3515 int enter_freq, exit_freq;
3516 machine_mode mode;
3517 enum reg_class rclass;
3518 ira_allocno_t a, parent_allocno, subloop_allocno;
3519 ira_loop_tree_node_t parent, loop_node, subloop_node;
3520 ira_allocno_iterator ai;
3522 for (;;)
3524 changed_p = false;
3525 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3526 fprintf (ira_dump_file, "New iteration of spill/restore move\n");
3527 FOR_EACH_ALLOCNO (a, ai)
3529 regno = ALLOCNO_REGNO (a);
3530 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
3531 if (ALLOCNO_CAP_MEMBER (a) != NULL
3532 || ALLOCNO_CAP (a) != NULL
3533 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
3534 || loop_node->children == NULL
3535 /* don't do the optimization because it can create
3536 copies and the reload pass can spill the allocno set
3537 by copy although the allocno will not get memory
3538 slot. */
3539 || ira_equiv_no_lvalue_p (regno)
3540 || !bitmap_bit_p (loop_node->border_allocnos, ALLOCNO_NUM (a))
3541 /* Do not spill static chain pointer pseudo when
3542 non-local goto is used. */
3543 || non_spilled_static_chain_regno_p (regno))
3544 continue;
3545 mode = ALLOCNO_MODE (a);
3546 rclass = ALLOCNO_CLASS (a);
3547 index = ira_class_hard_reg_index[rclass][hard_regno];
3548 ira_assert (index >= 0);
3549 cost = (ALLOCNO_MEMORY_COST (a)
3550 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
3551 ? ALLOCNO_CLASS_COST (a)
3552 : ALLOCNO_HARD_REG_COSTS (a)[index]));
3553 ira_init_register_move_cost_if_necessary (mode);
3554 for (subloop_node = loop_node->subloops;
3555 subloop_node != NULL;
3556 subloop_node = subloop_node->subloop_next)
3558 ira_assert (subloop_node->bb == NULL);
3559 subloop_allocno = subloop_node->regno_allocno_map[regno];
3560 if (subloop_allocno == NULL)
3561 continue;
3562 ira_assert (rclass == ALLOCNO_CLASS (subloop_allocno));
3563 /* We have accumulated cost. To get the real cost of
3564 allocno usage in the loop we should subtract costs of
3565 the subloop allocnos. */
3566 cost -= (ALLOCNO_MEMORY_COST (subloop_allocno)
3567 - (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
3568 ? ALLOCNO_CLASS_COST (subloop_allocno)
3569 : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]));
3570 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
3571 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
3572 if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
3573 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3574 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3575 else
3577 cost
3578 += (ira_memory_move_cost[mode][rclass][0] * exit_freq
3579 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3580 if (hard_regno2 != hard_regno)
3581 cost -= (ira_register_move_cost[mode][rclass][rclass]
3582 * (exit_freq + enter_freq));
3585 if ((parent = loop_node->parent) != NULL
3586 && (parent_allocno = parent->regno_allocno_map[regno]) != NULL)
3588 ira_assert (rclass == ALLOCNO_CLASS (parent_allocno));
3589 exit_freq = ira_loop_edge_freq (loop_node, regno, true);
3590 enter_freq = ira_loop_edge_freq (loop_node, regno, false);
3591 if ((hard_regno2 = ALLOCNO_HARD_REGNO (parent_allocno)) < 0)
3592 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3593 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3594 else
3596 cost
3597 += (ira_memory_move_cost[mode][rclass][1] * exit_freq
3598 + ira_memory_move_cost[mode][rclass][0] * enter_freq);
3599 if (hard_regno2 != hard_regno)
3600 cost -= (ira_register_move_cost[mode][rclass][rclass]
3601 * (exit_freq + enter_freq));
3604 if (cost < 0)
3606 ALLOCNO_HARD_REGNO (a) = -1;
3607 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3609 fprintf
3610 (ira_dump_file,
3611 " Moving spill/restore for a%dr%d up from loop %d",
3612 ALLOCNO_NUM (a), regno, loop_node->loop_num);
3613 fprintf (ira_dump_file, " - profit %d\n", -cost);
3615 changed_p = true;
3618 if (! changed_p)
3619 break;
3625 /* Update current hard reg costs and current conflict hard reg costs
3626 for allocno A. It is done by processing its copies containing
3627 other allocnos already assigned. */
3628 static void
3629 update_curr_costs (ira_allocno_t a)
3631 int i, hard_regno, cost;
3632 machine_mode mode;
3633 enum reg_class aclass, rclass;
3634 ira_allocno_t another_a;
3635 ira_copy_t cp, next_cp;
3637 ira_free_allocno_updated_costs (a);
3638 ira_assert (! ALLOCNO_ASSIGNED_P (a));
3639 aclass = ALLOCNO_CLASS (a);
3640 if (aclass == NO_REGS)
3641 return;
3642 mode = ALLOCNO_MODE (a);
3643 ira_init_register_move_cost_if_necessary (mode);
3644 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3646 if (cp->first == a)
3648 next_cp = cp->next_first_allocno_copy;
3649 another_a = cp->second;
3651 else if (cp->second == a)
3653 next_cp = cp->next_second_allocno_copy;
3654 another_a = cp->first;
3656 else
3657 gcc_unreachable ();
3658 if (! ira_reg_classes_intersect_p[aclass][ALLOCNO_CLASS (another_a)]
3659 || ! ALLOCNO_ASSIGNED_P (another_a)
3660 || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
3661 continue;
3662 rclass = REGNO_REG_CLASS (hard_regno);
3663 i = ira_class_hard_reg_index[aclass][hard_regno];
3664 if (i < 0)
3665 continue;
3666 cost = (cp->first == a
3667 ? ira_register_move_cost[mode][rclass][aclass]
3668 : ira_register_move_cost[mode][aclass][rclass]);
3669 ira_allocate_and_set_or_copy_costs
3670 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a),
3671 ALLOCNO_HARD_REG_COSTS (a));
3672 ira_allocate_and_set_or_copy_costs
3673 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
3674 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
3675 ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3676 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3680 /* Try to assign hard registers to the unassigned allocnos and
3681 allocnos conflicting with them or conflicting with allocnos whose
3682 regno >= START_REGNO. The function is called after ira_flattening,
3683 so more allocnos (including ones created in ira-emit.c) will have a
3684 chance to get a hard register. We use simple assignment algorithm
3685 based on priorities. */
3686 void
3687 ira_reassign_conflict_allocnos (int start_regno)
3689 int i, allocnos_to_color_num;
3690 ira_allocno_t a;
3691 enum reg_class aclass;
3692 bitmap allocnos_to_color;
3693 ira_allocno_iterator ai;
3695 allocnos_to_color = ira_allocate_bitmap ();
3696 allocnos_to_color_num = 0;
3697 FOR_EACH_ALLOCNO (a, ai)
3699 int n = ALLOCNO_NUM_OBJECTS (a);
3701 if (! ALLOCNO_ASSIGNED_P (a)
3702 && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
3704 if (ALLOCNO_CLASS (a) != NO_REGS)
3705 sorted_allocnos[allocnos_to_color_num++] = a;
3706 else
3708 ALLOCNO_ASSIGNED_P (a) = true;
3709 ALLOCNO_HARD_REGNO (a) = -1;
3710 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3711 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3713 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
3715 if (ALLOCNO_REGNO (a) < start_regno
3716 || (aclass = ALLOCNO_CLASS (a)) == NO_REGS)
3717 continue;
3718 for (i = 0; i < n; i++)
3720 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3721 ira_object_t conflict_obj;
3722 ira_object_conflict_iterator oci;
3724 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3726 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3728 ira_assert (ira_reg_classes_intersect_p
3729 [aclass][ALLOCNO_CLASS (conflict_a)]);
3730 if (!bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
3731 continue;
3732 sorted_allocnos[allocnos_to_color_num++] = conflict_a;
3736 ira_free_bitmap (allocnos_to_color);
3737 if (allocnos_to_color_num > 1)
3739 setup_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
3740 qsort (sorted_allocnos, allocnos_to_color_num, sizeof (ira_allocno_t),
3741 allocno_priority_compare_func);
3743 for (i = 0; i < allocnos_to_color_num; i++)
3745 a = sorted_allocnos[i];
3746 ALLOCNO_ASSIGNED_P (a) = false;
3747 update_curr_costs (a);
3749 for (i = 0; i < allocnos_to_color_num; i++)
3751 a = sorted_allocnos[i];
3752 if (assign_hard_reg (a, true))
3754 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3755 fprintf
3756 (ira_dump_file,
3757 " Secondary allocation: assign hard reg %d to reg %d\n",
3758 ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
3765 /* This page contains functions used to find conflicts using allocno
3766 live ranges. */
3768 #ifdef ENABLE_IRA_CHECKING
3770 /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
3771 intersect. This should be used when there is only one region.
3772 Currently this is used during reload. */
3773 static bool
3774 conflict_by_live_ranges_p (int regno1, int regno2)
3776 ira_allocno_t a1, a2;
3778 ira_assert (regno1 >= FIRST_PSEUDO_REGISTER
3779 && regno2 >= FIRST_PSEUDO_REGISTER);
3780 /* Reg info calculated by dataflow infrastructure can be different
3781 from one calculated by regclass. */
3782 if ((a1 = ira_loop_tree_root->regno_allocno_map[regno1]) == NULL
3783 || (a2 = ira_loop_tree_root->regno_allocno_map[regno2]) == NULL)
3784 return false;
3785 return allocnos_conflict_by_live_ranges_p (a1, a2);
3788 #endif
3792 /* This page contains code to coalesce memory stack slots used by
3793 spilled allocnos. This results in smaller stack frame, better data
3794 locality, and in smaller code for some architectures like
3795 x86/x86_64 where insn size depends on address displacement value.
3796 On the other hand, it can worsen insn scheduling after the RA but
3797 in practice it is less important than smaller stack frames. */
3799 /* TRUE if we coalesced some allocnos. In other words, if we got
3800 loops formed by members first_coalesced_allocno and
3801 next_coalesced_allocno containing more one allocno. */
3802 static bool allocno_coalesced_p;
3804 /* Bitmap used to prevent a repeated allocno processing because of
3805 coalescing. */
3806 static bitmap processed_coalesced_allocno_bitmap;
3808 /* See below. */
3809 typedef struct coalesce_data *coalesce_data_t;
3811 /* To decrease footprint of ira_allocno structure we store all data
3812 needed only for coalescing in the following structure. */
3813 struct coalesce_data
3815 /* Coalesced allocnos form a cyclic list. One allocno given by
3816 FIRST represents all coalesced allocnos. The
3817 list is chained by NEXT. */
3818 ira_allocno_t first;
3819 ira_allocno_t next;
3820 int temp;
3823 /* Container for storing allocno data concerning coalescing. */
3824 static coalesce_data_t allocno_coalesce_data;
3826 /* Macro to access the data concerning coalescing. */
3827 #define ALLOCNO_COALESCE_DATA(a) ((coalesce_data_t) ALLOCNO_ADD_DATA (a))
3829 /* Merge two sets of coalesced allocnos given correspondingly by
3830 allocnos A1 and A2 (more accurately merging A2 set into A1
3831 set). */
3832 static void
3833 merge_allocnos (ira_allocno_t a1, ira_allocno_t a2)
3835 ira_allocno_t a, first, last, next;
3837 first = ALLOCNO_COALESCE_DATA (a1)->first;
3838 a = ALLOCNO_COALESCE_DATA (a2)->first;
3839 if (first == a)
3840 return;
3841 for (last = a2, a = ALLOCNO_COALESCE_DATA (a2)->next;;
3842 a = ALLOCNO_COALESCE_DATA (a)->next)
3844 ALLOCNO_COALESCE_DATA (a)->first = first;
3845 if (a == a2)
3846 break;
3847 last = a;
3849 next = allocno_coalesce_data[ALLOCNO_NUM (first)].next;
3850 allocno_coalesce_data[ALLOCNO_NUM (first)].next = a2;
3851 allocno_coalesce_data[ALLOCNO_NUM (last)].next = next;
3854 /* Return TRUE if there are conflicting allocnos from two sets of
3855 coalesced allocnos given correspondingly by allocnos A1 and A2. We
3856 use live ranges to find conflicts because conflicts are represented
3857 only for allocnos of the same allocno class and during the reload
3858 pass we coalesce allocnos for sharing stack memory slots. */
3859 static bool
3860 coalesced_allocno_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
3862 ira_allocno_t a, conflict_a;
3864 if (allocno_coalesced_p)
3866 bitmap_clear (processed_coalesced_allocno_bitmap);
3867 for (a = ALLOCNO_COALESCE_DATA (a1)->next;;
3868 a = ALLOCNO_COALESCE_DATA (a)->next)
3870 bitmap_set_bit (processed_coalesced_allocno_bitmap, ALLOCNO_NUM (a));
3871 if (a == a1)
3872 break;
3875 for (a = ALLOCNO_COALESCE_DATA (a2)->next;;
3876 a = ALLOCNO_COALESCE_DATA (a)->next)
3878 for (conflict_a = ALLOCNO_COALESCE_DATA (a1)->next;;
3879 conflict_a = ALLOCNO_COALESCE_DATA (conflict_a)->next)
3881 if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
3882 return true;
3883 if (conflict_a == a1)
3884 break;
3886 if (a == a2)
3887 break;
3889 return false;
3892 /* The major function for aggressive allocno coalescing. We coalesce
3893 only spilled allocnos. If some allocnos have been coalesced, we
3894 set up flag allocno_coalesced_p. */
3895 static void
3896 coalesce_allocnos (void)
3898 ira_allocno_t a;
3899 ira_copy_t cp, next_cp;
3900 unsigned int j;
3901 int i, n, cp_num, regno;
3902 bitmap_iterator bi;
3904 cp_num = 0;
3905 /* Collect copies. */
3906 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
3908 a = ira_allocnos[j];
3909 regno = ALLOCNO_REGNO (a);
3910 if (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
3911 || ira_equiv_no_lvalue_p (regno))
3912 continue;
3913 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3915 if (cp->first == a)
3917 next_cp = cp->next_first_allocno_copy;
3918 regno = ALLOCNO_REGNO (cp->second);
3919 /* For priority coloring we coalesce allocnos only with
3920 the same allocno class not with intersected allocno
3921 classes as it were possible. It is done for
3922 simplicity. */
3923 if ((cp->insn != NULL || cp->constraint_p)
3924 && ALLOCNO_ASSIGNED_P (cp->second)
3925 && ALLOCNO_HARD_REGNO (cp->second) < 0
3926 && ! ira_equiv_no_lvalue_p (regno))
3927 sorted_copies[cp_num++] = cp;
3929 else if (cp->second == a)
3930 next_cp = cp->next_second_allocno_copy;
3931 else
3932 gcc_unreachable ();
3935 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
3936 /* Coalesced copies, most frequently executed first. */
3937 for (; cp_num != 0;)
3939 for (i = 0; i < cp_num; i++)
3941 cp = sorted_copies[i];
3942 if (! coalesced_allocno_conflict_p (cp->first, cp->second))
3944 allocno_coalesced_p = true;
3945 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3946 fprintf
3947 (ira_dump_file,
3948 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
3949 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
3950 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
3951 cp->freq);
3952 merge_allocnos (cp->first, cp->second);
3953 i++;
3954 break;
3957 /* Collect the rest of copies. */
3958 for (n = 0; i < cp_num; i++)
3960 cp = sorted_copies[i];
3961 if (allocno_coalesce_data[ALLOCNO_NUM (cp->first)].first
3962 != allocno_coalesce_data[ALLOCNO_NUM (cp->second)].first)
3963 sorted_copies[n++] = cp;
3965 cp_num = n;
3969 /* Usage cost and order number of coalesced allocno set to which
3970 given pseudo register belongs to. */
3971 static int *regno_coalesced_allocno_cost;
3972 static int *regno_coalesced_allocno_num;
3974 /* Sort pseudos according frequencies of coalesced allocno sets they
3975 belong to (putting most frequently ones first), and according to
3976 coalesced allocno set order numbers. */
3977 static int
3978 coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
3980 const int regno1 = *(const int *) v1p;
3981 const int regno2 = *(const int *) v2p;
3982 int diff;
3984 if ((diff = (regno_coalesced_allocno_cost[regno2]
3985 - regno_coalesced_allocno_cost[regno1])) != 0)
3986 return diff;
3987 if ((diff = (regno_coalesced_allocno_num[regno1]
3988 - regno_coalesced_allocno_num[regno2])) != 0)
3989 return diff;
3990 return regno1 - regno2;
3993 /* Widest width in which each pseudo reg is referred to (via subreg).
3994 It is used for sorting pseudo registers. */
3995 static machine_mode *regno_max_ref_mode;
3997 /* Sort pseudos according their slot numbers (putting ones with
3998 smaller numbers first, or last when the frame pointer is not
3999 needed). */
4000 static int
4001 coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
4003 const int regno1 = *(const int *) v1p;
4004 const int regno2 = *(const int *) v2p;
4005 ira_allocno_t a1 = ira_regno_allocno_map[regno1];
4006 ira_allocno_t a2 = ira_regno_allocno_map[regno2];
4007 int diff, slot_num1, slot_num2;
4008 machine_mode mode1, mode2;
4010 if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
4012 if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
4013 return regno1 - regno2;
4014 return 1;
4016 else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
4017 return -1;
4018 slot_num1 = -ALLOCNO_HARD_REGNO (a1);
4019 slot_num2 = -ALLOCNO_HARD_REGNO (a2);
4020 if ((diff = slot_num1 - slot_num2) != 0)
4021 return (frame_pointer_needed
4022 || (!FRAME_GROWS_DOWNWARD) == STACK_GROWS_DOWNWARD ? diff : -diff);
4023 mode1 = wider_subreg_mode (PSEUDO_REGNO_MODE (regno1),
4024 regno_max_ref_mode[regno1]);
4025 mode2 = wider_subreg_mode (PSEUDO_REGNO_MODE (regno2),
4026 regno_max_ref_mode[regno2]);
4027 if ((diff = compare_sizes_for_sort (GET_MODE_SIZE (mode2),
4028 GET_MODE_SIZE (mode1))) != 0)
4029 return diff;
4030 return regno1 - regno2;
4033 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
4034 for coalesced allocno sets containing allocnos with their regnos
4035 given in array PSEUDO_REGNOS of length N. */
4036 static void
4037 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
4039 int i, num, regno, cost;
4040 ira_allocno_t allocno, a;
4042 for (num = i = 0; i < n; i++)
4044 regno = pseudo_regnos[i];
4045 allocno = ira_regno_allocno_map[regno];
4046 if (allocno == NULL)
4048 regno_coalesced_allocno_cost[regno] = 0;
4049 regno_coalesced_allocno_num[regno] = ++num;
4050 continue;
4052 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
4053 continue;
4054 num++;
4055 for (cost = 0, a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4056 a = ALLOCNO_COALESCE_DATA (a)->next)
4058 cost += ALLOCNO_FREQ (a);
4059 if (a == allocno)
4060 break;
4062 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4063 a = ALLOCNO_COALESCE_DATA (a)->next)
4065 regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
4066 regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
4067 if (a == allocno)
4068 break;
4073 /* Collect spilled allocnos representing coalesced allocno sets (the
4074 first coalesced allocno). The collected allocnos are returned
4075 through array SPILLED_COALESCED_ALLOCNOS. The function returns the
4076 number of the collected allocnos. The allocnos are given by their
4077 regnos in array PSEUDO_REGNOS of length N. */
4078 static int
4079 collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
4080 ira_allocno_t *spilled_coalesced_allocnos)
4082 int i, num, regno;
4083 ira_allocno_t allocno;
4085 for (num = i = 0; i < n; i++)
4087 regno = pseudo_regnos[i];
4088 allocno = ira_regno_allocno_map[regno];
4089 if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
4090 || ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
4091 continue;
4092 spilled_coalesced_allocnos[num++] = allocno;
4094 return num;
4097 /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
4098 given slot contains live ranges of coalesced allocnos assigned to
4099 given slot. */
4100 static live_range_t *slot_coalesced_allocnos_live_ranges;
4102 /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
4103 ranges intersected with live ranges of coalesced allocnos assigned
4104 to slot with number N. */
4105 static bool
4106 slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno, int n)
4108 ira_allocno_t a;
4110 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4111 a = ALLOCNO_COALESCE_DATA (a)->next)
4113 int i;
4114 int nr = ALLOCNO_NUM_OBJECTS (a);
4115 gcc_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
4116 for (i = 0; i < nr; i++)
4118 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4120 if (ira_live_ranges_intersect_p
4121 (slot_coalesced_allocnos_live_ranges[n],
4122 OBJECT_LIVE_RANGES (obj)))
4123 return true;
4125 if (a == allocno)
4126 break;
4128 return false;
4131 /* Update live ranges of slot to which coalesced allocnos represented
4132 by ALLOCNO were assigned. */
4133 static void
4134 setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno)
4136 int i, n;
4137 ira_allocno_t a;
4138 live_range_t r;
4140 n = ALLOCNO_COALESCE_DATA (allocno)->temp;
4141 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4142 a = ALLOCNO_COALESCE_DATA (a)->next)
4144 int nr = ALLOCNO_NUM_OBJECTS (a);
4145 gcc_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
4146 for (i = 0; i < nr; i++)
4148 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4150 r = ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj));
4151 slot_coalesced_allocnos_live_ranges[n]
4152 = ira_merge_live_ranges
4153 (slot_coalesced_allocnos_live_ranges[n], r);
4155 if (a == allocno)
4156 break;
4160 /* We have coalesced allocnos involving in copies. Coalesce allocnos
4161 further in order to share the same memory stack slot. Allocnos
4162 representing sets of allocnos coalesced before the call are given
4163 in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
4164 some allocnos were coalesced in the function. */
4165 static bool
4166 coalesce_spill_slots (ira_allocno_t *spilled_coalesced_allocnos, int num)
4168 int i, j, n, last_coalesced_allocno_num;
4169 ira_allocno_t allocno, a;
4170 bool merged_p = false;
4171 bitmap set_jump_crosses = regstat_get_setjmp_crosses ();
4173 slot_coalesced_allocnos_live_ranges
4174 = (live_range_t *) ira_allocate (sizeof (live_range_t) * ira_allocnos_num);
4175 memset (slot_coalesced_allocnos_live_ranges, 0,
4176 sizeof (live_range_t) * ira_allocnos_num);
4177 last_coalesced_allocno_num = 0;
4178 /* Coalesce non-conflicting spilled allocnos preferring most
4179 frequently used. */
4180 for (i = 0; i < num; i++)
4182 allocno = spilled_coalesced_allocnos[i];
4183 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
4184 || bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (allocno))
4185 || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
4186 continue;
4187 for (j = 0; j < i; j++)
4189 a = spilled_coalesced_allocnos[j];
4190 n = ALLOCNO_COALESCE_DATA (a)->temp;
4191 if (ALLOCNO_COALESCE_DATA (a)->first == a
4192 && ! bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (a))
4193 && ! ira_equiv_no_lvalue_p (ALLOCNO_REGNO (a))
4194 && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno, n))
4195 break;
4197 if (j >= i)
4199 /* No coalescing: set up number for coalesced allocnos
4200 represented by ALLOCNO. */
4201 ALLOCNO_COALESCE_DATA (allocno)->temp = last_coalesced_allocno_num++;
4202 setup_slot_coalesced_allocno_live_ranges (allocno);
4204 else
4206 allocno_coalesced_p = true;
4207 merged_p = true;
4208 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4209 fprintf (ira_dump_file,
4210 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
4211 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
4212 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
4213 ALLOCNO_COALESCE_DATA (allocno)->temp
4214 = ALLOCNO_COALESCE_DATA (a)->temp;
4215 setup_slot_coalesced_allocno_live_ranges (allocno);
4216 merge_allocnos (a, allocno);
4217 ira_assert (ALLOCNO_COALESCE_DATA (a)->first == a);
4220 for (i = 0; i < ira_allocnos_num; i++)
4221 ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges[i]);
4222 ira_free (slot_coalesced_allocnos_live_ranges);
4223 return merged_p;
4226 /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
4227 subsequent assigning stack slots to them in the reload pass. To do
4228 this we coalesce spilled allocnos first to decrease the number of
4229 memory-memory move insns. This function is called by the
4230 reload. */
4231 void
4232 ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
4233 machine_mode *reg_max_ref_mode)
4235 int max_regno = max_reg_num ();
4236 int i, regno, num, slot_num;
4237 ira_allocno_t allocno, a;
4238 ira_allocno_iterator ai;
4239 ira_allocno_t *spilled_coalesced_allocnos;
4241 ira_assert (! ira_use_lra_p);
4243 /* Set up allocnos can be coalesced. */
4244 coloring_allocno_bitmap = ira_allocate_bitmap ();
4245 for (i = 0; i < n; i++)
4247 regno = pseudo_regnos[i];
4248 allocno = ira_regno_allocno_map[regno];
4249 if (allocno != NULL)
4250 bitmap_set_bit (coloring_allocno_bitmap, ALLOCNO_NUM (allocno));
4252 allocno_coalesced_p = false;
4253 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
4254 allocno_coalesce_data
4255 = (coalesce_data_t) ira_allocate (sizeof (struct coalesce_data)
4256 * ira_allocnos_num);
4257 /* Initialize coalesce data for allocnos. */
4258 FOR_EACH_ALLOCNO (a, ai)
4260 ALLOCNO_ADD_DATA (a) = allocno_coalesce_data + ALLOCNO_NUM (a);
4261 ALLOCNO_COALESCE_DATA (a)->first = a;
4262 ALLOCNO_COALESCE_DATA (a)->next = a;
4264 coalesce_allocnos ();
4265 ira_free_bitmap (coloring_allocno_bitmap);
4266 regno_coalesced_allocno_cost
4267 = (int *) ira_allocate (max_regno * sizeof (int));
4268 regno_coalesced_allocno_num
4269 = (int *) ira_allocate (max_regno * sizeof (int));
4270 memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
4271 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
4272 /* Sort regnos according frequencies of the corresponding coalesced
4273 allocno sets. */
4274 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
4275 spilled_coalesced_allocnos
4276 = (ira_allocno_t *) ira_allocate (ira_allocnos_num
4277 * sizeof (ira_allocno_t));
4278 /* Collect allocnos representing the spilled coalesced allocno
4279 sets. */
4280 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
4281 spilled_coalesced_allocnos);
4282 if (flag_ira_share_spill_slots
4283 && coalesce_spill_slots (spilled_coalesced_allocnos, num))
4285 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
4286 qsort (pseudo_regnos, n, sizeof (int),
4287 coalesced_pseudo_reg_freq_compare);
4288 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
4289 spilled_coalesced_allocnos);
4291 ira_free_bitmap (processed_coalesced_allocno_bitmap);
4292 allocno_coalesced_p = false;
4293 /* Assign stack slot numbers to spilled allocno sets, use smaller
4294 numbers for most frequently used coalesced allocnos. -1 is
4295 reserved for dynamic search of stack slots for pseudos spilled by
4296 the reload. */
4297 slot_num = 1;
4298 for (i = 0; i < num; i++)
4300 allocno = spilled_coalesced_allocnos[i];
4301 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
4302 || ALLOCNO_HARD_REGNO (allocno) >= 0
4303 || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
4304 continue;
4305 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4306 fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
4307 slot_num++;
4308 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4309 a = ALLOCNO_COALESCE_DATA (a)->next)
4311 ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
4312 ALLOCNO_HARD_REGNO (a) = -slot_num;
4313 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4315 machine_mode mode = wider_subreg_mode
4316 (PSEUDO_REGNO_MODE (ALLOCNO_REGNO (a)),
4317 reg_max_ref_mode[ALLOCNO_REGNO (a)]);
4318 fprintf (ira_dump_file, " a%dr%d(%d,",
4319 ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a));
4320 print_dec (GET_MODE_SIZE (mode), ira_dump_file, SIGNED);
4321 fprintf (ira_dump_file, ")\n");
4324 if (a == allocno)
4325 break;
4327 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4328 fprintf (ira_dump_file, "\n");
4330 ira_spilled_reg_stack_slots_num = slot_num - 1;
4331 ira_free (spilled_coalesced_allocnos);
4332 /* Sort regnos according the slot numbers. */
4333 regno_max_ref_mode = reg_max_ref_mode;
4334 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
4335 FOR_EACH_ALLOCNO (a, ai)
4336 ALLOCNO_ADD_DATA (a) = NULL;
4337 ira_free (allocno_coalesce_data);
4338 ira_free (regno_coalesced_allocno_num);
4339 ira_free (regno_coalesced_allocno_cost);
4344 /* This page contains code used by the reload pass to improve the
4345 final code. */
4347 /* The function is called from reload to mark changes in the
4348 allocation of REGNO made by the reload. Remember that reg_renumber
4349 reflects the change result. */
4350 void
4351 ira_mark_allocation_change (int regno)
4353 ira_allocno_t a = ira_regno_allocno_map[regno];
4354 int old_hard_regno, hard_regno, cost;
4355 enum reg_class aclass = ALLOCNO_CLASS (a);
4357 ira_assert (a != NULL);
4358 hard_regno = reg_renumber[regno];
4359 if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
4360 return;
4361 if (old_hard_regno < 0)
4362 cost = -ALLOCNO_MEMORY_COST (a);
4363 else
4365 ira_assert (ira_class_hard_reg_index[aclass][old_hard_regno] >= 0);
4366 cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
4367 ? ALLOCNO_CLASS_COST (a)
4368 : ALLOCNO_HARD_REG_COSTS (a)
4369 [ira_class_hard_reg_index[aclass][old_hard_regno]]);
4370 update_costs_from_copies (a, false, false);
4372 ira_overall_cost -= cost;
4373 ALLOCNO_HARD_REGNO (a) = hard_regno;
4374 if (hard_regno < 0)
4376 ALLOCNO_HARD_REGNO (a) = -1;
4377 cost += ALLOCNO_MEMORY_COST (a);
4379 else if (ira_class_hard_reg_index[aclass][hard_regno] >= 0)
4381 cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
4382 ? ALLOCNO_CLASS_COST (a)
4383 : ALLOCNO_HARD_REG_COSTS (a)
4384 [ira_class_hard_reg_index[aclass][hard_regno]]);
4385 update_costs_from_copies (a, true, false);
4387 else
4388 /* Reload changed class of the allocno. */
4389 cost = 0;
4390 ira_overall_cost += cost;
4393 /* This function is called when reload deletes memory-memory move. In
4394 this case we marks that the allocation of the corresponding
4395 allocnos should be not changed in future. Otherwise we risk to get
4396 a wrong code. */
4397 void
4398 ira_mark_memory_move_deletion (int dst_regno, int src_regno)
4400 ira_allocno_t dst = ira_regno_allocno_map[dst_regno];
4401 ira_allocno_t src = ira_regno_allocno_map[src_regno];
4403 ira_assert (dst != NULL && src != NULL
4404 && ALLOCNO_HARD_REGNO (dst) < 0
4405 && ALLOCNO_HARD_REGNO (src) < 0);
4406 ALLOCNO_DONT_REASSIGN_P (dst) = true;
4407 ALLOCNO_DONT_REASSIGN_P (src) = true;
4410 /* Try to assign a hard register (except for FORBIDDEN_REGS) to
4411 allocno A and return TRUE in the case of success. */
4412 static bool
4413 allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
4415 int hard_regno;
4416 enum reg_class aclass;
4417 int regno = ALLOCNO_REGNO (a);
4418 HARD_REG_SET saved[2];
4419 int i, n;
4421 n = ALLOCNO_NUM_OBJECTS (a);
4422 for (i = 0; i < n; i++)
4424 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4425 saved[i] = OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
4426 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) |= forbidden_regs;
4427 if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
4428 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) |= ira_need_caller_save_regs (a);
4430 ALLOCNO_ASSIGNED_P (a) = false;
4431 aclass = ALLOCNO_CLASS (a);
4432 update_curr_costs (a);
4433 assign_hard_reg (a, true);
4434 hard_regno = ALLOCNO_HARD_REGNO (a);
4435 reg_renumber[regno] = hard_regno;
4436 if (hard_regno < 0)
4437 ALLOCNO_HARD_REGNO (a) = -1;
4438 else
4440 ira_assert (ira_class_hard_reg_index[aclass][hard_regno] >= 0);
4441 ira_overall_cost
4442 -= (ALLOCNO_MEMORY_COST (a)
4443 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
4444 ? ALLOCNO_CLASS_COST (a)
4445 : ALLOCNO_HARD_REG_COSTS (a)[ira_class_hard_reg_index
4446 [aclass][hard_regno]]));
4447 if (ira_need_caller_save_p (a, hard_regno))
4449 ira_assert (flag_caller_saves);
4450 caller_save_needed = 1;
4454 /* If we found a hard register, modify the RTL for the pseudo
4455 register to show the hard register, and mark the pseudo register
4456 live. */
4457 if (reg_renumber[regno] >= 0)
4459 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4460 fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
4461 SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
4462 mark_home_live (regno);
4464 else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4465 fprintf (ira_dump_file, "\n");
4466 for (i = 0; i < n; i++)
4468 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4469 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) = saved[i];
4471 return reg_renumber[regno] >= 0;
4474 /* Sort pseudos according their usage frequencies (putting most
4475 frequently ones first). */
4476 static int
4477 pseudo_reg_compare (const void *v1p, const void *v2p)
4479 int regno1 = *(const int *) v1p;
4480 int regno2 = *(const int *) v2p;
4481 int diff;
4483 if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
4484 return diff;
4485 return regno1 - regno2;
4488 /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
4489 NUM of them) or spilled pseudos conflicting with pseudos in
4490 SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
4491 allocation has been changed. The function doesn't use
4492 BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
4493 PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
4494 is called by the reload pass at the end of each reload
4495 iteration. */
4496 bool
4497 ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
4498 HARD_REG_SET bad_spill_regs,
4499 HARD_REG_SET *pseudo_forbidden_regs,
4500 HARD_REG_SET *pseudo_previous_regs,
4501 bitmap spilled)
4503 int i, n, regno;
4504 bool changed_p;
4505 ira_allocno_t a;
4506 HARD_REG_SET forbidden_regs;
4507 bitmap temp = BITMAP_ALLOC (NULL);
4509 /* Add pseudos which conflict with pseudos already in
4510 SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
4511 to allocating in two steps as some of the conflicts might have
4512 a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
4513 for (i = 0; i < num; i++)
4514 bitmap_set_bit (temp, spilled_pseudo_regs[i]);
4516 for (i = 0, n = num; i < n; i++)
4518 int nr, j;
4519 int regno = spilled_pseudo_regs[i];
4520 bitmap_set_bit (temp, regno);
4522 a = ira_regno_allocno_map[regno];
4523 nr = ALLOCNO_NUM_OBJECTS (a);
4524 for (j = 0; j < nr; j++)
4526 ira_object_t conflict_obj;
4527 ira_object_t obj = ALLOCNO_OBJECT (a, j);
4528 ira_object_conflict_iterator oci;
4530 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
4532 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
4533 if (ALLOCNO_HARD_REGNO (conflict_a) < 0
4534 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
4535 && bitmap_set_bit (temp, ALLOCNO_REGNO (conflict_a)))
4537 spilled_pseudo_regs[num++] = ALLOCNO_REGNO (conflict_a);
4538 /* ?!? This seems wrong. */
4539 bitmap_set_bit (consideration_allocno_bitmap,
4540 ALLOCNO_NUM (conflict_a));
4546 if (num > 1)
4547 qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
4548 changed_p = false;
4549 /* Try to assign hard registers to pseudos from
4550 SPILLED_PSEUDO_REGS. */
4551 for (i = 0; i < num; i++)
4553 regno = spilled_pseudo_regs[i];
4554 forbidden_regs = (bad_spill_regs
4555 | pseudo_forbidden_regs[regno]
4556 | pseudo_previous_regs[regno]);
4557 gcc_assert (reg_renumber[regno] < 0);
4558 a = ira_regno_allocno_map[regno];
4559 ira_mark_allocation_change (regno);
4560 ira_assert (reg_renumber[regno] < 0);
4561 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4562 fprintf (ira_dump_file,
4563 " Try Assign %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
4564 ALLOCNO_MEMORY_COST (a)
4565 - ALLOCNO_CLASS_COST (a));
4566 allocno_reload_assign (a, forbidden_regs);
4567 if (reg_renumber[regno] >= 0)
4569 CLEAR_REGNO_REG_SET (spilled, regno);
4570 changed_p = true;
4573 BITMAP_FREE (temp);
4574 return changed_p;
4577 /* The function is called by reload and returns already allocated
4578 stack slot (if any) for REGNO with given INHERENT_SIZE and
4579 TOTAL_SIZE. In the case of failure to find a slot which can be
4580 used for REGNO, the function returns NULL. */
4582 ira_reuse_stack_slot (int regno, poly_uint64 inherent_size,
4583 poly_uint64 total_size)
4585 unsigned int i;
4586 int slot_num, best_slot_num;
4587 int cost, best_cost;
4588 ira_copy_t cp, next_cp;
4589 ira_allocno_t another_allocno, allocno = ira_regno_allocno_map[regno];
4590 rtx x;
4591 bitmap_iterator bi;
4592 class ira_spilled_reg_stack_slot *slot = NULL;
4594 ira_assert (! ira_use_lra_p);
4596 ira_assert (known_eq (inherent_size, PSEUDO_REGNO_BYTES (regno))
4597 && known_le (inherent_size, total_size)
4598 && ALLOCNO_HARD_REGNO (allocno) < 0);
4599 if (! flag_ira_share_spill_slots)
4600 return NULL_RTX;
4601 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4602 if (slot_num != -1)
4604 slot = &ira_spilled_reg_stack_slots[slot_num];
4605 x = slot->mem;
4607 else
4609 best_cost = best_slot_num = -1;
4610 x = NULL_RTX;
4611 /* It means that the pseudo was spilled in the reload pass, try
4612 to reuse a slot. */
4613 for (slot_num = 0;
4614 slot_num < ira_spilled_reg_stack_slots_num;
4615 slot_num++)
4617 slot = &ira_spilled_reg_stack_slots[slot_num];
4618 if (slot->mem == NULL_RTX)
4619 continue;
4620 if (maybe_lt (slot->width, total_size)
4621 || maybe_lt (GET_MODE_SIZE (GET_MODE (slot->mem)), inherent_size))
4622 continue;
4624 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4625 FIRST_PSEUDO_REGISTER, i, bi)
4627 another_allocno = ira_regno_allocno_map[i];
4628 if (allocnos_conflict_by_live_ranges_p (allocno,
4629 another_allocno))
4630 goto cont;
4632 for (cost = 0, cp = ALLOCNO_COPIES (allocno);
4633 cp != NULL;
4634 cp = next_cp)
4636 if (cp->first == allocno)
4638 next_cp = cp->next_first_allocno_copy;
4639 another_allocno = cp->second;
4641 else if (cp->second == allocno)
4643 next_cp = cp->next_second_allocno_copy;
4644 another_allocno = cp->first;
4646 else
4647 gcc_unreachable ();
4648 if (cp->insn == NULL_RTX)
4649 continue;
4650 if (bitmap_bit_p (&slot->spilled_regs,
4651 ALLOCNO_REGNO (another_allocno)))
4652 cost += cp->freq;
4654 if (cost > best_cost)
4656 best_cost = cost;
4657 best_slot_num = slot_num;
4659 cont:
4662 if (best_cost >= 0)
4664 slot_num = best_slot_num;
4665 slot = &ira_spilled_reg_stack_slots[slot_num];
4666 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4667 x = slot->mem;
4668 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4671 if (x != NULL_RTX)
4673 ira_assert (known_ge (slot->width, total_size));
4674 #ifdef ENABLE_IRA_CHECKING
4675 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4676 FIRST_PSEUDO_REGISTER, i, bi)
4678 ira_assert (! conflict_by_live_ranges_p (regno, i));
4680 #endif
4681 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4682 if (internal_flag_ira_verbose > 3 && ira_dump_file)
4684 fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
4685 regno, REG_FREQ (regno), slot_num);
4686 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4687 FIRST_PSEUDO_REGISTER, i, bi)
4689 if ((unsigned) regno != i)
4690 fprintf (ira_dump_file, " %d", i);
4692 fprintf (ira_dump_file, "\n");
4695 return x;
4698 /* This is called by reload every time a new stack slot X with
4699 TOTAL_SIZE was allocated for REGNO. We store this info for
4700 subsequent ira_reuse_stack_slot calls. */
4701 void
4702 ira_mark_new_stack_slot (rtx x, int regno, poly_uint64 total_size)
4704 class ira_spilled_reg_stack_slot *slot;
4705 int slot_num;
4706 ira_allocno_t allocno;
4708 ira_assert (! ira_use_lra_p);
4710 ira_assert (known_le (PSEUDO_REGNO_BYTES (regno), total_size));
4711 allocno = ira_regno_allocno_map[regno];
4712 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4713 if (slot_num == -1)
4715 slot_num = ira_spilled_reg_stack_slots_num++;
4716 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4718 slot = &ira_spilled_reg_stack_slots[slot_num];
4719 INIT_REG_SET (&slot->spilled_regs);
4720 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4721 slot->mem = x;
4722 slot->width = total_size;
4723 if (internal_flag_ira_verbose > 3 && ira_dump_file)
4724 fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
4725 regno, REG_FREQ (regno), slot_num);
4729 /* Return spill cost for pseudo-registers whose numbers are in array
4730 REGNOS (with a negative number as an end marker) for reload with
4731 given IN and OUT for INSN. Return also number points (through
4732 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
4733 the register pressure is high, number of references of the
4734 pseudo-registers (through NREFS), the number of psuedo registers
4735 whose allocated register wouldn't need saving in the prologue
4736 (through CALL_USED_COUNT), and the first hard regno occupied by the
4737 pseudo-registers (through FIRST_HARD_REGNO). */
4738 static int
4739 calculate_spill_cost (int *regnos, rtx in, rtx out, rtx_insn *insn,
4740 int *excess_pressure_live_length,
4741 int *nrefs, int *call_used_count, int *first_hard_regno)
4743 int i, cost, regno, hard_regno, count, saved_cost;
4744 bool in_p, out_p;
4745 int length;
4746 ira_allocno_t a;
4748 *nrefs = 0;
4749 for (length = count = cost = i = 0;; i++)
4751 regno = regnos[i];
4752 if (regno < 0)
4753 break;
4754 *nrefs += REG_N_REFS (regno);
4755 hard_regno = reg_renumber[regno];
4756 ira_assert (hard_regno >= 0);
4757 a = ira_regno_allocno_map[regno];
4758 length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) / ALLOCNO_NUM_OBJECTS (a);
4759 cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
4760 if (in_hard_reg_set_p (crtl->abi->full_reg_clobbers (),
4761 ALLOCNO_MODE (a), hard_regno))
4762 count++;
4763 in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
4764 out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
4765 if ((in_p || out_p)
4766 && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
4768 saved_cost = 0;
4769 if (in_p)
4770 saved_cost += ira_memory_move_cost
4771 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][1];
4772 if (out_p)
4773 saved_cost
4774 += ira_memory_move_cost
4775 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][0];
4776 cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
4779 *excess_pressure_live_length = length;
4780 *call_used_count = count;
4781 hard_regno = -1;
4782 if (regnos[0] >= 0)
4784 hard_regno = reg_renumber[regnos[0]];
4786 *first_hard_regno = hard_regno;
4787 return cost;
4790 /* Return TRUE if spilling pseudo-registers whose numbers are in array
4791 REGNOS is better than spilling pseudo-registers with numbers in
4792 OTHER_REGNOS for reload with given IN and OUT for INSN. The
4793 function used by the reload pass to make better register spilling
4794 decisions. */
4795 bool
4796 ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
4797 rtx in, rtx out, rtx_insn *insn)
4799 int cost, other_cost;
4800 int length, other_length;
4801 int nrefs, other_nrefs;
4802 int call_used_count, other_call_used_count;
4803 int hard_regno, other_hard_regno;
4805 cost = calculate_spill_cost (regnos, in, out, insn,
4806 &length, &nrefs, &call_used_count, &hard_regno);
4807 other_cost = calculate_spill_cost (other_regnos, in, out, insn,
4808 &other_length, &other_nrefs,
4809 &other_call_used_count,
4810 &other_hard_regno);
4811 if (nrefs == 0 && other_nrefs != 0)
4812 return true;
4813 if (nrefs != 0 && other_nrefs == 0)
4814 return false;
4815 if (cost != other_cost)
4816 return cost < other_cost;
4817 if (length != other_length)
4818 return length > other_length;
4819 #ifdef REG_ALLOC_ORDER
4820 if (hard_regno >= 0 && other_hard_regno >= 0)
4821 return (inv_reg_alloc_order[hard_regno]
4822 < inv_reg_alloc_order[other_hard_regno]);
4823 #else
4824 if (call_used_count != other_call_used_count)
4825 return call_used_count > other_call_used_count;
4826 #endif
4827 return false;
4832 /* Allocate and initialize data necessary for assign_hard_reg. */
4833 void
4834 ira_initiate_assign (void)
4836 sorted_allocnos
4837 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4838 * ira_allocnos_num);
4839 consideration_allocno_bitmap = ira_allocate_bitmap ();
4840 initiate_cost_update ();
4841 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4842 sorted_copies = (ira_copy_t *) ira_allocate (ira_copies_num
4843 * sizeof (ira_copy_t));
4846 /* Deallocate data used by assign_hard_reg. */
4847 void
4848 ira_finish_assign (void)
4850 ira_free (sorted_allocnos);
4851 ira_free_bitmap (consideration_allocno_bitmap);
4852 finish_cost_update ();
4853 ira_free (allocno_priorities);
4854 ira_free (sorted_copies);
4859 /* Entry function doing color-based register allocation. */
4860 static void
4861 color (void)
4863 allocno_stack_vec.create (ira_allocnos_num);
4864 memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
4865 ira_initiate_assign ();
4866 do_coloring ();
4867 ira_finish_assign ();
4868 allocno_stack_vec.release ();
4869 move_spill_restore ();
4874 /* This page contains a simple register allocator without usage of
4875 allocno conflicts. This is used for fast allocation for -O0. */
4877 /* Do register allocation by not using allocno conflicts. It uses
4878 only allocno live ranges. The algorithm is close to Chow's
4879 priority coloring. */
4880 static void
4881 fast_allocation (void)
4883 int i, j, k, num, class_size, hard_regno, best_hard_regno, cost, min_cost;
4884 int *costs;
4885 #ifdef STACK_REGS
4886 bool no_stack_reg_p;
4887 #endif
4888 enum reg_class aclass;
4889 machine_mode mode;
4890 ira_allocno_t a;
4891 ira_allocno_iterator ai;
4892 live_range_t r;
4893 HARD_REG_SET conflict_hard_regs, *used_hard_regs;
4895 sorted_allocnos = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4896 * ira_allocnos_num);
4897 num = 0;
4898 FOR_EACH_ALLOCNO (a, ai)
4899 sorted_allocnos[num++] = a;
4900 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4901 setup_allocno_priorities (sorted_allocnos, num);
4902 used_hard_regs = (HARD_REG_SET *) ira_allocate (sizeof (HARD_REG_SET)
4903 * ira_max_point);
4904 for (i = 0; i < ira_max_point; i++)
4905 CLEAR_HARD_REG_SET (used_hard_regs[i]);
4906 qsort (sorted_allocnos, num, sizeof (ira_allocno_t),
4907 allocno_priority_compare_func);
4908 for (i = 0; i < num; i++)
4910 int nr, l;
4912 a = sorted_allocnos[i];
4913 nr = ALLOCNO_NUM_OBJECTS (a);
4914 CLEAR_HARD_REG_SET (conflict_hard_regs);
4915 for (l = 0; l < nr; l++)
4917 ira_object_t obj = ALLOCNO_OBJECT (a, l);
4918 conflict_hard_regs |= OBJECT_CONFLICT_HARD_REGS (obj);
4919 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4920 for (j = r->start; j <= r->finish; j++)
4921 conflict_hard_regs |= used_hard_regs[j];
4923 aclass = ALLOCNO_CLASS (a);
4924 ALLOCNO_ASSIGNED_P (a) = true;
4925 ALLOCNO_HARD_REGNO (a) = -1;
4926 if (hard_reg_set_subset_p (reg_class_contents[aclass],
4927 conflict_hard_regs))
4928 continue;
4929 mode = ALLOCNO_MODE (a);
4930 #ifdef STACK_REGS
4931 no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
4932 #endif
4933 class_size = ira_class_hard_regs_num[aclass];
4934 costs = ALLOCNO_HARD_REG_COSTS (a);
4935 min_cost = INT_MAX;
4936 best_hard_regno = -1;
4937 for (j = 0; j < class_size; j++)
4939 hard_regno = ira_class_hard_regs[aclass][j];
4940 #ifdef STACK_REGS
4941 if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
4942 && hard_regno <= LAST_STACK_REG)
4943 continue;
4944 #endif
4945 if (ira_hard_reg_set_intersection_p (hard_regno, mode, conflict_hard_regs)
4946 || (TEST_HARD_REG_BIT
4947 (ira_prohibited_class_mode_regs[aclass][mode], hard_regno)))
4948 continue;
4949 if (costs == NULL)
4951 best_hard_regno = hard_regno;
4952 break;
4954 cost = costs[j];
4955 if (min_cost > cost)
4957 min_cost = cost;
4958 best_hard_regno = hard_regno;
4961 if (best_hard_regno < 0)
4962 continue;
4963 ALLOCNO_HARD_REGNO (a) = hard_regno = best_hard_regno;
4964 for (l = 0; l < nr; l++)
4966 ira_object_t obj = ALLOCNO_OBJECT (a, l);
4967 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4968 for (k = r->start; k <= r->finish; k++)
4969 used_hard_regs[k] |= ira_reg_mode_hard_regset[hard_regno][mode];
4972 ira_free (sorted_allocnos);
4973 ira_free (used_hard_regs);
4974 ira_free (allocno_priorities);
4975 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
4976 ira_print_disposition (ira_dump_file);
4981 /* Entry function doing coloring. */
4982 void
4983 ira_color (void)
4985 ira_allocno_t a;
4986 ira_allocno_iterator ai;
4988 /* Setup updated costs. */
4989 FOR_EACH_ALLOCNO (a, ai)
4991 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
4992 ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
4994 if (ira_conflicts_p)
4995 color ();
4996 else
4997 fast_allocation ();