* prerequisites.xml: Refer to GCC (instead of gcc) and GNU/Linux.
[official-gcc.git] / gcc / ira-color.c
blob2b21fdd8ad29fdad315a78f91806716f0d59bdb9
1 /* IRA allocation based on graph coloring.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "target.h"
29 #include "regs.h"
30 #include "flags.h"
31 #include "sbitmap.h"
32 #include "bitmap.h"
33 #include "hard-reg-set.h"
34 #include "basic-block.h"
35 #include "expr.h"
36 #include "diagnostic-core.h"
37 #include "reload.h"
38 #include "params.h"
39 #include "df.h"
40 #include "ira-int.h"
42 typedef struct object_hard_regs *object_hard_regs_t;
44 /* The structure contains information about hard registers can be
45 assigned to objects. Usually it is allocno profitable hard
46 registers but in some cases this set can be a bit different. Major
47 reason of the difference is a requirement to use hard register sets
48 that form a tree or a forest (set of trees), i.e. hard register set
49 of a node should contain hard register sets of its subnodes. */
50 struct object_hard_regs
52 /* Hard registers can be assigned to an allocno. */
53 HARD_REG_SET set;
54 /* Overall (spilling) cost of all allocnos with given register
55 set. */
56 long long int cost;
59 typedef struct object_hard_regs_node *object_hard_regs_node_t;
61 /* A node representing object hard registers. Such nodes form a
62 forest (set of trees). Each subnode of given node in the forest
63 refers for hard register set (usually object profitable hard
64 register set) which is a subset of one referred from given
65 node. */
66 struct object_hard_regs_node
68 /* Set up number of the node in preorder traversing of the forest. */
69 int preorder_num;
70 /* Used for different calculation like finding conflict size of an
71 allocno. */
72 int check;
73 /* Used for calculation of conflict size of an allocno. The
74 conflict size of the allocno is maximal number of given object
75 hard registers needed for allocation of the conflicting allocnos.
76 Given allocno is trivially colored if this number plus the number
77 of hard registers needed for given allocno is not greater than
78 the number of given allocno hard register set. */
79 int conflict_size;
80 /* The number of hard registers given by member hard_regs. */
81 int hard_regs_num;
82 /* The following member is used to form the final forest. */
83 bool used_p;
84 /* Pointer to the corresponding profitable hard registers. */
85 object_hard_regs_t hard_regs;
86 /* Parent, first subnode, previous and next node with the same
87 parent in the forest. */
88 object_hard_regs_node_t parent, first, prev, next;
91 /* To decrease footprint of ira_allocno structure we store all data
92 needed only for coloring in the following structure. */
93 struct allocno_color_data
95 /* TRUE value means that the allocno was not removed yet from the
96 conflicting graph during colouring. */
97 unsigned int in_graph_p : 1;
98 /* TRUE if it is put on the stack to make other allocnos
99 colorable. */
100 unsigned int may_be_spilled_p : 1;
101 /* TRUE if the object is trivially colorable. */
102 unsigned int colorable_p : 1;
103 /* Number of hard registers of the allocno class really
104 available for the allocno allocation. It is number of the
105 profitable hard regs. */
106 int available_regs_num;
107 /* Allocnos in a bucket (used in coloring) chained by the following
108 two members. */
109 ira_allocno_t next_bucket_allocno;
110 ira_allocno_t prev_bucket_allocno;
111 /* Used for temporary purposes. */
112 int temp;
115 /* See above. */
116 typedef struct allocno_color_data *allocno_color_data_t;
118 /* Container for storing allocno data concerning coloring. */
119 static allocno_color_data_t allocno_color_data;
121 /* Macro to access the data concerning coloring. */
122 #define ALLOCNO_COLOR_DATA(a) ((allocno_color_data_t) ALLOCNO_ADD_DATA (a))
124 /* To decrease footprint of ira_object structure we store all data
125 needed only for coloring in the following structure. */
126 struct object_color_data
128 /* Profitable hard regs available for this pseudo allocation. It
129 means that the set excludes unavailable hard regs and hard regs
130 conflicting with given pseudo. They should be of the allocno
131 class. */
132 HARD_REG_SET profitable_hard_regs;
133 /* The object hard registers node. */
134 object_hard_regs_node_t hard_regs_node;
135 /* Array of structures object_hard_regs_subnode representing
136 given object hard registers node (the 1st element in the array)
137 and all its subnodes in the tree (forest) of object hard
138 register nodes (see comments above). */
139 int hard_regs_subnodes_start;
140 /* The length of the previous array. */
141 int hard_regs_subnodes_num;
144 /* See above. */
145 typedef struct object_color_data *object_color_data_t;
147 /* Container for storing object data concerning coloring. */
148 static object_color_data_t object_color_data;
150 /* Macro to access the data concerning coloring. */
151 #define OBJECT_COLOR_DATA(o) ((object_color_data_t) OBJECT_ADD_DATA (o))
153 /* This file contains code for regional graph coloring, spill/restore
154 code placement optimization, and code helping the reload pass to do
155 a better job. */
157 /* Bitmap of allocnos which should be colored. */
158 static bitmap coloring_allocno_bitmap;
160 /* Bitmap of allocnos which should be taken into account during
161 coloring. In general case it contains allocnos from
162 coloring_allocno_bitmap plus other already colored conflicting
163 allocnos. */
164 static bitmap consideration_allocno_bitmap;
166 /* All allocnos sorted according their priorities. */
167 static ira_allocno_t *sorted_allocnos;
169 /* Vec representing the stack of allocnos used during coloring. */
170 static VEC(ira_allocno_t,heap) *allocno_stack_vec;
172 /* Helper for qsort comparison callbacks - return a positive integer if
173 X > Y, or a negative value otherwise. Use a conditional expression
174 instead of a difference computation to insulate from possible overflow
175 issues, e.g. X - Y < 0 for some X > 0 and Y < 0. */
176 #define SORTGT(x,y) (((x) > (y)) ? 1 : -1)
180 /* Definition of vector of object hard registers. */
181 DEF_VEC_P(object_hard_regs_t);
182 DEF_VEC_ALLOC_P(object_hard_regs_t, heap);
184 /* Vector of unique object hard registers. */
185 static VEC(object_hard_regs_t, heap) *object_hard_regs_vec;
187 /* Returns hash value for object hard registers V. */
188 static hashval_t
189 object_hard_regs_hash (const void *v)
191 const struct object_hard_regs *hv = (const struct object_hard_regs *) v;
193 return iterative_hash (&hv->set, sizeof (HARD_REG_SET), 0);
196 /* Compares object hard registers V1 and V2. */
197 static int
198 object_hard_regs_eq (const void *v1, const void *v2)
200 const struct object_hard_regs *hv1 = (const struct object_hard_regs *) v1;
201 const struct object_hard_regs *hv2 = (const struct object_hard_regs *) v2;
203 return hard_reg_set_equal_p (hv1->set, hv2->set);
206 /* Hash table of unique object hard registers. */
207 static htab_t object_hard_regs_htab;
209 /* Return object hard registers in the hash table equal to HV. */
210 static object_hard_regs_t
211 find_hard_regs (object_hard_regs_t hv)
213 return (object_hard_regs_t) htab_find (object_hard_regs_htab, hv);
216 /* Insert allocno hard registers HV in the hash table (if it is not
217 there yet) and return the value which in the table. */
218 static object_hard_regs_t
219 insert_hard_regs (object_hard_regs_t hv)
221 PTR *slot = htab_find_slot (object_hard_regs_htab, hv, INSERT);
223 if (*slot == NULL)
224 *slot = hv;
225 return (object_hard_regs_t) *slot;
228 /* Initialize data concerning object hard registers. */
229 static void
230 init_object_hard_regs (void)
232 object_hard_regs_vec = VEC_alloc (object_hard_regs_t, heap, 200);
233 object_hard_regs_htab
234 = htab_create (200, object_hard_regs_hash, object_hard_regs_eq, NULL);
237 /* Add (or update info about) object hard registers with SET and
238 COST. */
239 static object_hard_regs_t
240 add_object_hard_regs (HARD_REG_SET set, long long int cost)
242 struct object_hard_regs temp;
243 object_hard_regs_t hv;
245 gcc_assert (! hard_reg_set_empty_p (set));
246 COPY_HARD_REG_SET (temp.set, set);
247 if ((hv = find_hard_regs (&temp)) != NULL)
248 hv->cost += cost;
249 else
251 hv = ((struct object_hard_regs *)
252 ira_allocate (sizeof (struct object_hard_regs)));
253 COPY_HARD_REG_SET (hv->set, set);
254 hv->cost = cost;
255 VEC_safe_push (object_hard_regs_t, heap, object_hard_regs_vec, hv);
256 insert_hard_regs (hv);
258 return hv;
261 /* Finalize data concerning allocno hard registers. */
262 static void
263 finish_object_hard_regs (void)
265 int i;
266 object_hard_regs_t hv;
268 for (i = 0;
269 VEC_iterate (object_hard_regs_t, object_hard_regs_vec, i, hv);
270 i++)
271 ira_free (hv);
272 htab_delete (object_hard_regs_htab);
273 VEC_free (object_hard_regs_t, heap, object_hard_regs_vec);
276 /* Sort hard regs according to their frequency of usage. */
277 static int
278 object_hard_regs_compare (const void *v1p, const void *v2p)
280 object_hard_regs_t hv1 = *(const object_hard_regs_t *) v1p;
281 object_hard_regs_t hv2 = *(const object_hard_regs_t *) v2p;
283 if (hv2->cost > hv1->cost)
284 return 1;
285 else if (hv2->cost < hv1->cost)
286 return -1;
287 else
288 return 0;
293 /* Used for finding a common ancestor of two allocno hard registers
294 nodes in the forest. We use the current value of
295 'node_check_tick' to mark all nodes from one node to the top and
296 then walking up from another node until we find a marked node.
298 It is also used to figure out allocno colorability as a mark that
299 we already reset value of member 'conflict_size' for the forest
300 node corresponding to the processed allocno. */
301 static int node_check_tick;
303 /* Roots of the forest containing hard register sets can be assigned
304 to objects. */
305 static object_hard_regs_node_t hard_regs_roots;
307 /* Definition of vector of object hard register nodes. */
308 DEF_VEC_P(object_hard_regs_node_t);
309 DEF_VEC_ALLOC_P(object_hard_regs_node_t, heap);
311 /* Vector used to create the forest. */
312 static VEC(object_hard_regs_node_t, heap) *hard_regs_node_vec;
314 /* Create and return object hard registers node containing object
315 hard registers HV. */
316 static object_hard_regs_node_t
317 create_new_object_hard_regs_node (object_hard_regs_t hv)
319 object_hard_regs_node_t new_node;
321 new_node = ((struct object_hard_regs_node *)
322 ira_allocate (sizeof (struct object_hard_regs_node)));
323 new_node->check = 0;
324 new_node->hard_regs = hv;
325 new_node->hard_regs_num = hard_reg_set_size (hv->set);
326 new_node->first = NULL;
327 new_node->used_p = false;
328 return new_node;
331 /* Add object hard registers node NEW_NODE to the forest on its level
332 given by ROOTS. */
333 static void
334 add_new_object_hard_regs_node_to_forest (object_hard_regs_node_t *roots,
335 object_hard_regs_node_t new_node)
337 new_node->next = *roots;
338 if (new_node->next != NULL)
339 new_node->next->prev = new_node;
340 new_node->prev = NULL;
341 *roots = new_node;
344 /* Add object hard registers HV (or its best approximation if it is
345 not possible) to the forest on its level given by ROOTS. */
346 static void
347 add_object_hard_regs_to_forest (object_hard_regs_node_t *roots,
348 object_hard_regs_t hv)
350 unsigned int i, start;
351 object_hard_regs_node_t node, prev, new_node;
352 HARD_REG_SET temp_set;
353 object_hard_regs_t hv2;
355 start = VEC_length (object_hard_regs_node_t, hard_regs_node_vec);
356 for (node = *roots; node != NULL; node = node->next)
358 if (hard_reg_set_equal_p (hv->set, node->hard_regs->set))
359 return;
360 if (hard_reg_set_subset_p (hv->set, node->hard_regs->set))
362 add_object_hard_regs_to_forest (&node->first, hv);
363 return;
365 if (hard_reg_set_subset_p (node->hard_regs->set, hv->set))
366 VEC_safe_push (object_hard_regs_node_t, heap,
367 hard_regs_node_vec, node);
368 else if (hard_reg_set_intersect_p (hv->set, node->hard_regs->set))
370 COPY_HARD_REG_SET (temp_set, hv->set);
371 AND_HARD_REG_SET (temp_set, node->hard_regs->set);
372 hv2 = add_object_hard_regs (temp_set, hv->cost);
373 add_object_hard_regs_to_forest (&node->first, hv2);
376 if (VEC_length (object_hard_regs_node_t, hard_regs_node_vec)
377 > start + 1)
379 /* Create a new node which contains nodes in hard_regs_node_vec. */
380 CLEAR_HARD_REG_SET (temp_set);
381 for (i = start;
382 i < VEC_length (object_hard_regs_node_t, hard_regs_node_vec);
383 i++)
385 node = VEC_index (object_hard_regs_node_t, hard_regs_node_vec, i);
386 IOR_HARD_REG_SET (temp_set, node->hard_regs->set);
388 hv = add_object_hard_regs (temp_set, hv->cost);
389 new_node = create_new_object_hard_regs_node (hv);
390 prev = NULL;
391 for (i = start;
392 i < VEC_length (object_hard_regs_node_t, hard_regs_node_vec);
393 i++)
395 node = VEC_index (object_hard_regs_node_t, hard_regs_node_vec, i);
396 if (node->prev == NULL)
397 *roots = node->next;
398 else
399 node->prev->next = node->next;
400 if (node->next != NULL)
401 node->next->prev = node->prev;
402 if (prev == NULL)
403 new_node->first = node;
404 else
405 prev->next = node;
406 node->prev = prev;
407 node->next = NULL;
408 prev = node;
410 add_new_object_hard_regs_node_to_forest (roots, new_node);
412 VEC_truncate (object_hard_regs_node_t, hard_regs_node_vec, start);
415 /* Add object hard registers nodes starting with the forest level
416 given by FIRST which contains biggest set inside SET. */
417 static void
418 collect_object_hard_regs_cover (object_hard_regs_node_t first,
419 HARD_REG_SET set)
421 object_hard_regs_node_t node;
423 ira_assert (first != NULL);
424 for (node = first; node != NULL; node = node->next)
425 if (hard_reg_set_subset_p (node->hard_regs->set, set))
426 VEC_safe_push (object_hard_regs_node_t, heap, hard_regs_node_vec,
427 node);
428 else if (hard_reg_set_intersect_p (set, node->hard_regs->set))
429 collect_object_hard_regs_cover (node->first, set);
432 /* Set up field parent as PARENT in all object hard registers nodes
433 in forest given by FIRST. */
434 static void
435 setup_object_hard_regs_nodes_parent (object_hard_regs_node_t first,
436 object_hard_regs_node_t parent)
438 object_hard_regs_node_t node;
440 for (node = first; node != NULL; node = node->next)
442 node->parent = parent;
443 setup_object_hard_regs_nodes_parent (node->first, node);
447 /* Return object hard registers node which is a first common ancestor
448 node of FIRST and SECOND in the forest. */
449 static object_hard_regs_node_t
450 first_common_ancestor_node (object_hard_regs_node_t first,
451 object_hard_regs_node_t second)
453 object_hard_regs_node_t node;
455 node_check_tick++;
456 for (node = first; node != NULL; node = node->parent)
457 node->check = node_check_tick;
458 for (node = second; node != NULL; node = node->parent)
459 if (node->check == node_check_tick)
460 return node;
461 return first_common_ancestor_node (second, first);
464 /* Print hard reg set SET to F. */
465 static void
466 print_hard_reg_set (FILE *f, HARD_REG_SET set, bool new_line_p)
468 int i, start;
470 for (start = -1, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
472 if (TEST_HARD_REG_BIT (set, i))
474 if (i == 0 || ! TEST_HARD_REG_BIT (set, i - 1))
475 start = i;
477 if (start >= 0
478 && (i == FIRST_PSEUDO_REGISTER - 1 || ! TEST_HARD_REG_BIT (set, i)))
480 if (start == i - 1)
481 fprintf (f, " %d", start);
482 else if (start == i - 2)
483 fprintf (f, " %d %d", start, start + 1);
484 else
485 fprintf (f, " %d-%d", start, i - 1);
486 start = -1;
489 if (new_line_p)
490 fprintf (f, "\n");
493 /* Print object hard register subforest given by ROOTS and its LEVEL
494 to F. */
495 static void
496 print_hard_regs_subforest (FILE *f, object_hard_regs_node_t roots,
497 int level)
499 int i;
500 object_hard_regs_node_t node;
502 for (node = roots; node != NULL; node = node->next)
504 fprintf (f, " ");
505 for (i = 0; i < level * 2; i++)
506 fprintf (f, " ");
507 fprintf (f, "%d:(", node->preorder_num);
508 print_hard_reg_set (f, node->hard_regs->set, false);
509 fprintf (f, ")@%lld\n", node->hard_regs->cost);
510 print_hard_regs_subforest (f, node->first, level + 1);
514 /* Print the object hard register forest to F. */
515 static void
516 print_hard_regs_forest (FILE *f)
518 fprintf (f, " Hard reg set forest:\n");
519 print_hard_regs_subforest (f, hard_regs_roots, 1);
522 /* Print the object hard register forest to stderr. */
523 void
524 ira_debug_hard_regs_forest (void)
526 print_hard_regs_forest (stderr);
529 /* Remove unused object hard registers nodes from forest given by its
530 *ROOTS. */
531 static void
532 remove_unused_object_hard_regs_nodes (object_hard_regs_node_t *roots)
534 object_hard_regs_node_t node, prev, next, last;
536 for (prev = NULL, node = *roots; node != NULL; node = next)
538 next = node->next;
539 if (node->used_p)
541 remove_unused_object_hard_regs_nodes (&node->first);
542 prev = node;
544 else
546 for (last = node->first;
547 last != NULL && last->next != NULL;
548 last = last->next)
550 if (last != NULL)
552 if (prev == NULL)
553 *roots = node->first;
554 else
555 prev->next = node->first;
556 if (next != NULL)
557 next->prev = last;
558 last->next = next;
559 next = node->first;
561 else
563 if (prev == NULL)
564 *roots = next;
565 else
566 prev->next = next;
567 if (next != NULL)
568 next->prev = prev;
570 ira_free (node);
575 /* Set up fields preorder_num starting with START_NUM in all object
576 hard registers nodes in forest given by FIRST. Return biggest set
577 PREORDER_NUM increased by 1. */
578 static int
579 enumerate_object_hard_regs_nodes (object_hard_regs_node_t first,
580 object_hard_regs_node_t parent,
581 int start_num)
583 object_hard_regs_node_t node;
585 for (node = first; node != NULL; node = node->next)
587 node->preorder_num = start_num++;
588 node->parent = parent;
589 start_num = enumerate_object_hard_regs_nodes (node->first, node,
590 start_num);
592 return start_num;
595 /* Number of object hard registers nodes in the forest. */
596 static int object_hard_regs_nodes_num;
598 /* Table preorder number of object hard registers node in the forest
599 -> the object hard registers node. */
600 static object_hard_regs_node_t *object_hard_regs_nodes;
602 /* See below. */
603 typedef struct object_hard_regs_subnode *object_hard_regs_subnode_t;
605 /* The structure is used to describes all subnodes (not only immediate
606 ones) in the mentioned above tree for given object hard register
607 node. The usage of such data accelerates calculation of
608 colorability of given allocno. */
609 struct object_hard_regs_subnode
611 /* The conflict size of conflicting allocnos whose hard register
612 sets are equal sets (plus supersets if given node is given
613 object hard registers node) of one in the given node. */
614 int left_conflict_size;
615 /* The summary conflict size of conflicting allocnos whose hard
616 register sets are strict subsets of one in the given node.
617 Overall conflict size is
618 left_conflict_subnodes_size
619 + MIN (max_node_impact - left_conflict_subnodes_size,
620 left_conflict_size)
622 short left_conflict_subnodes_size;
623 short max_node_impact;
626 /* Container for hard regs subnodes of all objects. */
627 static object_hard_regs_subnode_t object_hard_regs_subnodes;
629 /* Table (preorder number of object hard registers node in the
630 forest, preorder number of object hard registers subnode) -> index
631 of the subnode relative to the node. -1 if it is not a
632 subnode. */
633 static int *object_hard_regs_subnode_index;
635 /* Setup arrays OBJECT_HARD_REGS_NODES and
636 OBJECT_HARD_REGS_SUBNODE_INDEX. */
637 static void
638 setup_object_hard_regs_subnode_index (object_hard_regs_node_t first)
640 object_hard_regs_node_t node, parent;
641 int index;
643 for (node = first; node != NULL; node = node->next)
645 object_hard_regs_nodes[node->preorder_num] = node;
646 for (parent = node; parent != NULL; parent = parent->parent)
648 index = parent->preorder_num * object_hard_regs_nodes_num;
649 object_hard_regs_subnode_index[index + node->preorder_num]
650 = node->preorder_num - parent->preorder_num;
652 setup_object_hard_regs_subnode_index (node->first);
656 /* Count all object hard registers nodes in tree ROOT. */
657 static int
658 get_object_hard_regs_subnodes_num (object_hard_regs_node_t root)
660 int len = 1;
662 for (root = root->first; root != NULL; root = root->next)
663 len += get_object_hard_regs_subnodes_num (root);
664 return len;
667 /* Build the forest of object hard registers nodes and assign each
668 allocno a node from the forest. */
669 static void
670 form_object_hard_regs_nodes_forest (void)
672 unsigned int i, j, size, len;
673 int start, k;
674 ira_allocno_t a;
675 object_hard_regs_t hv;
676 bitmap_iterator bi;
677 HARD_REG_SET temp;
678 object_hard_regs_node_t node, object_hard_regs_node;
680 node_check_tick = 0;
681 init_object_hard_regs ();
682 hard_regs_roots = NULL;
683 hard_regs_node_vec = VEC_alloc (object_hard_regs_node_t, heap, 100);
684 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
685 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
687 CLEAR_HARD_REG_SET (temp);
688 SET_HARD_REG_BIT (temp, i);
689 hv = add_object_hard_regs (temp, 0);
690 node = create_new_object_hard_regs_node (hv);
691 add_new_object_hard_regs_node_to_forest (&hard_regs_roots, node);
693 start = VEC_length (object_hard_regs_t, object_hard_regs_vec);
694 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
696 a = ira_allocnos[i];
697 for (k = 0; k < ALLOCNO_NUM_OBJECTS (a); k++)
699 ira_object_t obj = ALLOCNO_OBJECT (a, k);
700 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
702 if (hard_reg_set_empty_p (obj_data->profitable_hard_regs))
703 continue;
704 hv = (add_object_hard_regs
705 (obj_data->profitable_hard_regs,
706 ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a)));
709 SET_HARD_REG_SET (temp);
710 AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
711 add_object_hard_regs (temp, 0);
712 qsort (VEC_address (object_hard_regs_t, object_hard_regs_vec) + start,
713 VEC_length (object_hard_regs_t, object_hard_regs_vec) - start,
714 sizeof (object_hard_regs_t), object_hard_regs_compare);
715 for (i = start;
716 VEC_iterate (object_hard_regs_t, object_hard_regs_vec, i, hv);
717 i++)
719 add_object_hard_regs_to_forest (&hard_regs_roots, hv);
720 ira_assert (VEC_length (object_hard_regs_node_t,
721 hard_regs_node_vec) == 0);
723 /* We need to set up parent fields for right work of
724 first_common_ancestor_node. */
725 setup_object_hard_regs_nodes_parent (hard_regs_roots, NULL);
726 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
728 a = ira_allocnos[i];
729 for (k = 0; k < ALLOCNO_NUM_OBJECTS (a); k++)
731 ira_object_t obj = ALLOCNO_OBJECT (a, k);
732 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
734 if (hard_reg_set_empty_p (obj_data->profitable_hard_regs))
735 continue;
736 VEC_truncate (object_hard_regs_node_t, hard_regs_node_vec, 0);
737 collect_object_hard_regs_cover (hard_regs_roots,
738 obj_data->profitable_hard_regs);
739 object_hard_regs_node = NULL;
740 for (j = 0;
741 VEC_iterate (object_hard_regs_node_t, hard_regs_node_vec,
742 j, node);
743 j++)
744 object_hard_regs_node
745 = (j == 0
746 ? node
747 : first_common_ancestor_node (node, object_hard_regs_node));
748 /* That is a temporary storage. */
749 object_hard_regs_node->used_p = true;
750 obj_data->hard_regs_node = object_hard_regs_node;
753 ira_assert (hard_regs_roots->next == NULL);
754 hard_regs_roots->used_p = true;
755 remove_unused_object_hard_regs_nodes (&hard_regs_roots);
756 object_hard_regs_nodes_num
757 = enumerate_object_hard_regs_nodes (hard_regs_roots, NULL, 0);
758 object_hard_regs_nodes
759 = ((object_hard_regs_node_t *)
760 ira_allocate (object_hard_regs_nodes_num
761 * sizeof (object_hard_regs_node_t)));
762 size = object_hard_regs_nodes_num * object_hard_regs_nodes_num;
763 object_hard_regs_subnode_index
764 = (int *) ira_allocate (size * sizeof (int));
765 for (i = 0; i < size; i++)
766 object_hard_regs_subnode_index[i] = -1;
767 setup_object_hard_regs_subnode_index (hard_regs_roots);
768 start = 0;
769 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
771 a = ira_allocnos[i];
772 for (k = 0; k < ALLOCNO_NUM_OBJECTS (a); k++)
774 ira_object_t obj = ALLOCNO_OBJECT (a, k);
775 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
777 if (hard_reg_set_empty_p (obj_data->profitable_hard_regs))
778 continue;
779 len = get_object_hard_regs_subnodes_num (obj_data->hard_regs_node);
780 obj_data->hard_regs_subnodes_start = start;
781 obj_data->hard_regs_subnodes_num = len;
782 start += len;
785 object_hard_regs_subnodes
786 = ((object_hard_regs_subnode_t)
787 ira_allocate (sizeof (struct object_hard_regs_subnode) * start));
788 VEC_free (object_hard_regs_node_t, heap, hard_regs_node_vec);
791 /* Free tree of object hard registers nodes given by its ROOT. */
792 static void
793 finish_object_hard_regs_nodes_tree (object_hard_regs_node_t root)
795 object_hard_regs_node_t child, next;
797 for (child = root->first; child != NULL; child = next)
799 next = child->next;
800 finish_object_hard_regs_nodes_tree (child);
802 ira_free (root);
805 /* Finish work with the forest of object hard registers nodes. */
806 static void
807 finish_object_hard_regs_nodes_forest (void)
809 object_hard_regs_node_t node, next;
811 ira_free (object_hard_regs_subnodes);
812 for (node = hard_regs_roots; node != NULL; node = next)
814 next = node->next;
815 finish_object_hard_regs_nodes_tree (node);
817 ira_free (object_hard_regs_nodes);
818 ira_free (object_hard_regs_subnode_index);
819 finish_object_hard_regs ();
822 /* Set up left conflict sizes and left conflict subnodes sizes of hard
823 registers subnodes of allocno A. Return TRUE if allocno A is
824 trivially colorable. */
825 static bool
826 setup_left_conflict_sizes_p (ira_allocno_t a)
828 int k, nobj, conflict_size;
829 allocno_color_data_t data;
831 nobj = ALLOCNO_NUM_OBJECTS (a);
832 conflict_size = 0;
833 data = ALLOCNO_COLOR_DATA (a);
834 for (k = 0; k < nobj; k++)
836 int i, node_preorder_num, start, left_conflict_subnodes_size;
837 HARD_REG_SET profitable_hard_regs;
838 object_hard_regs_subnode_t subnodes;
839 object_hard_regs_node_t node;
840 HARD_REG_SET node_set;
841 ira_object_t obj = ALLOCNO_OBJECT (a, k);
842 ira_object_t conflict_obj;
843 ira_object_conflict_iterator oci;
844 object_color_data_t obj_data;
846 node_check_tick++;
847 obj_data = OBJECT_COLOR_DATA (obj);
848 subnodes = object_hard_regs_subnodes + obj_data->hard_regs_subnodes_start;
849 COPY_HARD_REG_SET (profitable_hard_regs, obj_data->profitable_hard_regs);
850 node = obj_data->hard_regs_node;
851 node_preorder_num = node->preorder_num;
852 COPY_HARD_REG_SET (node_set, node->hard_regs->set);
853 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
855 int size;
856 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
857 object_hard_regs_node_t conflict_node, temp_node;
858 HARD_REG_SET conflict_node_set;
859 object_color_data_t conflict_obj_data;
861 conflict_obj_data = OBJECT_COLOR_DATA (conflict_obj);
862 if (! ALLOCNO_COLOR_DATA (conflict_a)->in_graph_p
863 || ! hard_reg_set_intersect_p (profitable_hard_regs,
864 conflict_obj_data
865 ->profitable_hard_regs))
866 continue;
867 conflict_node = conflict_obj_data->hard_regs_node;
868 COPY_HARD_REG_SET (conflict_node_set, conflict_node->hard_regs->set);
869 if (hard_reg_set_subset_p (node_set, conflict_node_set))
870 temp_node = node;
871 else
873 ira_assert (hard_reg_set_subset_p (conflict_node_set, node_set));
874 temp_node = conflict_node;
876 if (temp_node->check != node_check_tick)
878 temp_node->check = node_check_tick;
879 temp_node->conflict_size = 0;
881 size = (ira_reg_class_max_nregs
882 [ALLOCNO_CLASS (conflict_a)][ALLOCNO_MODE (conflict_a)]);
883 if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1)
884 /* We will deal with the subwords individually. */
885 size = 1;
886 temp_node->conflict_size += size;
888 for (i = 0; i < obj_data->hard_regs_subnodes_num; i++)
890 object_hard_regs_node_t temp_node;
892 temp_node = object_hard_regs_nodes[i + node_preorder_num];
893 ira_assert (temp_node->preorder_num == i + node_preorder_num);
894 subnodes[i].left_conflict_size = (temp_node->check != node_check_tick
895 ? 0 : temp_node->conflict_size);
896 if (hard_reg_set_subset_p (temp_node->hard_regs->set,
897 profitable_hard_regs))
898 subnodes[i].max_node_impact = temp_node->hard_regs_num;
899 else
901 HARD_REG_SET temp_set;
902 int j, n;
903 enum reg_class aclass;
905 COPY_HARD_REG_SET (temp_set, temp_node->hard_regs->set);
906 AND_HARD_REG_SET (temp_set, profitable_hard_regs);
907 aclass = ALLOCNO_CLASS (a);
908 for (n = 0, j = ira_class_hard_regs_num[aclass] - 1; j >= 0; j--)
909 if (TEST_HARD_REG_BIT (temp_set, ira_class_hard_regs[aclass][j]))
910 n++;
911 subnodes[i].max_node_impact = n;
913 subnodes[i].left_conflict_subnodes_size = 0;
915 start = node_preorder_num * object_hard_regs_nodes_num;
916 for (i = obj_data->hard_regs_subnodes_num - 1; i >= 0; i--)
918 int size, parent_i;
919 object_hard_regs_node_t parent;
921 size = (subnodes[i].left_conflict_subnodes_size
922 + MIN (subnodes[i].max_node_impact
923 - subnodes[i].left_conflict_subnodes_size,
924 subnodes[i].left_conflict_size));
925 parent = object_hard_regs_nodes[i + node_preorder_num]->parent;
926 if (parent == NULL)
927 continue;
928 parent_i
929 = object_hard_regs_subnode_index[start + parent->preorder_num];
930 if (parent_i < 0)
931 continue;
932 subnodes[parent_i].left_conflict_subnodes_size += size;
934 left_conflict_subnodes_size = subnodes[0].left_conflict_subnodes_size;
935 conflict_size
936 += (left_conflict_subnodes_size
937 + MIN (subnodes[0].max_node_impact - left_conflict_subnodes_size,
938 subnodes[0].left_conflict_size));
940 conflict_size += ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
941 data->colorable_p = conflict_size <= data->available_regs_num;
942 return data->colorable_p;
945 /* Update left conflict sizes of hard registers subnodes of allocno A
946 after removing allocno containing object REMOVED_OBJ with SIZE from
947 the conflict graph. Return TRUE if A is trivially colorable. */
948 static bool
949 update_left_conflict_sizes_p (ira_allocno_t a,
950 ira_object_t removed_obj, int size)
952 int i, k, conflict_size, before_conflict_size, diff, start;
953 int node_preorder_num, parent_i;
954 object_hard_regs_node_t node, removed_node, parent;
955 object_hard_regs_subnode_t subnodes;
956 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
957 bool colorable_p = true;
959 ira_assert (! data->colorable_p);
960 for (k = 0; k < ALLOCNO_NUM_OBJECTS (a); k++)
962 ira_object_t obj = ALLOCNO_OBJECT (a, k);
963 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
965 node = obj_data->hard_regs_node;
966 node_preorder_num = node->preorder_num;
967 removed_node = OBJECT_COLOR_DATA (removed_obj)->hard_regs_node;
968 if (! hard_reg_set_subset_p (removed_node->hard_regs->set,
969 node->hard_regs->set)
970 && ! hard_reg_set_subset_p (node->hard_regs->set,
971 removed_node->hard_regs->set))
972 /* It is a rare case which can happen for conflicting
973 multi-object allocnos where only one pair of objects might
974 conflict. */
975 continue;
976 start = node_preorder_num * object_hard_regs_nodes_num;
977 i = object_hard_regs_subnode_index[start + removed_node->preorder_num];
978 if (i < 0)
979 i = 0;
980 subnodes = object_hard_regs_subnodes + obj_data->hard_regs_subnodes_start;
981 before_conflict_size
982 = (subnodes[i].left_conflict_subnodes_size
983 + MIN (subnodes[i].max_node_impact
984 - subnodes[i].left_conflict_subnodes_size,
985 subnodes[i].left_conflict_size));
986 subnodes[i].left_conflict_size -= size;
987 for (;;)
989 conflict_size
990 = (subnodes[i].left_conflict_subnodes_size
991 + MIN (subnodes[i].max_node_impact
992 - subnodes[i].left_conflict_subnodes_size,
993 subnodes[i].left_conflict_size));
994 if ((diff = before_conflict_size - conflict_size) == 0)
995 break;
996 ira_assert (conflict_size < before_conflict_size);
997 parent = object_hard_regs_nodes[i + node_preorder_num]->parent;
998 if (parent == NULL)
999 break;
1000 parent_i
1001 = object_hard_regs_subnode_index[start + parent->preorder_num];
1002 if (parent_i < 0)
1003 break;
1004 i = parent_i;
1005 before_conflict_size
1006 = (subnodes[i].left_conflict_subnodes_size
1007 + MIN (subnodes[i].max_node_impact
1008 - subnodes[i].left_conflict_subnodes_size,
1009 subnodes[i].left_conflict_size));
1010 subnodes[i].left_conflict_subnodes_size -= diff;
1012 if (i != 0
1013 || (conflict_size
1014 + ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
1015 > data->available_regs_num))
1017 colorable_p = false;
1018 break;
1021 if (colorable_p)
1023 data->colorable_p = true;
1024 return true;
1026 return false;
1029 /* Return true if allocno A has an object with empty profitable hard
1030 regs. */
1031 static bool
1032 empty_profitable_hard_regs (ira_allocno_t a)
1034 int k, nobj;
1036 nobj = ALLOCNO_NUM_OBJECTS (a);
1037 for (k = 0; k < nobj; k++)
1039 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1040 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
1042 if (hard_reg_set_empty_p (obj_data->profitable_hard_regs))
1043 return true;
1045 return false;
1048 /* Set up profitable hard registers for each allocno being
1049 colored. */
1050 static void
1051 setup_profitable_hard_regs (void)
1053 unsigned int i;
1054 int j, k, nobj, hard_regno, nregs, class_size;
1055 ira_allocno_t a;
1056 bitmap_iterator bi;
1057 enum reg_class aclass;
1058 enum machine_mode mode;
1060 /* Initial set up from allocno classes and explicitly conflicting
1061 hard regs. */
1062 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1064 a = ira_allocnos[i];
1065 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS)
1066 continue;
1067 mode = ALLOCNO_MODE (a);
1068 nobj = ALLOCNO_NUM_OBJECTS (a);
1069 for (k = 0; k < nobj; k++)
1071 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1072 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
1074 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL
1075 && ALLOCNO_CLASS_COST (a) > ALLOCNO_MEMORY_COST (a))
1076 CLEAR_HARD_REG_SET (obj_data->profitable_hard_regs);
1077 else
1079 COPY_HARD_REG_SET (obj_data->profitable_hard_regs,
1080 reg_class_contents[aclass]);
1081 AND_COMPL_HARD_REG_SET (obj_data->profitable_hard_regs,
1082 ira_no_alloc_regs);
1083 AND_COMPL_HARD_REG_SET (obj_data->profitable_hard_regs,
1084 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
1088 /* Exclude hard regs already assigned for conflicting objects. */
1089 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, i, bi)
1091 a = ira_allocnos[i];
1092 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1093 || ! ALLOCNO_ASSIGNED_P (a)
1094 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1095 continue;
1096 mode = ALLOCNO_MODE (a);
1097 nregs = hard_regno_nregs[hard_regno][mode];
1098 nobj = ALLOCNO_NUM_OBJECTS (a);
1099 for (k = 0; k < nobj; k++)
1101 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1102 ira_object_t conflict_obj;
1103 ira_object_conflict_iterator oci;
1105 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1107 if (nregs == nobj && nregs > 1)
1109 int num = OBJECT_SUBWORD (conflict_obj);
1111 if (WORDS_BIG_ENDIAN)
1112 CLEAR_HARD_REG_BIT
1113 (OBJECT_COLOR_DATA (conflict_obj)->profitable_hard_regs,
1114 hard_regno + nobj - num - 1);
1115 else
1116 CLEAR_HARD_REG_BIT
1117 (OBJECT_COLOR_DATA (conflict_obj)->profitable_hard_regs,
1118 hard_regno + num);
1120 else
1121 AND_COMPL_HARD_REG_SET
1122 (OBJECT_COLOR_DATA (conflict_obj)->profitable_hard_regs,
1123 ira_reg_mode_hard_regset[hard_regno][mode]);
1127 /* Exclude too costly hard regs. */
1128 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1130 int min_cost = INT_MAX;
1131 int *costs;
1133 a = ira_allocnos[i];
1134 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1135 || empty_profitable_hard_regs (a))
1136 continue;
1137 mode = ALLOCNO_MODE (a);
1138 nobj = ALLOCNO_NUM_OBJECTS (a);
1139 for (k = 0; k < nobj; k++)
1141 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1142 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
1144 if ((costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a)) != NULL
1145 || (costs = ALLOCNO_HARD_REG_COSTS (a)) != NULL)
1147 class_size = ira_class_hard_regs_num[aclass];
1148 for (j = 0; j < class_size; j++)
1150 hard_regno = ira_class_hard_regs[aclass][j];
1151 nregs = hard_regno_nregs[hard_regno][mode];
1152 if (nregs == nobj && nregs > 1)
1154 int num = OBJECT_SUBWORD (obj);
1156 if (WORDS_BIG_ENDIAN)
1157 hard_regno += nobj - num - 1;
1158 else
1159 hard_regno += num;
1161 if (! TEST_HARD_REG_BIT (obj_data->profitable_hard_regs,
1162 hard_regno))
1163 continue;
1164 if (ALLOCNO_UPDATED_MEMORY_COST (a) < costs[j])
1165 CLEAR_HARD_REG_BIT (obj_data->profitable_hard_regs,
1166 hard_regno);
1167 else if (min_cost > costs[j])
1168 min_cost = costs[j];
1171 else if (ALLOCNO_UPDATED_MEMORY_COST (a)
1172 < ALLOCNO_UPDATED_CLASS_COST (a))
1173 CLEAR_HARD_REG_SET (obj_data->profitable_hard_regs);
1175 if (ALLOCNO_UPDATED_CLASS_COST (a) > min_cost)
1176 ALLOCNO_UPDATED_CLASS_COST (a) = min_cost;
1182 /* This page contains functions used to choose hard registers for
1183 allocnos. */
1185 /* Array whose element value is TRUE if the corresponding hard
1186 register was already allocated for an allocno. */
1187 static bool allocated_hardreg_p[FIRST_PSEUDO_REGISTER];
1189 /* Describes one element in a queue of allocnos whose costs need to be
1190 updated. Each allocno in the queue is known to have an allocno
1191 class. */
1192 struct update_cost_queue_elem
1194 /* This element is in the queue iff CHECK == update_cost_check. */
1195 int check;
1197 /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
1198 connecting this allocno to the one being allocated. */
1199 int divisor;
1201 /* The next allocno in the queue, or null if this is the last element. */
1202 ira_allocno_t next;
1205 /* The first element in a queue of allocnos whose copy costs need to be
1206 updated. Null if the queue is empty. */
1207 static ira_allocno_t update_cost_queue;
1209 /* The last element in the queue described by update_cost_queue.
1210 Not valid if update_cost_queue is null. */
1211 static struct update_cost_queue_elem *update_cost_queue_tail;
1213 /* A pool of elements in the queue described by update_cost_queue.
1214 Elements are indexed by ALLOCNO_NUM. */
1215 static struct update_cost_queue_elem *update_cost_queue_elems;
1217 /* The current value of update_copy_cost call count. */
1218 static int update_cost_check;
1220 /* Allocate and initialize data necessary for function
1221 update_copy_costs. */
1222 static void
1223 initiate_cost_update (void)
1225 size_t size;
1227 size = ira_allocnos_num * sizeof (struct update_cost_queue_elem);
1228 update_cost_queue_elems
1229 = (struct update_cost_queue_elem *) ira_allocate (size);
1230 memset (update_cost_queue_elems, 0, size);
1231 update_cost_check = 0;
1234 /* Deallocate data used by function update_copy_costs. */
1235 static void
1236 finish_cost_update (void)
1238 ira_free (update_cost_queue_elems);
1241 /* When we traverse allocnos to update hard register costs, the cost
1242 divisor will be multiplied by the following macro value for each
1243 hop from given allocno to directly connected allocnos. */
1244 #define COST_HOP_DIVISOR 4
1246 /* Start a new cost-updating pass. */
1247 static void
1248 start_update_cost (void)
1250 update_cost_check++;
1251 update_cost_queue = NULL;
1254 /* Add (ALLOCNO, DIVISOR) to the end of update_cost_queue, unless
1255 ALLOCNO is already in the queue, or has NO_REGS class. */
1256 static inline void
1257 queue_update_cost (ira_allocno_t allocno, int divisor)
1259 struct update_cost_queue_elem *elem;
1261 elem = &update_cost_queue_elems[ALLOCNO_NUM (allocno)];
1262 if (elem->check != update_cost_check
1263 && ALLOCNO_CLASS (allocno) != NO_REGS)
1265 elem->check = update_cost_check;
1266 elem->divisor = divisor;
1267 elem->next = NULL;
1268 if (update_cost_queue == NULL)
1269 update_cost_queue = allocno;
1270 else
1271 update_cost_queue_tail->next = allocno;
1272 update_cost_queue_tail = elem;
1276 /* Try to remove the first element from update_cost_queue. Return false
1277 if the queue was empty, otherwise make (*ALLOCNO, *DIVISOR) describe
1278 the removed element. */
1279 static inline bool
1280 get_next_update_cost (ira_allocno_t *allocno, int *divisor)
1282 struct update_cost_queue_elem *elem;
1284 if (update_cost_queue == NULL)
1285 return false;
1287 *allocno = update_cost_queue;
1288 elem = &update_cost_queue_elems[ALLOCNO_NUM (*allocno)];
1289 *divisor = elem->divisor;
1290 update_cost_queue = elem->next;
1291 return true;
1294 /* Update the cost of allocnos to increase chances to remove some
1295 copies as the result of subsequent assignment. */
1296 static void
1297 update_copy_costs (ira_allocno_t allocno, bool decr_p)
1299 int i, cost, update_cost, hard_regno, divisor;
1300 enum machine_mode mode;
1301 enum reg_class rclass, aclass;
1302 ira_allocno_t another_allocno;
1303 ira_copy_t cp, next_cp;
1305 hard_regno = ALLOCNO_HARD_REGNO (allocno);
1306 ira_assert (hard_regno >= 0);
1308 aclass = ALLOCNO_CLASS (allocno);
1309 if (aclass == NO_REGS)
1310 return;
1311 i = ira_class_hard_reg_index[aclass][hard_regno];
1312 ira_assert (i >= 0);
1313 rclass = REGNO_REG_CLASS (hard_regno);
1315 start_update_cost ();
1316 divisor = 1;
1319 mode = ALLOCNO_MODE (allocno);
1320 ira_init_register_move_cost_if_necessary (mode);
1321 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1323 if (cp->first == allocno)
1325 next_cp = cp->next_first_allocno_copy;
1326 another_allocno = cp->second;
1328 else if (cp->second == allocno)
1330 next_cp = cp->next_second_allocno_copy;
1331 another_allocno = cp->first;
1333 else
1334 gcc_unreachable ();
1336 aclass = ALLOCNO_CLASS (another_allocno);
1337 if (! TEST_HARD_REG_BIT (reg_class_contents[aclass],
1338 hard_regno)
1339 || ALLOCNO_ASSIGNED_P (another_allocno))
1340 continue;
1342 cost = (cp->second == allocno
1343 ? ira_register_move_cost[mode][rclass][aclass]
1344 : ira_register_move_cost[mode][aclass][rclass]);
1345 if (decr_p)
1346 cost = -cost;
1348 update_cost = cp->freq * cost / divisor;
1349 if (update_cost == 0)
1350 continue;
1352 ira_allocate_and_set_or_copy_costs
1353 (&ALLOCNO_UPDATED_HARD_REG_COSTS (another_allocno), aclass,
1354 ALLOCNO_UPDATED_CLASS_COST (another_allocno),
1355 ALLOCNO_HARD_REG_COSTS (another_allocno));
1356 ira_allocate_and_set_or_copy_costs
1357 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
1358 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
1359 i = ira_class_hard_reg_index[aclass][hard_regno];
1360 if (i < 0)
1361 continue;
1362 ALLOCNO_UPDATED_HARD_REG_COSTS (another_allocno)[i] += update_cost;
1363 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno)[i]
1364 += update_cost;
1366 queue_update_cost (another_allocno, divisor * COST_HOP_DIVISOR);
1369 while (get_next_update_cost (&allocno, &divisor));
1372 /* This function updates COSTS (decrease if DECR_P) for hard_registers
1373 of ACLASS by conflict costs of the unassigned allocnos
1374 connected by copies with allocnos in update_cost_queue. This
1375 update increases chances to remove some copies. */
1376 static void
1377 update_conflict_hard_regno_costs (int *costs, enum reg_class aclass,
1378 bool decr_p)
1380 int i, cost, class_size, freq, mult, div, divisor;
1381 int index, hard_regno;
1382 int *conflict_costs;
1383 bool cont_p;
1384 enum reg_class another_aclass;
1385 ira_allocno_t allocno, another_allocno;
1386 ira_copy_t cp, next_cp;
1388 while (get_next_update_cost (&allocno, &divisor))
1389 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1391 if (cp->first == allocno)
1393 next_cp = cp->next_first_allocno_copy;
1394 another_allocno = cp->second;
1396 else if (cp->second == allocno)
1398 next_cp = cp->next_second_allocno_copy;
1399 another_allocno = cp->first;
1401 else
1402 gcc_unreachable ();
1403 another_aclass = ALLOCNO_CLASS (another_allocno);
1404 if (! ira_reg_classes_intersect_p[aclass][another_aclass]
1405 || ALLOCNO_ASSIGNED_P (another_allocno)
1406 || ALLOCNO_COLOR_DATA (another_allocno)->may_be_spilled_p)
1407 continue;
1408 class_size = ira_class_hard_regs_num[another_aclass];
1409 ira_allocate_and_copy_costs
1410 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
1411 another_aclass, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
1412 conflict_costs
1413 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno);
1414 if (conflict_costs == NULL)
1415 cont_p = true;
1416 else
1418 mult = cp->freq;
1419 freq = ALLOCNO_FREQ (another_allocno);
1420 if (freq == 0)
1421 freq = 1;
1422 div = freq * divisor;
1423 cont_p = false;
1424 for (i = class_size - 1; i >= 0; i--)
1426 hard_regno = ira_class_hard_regs[another_aclass][i];
1427 ira_assert (hard_regno >= 0);
1428 index = ira_class_hard_reg_index[aclass][hard_regno];
1429 if (index < 0)
1430 continue;
1431 cost = conflict_costs [i] * mult / div;
1432 if (cost == 0)
1433 continue;
1434 cont_p = true;
1435 if (decr_p)
1436 cost = -cost;
1437 costs[index] += cost;
1440 /* Probably 5 hops will be enough. */
1441 if (cont_p
1442 && divisor <= (COST_HOP_DIVISOR
1443 * COST_HOP_DIVISOR
1444 * COST_HOP_DIVISOR
1445 * COST_HOP_DIVISOR))
1446 queue_update_cost (another_allocno, divisor * COST_HOP_DIVISOR);
1450 /* Set up conflicting and profitable regs (through CONFLICT_REGS and
1451 PROFITABLE_REGS) for each object of allocno A. Remember that the
1452 profitable regs exclude hard regs which can not hold value of mode
1453 of allocno A. */
1454 static inline void
1455 get_conflict_profitable_regs (ira_allocno_t a, bool retry_p,
1456 HARD_REG_SET *conflict_regs,
1457 HARD_REG_SET *profitable_regs)
1459 int i, nwords;
1460 ira_object_t obj;
1462 nwords = ALLOCNO_NUM_OBJECTS (a);
1463 for (i = 0; i < nwords; i++)
1465 obj = ALLOCNO_OBJECT (a, i);
1466 COPY_HARD_REG_SET (conflict_regs[i],
1467 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
1468 if (retry_p)
1470 COPY_HARD_REG_SET (profitable_regs[i],
1471 reg_class_contents[ALLOCNO_CLASS (a)]);
1472 AND_COMPL_HARD_REG_SET (profitable_regs[i],
1473 ira_prohibited_class_mode_regs
1474 [ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
1476 else
1477 COPY_HARD_REG_SET (profitable_regs[i],
1478 OBJECT_COLOR_DATA (obj)->profitable_hard_regs);
1482 /* Return true if HARD_REGNO is ok for assigning to allocno A whose
1483 objects have corresponding CONFLICT_REGS and PROFITABLE_REGS. */
1484 static inline bool
1485 check_hard_reg_p (ira_allocno_t a, int hard_regno,
1486 HARD_REG_SET *conflict_regs, HARD_REG_SET *profitable_regs)
1488 int j, nwords, nregs;
1489 enum reg_class aclass;
1490 enum machine_mode mode;
1492 aclass = ALLOCNO_CLASS (a);
1493 mode = ALLOCNO_MODE (a);
1494 if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[aclass][mode],
1495 hard_regno))
1496 return false;
1497 nregs = hard_regno_nregs[hard_regno][mode];
1498 nwords = ALLOCNO_NUM_OBJECTS (a);
1499 for (j = 0; j < nregs; j++)
1501 int k;
1502 int set_to_test_start = 0, set_to_test_end = nwords;
1504 if (nregs == nwords)
1506 if (WORDS_BIG_ENDIAN)
1507 set_to_test_start = nwords - j - 1;
1508 else
1509 set_to_test_start = j;
1510 set_to_test_end = set_to_test_start + 1;
1512 for (k = set_to_test_start; k < set_to_test_end; k++)
1513 /* Checking only profitable hard regs. */
1514 if (TEST_HARD_REG_BIT (conflict_regs[k], hard_regno + j)
1515 || ! TEST_HARD_REG_BIT (profitable_regs[k], hard_regno + j))
1516 break;
1517 if (k != set_to_test_end)
1518 break;
1520 return j == nregs;
1522 #ifndef HONOR_REG_ALLOC_ORDER
1524 /* Return number of registers needed to be saved and restored at
1525 function prologue/epilogue if we allocate HARD_REGNO to hold value
1526 of MODE. */
1527 static int
1528 calculate_saved_nregs (int hard_regno, enum machine_mode mode)
1530 int i;
1531 int nregs = 0;
1533 ira_assert (hard_regno >= 0);
1534 for (i = hard_regno_nregs[hard_regno][mode] - 1; i >= 0; i--)
1535 if (!allocated_hardreg_p[hard_regno + i]
1536 && !TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + i)
1537 && !LOCAL_REGNO (hard_regno + i))
1538 nregs++;
1539 return nregs;
1541 #endif
1543 /* Choose a hard register for allocno A. If RETRY_P is TRUE, it means
1544 that the function called from function
1545 `ira_reassign_conflict_allocnos' and `allocno_reload_assign'. In
1546 this case some allocno data are not defined or updated and we
1547 should not touch these data. The function returns true if we
1548 managed to assign a hard register to the allocno.
1550 To assign a hard register, first of all we calculate all conflict
1551 hard registers which can come from conflicting allocnos with
1552 already assigned hard registers. After that we find first free
1553 hard register with the minimal cost. During hard register cost
1554 calculation we take conflict hard register costs into account to
1555 give a chance for conflicting allocnos to get a better hard
1556 register in the future.
1558 If the best hard register cost is bigger than cost of memory usage
1559 for the allocno, we don't assign a hard register to given allocno
1560 at all.
1562 If we assign a hard register to the allocno, we update costs of the
1563 hard register for allocnos connected by copies to improve a chance
1564 to coalesce insns represented by the copies when we assign hard
1565 registers to the allocnos connected by the copies. */
1566 static bool
1567 assign_hard_reg (ira_allocno_t a, bool retry_p)
1569 HARD_REG_SET conflicting_regs[2], profitable_hard_regs[2];
1570 int i, j, hard_regno, best_hard_regno, class_size;
1571 int cost, mem_cost, min_cost, full_cost, min_full_cost, nwords, word;
1572 int *a_costs;
1573 enum reg_class aclass;
1574 enum machine_mode mode;
1575 static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
1576 #ifndef HONOR_REG_ALLOC_ORDER
1577 int saved_nregs;
1578 enum reg_class rclass;
1579 int add_cost;
1580 #endif
1581 #ifdef STACK_REGS
1582 bool no_stack_reg_p;
1583 #endif
1585 ira_assert (! ALLOCNO_ASSIGNED_P (a));
1586 get_conflict_profitable_regs (a, retry_p,
1587 conflicting_regs, profitable_hard_regs);
1588 aclass = ALLOCNO_CLASS (a);
1589 class_size = ira_class_hard_regs_num[aclass];
1590 best_hard_regno = -1;
1591 memset (full_costs, 0, sizeof (int) * class_size);
1592 mem_cost = 0;
1593 memset (costs, 0, sizeof (int) * class_size);
1594 memset (full_costs, 0, sizeof (int) * class_size);
1595 #ifdef STACK_REGS
1596 no_stack_reg_p = false;
1597 #endif
1598 if (! retry_p)
1599 start_update_cost ();
1600 mem_cost += ALLOCNO_UPDATED_MEMORY_COST (a);
1602 ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
1603 aclass, ALLOCNO_HARD_REG_COSTS (a));
1604 a_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
1605 #ifdef STACK_REGS
1606 no_stack_reg_p = no_stack_reg_p || ALLOCNO_TOTAL_NO_STACK_REG_P (a);
1607 #endif
1608 cost = ALLOCNO_UPDATED_CLASS_COST (a);
1609 for (i = 0; i < class_size; i++)
1610 if (a_costs != NULL)
1612 costs[i] += a_costs[i];
1613 full_costs[i] += a_costs[i];
1615 else
1617 costs[i] += cost;
1618 full_costs[i] += cost;
1620 nwords = ALLOCNO_NUM_OBJECTS (a);
1621 for (word = 0; word < nwords; word++)
1623 ira_object_t conflict_obj;
1624 ira_object_t obj = ALLOCNO_OBJECT (a, word);
1625 ira_object_conflict_iterator oci;
1627 /* Take preferences of conflicting allocnos into account. */
1628 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1630 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1631 enum reg_class conflict_aclass;
1633 /* Reload can give another class so we need to check all
1634 allocnos. */
1635 if (!retry_p
1636 && (!bitmap_bit_p (consideration_allocno_bitmap,
1637 ALLOCNO_NUM (conflict_a))
1638 || ((!ALLOCNO_ASSIGNED_P (conflict_a)
1639 || ALLOCNO_HARD_REGNO (conflict_a) < 0)
1640 && !(hard_reg_set_intersect_p
1641 (profitable_hard_regs[word],
1642 OBJECT_COLOR_DATA
1643 (conflict_obj)->profitable_hard_regs)))))
1644 continue;
1645 conflict_aclass = ALLOCNO_CLASS (conflict_a);
1646 ira_assert (ira_reg_classes_intersect_p
1647 [aclass][conflict_aclass]);
1648 if (ALLOCNO_ASSIGNED_P (conflict_a))
1650 hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
1651 if (hard_regno >= 0
1652 && (ira_hard_reg_set_intersection_p
1653 (hard_regno, ALLOCNO_MODE (conflict_a),
1654 reg_class_contents[aclass])))
1656 int n_objects = ALLOCNO_NUM_OBJECTS (conflict_a);
1657 int conflict_nregs;
1659 mode = ALLOCNO_MODE (conflict_a);
1660 conflict_nregs = hard_regno_nregs[hard_regno][mode];
1661 if (conflict_nregs == n_objects && conflict_nregs > 1)
1663 int num = OBJECT_SUBWORD (conflict_obj);
1665 if (WORDS_BIG_ENDIAN)
1666 SET_HARD_REG_BIT (conflicting_regs[word],
1667 hard_regno + n_objects - num - 1);
1668 else
1669 SET_HARD_REG_BIT (conflicting_regs[word],
1670 hard_regno + num);
1672 else
1673 IOR_HARD_REG_SET
1674 (conflicting_regs[word],
1675 ira_reg_mode_hard_regset[hard_regno][mode]);
1676 if (hard_reg_set_subset_p (profitable_hard_regs[word],
1677 conflicting_regs[word]))
1678 goto fail;
1681 else if (! retry_p
1682 && ! ALLOCNO_COLOR_DATA (conflict_a)->may_be_spilled_p)
1684 int k, *conflict_costs;
1686 ira_allocate_and_copy_costs
1687 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a),
1688 conflict_aclass,
1689 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_a));
1690 conflict_costs
1691 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a);
1692 if (conflict_costs != NULL)
1693 for (j = class_size - 1; j >= 0; j--)
1695 hard_regno = ira_class_hard_regs[aclass][j];
1696 ira_assert (hard_regno >= 0);
1697 k = ira_class_hard_reg_index[conflict_aclass][hard_regno];
1698 if (k < 0)
1699 continue;
1700 full_costs[j] -= conflict_costs[k];
1702 queue_update_cost (conflict_a, COST_HOP_DIVISOR);
1706 if (! retry_p)
1707 /* Take into account preferences of allocnos connected by copies to
1708 the conflict allocnos. */
1709 update_conflict_hard_regno_costs (full_costs, aclass, true);
1711 /* Take preferences of allocnos connected by copies into
1712 account. */
1713 if (! retry_p)
1715 start_update_cost ();
1716 queue_update_cost (a, COST_HOP_DIVISOR);
1717 update_conflict_hard_regno_costs (full_costs, aclass, false);
1719 min_cost = min_full_cost = INT_MAX;
1721 /* We don't care about giving callee saved registers to allocnos no
1722 living through calls because call clobbered registers are
1723 allocated first (it is usual practice to put them first in
1724 REG_ALLOC_ORDER). */
1725 mode = ALLOCNO_MODE (a);
1726 for (i = 0; i < class_size; i++)
1728 hard_regno = ira_class_hard_regs[aclass][i];
1729 #ifdef STACK_REGS
1730 if (no_stack_reg_p
1731 && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
1732 continue;
1733 #endif
1734 if (! check_hard_reg_p (a, hard_regno,
1735 conflicting_regs, profitable_hard_regs))
1736 continue;
1737 cost = costs[i];
1738 full_cost = full_costs[i];
1739 #ifndef HONOR_REG_ALLOC_ORDER
1740 if ((saved_nregs = calculate_saved_nregs (hard_regno, mode)) != 0)
1741 /* We need to save/restore the hard register in
1742 epilogue/prologue. Therefore we increase the cost. */
1744 rclass = REGNO_REG_CLASS (hard_regno);
1745 add_cost = ((ira_memory_move_cost[mode][rclass][0]
1746 + ira_memory_move_cost[mode][rclass][1])
1747 * saved_nregs / hard_regno_nregs[hard_regno][mode] - 1);
1748 cost += add_cost;
1749 full_cost += add_cost;
1751 #endif
1752 if (min_cost > cost)
1753 min_cost = cost;
1754 if (min_full_cost > full_cost)
1756 min_full_cost = full_cost;
1757 best_hard_regno = hard_regno;
1758 ira_assert (hard_regno >= 0);
1761 if (min_full_cost > mem_cost)
1763 if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1764 fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
1765 mem_cost, min_full_cost);
1766 best_hard_regno = -1;
1768 fail:
1769 if (best_hard_regno >= 0)
1771 for (i = hard_regno_nregs[best_hard_regno][mode] - 1; i >= 0; i--)
1772 allocated_hardreg_p[best_hard_regno + i] = true;
1774 ALLOCNO_HARD_REGNO (a) = best_hard_regno;
1775 ALLOCNO_ASSIGNED_P (a) = true;
1776 if (best_hard_regno >= 0)
1777 update_copy_costs (a, true);
1778 ira_assert (ALLOCNO_CLASS (a) == aclass);
1779 /* We don't need updated costs anymore: */
1780 ira_free_allocno_updated_costs (a);
1781 return best_hard_regno >= 0;
1786 /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
1788 /* Bucket of allocnos that can colored currently without spilling. */
1789 static ira_allocno_t colorable_allocno_bucket;
1791 /* Bucket of allocnos that might be not colored currently without
1792 spilling. */
1793 static ira_allocno_t uncolorable_allocno_bucket;
1795 /* The current number of allocnos in the uncolorable_bucket. */
1796 static int uncolorable_allocnos_num;
1798 /* Return the current spill priority of allocno A. The less the
1799 number, the more preferable the allocno for spilling. */
1800 static inline int
1801 allocno_spill_priority (ira_allocno_t a)
1803 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
1805 return (data->temp
1806 / (ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
1807 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
1808 + 1));
1811 /* Add allocno A to bucket *BUCKET_PTR. A should be not in a bucket
1812 before the call. */
1813 static void
1814 add_allocno_to_bucket (ira_allocno_t a, ira_allocno_t *bucket_ptr)
1816 ira_allocno_t first_a;
1817 allocno_color_data_t data;
1819 if (bucket_ptr == &uncolorable_allocno_bucket
1820 && ALLOCNO_CLASS (a) != NO_REGS)
1822 uncolorable_allocnos_num++;
1823 ira_assert (uncolorable_allocnos_num > 0);
1825 first_a = *bucket_ptr;
1826 data = ALLOCNO_COLOR_DATA (a);
1827 data->next_bucket_allocno = first_a;
1828 data->prev_bucket_allocno = NULL;
1829 if (first_a != NULL)
1830 ALLOCNO_COLOR_DATA (first_a)->prev_bucket_allocno = a;
1831 *bucket_ptr = a;
1834 /* Compare two allocnos to define which allocno should be pushed first
1835 into the coloring stack. If the return is a negative number, the
1836 allocno given by the first parameter will be pushed first. In this
1837 case such allocno has less priority than the second one and the
1838 hard register will be assigned to it after assignment to the second
1839 one. As the result of such assignment order, the second allocno
1840 has a better chance to get the best hard register. */
1841 static int
1842 bucket_allocno_compare_func (const void *v1p, const void *v2p)
1844 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
1845 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
1846 int diff, a1_freq, a2_freq, a1_num, a2_num;
1848 if ((diff = (int) ALLOCNO_CLASS (a2) - ALLOCNO_CLASS (a1)) != 0)
1849 return diff;
1850 a1_freq = ALLOCNO_FREQ (a1);
1851 a2_freq = ALLOCNO_FREQ (a2);
1852 if ((diff = a1_freq - a2_freq) != 0)
1853 return diff;
1854 a1_num = ALLOCNO_COLOR_DATA (a1)->available_regs_num;
1855 a2_num = ALLOCNO_COLOR_DATA (a2)->available_regs_num;
1856 if ((diff = a2_num - a1_num) != 0)
1857 return diff;
1858 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
1861 /* Sort bucket *BUCKET_PTR and return the result through
1862 BUCKET_PTR. */
1863 static void
1864 sort_bucket (ira_allocno_t *bucket_ptr,
1865 int (*compare_func) (const void *, const void *))
1867 ira_allocno_t a, head;
1868 int n;
1870 for (n = 0, a = *bucket_ptr;
1871 a != NULL;
1872 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
1873 sorted_allocnos[n++] = a;
1874 if (n <= 1)
1875 return;
1876 qsort (sorted_allocnos, n, sizeof (ira_allocno_t), compare_func);
1877 head = NULL;
1878 for (n--; n >= 0; n--)
1880 a = sorted_allocnos[n];
1881 ALLOCNO_COLOR_DATA (a)->next_bucket_allocno = head;
1882 ALLOCNO_COLOR_DATA (a)->prev_bucket_allocno = NULL;
1883 if (head != NULL)
1884 ALLOCNO_COLOR_DATA (head)->prev_bucket_allocno = a;
1885 head = a;
1887 *bucket_ptr = head;
1890 /* Add ALLOCNO to bucket *BUCKET_PTR maintaining the order according
1891 their priority. ALLOCNO should be not in a bucket before the
1892 call. */
1893 static void
1894 add_allocno_to_ordered_bucket (ira_allocno_t allocno,
1895 ira_allocno_t *bucket_ptr)
1897 ira_allocno_t before, after;
1899 if (bucket_ptr == &uncolorable_allocno_bucket
1900 && ALLOCNO_CLASS (allocno) != NO_REGS)
1902 uncolorable_allocnos_num++;
1903 ira_assert (uncolorable_allocnos_num > 0);
1905 for (before = *bucket_ptr, after = NULL;
1906 before != NULL;
1907 after = before,
1908 before = ALLOCNO_COLOR_DATA (before)->next_bucket_allocno)
1909 if (bucket_allocno_compare_func (&allocno, &before) < 0)
1910 break;
1911 ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno = before;
1912 ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno = after;
1913 if (after == NULL)
1914 *bucket_ptr = allocno;
1915 else
1916 ALLOCNO_COLOR_DATA (after)->next_bucket_allocno = allocno;
1917 if (before != NULL)
1918 ALLOCNO_COLOR_DATA (before)->prev_bucket_allocno = allocno;
1921 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
1922 the call. */
1923 static void
1924 delete_allocno_from_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
1926 ira_allocno_t prev_allocno, next_allocno;
1928 if (bucket_ptr == &uncolorable_allocno_bucket
1929 && ALLOCNO_CLASS (allocno) != NO_REGS)
1931 uncolorable_allocnos_num--;
1932 ira_assert (uncolorable_allocnos_num >= 0);
1934 prev_allocno = ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno;
1935 next_allocno = ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno;
1936 if (prev_allocno != NULL)
1937 ALLOCNO_COLOR_DATA (prev_allocno)->next_bucket_allocno = next_allocno;
1938 else
1940 ira_assert (*bucket_ptr == allocno);
1941 *bucket_ptr = next_allocno;
1943 if (next_allocno != NULL)
1944 ALLOCNO_COLOR_DATA (next_allocno)->prev_bucket_allocno = prev_allocno;
1947 /* Put allocno A onto the coloring stack without removing it from its
1948 bucket. Pushing allocno to the coloring stack can result in moving
1949 conflicting allocnos from the uncolorable bucket to the colorable
1950 one. */
1951 static void
1952 push_allocno_to_stack (ira_allocno_t a)
1954 enum reg_class aclass;
1955 allocno_color_data_t data, conflict_data;
1956 int size, i, n = ALLOCNO_NUM_OBJECTS (a);
1958 data = ALLOCNO_COLOR_DATA (a);
1959 data->in_graph_p = false;
1960 VEC_safe_push (ira_allocno_t, heap, allocno_stack_vec, a);
1961 aclass = ALLOCNO_CLASS (a);
1962 if (aclass == NO_REGS)
1963 return;
1964 size = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
1965 if (n > 1)
1967 /* We will deal with the subwords individually. */
1968 gcc_assert (size == ALLOCNO_NUM_OBJECTS (a));
1969 size = 1;
1971 for (i = 0; i < n; i++)
1973 ira_object_t obj = ALLOCNO_OBJECT (a, i);
1974 ira_object_t conflict_obj;
1975 ira_object_conflict_iterator oci;
1977 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1979 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1981 conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
1982 if (conflict_data->colorable_p
1983 || ! conflict_data->in_graph_p
1984 || ALLOCNO_ASSIGNED_P (conflict_a)
1985 || !(hard_reg_set_intersect_p
1986 (OBJECT_COLOR_DATA (obj)->profitable_hard_regs,
1987 OBJECT_COLOR_DATA (conflict_obj)->profitable_hard_regs)))
1988 continue;
1989 ira_assert (bitmap_bit_p (coloring_allocno_bitmap,
1990 ALLOCNO_NUM (conflict_a)));
1991 if (update_left_conflict_sizes_p (conflict_a, obj, size))
1993 delete_allocno_from_bucket
1994 (conflict_a, &uncolorable_allocno_bucket);
1995 add_allocno_to_ordered_bucket
1996 (conflict_a, &colorable_allocno_bucket);
1997 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
1999 fprintf (ira_dump_file, " Making");
2000 ira_print_expanded_allocno (conflict_a);
2001 fprintf (ira_dump_file, " colorable\n");
2009 /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
2010 The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
2011 static void
2012 remove_allocno_from_bucket_and_push (ira_allocno_t allocno, bool colorable_p)
2014 if (colorable_p)
2015 delete_allocno_from_bucket (allocno, &colorable_allocno_bucket);
2016 else
2017 delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
2018 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2020 fprintf (ira_dump_file, " Pushing");
2021 ira_print_expanded_allocno (allocno);
2022 if (colorable_p)
2023 fprintf (ira_dump_file, "(cost %d)\n",
2024 ALLOCNO_COLOR_DATA (allocno)->temp);
2025 else
2026 fprintf (ira_dump_file, "(potential spill: %spri=%d, cost=%d)\n",
2027 ALLOCNO_BAD_SPILL_P (allocno) ? "bad spill, " : "",
2028 allocno_spill_priority (allocno),
2029 ALLOCNO_COLOR_DATA (allocno)->temp);
2031 if (! colorable_p)
2032 ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p = true;
2033 push_allocno_to_stack (allocno);
2036 /* Put all allocnos from colorable bucket onto the coloring stack. */
2037 static void
2038 push_only_colorable (void)
2040 sort_bucket (&colorable_allocno_bucket, bucket_allocno_compare_func);
2041 for (;colorable_allocno_bucket != NULL;)
2042 remove_allocno_from_bucket_and_push (colorable_allocno_bucket, true);
2045 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
2046 loop given by its LOOP_NODE. */
2048 ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
2050 int freq, i;
2051 edge_iterator ei;
2052 edge e;
2053 VEC (edge, heap) *edges;
2055 ira_assert (loop_node->loop != NULL
2056 && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
2057 freq = 0;
2058 if (! exit_p)
2060 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
2061 if (e->src != loop_node->loop->latch
2062 && (regno < 0
2063 || (bitmap_bit_p (DF_LR_OUT (e->src), regno)
2064 && bitmap_bit_p (DF_LR_IN (e->dest), regno))))
2065 freq += EDGE_FREQUENCY (e);
2067 else
2069 edges = get_loop_exit_edges (loop_node->loop);
2070 FOR_EACH_VEC_ELT (edge, edges, i, e)
2071 if (regno < 0
2072 || (bitmap_bit_p (DF_LR_OUT (e->src), regno)
2073 && bitmap_bit_p (DF_LR_IN (e->dest), regno)))
2074 freq += EDGE_FREQUENCY (e);
2075 VEC_free (edge, heap, edges);
2078 return REG_FREQ_FROM_EDGE_FREQ (freq);
2081 /* Calculate and return the cost of putting allocno A into memory. */
2082 static int
2083 calculate_allocno_spill_cost (ira_allocno_t a)
2085 int regno, cost;
2086 enum machine_mode mode;
2087 enum reg_class rclass;
2088 ira_allocno_t parent_allocno;
2089 ira_loop_tree_node_t parent_node, loop_node;
2091 regno = ALLOCNO_REGNO (a);
2092 cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_UPDATED_CLASS_COST (a);
2093 if (ALLOCNO_CAP (a) != NULL)
2094 return cost;
2095 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
2096 if ((parent_node = loop_node->parent) == NULL)
2097 return cost;
2098 if ((parent_allocno = parent_node->regno_allocno_map[regno]) == NULL)
2099 return cost;
2100 mode = ALLOCNO_MODE (a);
2101 rclass = ALLOCNO_CLASS (a);
2102 if (ALLOCNO_HARD_REGNO (parent_allocno) < 0)
2103 cost -= (ira_memory_move_cost[mode][rclass][0]
2104 * ira_loop_edge_freq (loop_node, regno, true)
2105 + ira_memory_move_cost[mode][rclass][1]
2106 * ira_loop_edge_freq (loop_node, regno, false));
2107 else
2109 ira_init_register_move_cost_if_necessary (mode);
2110 cost += ((ira_memory_move_cost[mode][rclass][1]
2111 * ira_loop_edge_freq (loop_node, regno, true)
2112 + ira_memory_move_cost[mode][rclass][0]
2113 * ira_loop_edge_freq (loop_node, regno, false))
2114 - (ira_register_move_cost[mode][rclass][rclass]
2115 * (ira_loop_edge_freq (loop_node, regno, false)
2116 + ira_loop_edge_freq (loop_node, regno, true))));
2118 return cost;
2121 /* Used for sorting allocnos for spilling. */
2122 static inline int
2123 allocno_spill_priority_compare (ira_allocno_t a1, ira_allocno_t a2)
2125 int pri1, pri2, diff;
2127 if (ALLOCNO_BAD_SPILL_P (a1) && ! ALLOCNO_BAD_SPILL_P (a2))
2128 return 1;
2129 if (ALLOCNO_BAD_SPILL_P (a2) && ! ALLOCNO_BAD_SPILL_P (a1))
2130 return -1;
2131 pri1 = allocno_spill_priority (a1);
2132 pri2 = allocno_spill_priority (a2);
2133 if ((diff = pri1 - pri2) != 0)
2134 return diff;
2135 if ((diff
2136 = ALLOCNO_COLOR_DATA (a1)->temp - ALLOCNO_COLOR_DATA (a2)->temp) != 0)
2137 return diff;
2138 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2141 /* Used for sorting allocnos for spilling. */
2142 static int
2143 allocno_spill_sort_compare (const void *v1p, const void *v2p)
2145 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2146 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2148 return allocno_spill_priority_compare (p1, p2);
2151 /* Push allocnos to the coloring stack. The order of allocnos in the
2152 stack defines the order for the subsequent coloring. */
2153 static void
2154 push_allocnos_to_stack (void)
2156 ira_allocno_t a;
2157 int cost;
2159 /* Calculate uncolorable allocno spill costs. */
2160 for (a = uncolorable_allocno_bucket;
2161 a != NULL;
2162 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2163 if (ALLOCNO_CLASS (a) != NO_REGS)
2165 cost = calculate_allocno_spill_cost (a);
2166 /* ??? Remove cost of copies between the coalesced
2167 allocnos. */
2168 ALLOCNO_COLOR_DATA (a)->temp = cost;
2170 sort_bucket (&uncolorable_allocno_bucket, allocno_spill_sort_compare);
2171 for (;;)
2173 push_only_colorable ();
2174 a = uncolorable_allocno_bucket;
2175 if (a == NULL)
2176 break;
2177 remove_allocno_from_bucket_and_push (a, false);
2179 ira_assert (colorable_allocno_bucket == NULL
2180 && uncolorable_allocno_bucket == NULL);
2181 ira_assert (uncolorable_allocnos_num == 0);
2184 /* Pop the coloring stack and assign hard registers to the popped
2185 allocnos. */
2186 static void
2187 pop_allocnos_from_stack (void)
2189 ira_allocno_t allocno;
2190 enum reg_class aclass;
2192 for (;VEC_length (ira_allocno_t, allocno_stack_vec) != 0;)
2194 allocno = VEC_pop (ira_allocno_t, allocno_stack_vec);
2195 aclass = ALLOCNO_CLASS (allocno);
2196 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2198 fprintf (ira_dump_file, " Popping");
2199 ira_print_expanded_allocno (allocno);
2200 fprintf (ira_dump_file, " -- ");
2202 if (aclass == NO_REGS)
2204 ALLOCNO_HARD_REGNO (allocno) = -1;
2205 ALLOCNO_ASSIGNED_P (allocno) = true;
2206 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
2207 ira_assert
2208 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
2209 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2210 fprintf (ira_dump_file, "assign memory\n");
2212 else if (assign_hard_reg (allocno, false))
2214 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2215 fprintf (ira_dump_file, "assign reg %d\n",
2216 ALLOCNO_HARD_REGNO (allocno));
2218 else if (ALLOCNO_ASSIGNED_P (allocno))
2220 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2221 fprintf (ira_dump_file, "spill\n");
2223 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2227 /* Set up number of available hard registers for allocno A. */
2228 static void
2229 setup_allocno_available_regs_num (ira_allocno_t a)
2231 int i, j, n, hard_regno, hard_regs_num, nwords, nregs;
2232 enum reg_class aclass;
2233 enum machine_mode mode;
2234 allocno_color_data_t data;
2236 aclass = ALLOCNO_CLASS (a);
2237 data = ALLOCNO_COLOR_DATA (a);
2238 data->available_regs_num = 0;
2239 if (aclass == NO_REGS)
2240 return;
2241 hard_regs_num = ira_class_hard_regs_num[aclass];
2242 mode = ALLOCNO_MODE (a);
2243 nwords = ALLOCNO_NUM_OBJECTS (a);
2244 for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
2246 hard_regno = ira_class_hard_regs[aclass][i];
2247 nregs = hard_regno_nregs[hard_regno][mode];
2248 for (j = 0; j < nregs; j++)
2250 int k;
2251 int set_to_test_start = 0, set_to_test_end = nwords;
2253 if (nregs == nwords)
2255 if (WORDS_BIG_ENDIAN)
2256 set_to_test_start = nwords - j - 1;
2257 else
2258 set_to_test_start = j;
2259 set_to_test_end = set_to_test_start + 1;
2261 for (k = set_to_test_start; k < set_to_test_end; k++)
2263 ira_object_t obj = ALLOCNO_OBJECT (a, k);
2264 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
2266 /* Checking only profitable hard regs which exclude
2267 object's conflict hard regs. */
2268 if (TEST_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2269 hard_regno + j)
2270 || ! TEST_HARD_REG_BIT (obj_data->profitable_hard_regs,
2271 hard_regno + j))
2272 break;
2274 if (k != set_to_test_end)
2275 break;
2277 if (j == nregs)
2278 n++;
2280 data->available_regs_num = n;
2281 if (internal_flag_ira_verbose <= 2 || ira_dump_file == NULL)
2282 return;
2283 fprintf
2284 (ira_dump_file,
2285 " Allocno a%dr%d of %s(%d) has %d avail. regs",
2286 ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2287 reg_class_names[aclass], ira_class_hard_regs_num[aclass], n);
2288 for (i = 0; i < nwords; i++)
2290 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2291 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
2293 if (nwords != 1)
2295 if (i != 0)
2296 fprintf (ira_dump_file, ", ");
2297 fprintf (ira_dump_file, " obj %d", i);
2299 print_hard_reg_set (ira_dump_file, obj_data->profitable_hard_regs, false);
2300 fprintf (ira_dump_file, " (confl regs = ");
2301 print_hard_reg_set (ira_dump_file, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2302 false);
2303 fprintf (ira_dump_file, " ) %snode: ",
2304 hard_reg_set_equal_p (obj_data->profitable_hard_regs,
2305 obj_data->hard_regs_node->hard_regs->set)
2306 ? "" : "^");
2307 print_hard_reg_set (ira_dump_file,
2308 obj_data->hard_regs_node->hard_regs->set, false);
2311 fprintf (ira_dump_file, "\n");
2314 /* Put ALLOCNO in a bucket corresponding to its number and size of its
2315 conflicting allocnos and hard registers. */
2316 static void
2317 put_allocno_into_bucket (ira_allocno_t allocno)
2319 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2320 setup_allocno_available_regs_num (allocno);
2321 if (setup_left_conflict_sizes_p (allocno))
2322 add_allocno_to_bucket (allocno, &colorable_allocno_bucket);
2323 else
2324 add_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
2327 /* Map: allocno number -> allocno priority. */
2328 static int *allocno_priorities;
2330 /* Set up priorities for N allocnos in array
2331 CONSIDERATION_ALLOCNOS. */
2332 static void
2333 setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
2335 int i, length, nrefs, priority, max_priority, mult;
2336 ira_allocno_t a;
2338 max_priority = 0;
2339 for (i = 0; i < n; i++)
2341 a = consideration_allocnos[i];
2342 nrefs = ALLOCNO_NREFS (a);
2343 ira_assert (nrefs >= 0);
2344 mult = floor_log2 (ALLOCNO_NREFS (a)) + 1;
2345 ira_assert (mult >= 0);
2346 allocno_priorities[ALLOCNO_NUM (a)]
2347 = priority
2348 = (mult
2349 * (ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a))
2350 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
2351 if (priority < 0)
2352 priority = -priority;
2353 if (max_priority < priority)
2354 max_priority = priority;
2356 mult = max_priority == 0 ? 1 : INT_MAX / max_priority;
2357 for (i = 0; i < n; i++)
2359 a = consideration_allocnos[i];
2360 length = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2361 if (ALLOCNO_NUM_OBJECTS (a) > 1)
2362 length /= ALLOCNO_NUM_OBJECTS (a);
2363 if (length <= 0)
2364 length = 1;
2365 allocno_priorities[ALLOCNO_NUM (a)]
2366 = allocno_priorities[ALLOCNO_NUM (a)] * mult / length;
2370 /* Sort allocnos according to the profit of usage of a hard register
2371 instead of memory for them. */
2372 static int
2373 allocno_cost_compare_func (const void *v1p, const void *v2p)
2375 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2376 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2377 int c1, c2;
2379 c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_UPDATED_CLASS_COST (p1);
2380 c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_UPDATED_CLASS_COST (p2);
2381 if (c1 - c2)
2382 return c1 - c2;
2384 /* If regs are equally good, sort by allocno numbers, so that the
2385 results of qsort leave nothing to chance. */
2386 return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
2389 /* We used Chaitin-Briggs coloring to assign as many pseudos as
2390 possible to hard registers. Let us try to improve allocation with
2391 cost point of view. This function improves the allocation by
2392 spilling some allocnos and assigning the freed hard registers to
2393 other allocnos if it decreases the overall allocation cost. */
2394 static void
2395 improve_allocation (void)
2397 unsigned int i;
2398 int j, k, n, hregno, conflict_hregno, base_cost, class_size, word, nwords;
2399 int check, spill_cost, min_cost, nregs, conflict_nregs, r, best;
2400 bool try_p;
2401 enum reg_class aclass;
2402 enum machine_mode mode;
2403 int *allocno_costs;
2404 int costs[FIRST_PSEUDO_REGISTER];
2405 HARD_REG_SET conflicting_regs[2], profitable_hard_regs[2];
2406 ira_allocno_t a;
2407 bitmap_iterator bi;
2409 /* Clear counts used to process conflicting allocnos only once for
2410 each allocno. */
2411 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2412 ALLOCNO_COLOR_DATA (ira_allocnos[i])->temp = 0;
2413 check = n = 0;
2414 /* Process each allocno and try to assign a hard register to it by
2415 spilling some its conflicting allocnos. */
2416 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2418 a = ira_allocnos[i];
2419 ALLOCNO_COLOR_DATA (a)->temp = 0;
2420 if (empty_profitable_hard_regs (a))
2421 continue;
2422 check++;
2423 aclass = ALLOCNO_CLASS (a);
2424 allocno_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
2425 if (allocno_costs == NULL)
2426 allocno_costs = ALLOCNO_HARD_REG_COSTS (a);
2427 if ((hregno = ALLOCNO_HARD_REGNO (a)) < 0)
2428 base_cost = ALLOCNO_UPDATED_MEMORY_COST (a);
2429 else if (allocno_costs == NULL)
2430 /* It means that assigning a hard register is not profitable
2431 (we don't waste memory for hard register costs in this
2432 case). */
2433 continue;
2434 else
2435 base_cost = allocno_costs[ira_class_hard_reg_index[aclass][hregno]];
2436 try_p = false;
2437 get_conflict_profitable_regs (a, false,
2438 conflicting_regs, profitable_hard_regs);
2439 class_size = ira_class_hard_regs_num[aclass];
2440 /* Set up cost improvement for usage of each profitable hard
2441 register for allocno A. */
2442 for (j = 0; j < class_size; j++)
2444 hregno = ira_class_hard_regs[aclass][j];
2445 if (! check_hard_reg_p (a, hregno,
2446 conflicting_regs, profitable_hard_regs))
2447 continue;
2448 ira_assert (ira_class_hard_reg_index[aclass][hregno] == j);
2449 k = allocno_costs == NULL ? 0 : j;
2450 costs[hregno] = (allocno_costs == NULL
2451 ? ALLOCNO_UPDATED_CLASS_COST (a) : allocno_costs[k]);
2452 costs[hregno] -= base_cost;
2453 if (costs[hregno] < 0)
2454 try_p = true;
2456 if (! try_p)
2457 /* There is no chance to improve the allocation cost by
2458 assigning hard register to allocno A even without spilling
2459 conflicting allocnos. */
2460 continue;
2461 mode = ALLOCNO_MODE (a);
2462 nwords = ALLOCNO_NUM_OBJECTS (a);
2463 /* Process each allocno conflicting with A and update the cost
2464 improvement for profitable hard registers of A. To use a
2465 hard register for A we need to spill some conflicting
2466 allocnos and that creates penalty for the cost
2467 improvement. */
2468 for (word = 0; word < nwords; word++)
2470 ira_object_t conflict_obj;
2471 ira_object_t obj = ALLOCNO_OBJECT (a, word);
2472 ira_object_conflict_iterator oci;
2474 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2476 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2478 if (ALLOCNO_COLOR_DATA (conflict_a)->temp == check)
2479 /* We already processed this conflicting allocno
2480 because we processed earlier another object of the
2481 conflicting allocno. */
2482 continue;
2483 ALLOCNO_COLOR_DATA (conflict_a)->temp = check;
2484 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
2485 continue;
2486 spill_cost = ALLOCNO_UPDATED_MEMORY_COST (conflict_a);
2487 k = (ira_class_hard_reg_index
2488 [ALLOCNO_CLASS (conflict_a)][conflict_hregno]);
2489 ira_assert (k >= 0);
2490 if ((allocno_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (conflict_a))
2491 != NULL)
2492 spill_cost -= allocno_costs[k];
2493 else if ((allocno_costs = ALLOCNO_HARD_REG_COSTS (conflict_a))
2494 != NULL)
2495 spill_cost -= allocno_costs[k];
2496 else
2497 spill_cost -= ALLOCNO_UPDATED_CLASS_COST (conflict_a);
2498 conflict_nregs
2499 = hard_regno_nregs[conflict_hregno][ALLOCNO_MODE (conflict_a)];
2500 for (r = conflict_hregno;
2501 r >= 0 && r + hard_regno_nregs[r][mode] > conflict_hregno;
2502 r--)
2503 if (check_hard_reg_p (a, r,
2504 conflicting_regs, profitable_hard_regs))
2505 costs[r] += spill_cost;
2506 for (r = conflict_hregno + 1;
2507 r < conflict_hregno + conflict_nregs;
2508 r++)
2509 if (check_hard_reg_p (a, r,
2510 conflicting_regs, profitable_hard_regs))
2511 costs[r] += spill_cost;
2514 min_cost = INT_MAX;
2515 best = -1;
2516 /* Now we choose hard register for A which results in highest
2517 allocation cost improvement. */
2518 for (j = 0; j < class_size; j++)
2520 hregno = ira_class_hard_regs[aclass][j];
2521 if (check_hard_reg_p (a, hregno,
2522 conflicting_regs, profitable_hard_regs)
2523 && min_cost > costs[hregno])
2525 best = hregno;
2526 min_cost = costs[hregno];
2529 if (min_cost >= 0)
2530 /* We are in a situation when assigning any hard register to A
2531 by spilling some conflicting allocnos does not improve the
2532 allocation cost. */
2533 continue;
2534 nregs = hard_regno_nregs[best][mode];
2535 /* Now spill conflicting allocnos which contain a hard register
2536 of A when we assign the best chosen hard register to it. */
2537 for (word = 0; word < nwords; word++)
2539 ira_object_t conflict_obj;
2540 ira_object_t obj = ALLOCNO_OBJECT (a, word);
2541 ira_object_conflict_iterator oci;
2543 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2545 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2547 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
2548 continue;
2549 conflict_nregs
2550 = hard_regno_nregs[conflict_hregno][ALLOCNO_MODE (conflict_a)];
2551 if (best + nregs <= conflict_hregno
2552 || conflict_hregno + conflict_nregs <= best)
2553 /* No intersection. */
2554 continue;
2555 ALLOCNO_HARD_REGNO (conflict_a) = -1;
2556 sorted_allocnos[n++] = conflict_a;
2557 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2558 fprintf (ira_dump_file, "Spilling a%dr%d for a%dr%d\n",
2559 ALLOCNO_NUM (conflict_a), ALLOCNO_REGNO (conflict_a),
2560 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2563 /* Assign the best chosen hard register to A. */
2564 ALLOCNO_HARD_REGNO (a) = best;
2565 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2566 fprintf (ira_dump_file, "Assigning %d to a%dr%d\n",
2567 best, ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2569 if (n == 0)
2570 return;
2571 /* We spilled some allocnos to assign their hard registers to other
2572 allocnos. The spilled allocnos are now in array
2573 'sorted_allocnos'. There is still a possibility that some of the
2574 spilled allocnos can get hard registers. So let us try assign
2575 them hard registers again (just a reminder -- function
2576 'assign_hard_reg' assigns hard registers only if it is possible
2577 and profitable). We process the spilled allocnos with biggest
2578 benefit to get hard register first -- see function
2579 'allocno_cost_compare_func'. */
2580 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
2581 allocno_cost_compare_func);
2582 for (j = 0; j < n; j++)
2584 a = sorted_allocnos[j];
2585 ALLOCNO_ASSIGNED_P (a) = false;
2586 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2588 fprintf (ira_dump_file, " ");
2589 ira_print_expanded_allocno (a);
2590 fprintf (ira_dump_file, " -- ");
2592 if (assign_hard_reg (a, false))
2594 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2595 fprintf (ira_dump_file, "assign hard reg %d\n",
2596 ALLOCNO_HARD_REGNO (a));
2598 else
2600 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2601 fprintf (ira_dump_file, "assign memory\n");
2606 /* Sort allocnos according to their priorities which are calculated
2607 analogous to ones in file `global.c'. */
2608 static int
2609 allocno_priority_compare_func (const void *v1p, const void *v2p)
2611 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2612 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2613 int pri1, pri2;
2615 pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
2616 pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
2617 if (pri2 != pri1)
2618 return SORTGT (pri2, pri1);
2620 /* If regs are equally good, sort by allocnos, so that the results of
2621 qsort leave nothing to chance. */
2622 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2625 /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
2626 taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
2627 static void
2628 color_allocnos (void)
2630 unsigned int i, n;
2631 bitmap_iterator bi;
2632 ira_allocno_t a;
2634 setup_profitable_hard_regs ();
2635 if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
2637 n = 0;
2638 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2640 a = ira_allocnos[i];
2641 if (ALLOCNO_CLASS (a) == NO_REGS)
2643 ALLOCNO_HARD_REGNO (a) = -1;
2644 ALLOCNO_ASSIGNED_P (a) = true;
2645 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
2646 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
2647 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2649 fprintf (ira_dump_file, " Spill");
2650 ira_print_expanded_allocno (a);
2651 fprintf (ira_dump_file, "\n");
2653 continue;
2655 sorted_allocnos[n++] = a;
2657 if (n != 0)
2659 setup_allocno_priorities (sorted_allocnos, n);
2660 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
2661 allocno_priority_compare_func);
2662 for (i = 0; i < n; i++)
2664 a = sorted_allocnos[i];
2665 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2667 fprintf (ira_dump_file, " ");
2668 ira_print_expanded_allocno (a);
2669 fprintf (ira_dump_file, " -- ");
2671 if (assign_hard_reg (a, false))
2673 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2674 fprintf (ira_dump_file, "assign hard reg %d\n",
2675 ALLOCNO_HARD_REGNO (a));
2677 else
2679 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2680 fprintf (ira_dump_file, "assign memory\n");
2685 else
2687 form_object_hard_regs_nodes_forest ();
2688 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2689 print_hard_regs_forest (ira_dump_file);
2690 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2692 a = ira_allocnos[i];
2693 if (ALLOCNO_CLASS (a) != NO_REGS && ! empty_profitable_hard_regs (a))
2694 ALLOCNO_COLOR_DATA (a)->in_graph_p = true;
2695 else
2697 ALLOCNO_HARD_REGNO (a) = -1;
2698 ALLOCNO_ASSIGNED_P (a) = true;
2699 /* We don't need updated costs anymore. */
2700 ira_free_allocno_updated_costs (a);
2701 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2703 fprintf (ira_dump_file, " Spill");
2704 ira_print_expanded_allocno (a);
2705 fprintf (ira_dump_file, "\n");
2709 /* Put the allocnos into the corresponding buckets. */
2710 colorable_allocno_bucket = NULL;
2711 uncolorable_allocno_bucket = NULL;
2712 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2714 a = ira_allocnos[i];
2715 if (ALLOCNO_COLOR_DATA (a)->in_graph_p)
2716 put_allocno_into_bucket (a);
2718 push_allocnos_to_stack ();
2719 pop_allocnos_from_stack ();
2720 finish_object_hard_regs_nodes_forest ();
2722 improve_allocation ();
2727 /* Output information about the loop given by its LOOP_TREE_NODE. */
2728 static void
2729 print_loop_title (ira_loop_tree_node_t loop_tree_node)
2731 unsigned int j;
2732 bitmap_iterator bi;
2733 ira_loop_tree_node_t subloop_node, dest_loop_node;
2734 edge e;
2735 edge_iterator ei;
2737 ira_assert (loop_tree_node->loop != NULL);
2738 fprintf (ira_dump_file,
2739 "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
2740 loop_tree_node->loop->num,
2741 (loop_tree_node->parent == NULL
2742 ? -1 : loop_tree_node->parent->loop->num),
2743 loop_tree_node->loop->header->index,
2744 loop_depth (loop_tree_node->loop));
2745 for (subloop_node = loop_tree_node->children;
2746 subloop_node != NULL;
2747 subloop_node = subloop_node->next)
2748 if (subloop_node->bb != NULL)
2750 fprintf (ira_dump_file, " %d", subloop_node->bb->index);
2751 FOR_EACH_EDGE (e, ei, subloop_node->bb->succs)
2752 if (e->dest != EXIT_BLOCK_PTR
2753 && ((dest_loop_node = IRA_BB_NODE (e->dest)->parent)
2754 != loop_tree_node))
2755 fprintf (ira_dump_file, "(->%d:l%d)",
2756 e->dest->index, dest_loop_node->loop->num);
2758 fprintf (ira_dump_file, "\n all:");
2759 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
2760 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
2761 fprintf (ira_dump_file, "\n modified regnos:");
2762 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
2763 fprintf (ira_dump_file, " %d", j);
2764 fprintf (ira_dump_file, "\n border:");
2765 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
2766 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
2767 fprintf (ira_dump_file, "\n Pressure:");
2768 for (j = 0; (int) j < ira_pressure_classes_num; j++)
2770 enum reg_class pclass;
2772 pclass = ira_pressure_classes[j];
2773 if (loop_tree_node->reg_pressure[pclass] == 0)
2774 continue;
2775 fprintf (ira_dump_file, " %s=%d", reg_class_names[pclass],
2776 loop_tree_node->reg_pressure[pclass]);
2778 fprintf (ira_dump_file, "\n");
2781 /* Color the allocnos inside loop (in the extreme case it can be all
2782 of the function) given the corresponding LOOP_TREE_NODE. The
2783 function is called for each loop during top-down traverse of the
2784 loop tree. */
2785 static void
2786 color_pass (ira_loop_tree_node_t loop_tree_node)
2788 int i, regno, hard_regno, index = -1, n, nobj;
2789 int cost, exit_freq, enter_freq;
2790 unsigned int j;
2791 bitmap_iterator bi;
2792 enum machine_mode mode;
2793 enum reg_class rclass, aclass, pclass;
2794 ira_allocno_t a, subloop_allocno;
2795 ira_loop_tree_node_t subloop_node;
2797 ira_assert (loop_tree_node->bb == NULL);
2798 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2799 print_loop_title (loop_tree_node);
2801 bitmap_copy (coloring_allocno_bitmap, loop_tree_node->all_allocnos);
2802 bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
2803 n = nobj = 0;
2804 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2806 a = ira_allocnos[j];
2807 n++;
2808 nobj += ALLOCNO_NUM_OBJECTS (a);
2809 if (! ALLOCNO_ASSIGNED_P (a))
2810 continue;
2811 bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
2813 allocno_color_data
2814 = (allocno_color_data_t) ira_allocate (sizeof (struct allocno_color_data)
2815 * n);
2816 memset (allocno_color_data, 0, sizeof (struct allocno_color_data) * n);
2817 object_color_data
2818 = (object_color_data_t) ira_allocate (sizeof (struct object_color_data)
2819 * nobj);
2820 memset (object_color_data, 0, sizeof (struct object_color_data) * nobj);
2821 n = nobj = 0;
2822 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2824 a = ira_allocnos[j];
2825 ALLOCNO_ADD_DATA (a) = allocno_color_data + n;
2826 n++;
2827 for (i = 0; i < ALLOCNO_NUM_OBJECTS (a); i++)
2829 OBJECT_ADD_DATA (ALLOCNO_OBJECT (a, i)) = object_color_data + nobj;
2830 nobj++;
2833 /* Color all mentioned allocnos including transparent ones. */
2834 color_allocnos ();
2835 /* Process caps. They are processed just once. */
2836 if (flag_ira_region == IRA_REGION_MIXED
2837 || flag_ira_region == IRA_REGION_ALL)
2838 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
2840 a = ira_allocnos[j];
2841 if (ALLOCNO_CAP_MEMBER (a) == NULL)
2842 continue;
2843 /* Remove from processing in the next loop. */
2844 bitmap_clear_bit (consideration_allocno_bitmap, j);
2845 rclass = ALLOCNO_CLASS (a);
2846 pclass = ira_pressure_class_translate[rclass];
2847 if (flag_ira_region == IRA_REGION_MIXED
2848 && (loop_tree_node->reg_pressure[pclass]
2849 <= ira_available_class_regs[pclass]))
2851 mode = ALLOCNO_MODE (a);
2852 hard_regno = ALLOCNO_HARD_REGNO (a);
2853 if (hard_regno >= 0)
2855 index = ira_class_hard_reg_index[rclass][hard_regno];
2856 ira_assert (index >= 0);
2858 regno = ALLOCNO_REGNO (a);
2859 subloop_allocno = ALLOCNO_CAP_MEMBER (a);
2860 subloop_node = ALLOCNO_LOOP_TREE_NODE (subloop_allocno);
2861 ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno));
2862 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
2863 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
2864 if (hard_regno >= 0)
2865 update_copy_costs (subloop_allocno, true);
2866 /* We don't need updated costs anymore: */
2867 ira_free_allocno_updated_costs (subloop_allocno);
2870 /* Update costs of the corresponding allocnos (not caps) in the
2871 subloops. */
2872 for (subloop_node = loop_tree_node->subloops;
2873 subloop_node != NULL;
2874 subloop_node = subloop_node->subloop_next)
2876 ira_assert (subloop_node->bb == NULL);
2877 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2879 a = ira_allocnos[j];
2880 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2881 mode = ALLOCNO_MODE (a);
2882 rclass = ALLOCNO_CLASS (a);
2883 pclass = ira_pressure_class_translate[rclass];
2884 hard_regno = ALLOCNO_HARD_REGNO (a);
2885 /* Use hard register class here. ??? */
2886 if (hard_regno >= 0)
2888 index = ira_class_hard_reg_index[rclass][hard_regno];
2889 ira_assert (index >= 0);
2891 regno = ALLOCNO_REGNO (a);
2892 /* ??? conflict costs */
2893 subloop_allocno = subloop_node->regno_allocno_map[regno];
2894 if (subloop_allocno == NULL
2895 || ALLOCNO_CAP (subloop_allocno) != NULL)
2896 continue;
2897 ira_assert (ALLOCNO_CLASS (subloop_allocno) == rclass);
2898 ira_assert (bitmap_bit_p (subloop_node->all_allocnos,
2899 ALLOCNO_NUM (subloop_allocno)));
2900 if ((flag_ira_region == IRA_REGION_MIXED)
2901 && (loop_tree_node->reg_pressure[pclass]
2902 <= ira_available_class_regs[pclass]))
2904 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
2906 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
2907 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
2908 if (hard_regno >= 0)
2909 update_copy_costs (subloop_allocno, true);
2910 /* We don't need updated costs anymore: */
2911 ira_free_allocno_updated_costs (subloop_allocno);
2913 continue;
2915 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
2916 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
2917 ira_assert (regno < ira_reg_equiv_len);
2918 if (ira_reg_equiv_invariant_p[regno]
2919 || ira_reg_equiv_const[regno] != NULL_RTX)
2921 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
2923 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
2924 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
2925 if (hard_regno >= 0)
2926 update_copy_costs (subloop_allocno, true);
2927 /* We don't need updated costs anymore: */
2928 ira_free_allocno_updated_costs (subloop_allocno);
2931 else if (hard_regno < 0)
2933 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
2934 -= ((ira_memory_move_cost[mode][rclass][1] * enter_freq)
2935 + (ira_memory_move_cost[mode][rclass][0] * exit_freq));
2937 else
2939 aclass = ALLOCNO_CLASS (subloop_allocno);
2940 ira_init_register_move_cost_if_necessary (mode);
2941 cost = (ira_register_move_cost[mode][rclass][rclass]
2942 * (exit_freq + enter_freq));
2943 ira_allocate_and_set_or_copy_costs
2944 (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno), aclass,
2945 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno),
2946 ALLOCNO_HARD_REG_COSTS (subloop_allocno));
2947 ira_allocate_and_set_or_copy_costs
2948 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno),
2949 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno));
2950 ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
2951 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
2952 -= cost;
2953 if (ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
2954 > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index])
2955 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
2956 = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index];
2957 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
2958 += (ira_memory_move_cost[mode][rclass][0] * enter_freq
2959 + ira_memory_move_cost[mode][rclass][1] * exit_freq);
2963 ira_free (object_color_data);
2964 ira_free (allocno_color_data);
2965 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
2967 a = ira_allocnos[j];
2968 ALLOCNO_ADD_DATA (a) = NULL;
2969 for (i = 0; i < ALLOCNO_NUM_OBJECTS (a); i++)
2970 OBJECT_ADD_DATA (a) = NULL;
2974 /* Initialize the common data for coloring and calls functions to do
2975 Chaitin-Briggs and regional coloring. */
2976 static void
2977 do_coloring (void)
2979 coloring_allocno_bitmap = ira_allocate_bitmap ();
2980 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2981 fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
2983 ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
2985 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2986 ira_print_disposition (ira_dump_file);
2988 ira_free_bitmap (coloring_allocno_bitmap);
2993 /* Move spill/restore code, which are to be generated in ira-emit.c,
2994 to less frequent points (if it is profitable) by reassigning some
2995 allocnos (in loop with subloops containing in another loop) to
2996 memory which results in longer live-range where the corresponding
2997 pseudo-registers will be in memory. */
2998 static void
2999 move_spill_restore (void)
3001 int cost, regno, hard_regno, hard_regno2, index;
3002 bool changed_p;
3003 int enter_freq, exit_freq;
3004 enum machine_mode mode;
3005 enum reg_class rclass;
3006 ira_allocno_t a, parent_allocno, subloop_allocno;
3007 ira_loop_tree_node_t parent, loop_node, subloop_node;
3008 ira_allocno_iterator ai;
3010 for (;;)
3012 changed_p = false;
3013 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3014 fprintf (ira_dump_file, "New iteration of spill/restore move\n");
3015 FOR_EACH_ALLOCNO (a, ai)
3017 regno = ALLOCNO_REGNO (a);
3018 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
3019 if (ALLOCNO_CAP_MEMBER (a) != NULL
3020 || ALLOCNO_CAP (a) != NULL
3021 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
3022 || loop_node->children == NULL
3023 /* don't do the optimization because it can create
3024 copies and the reload pass can spill the allocno set
3025 by copy although the allocno will not get memory
3026 slot. */
3027 || ira_reg_equiv_invariant_p[regno]
3028 || ira_reg_equiv_const[regno] != NULL_RTX
3029 || !bitmap_bit_p (loop_node->border_allocnos, ALLOCNO_NUM (a)))
3030 continue;
3031 mode = ALLOCNO_MODE (a);
3032 rclass = ALLOCNO_CLASS (a);
3033 index = ira_class_hard_reg_index[rclass][hard_regno];
3034 ira_assert (index >= 0);
3035 cost = (ALLOCNO_MEMORY_COST (a)
3036 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
3037 ? ALLOCNO_CLASS_COST (a)
3038 : ALLOCNO_HARD_REG_COSTS (a)[index]));
3039 ira_init_register_move_cost_if_necessary (mode);
3040 for (subloop_node = loop_node->subloops;
3041 subloop_node != NULL;
3042 subloop_node = subloop_node->subloop_next)
3044 ira_assert (subloop_node->bb == NULL);
3045 subloop_allocno = subloop_node->regno_allocno_map[regno];
3046 if (subloop_allocno == NULL)
3047 continue;
3048 ira_assert (rclass == ALLOCNO_CLASS (subloop_allocno));
3049 /* We have accumulated cost. To get the real cost of
3050 allocno usage in the loop we should subtract costs of
3051 the subloop allocnos. */
3052 cost -= (ALLOCNO_MEMORY_COST (subloop_allocno)
3053 - (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
3054 ? ALLOCNO_CLASS_COST (subloop_allocno)
3055 : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]));
3056 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
3057 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
3058 if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
3059 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3060 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3061 else
3063 cost
3064 += (ira_memory_move_cost[mode][rclass][0] * exit_freq
3065 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3066 if (hard_regno2 != hard_regno)
3067 cost -= (ira_register_move_cost[mode][rclass][rclass]
3068 * (exit_freq + enter_freq));
3071 if ((parent = loop_node->parent) != NULL
3072 && (parent_allocno = parent->regno_allocno_map[regno]) != NULL)
3074 ira_assert (rclass == ALLOCNO_CLASS (parent_allocno));
3075 exit_freq = ira_loop_edge_freq (loop_node, regno, true);
3076 enter_freq = ira_loop_edge_freq (loop_node, regno, false);
3077 if ((hard_regno2 = ALLOCNO_HARD_REGNO (parent_allocno)) < 0)
3078 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3079 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3080 else
3082 cost
3083 += (ira_memory_move_cost[mode][rclass][1] * exit_freq
3084 + ira_memory_move_cost[mode][rclass][0] * enter_freq);
3085 if (hard_regno2 != hard_regno)
3086 cost -= (ira_register_move_cost[mode][rclass][rclass]
3087 * (exit_freq + enter_freq));
3090 if (cost < 0)
3092 ALLOCNO_HARD_REGNO (a) = -1;
3093 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3095 fprintf
3096 (ira_dump_file,
3097 " Moving spill/restore for a%dr%d up from loop %d",
3098 ALLOCNO_NUM (a), regno, loop_node->loop->num);
3099 fprintf (ira_dump_file, " - profit %d\n", -cost);
3101 changed_p = true;
3104 if (! changed_p)
3105 break;
3111 /* Update current hard reg costs and current conflict hard reg costs
3112 for allocno A. It is done by processing its copies containing
3113 other allocnos already assigned. */
3114 static void
3115 update_curr_costs (ira_allocno_t a)
3117 int i, hard_regno, cost;
3118 enum machine_mode mode;
3119 enum reg_class aclass, rclass;
3120 ira_allocno_t another_a;
3121 ira_copy_t cp, next_cp;
3123 ira_free_allocno_updated_costs (a);
3124 ira_assert (! ALLOCNO_ASSIGNED_P (a));
3125 aclass = ALLOCNO_CLASS (a);
3126 if (aclass == NO_REGS)
3127 return;
3128 mode = ALLOCNO_MODE (a);
3129 ira_init_register_move_cost_if_necessary (mode);
3130 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3132 if (cp->first == a)
3134 next_cp = cp->next_first_allocno_copy;
3135 another_a = cp->second;
3137 else if (cp->second == a)
3139 next_cp = cp->next_second_allocno_copy;
3140 another_a = cp->first;
3142 else
3143 gcc_unreachable ();
3144 if (! ira_reg_classes_intersect_p[aclass][ALLOCNO_CLASS (another_a)]
3145 || ! ALLOCNO_ASSIGNED_P (another_a)
3146 || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
3147 continue;
3148 rclass = REGNO_REG_CLASS (hard_regno);
3149 i = ira_class_hard_reg_index[aclass][hard_regno];
3150 if (i < 0)
3151 continue;
3152 cost = (cp->first == a
3153 ? ira_register_move_cost[mode][rclass][aclass]
3154 : ira_register_move_cost[mode][aclass][rclass]);
3155 ira_allocate_and_set_or_copy_costs
3156 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a),
3157 ALLOCNO_HARD_REG_COSTS (a));
3158 ira_allocate_and_set_or_copy_costs
3159 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
3160 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
3161 ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3162 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3166 /* Try to assign hard registers to the unassigned allocnos and
3167 allocnos conflicting with them or conflicting with allocnos whose
3168 regno >= START_REGNO. The function is called after ira_flattening,
3169 so more allocnos (including ones created in ira-emit.c) will have a
3170 chance to get a hard register. We use simple assignment algorithm
3171 based on priorities. */
3172 void
3173 ira_reassign_conflict_allocnos (int start_regno)
3175 int i, allocnos_to_color_num;
3176 ira_allocno_t a;
3177 enum reg_class aclass;
3178 bitmap allocnos_to_color;
3179 ira_allocno_iterator ai;
3181 allocnos_to_color = ira_allocate_bitmap ();
3182 allocnos_to_color_num = 0;
3183 FOR_EACH_ALLOCNO (a, ai)
3185 int n = ALLOCNO_NUM_OBJECTS (a);
3187 if (! ALLOCNO_ASSIGNED_P (a)
3188 && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
3190 if (ALLOCNO_CLASS (a) != NO_REGS)
3191 sorted_allocnos[allocnos_to_color_num++] = a;
3192 else
3194 ALLOCNO_ASSIGNED_P (a) = true;
3195 ALLOCNO_HARD_REGNO (a) = -1;
3196 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3197 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3199 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
3201 if (ALLOCNO_REGNO (a) < start_regno
3202 || (aclass = ALLOCNO_CLASS (a)) == NO_REGS)
3203 continue;
3204 for (i = 0; i < n; i++)
3206 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3207 ira_object_t conflict_obj;
3208 ira_object_conflict_iterator oci;
3210 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3212 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3214 ira_assert (ira_reg_classes_intersect_p
3215 [aclass][ALLOCNO_CLASS (conflict_a)]);
3216 if (!bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
3217 continue;
3218 sorted_allocnos[allocnos_to_color_num++] = conflict_a;
3222 ira_free_bitmap (allocnos_to_color);
3223 if (allocnos_to_color_num > 1)
3225 setup_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
3226 qsort (sorted_allocnos, allocnos_to_color_num, sizeof (ira_allocno_t),
3227 allocno_priority_compare_func);
3229 for (i = 0; i < allocnos_to_color_num; i++)
3231 a = sorted_allocnos[i];
3232 ALLOCNO_ASSIGNED_P (a) = false;
3233 update_curr_costs (a);
3235 for (i = 0; i < allocnos_to_color_num; i++)
3237 a = sorted_allocnos[i];
3238 if (assign_hard_reg (a, true))
3240 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3241 fprintf
3242 (ira_dump_file,
3243 " Secondary allocation: assign hard reg %d to reg %d\n",
3244 ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
3251 /* This page contains functions used to find conflicts using allocno
3252 live ranges. */
3254 /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
3255 used to find a conflict for new allocnos or allocnos with the
3256 different allocno classes. */
3257 static bool
3258 allocnos_conflict_by_live_ranges_p (ira_allocno_t a1, ira_allocno_t a2)
3260 rtx reg1, reg2;
3261 int i, j;
3262 int n1 = ALLOCNO_NUM_OBJECTS (a1);
3263 int n2 = ALLOCNO_NUM_OBJECTS (a2);
3265 if (a1 == a2)
3266 return false;
3267 reg1 = regno_reg_rtx[ALLOCNO_REGNO (a1)];
3268 reg2 = regno_reg_rtx[ALLOCNO_REGNO (a2)];
3269 if (reg1 != NULL && reg2 != NULL
3270 && ORIGINAL_REGNO (reg1) == ORIGINAL_REGNO (reg2))
3271 return false;
3273 for (i = 0; i < n1; i++)
3275 ira_object_t c1 = ALLOCNO_OBJECT (a1, i);
3277 for (j = 0; j < n2; j++)
3279 ira_object_t c2 = ALLOCNO_OBJECT (a2, j);
3281 if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1),
3282 OBJECT_LIVE_RANGES (c2)))
3283 return true;
3286 return false;
3289 #ifdef ENABLE_IRA_CHECKING
3291 /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
3292 intersect. This should be used when there is only one region.
3293 Currently this is used during reload. */
3294 static bool
3295 conflict_by_live_ranges_p (int regno1, int regno2)
3297 ira_allocno_t a1, a2;
3299 ira_assert (regno1 >= FIRST_PSEUDO_REGISTER
3300 && regno2 >= FIRST_PSEUDO_REGISTER);
3301 /* Reg info caclulated by dataflow infrastructure can be different
3302 from one calculated by regclass. */
3303 if ((a1 = ira_loop_tree_root->regno_allocno_map[regno1]) == NULL
3304 || (a2 = ira_loop_tree_root->regno_allocno_map[regno2]) == NULL)
3305 return false;
3306 return allocnos_conflict_by_live_ranges_p (a1, a2);
3309 #endif
3313 /* This page contains code to coalesce memory stack slots used by
3314 spilled allocnos. This results in smaller stack frame, better data
3315 locality, and in smaller code for some architectures like
3316 x86/x86_64 where insn size depends on address displacement value.
3317 On the other hand, it can worsen insn scheduling after the RA but
3318 in practice it is less important than smaller stack frames. */
3320 /* TRUE if we coalesced some allocnos. In other words, if we got
3321 loops formed by members first_coalesced_allocno and
3322 next_coalesced_allocno containing more one allocno. */
3323 static bool allocno_coalesced_p;
3325 /* Bitmap used to prevent a repeated allocno processing because of
3326 coalescing. */
3327 static bitmap processed_coalesced_allocno_bitmap;
3329 /* See below. */
3330 typedef struct coalesce_data *coalesce_data_t;
3332 /* To decrease footprint of ira_allocno structure we store all data
3333 needed only for coalescing in the following structure. */
3334 struct coalesce_data
3336 /* Coalesced allocnos form a cyclic list. One allocno given by
3337 FIRST represents all coalesced allocnos. The
3338 list is chained by NEXT. */
3339 ira_allocno_t first;
3340 ira_allocno_t next;
3341 int temp;
3344 /* Container for storing allocno data concerning coalescing. */
3345 static coalesce_data_t allocno_coalesce_data;
3347 /* Macro to access the data concerning coalescing. */
3348 #define ALLOCNO_COALESCE_DATA(a) ((coalesce_data_t) ALLOCNO_ADD_DATA (a))
3350 /* The function is used to sort allocnos according to their execution
3351 frequencies. */
3352 static int
3353 copy_freq_compare_func (const void *v1p, const void *v2p)
3355 ira_copy_t cp1 = *(const ira_copy_t *) v1p, cp2 = *(const ira_copy_t *) v2p;
3356 int pri1, pri2;
3358 pri1 = cp1->freq;
3359 pri2 = cp2->freq;
3360 if (pri2 - pri1)
3361 return pri2 - pri1;
3363 /* If freqencies are equal, sort by copies, so that the results of
3364 qsort leave nothing to chance. */
3365 return cp1->num - cp2->num;
3368 /* Merge two sets of coalesced allocnos given correspondingly by
3369 allocnos A1 and A2 (more accurately merging A2 set into A1
3370 set). */
3371 static void
3372 merge_allocnos (ira_allocno_t a1, ira_allocno_t a2)
3374 ira_allocno_t a, first, last, next;
3376 first = ALLOCNO_COALESCE_DATA (a1)->first;
3377 a = ALLOCNO_COALESCE_DATA (a2)->first;
3378 if (first == a)
3379 return;
3380 for (last = a2, a = ALLOCNO_COALESCE_DATA (a2)->next;;
3381 a = ALLOCNO_COALESCE_DATA (a)->next)
3383 ALLOCNO_COALESCE_DATA (a)->first = first;
3384 if (a == a2)
3385 break;
3386 last = a;
3388 next = allocno_coalesce_data[ALLOCNO_NUM (first)].next;
3389 allocno_coalesce_data[ALLOCNO_NUM (first)].next = a2;
3390 allocno_coalesce_data[ALLOCNO_NUM (last)].next = next;
3393 /* Return TRUE if there are conflicting allocnos from two sets of
3394 coalesced allocnos given correspondingly by allocnos A1 and A2. We
3395 use live ranges to find conflicts because conflicts are represented
3396 only for allocnos of the same allocno class and during the reload
3397 pass we coalesce allocnos for sharing stack memory slots. */
3398 static bool
3399 coalesced_allocno_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
3401 ira_allocno_t a, conflict_a;
3403 if (allocno_coalesced_p)
3405 bitmap_clear (processed_coalesced_allocno_bitmap);
3406 for (a = ALLOCNO_COALESCE_DATA (a1)->next;;
3407 a = ALLOCNO_COALESCE_DATA (a)->next)
3409 bitmap_set_bit (processed_coalesced_allocno_bitmap, ALLOCNO_NUM (a));
3410 if (a == a1)
3411 break;
3414 for (a = ALLOCNO_COALESCE_DATA (a2)->next;;
3415 a = ALLOCNO_COALESCE_DATA (a)->next)
3417 for (conflict_a = ALLOCNO_COALESCE_DATA (a1)->next;;
3418 conflict_a = ALLOCNO_COALESCE_DATA (conflict_a)->next)
3420 if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
3421 return true;
3422 if (conflict_a == a1)
3423 break;
3425 if (a == a2)
3426 break;
3428 return false;
3431 /* The major function for aggressive allocno coalescing. We coalesce
3432 only spilled allocnos. If some allocnos have been coalesced, we
3433 set up flag allocno_coalesced_p. */
3434 static void
3435 coalesce_allocnos (void)
3437 ira_allocno_t a;
3438 ira_copy_t cp, next_cp, *sorted_copies;
3439 unsigned int j;
3440 int i, n, cp_num, regno;
3441 bitmap_iterator bi;
3443 sorted_copies = (ira_copy_t *) ira_allocate (ira_copies_num
3444 * sizeof (ira_copy_t));
3445 cp_num = 0;
3446 /* Collect copies. */
3447 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
3449 a = ira_allocnos[j];
3450 regno = ALLOCNO_REGNO (a);
3451 if (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
3452 || (regno < ira_reg_equiv_len
3453 && (ira_reg_equiv_const[regno] != NULL_RTX
3454 || ira_reg_equiv_invariant_p[regno])))
3455 continue;
3456 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3458 if (cp->first == a)
3460 next_cp = cp->next_first_allocno_copy;
3461 regno = ALLOCNO_REGNO (cp->second);
3462 /* For priority coloring we coalesce allocnos only with
3463 the same allocno class not with intersected allocno
3464 classes as it were possible. It is done for
3465 simplicity. */
3466 if ((cp->insn != NULL || cp->constraint_p)
3467 && ALLOCNO_ASSIGNED_P (cp->second)
3468 && ALLOCNO_HARD_REGNO (cp->second) < 0
3469 && (regno >= ira_reg_equiv_len
3470 || (! ira_reg_equiv_invariant_p[regno]
3471 && ira_reg_equiv_const[regno] == NULL_RTX)))
3472 sorted_copies[cp_num++] = cp;
3474 else if (cp->second == a)
3475 next_cp = cp->next_second_allocno_copy;
3476 else
3477 gcc_unreachable ();
3480 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
3481 /* Coalesced copies, most frequently executed first. */
3482 for (; cp_num != 0;)
3484 for (i = 0; i < cp_num; i++)
3486 cp = sorted_copies[i];
3487 if (! coalesced_allocno_conflict_p (cp->first, cp->second))
3489 allocno_coalesced_p = true;
3490 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3491 fprintf
3492 (ira_dump_file,
3493 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
3494 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
3495 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
3496 cp->freq);
3497 merge_allocnos (cp->first, cp->second);
3498 i++;
3499 break;
3502 /* Collect the rest of copies. */
3503 for (n = 0; i < cp_num; i++)
3505 cp = sorted_copies[i];
3506 if (allocno_coalesce_data[ALLOCNO_NUM (cp->first)].first
3507 != allocno_coalesce_data[ALLOCNO_NUM (cp->second)].first)
3508 sorted_copies[n++] = cp;
3510 cp_num = n;
3512 ira_free (sorted_copies);
3515 /* Usage cost and order number of coalesced allocno set to which
3516 given pseudo register belongs to. */
3517 static int *regno_coalesced_allocno_cost;
3518 static int *regno_coalesced_allocno_num;
3520 /* Sort pseudos according frequencies of coalesced allocno sets they
3521 belong to (putting most frequently ones first), and according to
3522 coalesced allocno set order numbers. */
3523 static int
3524 coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
3526 const int regno1 = *(const int *) v1p;
3527 const int regno2 = *(const int *) v2p;
3528 int diff;
3530 if ((diff = (regno_coalesced_allocno_cost[regno2]
3531 - regno_coalesced_allocno_cost[regno1])) != 0)
3532 return diff;
3533 if ((diff = (regno_coalesced_allocno_num[regno1]
3534 - regno_coalesced_allocno_num[regno2])) != 0)
3535 return diff;
3536 return regno1 - regno2;
3539 /* Widest width in which each pseudo reg is referred to (via subreg).
3540 It is used for sorting pseudo registers. */
3541 static unsigned int *regno_max_ref_width;
3543 /* Redefine STACK_GROWS_DOWNWARD in terms of 0 or 1. */
3544 #ifdef STACK_GROWS_DOWNWARD
3545 # undef STACK_GROWS_DOWNWARD
3546 # define STACK_GROWS_DOWNWARD 1
3547 #else
3548 # define STACK_GROWS_DOWNWARD 0
3549 #endif
3551 /* Sort pseudos according their slot numbers (putting ones with
3552 smaller numbers first, or last when the frame pointer is not
3553 needed). */
3554 static int
3555 coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
3557 const int regno1 = *(const int *) v1p;
3558 const int regno2 = *(const int *) v2p;
3559 ira_allocno_t a1 = ira_regno_allocno_map[regno1];
3560 ira_allocno_t a2 = ira_regno_allocno_map[regno2];
3561 int diff, slot_num1, slot_num2;
3562 int total_size1, total_size2;
3564 if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
3566 if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
3567 return regno1 - regno2;
3568 return 1;
3570 else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
3571 return -1;
3572 slot_num1 = -ALLOCNO_HARD_REGNO (a1);
3573 slot_num2 = -ALLOCNO_HARD_REGNO (a2);
3574 if ((diff = slot_num1 - slot_num2) != 0)
3575 return (frame_pointer_needed
3576 || !FRAME_GROWS_DOWNWARD == STACK_GROWS_DOWNWARD ? diff : -diff);
3577 total_size1 = MAX (PSEUDO_REGNO_BYTES (regno1),
3578 regno_max_ref_width[regno1]);
3579 total_size2 = MAX (PSEUDO_REGNO_BYTES (regno2),
3580 regno_max_ref_width[regno2]);
3581 if ((diff = total_size2 - total_size1) != 0)
3582 return diff;
3583 return regno1 - regno2;
3586 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
3587 for coalesced allocno sets containing allocnos with their regnos
3588 given in array PSEUDO_REGNOS of length N. */
3589 static void
3590 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
3592 int i, num, regno, cost;
3593 ira_allocno_t allocno, a;
3595 for (num = i = 0; i < n; i++)
3597 regno = pseudo_regnos[i];
3598 allocno = ira_regno_allocno_map[regno];
3599 if (allocno == NULL)
3601 regno_coalesced_allocno_cost[regno] = 0;
3602 regno_coalesced_allocno_num[regno] = ++num;
3603 continue;
3605 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
3606 continue;
3607 num++;
3608 for (cost = 0, a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3609 a = ALLOCNO_COALESCE_DATA (a)->next)
3611 cost += ALLOCNO_FREQ (a);
3612 if (a == allocno)
3613 break;
3615 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3616 a = ALLOCNO_COALESCE_DATA (a)->next)
3618 regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
3619 regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
3620 if (a == allocno)
3621 break;
3626 /* Collect spilled allocnos representing coalesced allocno sets (the
3627 first coalesced allocno). The collected allocnos are returned
3628 through array SPILLED_COALESCED_ALLOCNOS. The function returns the
3629 number of the collected allocnos. The allocnos are given by their
3630 regnos in array PSEUDO_REGNOS of length N. */
3631 static int
3632 collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
3633 ira_allocno_t *spilled_coalesced_allocnos)
3635 int i, num, regno;
3636 ira_allocno_t allocno;
3638 for (num = i = 0; i < n; i++)
3640 regno = pseudo_regnos[i];
3641 allocno = ira_regno_allocno_map[regno];
3642 if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
3643 || ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
3644 continue;
3645 spilled_coalesced_allocnos[num++] = allocno;
3647 return num;
3650 /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
3651 given slot contains live ranges of coalesced allocnos assigned to
3652 given slot. */
3653 static live_range_t *slot_coalesced_allocnos_live_ranges;
3655 /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
3656 ranges intersected with live ranges of coalesced allocnos assigned
3657 to slot with number N. */
3658 static bool
3659 slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno, int n)
3661 ira_allocno_t a;
3663 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3664 a = ALLOCNO_COALESCE_DATA (a)->next)
3666 int i;
3667 int nr = ALLOCNO_NUM_OBJECTS (a);
3669 for (i = 0; i < nr; i++)
3671 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3673 if (ira_live_ranges_intersect_p
3674 (slot_coalesced_allocnos_live_ranges[n],
3675 OBJECT_LIVE_RANGES (obj)))
3676 return true;
3678 if (a == allocno)
3679 break;
3681 return false;
3684 /* Update live ranges of slot to which coalesced allocnos represented
3685 by ALLOCNO were assigned. */
3686 static void
3687 setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno)
3689 int i, n;
3690 ira_allocno_t a;
3691 live_range_t r;
3693 n = ALLOCNO_COALESCE_DATA (allocno)->temp;
3694 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3695 a = ALLOCNO_COALESCE_DATA (a)->next)
3697 int nr = ALLOCNO_NUM_OBJECTS (a);
3698 for (i = 0; i < nr; i++)
3700 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3702 r = ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj));
3703 slot_coalesced_allocnos_live_ranges[n]
3704 = ira_merge_live_ranges
3705 (slot_coalesced_allocnos_live_ranges[n], r);
3707 if (a == allocno)
3708 break;
3712 /* We have coalesced allocnos involving in copies. Coalesce allocnos
3713 further in order to share the same memory stack slot. Allocnos
3714 representing sets of allocnos coalesced before the call are given
3715 in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
3716 some allocnos were coalesced in the function. */
3717 static bool
3718 coalesce_spill_slots (ira_allocno_t *spilled_coalesced_allocnos, int num)
3720 int i, j, n, last_coalesced_allocno_num;
3721 ira_allocno_t allocno, a;
3722 bool merged_p = false;
3723 bitmap set_jump_crosses = regstat_get_setjmp_crosses ();
3725 slot_coalesced_allocnos_live_ranges
3726 = (live_range_t *) ira_allocate (sizeof (live_range_t) * ira_allocnos_num);
3727 memset (slot_coalesced_allocnos_live_ranges, 0,
3728 sizeof (live_range_t) * ira_allocnos_num);
3729 last_coalesced_allocno_num = 0;
3730 /* Coalesce non-conflicting spilled allocnos preferring most
3731 frequently used. */
3732 for (i = 0; i < num; i++)
3734 allocno = spilled_coalesced_allocnos[i];
3735 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
3736 || bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (allocno))
3737 || (ALLOCNO_REGNO (allocno) < ira_reg_equiv_len
3738 && (ira_reg_equiv_const[ALLOCNO_REGNO (allocno)] != NULL_RTX
3739 || ira_reg_equiv_invariant_p[ALLOCNO_REGNO (allocno)])))
3740 continue;
3741 for (j = 0; j < i; j++)
3743 a = spilled_coalesced_allocnos[j];
3744 n = ALLOCNO_COALESCE_DATA (a)->temp;
3745 if (ALLOCNO_COALESCE_DATA (a)->first == a
3746 && ! bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (a))
3747 && (ALLOCNO_REGNO (a) >= ira_reg_equiv_len
3748 || (! ira_reg_equiv_invariant_p[ALLOCNO_REGNO (a)]
3749 && ira_reg_equiv_const[ALLOCNO_REGNO (a)] == NULL_RTX))
3750 && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno, n))
3751 break;
3753 if (j >= i)
3755 /* No coalescing: set up number for coalesced allocnos
3756 represented by ALLOCNO. */
3757 ALLOCNO_COALESCE_DATA (allocno)->temp = last_coalesced_allocno_num++;
3758 setup_slot_coalesced_allocno_live_ranges (allocno);
3760 else
3762 allocno_coalesced_p = true;
3763 merged_p = true;
3764 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3765 fprintf (ira_dump_file,
3766 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
3767 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
3768 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3769 ALLOCNO_COALESCE_DATA (allocno)->temp
3770 = ALLOCNO_COALESCE_DATA (a)->temp;
3771 setup_slot_coalesced_allocno_live_ranges (allocno);
3772 merge_allocnos (a, allocno);
3773 ira_assert (ALLOCNO_COALESCE_DATA (a)->first == a);
3776 for (i = 0; i < ira_allocnos_num; i++)
3777 ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges[i]);
3778 ira_free (slot_coalesced_allocnos_live_ranges);
3779 return merged_p;
3782 /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
3783 subsequent assigning stack slots to them in the reload pass. To do
3784 this we coalesce spilled allocnos first to decrease the number of
3785 memory-memory move insns. This function is called by the
3786 reload. */
3787 void
3788 ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
3789 unsigned int *reg_max_ref_width)
3791 int max_regno = max_reg_num ();
3792 int i, regno, num, slot_num;
3793 ira_allocno_t allocno, a;
3794 ira_allocno_iterator ai;
3795 ira_allocno_t *spilled_coalesced_allocnos;
3797 /* Set up allocnos can be coalesced. */
3798 coloring_allocno_bitmap = ira_allocate_bitmap ();
3799 for (i = 0; i < n; i++)
3801 regno = pseudo_regnos[i];
3802 allocno = ira_regno_allocno_map[regno];
3803 if (allocno != NULL)
3804 bitmap_set_bit (coloring_allocno_bitmap, ALLOCNO_NUM (allocno));
3806 allocno_coalesced_p = false;
3807 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
3808 allocno_coalesce_data
3809 = (coalesce_data_t) ira_allocate (sizeof (struct coalesce_data)
3810 * ira_allocnos_num);
3811 /* Initialize coalesce data for allocnos. */
3812 FOR_EACH_ALLOCNO (a, ai)
3814 ALLOCNO_ADD_DATA (a) = allocno_coalesce_data + ALLOCNO_NUM (a);
3815 ALLOCNO_COALESCE_DATA (a)->first = a;
3816 ALLOCNO_COALESCE_DATA (a)->next = a;
3818 coalesce_allocnos ();
3819 ira_free_bitmap (coloring_allocno_bitmap);
3820 regno_coalesced_allocno_cost
3821 = (int *) ira_allocate (max_regno * sizeof (int));
3822 regno_coalesced_allocno_num
3823 = (int *) ira_allocate (max_regno * sizeof (int));
3824 memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
3825 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
3826 /* Sort regnos according frequencies of the corresponding coalesced
3827 allocno sets. */
3828 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
3829 spilled_coalesced_allocnos
3830 = (ira_allocno_t *) ira_allocate (ira_allocnos_num
3831 * sizeof (ira_allocno_t));
3832 /* Collect allocnos representing the spilled coalesced allocno
3833 sets. */
3834 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
3835 spilled_coalesced_allocnos);
3836 if (flag_ira_share_spill_slots
3837 && coalesce_spill_slots (spilled_coalesced_allocnos, num))
3839 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
3840 qsort (pseudo_regnos, n, sizeof (int),
3841 coalesced_pseudo_reg_freq_compare);
3842 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
3843 spilled_coalesced_allocnos);
3845 ira_free_bitmap (processed_coalesced_allocno_bitmap);
3846 allocno_coalesced_p = false;
3847 /* Assign stack slot numbers to spilled allocno sets, use smaller
3848 numbers for most frequently used coalesced allocnos. -1 is
3849 reserved for dynamic search of stack slots for pseudos spilled by
3850 the reload. */
3851 slot_num = 1;
3852 for (i = 0; i < num; i++)
3854 allocno = spilled_coalesced_allocnos[i];
3855 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
3856 || ALLOCNO_HARD_REGNO (allocno) >= 0
3857 || (ALLOCNO_REGNO (allocno) < ira_reg_equiv_len
3858 && (ira_reg_equiv_const[ALLOCNO_REGNO (allocno)] != NULL_RTX
3859 || ira_reg_equiv_invariant_p[ALLOCNO_REGNO (allocno)])))
3860 continue;
3861 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3862 fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
3863 slot_num++;
3864 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3865 a = ALLOCNO_COALESCE_DATA (a)->next)
3867 ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
3868 ALLOCNO_HARD_REGNO (a) = -slot_num;
3869 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3870 fprintf (ira_dump_file, " a%dr%d(%d,%d)",
3871 ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a),
3872 MAX (PSEUDO_REGNO_BYTES (ALLOCNO_REGNO (a)),
3873 reg_max_ref_width[ALLOCNO_REGNO (a)]));
3875 if (a == allocno)
3876 break;
3878 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3879 fprintf (ira_dump_file, "\n");
3881 ira_spilled_reg_stack_slots_num = slot_num - 1;
3882 ira_free (spilled_coalesced_allocnos);
3883 /* Sort regnos according the slot numbers. */
3884 regno_max_ref_width = reg_max_ref_width;
3885 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
3886 FOR_EACH_ALLOCNO (a, ai)
3887 ALLOCNO_ADD_DATA (a) = NULL;
3888 ira_free (allocno_coalesce_data);
3889 ira_free (regno_coalesced_allocno_num);
3890 ira_free (regno_coalesced_allocno_cost);
3895 /* This page contains code used by the reload pass to improve the
3896 final code. */
3898 /* The function is called from reload to mark changes in the
3899 allocation of REGNO made by the reload. Remember that reg_renumber
3900 reflects the change result. */
3901 void
3902 ira_mark_allocation_change (int regno)
3904 ira_allocno_t a = ira_regno_allocno_map[regno];
3905 int old_hard_regno, hard_regno, cost;
3906 enum reg_class aclass = ALLOCNO_CLASS (a);
3908 ira_assert (a != NULL);
3909 hard_regno = reg_renumber[regno];
3910 if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
3911 return;
3912 if (old_hard_regno < 0)
3913 cost = -ALLOCNO_MEMORY_COST (a);
3914 else
3916 ira_assert (ira_class_hard_reg_index[aclass][old_hard_regno] >= 0);
3917 cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
3918 ? ALLOCNO_CLASS_COST (a)
3919 : ALLOCNO_HARD_REG_COSTS (a)
3920 [ira_class_hard_reg_index[aclass][old_hard_regno]]);
3921 update_copy_costs (a, false);
3923 ira_overall_cost -= cost;
3924 ALLOCNO_HARD_REGNO (a) = hard_regno;
3925 if (hard_regno < 0)
3927 ALLOCNO_HARD_REGNO (a) = -1;
3928 cost += ALLOCNO_MEMORY_COST (a);
3930 else if (ira_class_hard_reg_index[aclass][hard_regno] >= 0)
3932 cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
3933 ? ALLOCNO_CLASS_COST (a)
3934 : ALLOCNO_HARD_REG_COSTS (a)
3935 [ira_class_hard_reg_index[aclass][hard_regno]]);
3936 update_copy_costs (a, true);
3938 else
3939 /* Reload changed class of the allocno. */
3940 cost = 0;
3941 ira_overall_cost += cost;
3944 /* This function is called when reload deletes memory-memory move. In
3945 this case we marks that the allocation of the corresponding
3946 allocnos should be not changed in future. Otherwise we risk to get
3947 a wrong code. */
3948 void
3949 ira_mark_memory_move_deletion (int dst_regno, int src_regno)
3951 ira_allocno_t dst = ira_regno_allocno_map[dst_regno];
3952 ira_allocno_t src = ira_regno_allocno_map[src_regno];
3954 ira_assert (dst != NULL && src != NULL
3955 && ALLOCNO_HARD_REGNO (dst) < 0
3956 && ALLOCNO_HARD_REGNO (src) < 0);
3957 ALLOCNO_DONT_REASSIGN_P (dst) = true;
3958 ALLOCNO_DONT_REASSIGN_P (src) = true;
3961 /* Try to assign a hard register (except for FORBIDDEN_REGS) to
3962 allocno A and return TRUE in the case of success. */
3963 static bool
3964 allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
3966 int hard_regno;
3967 enum reg_class aclass;
3968 int regno = ALLOCNO_REGNO (a);
3969 HARD_REG_SET saved[2];
3970 int i, n;
3972 n = ALLOCNO_NUM_OBJECTS (a);
3973 for (i = 0; i < n; i++)
3975 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3976 COPY_HARD_REG_SET (saved[i], OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
3977 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), forbidden_regs);
3978 if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
3979 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
3980 call_used_reg_set);
3982 ALLOCNO_ASSIGNED_P (a) = false;
3983 aclass = ALLOCNO_CLASS (a);
3984 update_curr_costs (a);
3985 assign_hard_reg (a, true);
3986 hard_regno = ALLOCNO_HARD_REGNO (a);
3987 reg_renumber[regno] = hard_regno;
3988 if (hard_regno < 0)
3989 ALLOCNO_HARD_REGNO (a) = -1;
3990 else
3992 ira_assert (ira_class_hard_reg_index[aclass][hard_regno] >= 0);
3993 ira_overall_cost
3994 -= (ALLOCNO_MEMORY_COST (a)
3995 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
3996 ? ALLOCNO_CLASS_COST (a)
3997 : ALLOCNO_HARD_REG_COSTS (a)[ira_class_hard_reg_index
3998 [aclass][hard_regno]]));
3999 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
4000 && ira_hard_reg_set_intersection_p (hard_regno, ALLOCNO_MODE (a),
4001 call_used_reg_set))
4003 ira_assert (flag_caller_saves);
4004 caller_save_needed = 1;
4008 /* If we found a hard register, modify the RTL for the pseudo
4009 register to show the hard register, and mark the pseudo register
4010 live. */
4011 if (reg_renumber[regno] >= 0)
4013 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4014 fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
4015 SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
4016 mark_home_live (regno);
4018 else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4019 fprintf (ira_dump_file, "\n");
4020 for (i = 0; i < n; i++)
4022 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4023 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), saved[i]);
4025 return reg_renumber[regno] >= 0;
4028 /* Sort pseudos according their usage frequencies (putting most
4029 frequently ones first). */
4030 static int
4031 pseudo_reg_compare (const void *v1p, const void *v2p)
4033 int regno1 = *(const int *) v1p;
4034 int regno2 = *(const int *) v2p;
4035 int diff;
4037 if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
4038 return diff;
4039 return regno1 - regno2;
4042 /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
4043 NUM of them) or spilled pseudos conflicting with pseudos in
4044 SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
4045 allocation has been changed. The function doesn't use
4046 BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
4047 PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
4048 is called by the reload pass at the end of each reload
4049 iteration. */
4050 bool
4051 ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
4052 HARD_REG_SET bad_spill_regs,
4053 HARD_REG_SET *pseudo_forbidden_regs,
4054 HARD_REG_SET *pseudo_previous_regs,
4055 bitmap spilled)
4057 int i, n, regno;
4058 bool changed_p;
4059 ira_allocno_t a;
4060 HARD_REG_SET forbidden_regs;
4061 bitmap temp = BITMAP_ALLOC (NULL);
4063 /* Add pseudos which conflict with pseudos already in
4064 SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
4065 to allocating in two steps as some of the conflicts might have
4066 a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
4067 for (i = 0; i < num; i++)
4068 bitmap_set_bit (temp, spilled_pseudo_regs[i]);
4070 for (i = 0, n = num; i < n; i++)
4072 int nr, j;
4073 int regno = spilled_pseudo_regs[i];
4074 bitmap_set_bit (temp, regno);
4076 a = ira_regno_allocno_map[regno];
4077 nr = ALLOCNO_NUM_OBJECTS (a);
4078 for (j = 0; j < nr; j++)
4080 ira_object_t conflict_obj;
4081 ira_object_t obj = ALLOCNO_OBJECT (a, j);
4082 ira_object_conflict_iterator oci;
4084 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
4086 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
4087 if (ALLOCNO_HARD_REGNO (conflict_a) < 0
4088 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
4089 && bitmap_set_bit (temp, ALLOCNO_REGNO (conflict_a)))
4091 spilled_pseudo_regs[num++] = ALLOCNO_REGNO (conflict_a);
4092 /* ?!? This seems wrong. */
4093 bitmap_set_bit (consideration_allocno_bitmap,
4094 ALLOCNO_NUM (conflict_a));
4100 if (num > 1)
4101 qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
4102 changed_p = false;
4103 /* Try to assign hard registers to pseudos from
4104 SPILLED_PSEUDO_REGS. */
4105 for (i = 0; i < num; i++)
4107 regno = spilled_pseudo_regs[i];
4108 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
4109 IOR_HARD_REG_SET (forbidden_regs, pseudo_forbidden_regs[regno]);
4110 IOR_HARD_REG_SET (forbidden_regs, pseudo_previous_regs[regno]);
4111 gcc_assert (reg_renumber[regno] < 0);
4112 a = ira_regno_allocno_map[regno];
4113 ira_mark_allocation_change (regno);
4114 ira_assert (reg_renumber[regno] < 0);
4115 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4116 fprintf (ira_dump_file,
4117 " Try Assign %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
4118 ALLOCNO_MEMORY_COST (a)
4119 - ALLOCNO_CLASS_COST (a));
4120 allocno_reload_assign (a, forbidden_regs);
4121 if (reg_renumber[regno] >= 0)
4123 CLEAR_REGNO_REG_SET (spilled, regno);
4124 changed_p = true;
4127 BITMAP_FREE (temp);
4128 return changed_p;
4131 /* The function is called by reload and returns already allocated
4132 stack slot (if any) for REGNO with given INHERENT_SIZE and
4133 TOTAL_SIZE. In the case of failure to find a slot which can be
4134 used for REGNO, the function returns NULL. */
4136 ira_reuse_stack_slot (int regno, unsigned int inherent_size,
4137 unsigned int total_size)
4139 unsigned int i;
4140 int slot_num, best_slot_num;
4141 int cost, best_cost;
4142 ira_copy_t cp, next_cp;
4143 ira_allocno_t another_allocno, allocno = ira_regno_allocno_map[regno];
4144 rtx x;
4145 bitmap_iterator bi;
4146 struct ira_spilled_reg_stack_slot *slot = NULL;
4148 ira_assert (inherent_size == PSEUDO_REGNO_BYTES (regno)
4149 && inherent_size <= total_size
4150 && ALLOCNO_HARD_REGNO (allocno) < 0);
4151 if (! flag_ira_share_spill_slots)
4152 return NULL_RTX;
4153 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4154 if (slot_num != -1)
4156 slot = &ira_spilled_reg_stack_slots[slot_num];
4157 x = slot->mem;
4159 else
4161 best_cost = best_slot_num = -1;
4162 x = NULL_RTX;
4163 /* It means that the pseudo was spilled in the reload pass, try
4164 to reuse a slot. */
4165 for (slot_num = 0;
4166 slot_num < ira_spilled_reg_stack_slots_num;
4167 slot_num++)
4169 slot = &ira_spilled_reg_stack_slots[slot_num];
4170 if (slot->mem == NULL_RTX)
4171 continue;
4172 if (slot->width < total_size
4173 || GET_MODE_SIZE (GET_MODE (slot->mem)) < inherent_size)
4174 continue;
4176 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4177 FIRST_PSEUDO_REGISTER, i, bi)
4179 another_allocno = ira_regno_allocno_map[i];
4180 if (allocnos_conflict_by_live_ranges_p (allocno,
4181 another_allocno))
4182 goto cont;
4184 for (cost = 0, cp = ALLOCNO_COPIES (allocno);
4185 cp != NULL;
4186 cp = next_cp)
4188 if (cp->first == allocno)
4190 next_cp = cp->next_first_allocno_copy;
4191 another_allocno = cp->second;
4193 else if (cp->second == allocno)
4195 next_cp = cp->next_second_allocno_copy;
4196 another_allocno = cp->first;
4198 else
4199 gcc_unreachable ();
4200 if (cp->insn == NULL_RTX)
4201 continue;
4202 if (bitmap_bit_p (&slot->spilled_regs,
4203 ALLOCNO_REGNO (another_allocno)))
4204 cost += cp->freq;
4206 if (cost > best_cost)
4208 best_cost = cost;
4209 best_slot_num = slot_num;
4211 cont:
4214 if (best_cost >= 0)
4216 slot_num = best_slot_num;
4217 slot = &ira_spilled_reg_stack_slots[slot_num];
4218 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4219 x = slot->mem;
4220 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4223 if (x != NULL_RTX)
4225 ira_assert (slot->width >= total_size);
4226 #ifdef ENABLE_IRA_CHECKING
4227 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4228 FIRST_PSEUDO_REGISTER, i, bi)
4230 ira_assert (! conflict_by_live_ranges_p (regno, i));
4232 #endif
4233 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4234 if (internal_flag_ira_verbose > 3 && ira_dump_file)
4236 fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
4237 regno, REG_FREQ (regno), slot_num);
4238 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4239 FIRST_PSEUDO_REGISTER, i, bi)
4241 if ((unsigned) regno != i)
4242 fprintf (ira_dump_file, " %d", i);
4244 fprintf (ira_dump_file, "\n");
4247 return x;
4250 /* This is called by reload every time a new stack slot X with
4251 TOTAL_SIZE was allocated for REGNO. We store this info for
4252 subsequent ira_reuse_stack_slot calls. */
4253 void
4254 ira_mark_new_stack_slot (rtx x, int regno, unsigned int total_size)
4256 struct ira_spilled_reg_stack_slot *slot;
4257 int slot_num;
4258 ira_allocno_t allocno;
4260 ira_assert (PSEUDO_REGNO_BYTES (regno) <= total_size);
4261 allocno = ira_regno_allocno_map[regno];
4262 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4263 if (slot_num == -1)
4265 slot_num = ira_spilled_reg_stack_slots_num++;
4266 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4268 slot = &ira_spilled_reg_stack_slots[slot_num];
4269 INIT_REG_SET (&slot->spilled_regs);
4270 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4271 slot->mem = x;
4272 slot->width = total_size;
4273 if (internal_flag_ira_verbose > 3 && ira_dump_file)
4274 fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
4275 regno, REG_FREQ (regno), slot_num);
4279 /* Return spill cost for pseudo-registers whose numbers are in array
4280 REGNOS (with a negative number as an end marker) for reload with
4281 given IN and OUT for INSN. Return also number points (through
4282 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
4283 the register pressure is high, number of references of the
4284 pseudo-registers (through NREFS), number of callee-clobbered
4285 hard-registers occupied by the pseudo-registers (through
4286 CALL_USED_COUNT), and the first hard regno occupied by the
4287 pseudo-registers (through FIRST_HARD_REGNO). */
4288 static int
4289 calculate_spill_cost (int *regnos, rtx in, rtx out, rtx insn,
4290 int *excess_pressure_live_length,
4291 int *nrefs, int *call_used_count, int *first_hard_regno)
4293 int i, cost, regno, hard_regno, j, count, saved_cost, nregs;
4294 bool in_p, out_p;
4295 int length;
4296 ira_allocno_t a;
4298 *nrefs = 0;
4299 for (length = count = cost = i = 0;; i++)
4301 regno = regnos[i];
4302 if (regno < 0)
4303 break;
4304 *nrefs += REG_N_REFS (regno);
4305 hard_regno = reg_renumber[regno];
4306 ira_assert (hard_regno >= 0);
4307 a = ira_regno_allocno_map[regno];
4308 length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) / ALLOCNO_NUM_OBJECTS (a);
4309 cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
4310 nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
4311 for (j = 0; j < nregs; j++)
4312 if (! TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + j))
4313 break;
4314 if (j == nregs)
4315 count++;
4316 in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
4317 out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
4318 if ((in_p || out_p)
4319 && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
4321 saved_cost = 0;
4322 if (in_p)
4323 saved_cost += ira_memory_move_cost
4324 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][1];
4325 if (out_p)
4326 saved_cost
4327 += ira_memory_move_cost
4328 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][0];
4329 cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
4332 *excess_pressure_live_length = length;
4333 *call_used_count = count;
4334 hard_regno = -1;
4335 if (regnos[0] >= 0)
4337 hard_regno = reg_renumber[regnos[0]];
4339 *first_hard_regno = hard_regno;
4340 return cost;
4343 /* Return TRUE if spilling pseudo-registers whose numbers are in array
4344 REGNOS is better than spilling pseudo-registers with numbers in
4345 OTHER_REGNOS for reload with given IN and OUT for INSN. The
4346 function used by the reload pass to make better register spilling
4347 decisions. */
4348 bool
4349 ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
4350 rtx in, rtx out, rtx insn)
4352 int cost, other_cost;
4353 int length, other_length;
4354 int nrefs, other_nrefs;
4355 int call_used_count, other_call_used_count;
4356 int hard_regno, other_hard_regno;
4358 cost = calculate_spill_cost (regnos, in, out, insn,
4359 &length, &nrefs, &call_used_count, &hard_regno);
4360 other_cost = calculate_spill_cost (other_regnos, in, out, insn,
4361 &other_length, &other_nrefs,
4362 &other_call_used_count,
4363 &other_hard_regno);
4364 if (nrefs == 0 && other_nrefs != 0)
4365 return true;
4366 if (nrefs != 0 && other_nrefs == 0)
4367 return false;
4368 if (cost != other_cost)
4369 return cost < other_cost;
4370 if (length != other_length)
4371 return length > other_length;
4372 #ifdef REG_ALLOC_ORDER
4373 if (hard_regno >= 0 && other_hard_regno >= 0)
4374 return (inv_reg_alloc_order[hard_regno]
4375 < inv_reg_alloc_order[other_hard_regno]);
4376 #else
4377 if (call_used_count != other_call_used_count)
4378 return call_used_count > other_call_used_count;
4379 #endif
4380 return false;
4385 /* Allocate and initialize data necessary for assign_hard_reg. */
4386 void
4387 ira_initiate_assign (void)
4389 sorted_allocnos
4390 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4391 * ira_allocnos_num);
4392 consideration_allocno_bitmap = ira_allocate_bitmap ();
4393 initiate_cost_update ();
4394 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4397 /* Deallocate data used by assign_hard_reg. */
4398 void
4399 ira_finish_assign (void)
4401 ira_free (sorted_allocnos);
4402 ira_free_bitmap (consideration_allocno_bitmap);
4403 finish_cost_update ();
4404 ira_free (allocno_priorities);
4409 /* Entry function doing color-based register allocation. */
4410 static void
4411 color (void)
4413 allocno_stack_vec = VEC_alloc (ira_allocno_t, heap, ira_allocnos_num);
4414 memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
4415 ira_initiate_assign ();
4416 do_coloring ();
4417 ira_finish_assign ();
4418 VEC_free (ira_allocno_t, heap, allocno_stack_vec);
4419 move_spill_restore ();
4424 /* This page contains a simple register allocator without usage of
4425 allocno conflicts. This is used for fast allocation for -O0. */
4427 /* Do register allocation by not using allocno conflicts. It uses
4428 only allocno live ranges. The algorithm is close to Chow's
4429 priority coloring. */
4430 static void
4431 fast_allocation (void)
4433 int i, j, k, num, class_size, hard_regno;
4434 #ifdef STACK_REGS
4435 bool no_stack_reg_p;
4436 #endif
4437 enum reg_class aclass;
4438 enum machine_mode mode;
4439 ira_allocno_t a;
4440 ira_allocno_iterator ai;
4441 live_range_t r;
4442 HARD_REG_SET conflict_hard_regs, *used_hard_regs;
4444 sorted_allocnos = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4445 * ira_allocnos_num);
4446 num = 0;
4447 FOR_EACH_ALLOCNO (a, ai)
4448 sorted_allocnos[num++] = a;
4449 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4450 setup_allocno_priorities (sorted_allocnos, num);
4451 used_hard_regs = (HARD_REG_SET *) ira_allocate (sizeof (HARD_REG_SET)
4452 * ira_max_point);
4453 for (i = 0; i < ira_max_point; i++)
4454 CLEAR_HARD_REG_SET (used_hard_regs[i]);
4455 qsort (sorted_allocnos, num, sizeof (ira_allocno_t),
4456 allocno_priority_compare_func);
4457 for (i = 0; i < num; i++)
4459 int nr, l;
4461 a = sorted_allocnos[i];
4462 nr = ALLOCNO_NUM_OBJECTS (a);
4463 CLEAR_HARD_REG_SET (conflict_hard_regs);
4464 for (l = 0; l < nr; l++)
4466 ira_object_t obj = ALLOCNO_OBJECT (a, l);
4467 IOR_HARD_REG_SET (conflict_hard_regs,
4468 OBJECT_CONFLICT_HARD_REGS (obj));
4469 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4470 for (j = r->start; j <= r->finish; j++)
4471 IOR_HARD_REG_SET (conflict_hard_regs, used_hard_regs[j]);
4473 aclass = ALLOCNO_CLASS (a);
4474 ALLOCNO_ASSIGNED_P (a) = true;
4475 ALLOCNO_HARD_REGNO (a) = -1;
4476 if (hard_reg_set_subset_p (reg_class_contents[aclass],
4477 conflict_hard_regs))
4478 continue;
4479 mode = ALLOCNO_MODE (a);
4480 #ifdef STACK_REGS
4481 no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
4482 #endif
4483 class_size = ira_class_hard_regs_num[aclass];
4484 for (j = 0; j < class_size; j++)
4486 hard_regno = ira_class_hard_regs[aclass][j];
4487 #ifdef STACK_REGS
4488 if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
4489 && hard_regno <= LAST_STACK_REG)
4490 continue;
4491 #endif
4492 if (ira_hard_reg_set_intersection_p (hard_regno, mode, conflict_hard_regs)
4493 || (TEST_HARD_REG_BIT
4494 (ira_prohibited_class_mode_regs[aclass][mode], hard_regno)))
4495 continue;
4496 ALLOCNO_HARD_REGNO (a) = hard_regno;
4497 for (l = 0; l < nr; l++)
4499 ira_object_t obj = ALLOCNO_OBJECT (a, l);
4500 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4501 for (k = r->start; k <= r->finish; k++)
4502 IOR_HARD_REG_SET (used_hard_regs[k],
4503 ira_reg_mode_hard_regset[hard_regno][mode]);
4505 break;
4508 ira_free (sorted_allocnos);
4509 ira_free (used_hard_regs);
4510 ira_free (allocno_priorities);
4511 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
4512 ira_print_disposition (ira_dump_file);
4517 /* Entry function doing coloring. */
4518 void
4519 ira_color (void)
4521 ira_allocno_t a;
4522 ira_allocno_iterator ai;
4524 /* Setup updated costs. */
4525 FOR_EACH_ALLOCNO (a, ai)
4527 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
4528 ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
4530 if (ira_conflicts_p)
4531 color ();
4532 else
4533 fast_allocation ();