2011-03-29 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ira-color.c
blobc29367010ff5a3cea2e6d11b97c2040734e29e9d
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 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1062 a = ira_allocnos[i];
1063 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS)
1064 continue;
1065 mode = ALLOCNO_MODE (a);
1066 nobj = ALLOCNO_NUM_OBJECTS (a);
1067 for (k = 0; k < nobj; k++)
1069 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1070 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
1072 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL
1073 && ALLOCNO_CLASS_COST (a) > ALLOCNO_MEMORY_COST (a))
1074 CLEAR_HARD_REG_SET (obj_data->profitable_hard_regs);
1075 else
1077 COPY_HARD_REG_SET (obj_data->profitable_hard_regs,
1078 reg_class_contents[aclass]);
1079 AND_COMPL_HARD_REG_SET
1080 (obj_data->profitable_hard_regs,
1081 ira_prohibited_class_mode_regs[aclass][mode]);
1082 AND_COMPL_HARD_REG_SET (obj_data->profitable_hard_regs,
1083 ira_no_alloc_regs);
1084 AND_COMPL_HARD_REG_SET (obj_data->profitable_hard_regs,
1085 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
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 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1129 int min_cost = INT_MAX;
1130 int *costs;
1132 a = ira_allocnos[i];
1133 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1134 || empty_profitable_hard_regs (a))
1135 continue;
1136 mode = ALLOCNO_MODE (a);
1137 nobj = ALLOCNO_NUM_OBJECTS (a);
1138 for (k = 0; k < nobj; k++)
1140 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1141 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
1143 if ((costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a)) != NULL
1144 || (costs = ALLOCNO_HARD_REG_COSTS (a)) != NULL)
1146 class_size = ira_class_hard_regs_num[aclass];
1147 for (j = 0; j < class_size; j++)
1149 hard_regno = ira_class_hard_regs[aclass][j];
1150 nregs = hard_regno_nregs[hard_regno][mode];
1151 if (nregs == nobj && nregs > 1)
1153 int num = OBJECT_SUBWORD (obj);
1155 if (WORDS_BIG_ENDIAN)
1156 hard_regno += nobj - num - 1;
1157 else
1158 hard_regno += num;
1160 if (! TEST_HARD_REG_BIT (obj_data->profitable_hard_regs,
1161 hard_regno))
1162 continue;
1163 if (ALLOCNO_UPDATED_MEMORY_COST (a) < costs[j])
1164 CLEAR_HARD_REG_BIT (obj_data->profitable_hard_regs,
1165 hard_regno);
1166 else if (min_cost > costs[j])
1167 min_cost = costs[j];
1170 else if (ALLOCNO_UPDATED_MEMORY_COST (a)
1171 < ALLOCNO_UPDATED_CLASS_COST (a))
1172 CLEAR_HARD_REG_SET (obj_data->profitable_hard_regs);
1174 if (ALLOCNO_UPDATED_CLASS_COST (a) > min_cost)
1175 ALLOCNO_UPDATED_CLASS_COST (a) = min_cost;
1181 /* This page contains functions used to choose hard registers for
1182 allocnos. */
1184 /* Array whose element value is TRUE if the corresponding hard
1185 register was already allocated for an allocno. */
1186 static bool allocated_hardreg_p[FIRST_PSEUDO_REGISTER];
1188 /* Describes one element in a queue of allocnos whose costs need to be
1189 updated. Each allocno in the queue is known to have an allocno
1190 class. */
1191 struct update_cost_queue_elem
1193 /* This element is in the queue iff CHECK == update_cost_check. */
1194 int check;
1196 /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
1197 connecting this allocno to the one being allocated. */
1198 int divisor;
1200 /* The next allocno in the queue, or null if this is the last element. */
1201 ira_allocno_t next;
1204 /* The first element in a queue of allocnos whose copy costs need to be
1205 updated. Null if the queue is empty. */
1206 static ira_allocno_t update_cost_queue;
1208 /* The last element in the queue described by update_cost_queue.
1209 Not valid if update_cost_queue is null. */
1210 static struct update_cost_queue_elem *update_cost_queue_tail;
1212 /* A pool of elements in the queue described by update_cost_queue.
1213 Elements are indexed by ALLOCNO_NUM. */
1214 static struct update_cost_queue_elem *update_cost_queue_elems;
1216 /* The current value of update_copy_cost call count. */
1217 static int update_cost_check;
1219 /* Allocate and initialize data necessary for function
1220 update_copy_costs. */
1221 static void
1222 initiate_cost_update (void)
1224 size_t size;
1226 size = ira_allocnos_num * sizeof (struct update_cost_queue_elem);
1227 update_cost_queue_elems
1228 = (struct update_cost_queue_elem *) ira_allocate (size);
1229 memset (update_cost_queue_elems, 0, size);
1230 update_cost_check = 0;
1233 /* Deallocate data used by function update_copy_costs. */
1234 static void
1235 finish_cost_update (void)
1237 ira_free (update_cost_queue_elems);
1240 /* When we traverse allocnos to update hard register costs, the cost
1241 divisor will be multiplied by the following macro value for each
1242 hop from given allocno to directly connected allocnos. */
1243 #define COST_HOP_DIVISOR 4
1245 /* Start a new cost-updating pass. */
1246 static void
1247 start_update_cost (void)
1249 update_cost_check++;
1250 update_cost_queue = NULL;
1253 /* Add (ALLOCNO, DIVISOR) to the end of update_cost_queue, unless
1254 ALLOCNO is already in the queue, or has NO_REGS class. */
1255 static inline void
1256 queue_update_cost (ira_allocno_t allocno, int divisor)
1258 struct update_cost_queue_elem *elem;
1260 elem = &update_cost_queue_elems[ALLOCNO_NUM (allocno)];
1261 if (elem->check != update_cost_check
1262 && ALLOCNO_CLASS (allocno) != NO_REGS)
1264 elem->check = update_cost_check;
1265 elem->divisor = divisor;
1266 elem->next = NULL;
1267 if (update_cost_queue == NULL)
1268 update_cost_queue = allocno;
1269 else
1270 update_cost_queue_tail->next = allocno;
1271 update_cost_queue_tail = elem;
1275 /* Try to remove the first element from update_cost_queue. Return false
1276 if the queue was empty, otherwise make (*ALLOCNO, *DIVISOR) describe
1277 the removed element. */
1278 static inline bool
1279 get_next_update_cost (ira_allocno_t *allocno, int *divisor)
1281 struct update_cost_queue_elem *elem;
1283 if (update_cost_queue == NULL)
1284 return false;
1286 *allocno = update_cost_queue;
1287 elem = &update_cost_queue_elems[ALLOCNO_NUM (*allocno)];
1288 *divisor = elem->divisor;
1289 update_cost_queue = elem->next;
1290 return true;
1293 /* Update the cost of allocnos to increase chances to remove some
1294 copies as the result of subsequent assignment. */
1295 static void
1296 update_copy_costs (ira_allocno_t allocno, bool decr_p)
1298 int i, cost, update_cost, hard_regno, divisor;
1299 enum machine_mode mode;
1300 enum reg_class rclass, aclass;
1301 ira_allocno_t another_allocno;
1302 ira_copy_t cp, next_cp;
1304 hard_regno = ALLOCNO_HARD_REGNO (allocno);
1305 ira_assert (hard_regno >= 0);
1307 aclass = ALLOCNO_CLASS (allocno);
1308 if (aclass == NO_REGS)
1309 return;
1310 i = ira_class_hard_reg_index[aclass][hard_regno];
1311 ira_assert (i >= 0);
1312 rclass = REGNO_REG_CLASS (hard_regno);
1314 start_update_cost ();
1315 divisor = 1;
1318 mode = ALLOCNO_MODE (allocno);
1319 ira_init_register_move_cost_if_necessary (mode);
1320 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1322 if (cp->first == allocno)
1324 next_cp = cp->next_first_allocno_copy;
1325 another_allocno = cp->second;
1327 else if (cp->second == allocno)
1329 next_cp = cp->next_second_allocno_copy;
1330 another_allocno = cp->first;
1332 else
1333 gcc_unreachable ();
1335 aclass = ALLOCNO_CLASS (another_allocno);
1336 if (! TEST_HARD_REG_BIT (reg_class_contents[aclass],
1337 hard_regno)
1338 || ALLOCNO_ASSIGNED_P (another_allocno))
1339 continue;
1341 cost = (cp->second == allocno
1342 ? ira_register_move_cost[mode][rclass][aclass]
1343 : ira_register_move_cost[mode][aclass][rclass]);
1344 if (decr_p)
1345 cost = -cost;
1347 update_cost = cp->freq * cost / divisor;
1348 if (update_cost == 0)
1349 continue;
1351 ira_allocate_and_set_or_copy_costs
1352 (&ALLOCNO_UPDATED_HARD_REG_COSTS (another_allocno), aclass,
1353 ALLOCNO_UPDATED_CLASS_COST (another_allocno),
1354 ALLOCNO_HARD_REG_COSTS (another_allocno));
1355 ira_allocate_and_set_or_copy_costs
1356 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
1357 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
1358 i = ira_class_hard_reg_index[aclass][hard_regno];
1359 if (i < 0)
1360 continue;
1361 ALLOCNO_UPDATED_HARD_REG_COSTS (another_allocno)[i] += update_cost;
1362 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno)[i]
1363 += update_cost;
1365 queue_update_cost (another_allocno, divisor * COST_HOP_DIVISOR);
1368 while (get_next_update_cost (&allocno, &divisor));
1371 /* This function updates COSTS (decrease if DECR_P) for hard_registers
1372 of ACLASS by conflict costs of the unassigned allocnos
1373 connected by copies with allocnos in update_cost_queue. This
1374 update increases chances to remove some copies. */
1375 static void
1376 update_conflict_hard_regno_costs (int *costs, enum reg_class aclass,
1377 bool decr_p)
1379 int i, cost, class_size, freq, mult, div, divisor;
1380 int index, hard_regno;
1381 int *conflict_costs;
1382 bool cont_p;
1383 enum reg_class another_aclass;
1384 ira_allocno_t allocno, another_allocno;
1385 ira_copy_t cp, next_cp;
1387 while (get_next_update_cost (&allocno, &divisor))
1388 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1390 if (cp->first == allocno)
1392 next_cp = cp->next_first_allocno_copy;
1393 another_allocno = cp->second;
1395 else if (cp->second == allocno)
1397 next_cp = cp->next_second_allocno_copy;
1398 another_allocno = cp->first;
1400 else
1401 gcc_unreachable ();
1402 another_aclass = ALLOCNO_CLASS (another_allocno);
1403 if (! ira_reg_classes_intersect_p[aclass][another_aclass]
1404 || ALLOCNO_ASSIGNED_P (another_allocno)
1405 || ALLOCNO_COLOR_DATA (another_allocno)->may_be_spilled_p)
1406 continue;
1407 class_size = ira_class_hard_regs_num[another_aclass];
1408 ira_allocate_and_copy_costs
1409 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
1410 another_aclass, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
1411 conflict_costs
1412 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno);
1413 if (conflict_costs == NULL)
1414 cont_p = true;
1415 else
1417 mult = cp->freq;
1418 freq = ALLOCNO_FREQ (another_allocno);
1419 if (freq == 0)
1420 freq = 1;
1421 div = freq * divisor;
1422 cont_p = false;
1423 for (i = class_size - 1; i >= 0; i--)
1425 hard_regno = ira_class_hard_regs[another_aclass][i];
1426 ira_assert (hard_regno >= 0);
1427 index = ira_class_hard_reg_index[aclass][hard_regno];
1428 if (index < 0)
1429 continue;
1430 cost = conflict_costs [i] * mult / div;
1431 if (cost == 0)
1432 continue;
1433 cont_p = true;
1434 if (decr_p)
1435 cost = -cost;
1436 costs[index] += cost;
1439 /* Probably 5 hops will be enough. */
1440 if (cont_p
1441 && divisor <= (COST_HOP_DIVISOR
1442 * COST_HOP_DIVISOR
1443 * COST_HOP_DIVISOR
1444 * COST_HOP_DIVISOR))
1445 queue_update_cost (another_allocno, divisor * COST_HOP_DIVISOR);
1449 /* Set up conflicting and profitable regs (through CONFLICT_REGS and
1450 PROFITABLE_REGS) for each object of allocno A. Remember that the
1451 profitable regs exclude hard regs which can not hold value of mode
1452 of allocno A. */
1453 static inline void
1454 setup_conflict_profitable_regs (ira_allocno_t a, bool retry_p,
1455 HARD_REG_SET *conflict_regs,
1456 HARD_REG_SET *profitable_regs)
1458 int i, nwords;
1459 ira_object_t obj;
1461 nwords = ALLOCNO_NUM_OBJECTS (a);
1462 for (i = 0; i < nwords; i++)
1464 obj = ALLOCNO_OBJECT (a, i);
1465 COPY_HARD_REG_SET (conflict_regs[i],
1466 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
1467 if (retry_p)
1469 COPY_HARD_REG_SET (profitable_regs[i],
1470 reg_class_contents[ALLOCNO_CLASS (a)]);
1471 AND_COMPL_HARD_REG_SET (profitable_regs[i],
1472 ira_prohibited_class_mode_regs
1473 [ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
1475 else
1476 COPY_HARD_REG_SET (profitable_regs[i],
1477 OBJECT_COLOR_DATA (obj)->profitable_hard_regs);
1481 /* Return true if HARD_REGNO is ok for assigning to allocno A whose
1482 objects have corresponding CONFLICT_REGS and PROFITABLE_REGS. */
1483 static inline bool
1484 check_hard_reg_p (ira_allocno_t a, int hard_regno,
1485 HARD_REG_SET *conflict_regs, HARD_REG_SET *profitable_regs)
1487 int j, nwords, nregs;
1489 nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
1490 nwords = ALLOCNO_NUM_OBJECTS (a);
1491 for (j = 0; j < nregs; j++)
1493 int k;
1494 int set_to_test_start = 0, set_to_test_end = nwords;
1496 if (nregs == nwords)
1498 if (WORDS_BIG_ENDIAN)
1499 set_to_test_start = nwords - j - 1;
1500 else
1501 set_to_test_start = j;
1502 set_to_test_end = set_to_test_start + 1;
1504 for (k = set_to_test_start; k < set_to_test_end; k++)
1505 /* Checking only profitable hard regs. */
1506 if (TEST_HARD_REG_BIT (conflict_regs[k], hard_regno + j)
1507 || ! TEST_HARD_REG_BIT (profitable_regs[k], hard_regno + j))
1508 break;
1509 if (k != set_to_test_end)
1510 break;
1512 return j == nregs;
1515 /* Choose a hard register for allocno A. If RETRY_P is TRUE, it means
1516 that the function called from function
1517 `ira_reassign_conflict_allocnos' and `allocno_reload_assign'. In
1518 this case some allocno data are not defined or updated and we
1519 should not touch these data. The function returns true if we
1520 managed to assign a hard register to the allocno.
1522 To assign a hard register, first of all we calculate all conflict
1523 hard registers which can come from conflicting allocnos with
1524 already assigned hard registers. After that we find first free
1525 hard register with the minimal cost. During hard register cost
1526 calculation we take conflict hard register costs into account to
1527 give a chance for conflicting allocnos to get a better hard
1528 register in the future.
1530 If the best hard register cost is bigger than cost of memory usage
1531 for the allocno, we don't assign a hard register to given allocno
1532 at all.
1534 If we assign a hard register to the allocno, we update costs of the
1535 hard register for allocnos connected by copies to improve a chance
1536 to coalesce insns represented by the copies when we assign hard
1537 registers to the allocnos connected by the copies. */
1538 static bool
1539 assign_hard_reg (ira_allocno_t a, bool retry_p)
1541 HARD_REG_SET conflicting_regs[2], profitable_hard_regs[2];
1542 int i, j, hard_regno, best_hard_regno, class_size;
1543 int cost, mem_cost, min_cost, full_cost, min_full_cost, nwords, word;
1544 int *a_costs;
1545 enum reg_class aclass;
1546 enum machine_mode mode;
1547 static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
1548 #ifndef HONOR_REG_ALLOC_ORDER
1549 enum reg_class rclass;
1550 int add_cost;
1551 #endif
1552 #ifdef STACK_REGS
1553 bool no_stack_reg_p;
1554 #endif
1556 ira_assert (! ALLOCNO_ASSIGNED_P (a));
1557 setup_conflict_profitable_regs (a, retry_p,
1558 conflicting_regs, profitable_hard_regs);
1559 aclass = ALLOCNO_CLASS (a);
1560 class_size = ira_class_hard_regs_num[aclass];
1561 best_hard_regno = -1;
1562 memset (full_costs, 0, sizeof (int) * class_size);
1563 mem_cost = 0;
1564 memset (costs, 0, sizeof (int) * class_size);
1565 memset (full_costs, 0, sizeof (int) * class_size);
1566 #ifdef STACK_REGS
1567 no_stack_reg_p = false;
1568 #endif
1569 if (! retry_p)
1570 start_update_cost ();
1571 mem_cost += ALLOCNO_UPDATED_MEMORY_COST (a);
1573 ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
1574 aclass, ALLOCNO_HARD_REG_COSTS (a));
1575 a_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
1576 #ifdef STACK_REGS
1577 no_stack_reg_p = no_stack_reg_p || ALLOCNO_TOTAL_NO_STACK_REG_P (a);
1578 #endif
1579 cost = ALLOCNO_UPDATED_CLASS_COST (a);
1580 for (i = 0; i < class_size; i++)
1581 if (a_costs != NULL)
1583 costs[i] += a_costs[i];
1584 full_costs[i] += a_costs[i];
1586 else
1588 costs[i] += cost;
1589 full_costs[i] += cost;
1591 nwords = ALLOCNO_NUM_OBJECTS (a);
1592 for (word = 0; word < nwords; word++)
1594 ira_object_t conflict_obj;
1595 ira_object_t obj = ALLOCNO_OBJECT (a, word);
1596 ira_object_conflict_iterator oci;
1598 /* Take preferences of conflicting allocnos into account. */
1599 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1601 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1602 enum reg_class conflict_aclass;
1604 /* Reload can give another class so we need to check all
1605 allocnos. */
1606 if (!retry_p
1607 && (!bitmap_bit_p (consideration_allocno_bitmap,
1608 ALLOCNO_NUM (conflict_a))
1609 || ((!ALLOCNO_ASSIGNED_P (conflict_a)
1610 || ALLOCNO_HARD_REGNO (conflict_a) < 0)
1611 && !(hard_reg_set_intersect_p
1612 (profitable_hard_regs[word],
1613 OBJECT_COLOR_DATA
1614 (conflict_obj)->profitable_hard_regs)))))
1615 continue;
1616 conflict_aclass = ALLOCNO_CLASS (conflict_a);
1617 ira_assert (ira_reg_classes_intersect_p
1618 [aclass][conflict_aclass]);
1619 if (ALLOCNO_ASSIGNED_P (conflict_a))
1621 hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
1622 if (hard_regno >= 0
1623 && ira_class_hard_reg_index[aclass][hard_regno] >= 0)
1625 enum machine_mode mode = ALLOCNO_MODE (conflict_a);
1626 int conflict_nregs = hard_regno_nregs[hard_regno][mode];
1627 int n_objects = ALLOCNO_NUM_OBJECTS (conflict_a);
1629 if (conflict_nregs == n_objects && conflict_nregs > 1)
1631 int num = OBJECT_SUBWORD (conflict_obj);
1633 if (WORDS_BIG_ENDIAN)
1634 SET_HARD_REG_BIT (conflicting_regs[word],
1635 hard_regno + n_objects - num - 1);
1636 else
1637 SET_HARD_REG_BIT (conflicting_regs[word],
1638 hard_regno + num);
1640 else
1641 IOR_HARD_REG_SET
1642 (conflicting_regs[word],
1643 ira_reg_mode_hard_regset[hard_regno][mode]);
1644 if (hard_reg_set_subset_p (profitable_hard_regs[word],
1645 conflicting_regs[word]))
1646 goto fail;
1649 else if (! retry_p
1650 && ! ALLOCNO_COLOR_DATA (conflict_a)->may_be_spilled_p)
1652 int k, *conflict_costs;
1654 ira_allocate_and_copy_costs
1655 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a),
1656 conflict_aclass,
1657 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_a));
1658 conflict_costs
1659 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a);
1660 if (conflict_costs != NULL)
1661 for (j = class_size - 1; j >= 0; j--)
1663 hard_regno = ira_class_hard_regs[aclass][j];
1664 ira_assert (hard_regno >= 0);
1665 k = ira_class_hard_reg_index[conflict_aclass][hard_regno];
1666 if (k < 0)
1667 continue;
1668 full_costs[j] -= conflict_costs[k];
1670 queue_update_cost (conflict_a, COST_HOP_DIVISOR);
1674 if (! retry_p)
1675 /* Take into account preferences of allocnos connected by copies to
1676 the conflict allocnos. */
1677 update_conflict_hard_regno_costs (full_costs, aclass, true);
1679 /* Take preferences of allocnos connected by copies into
1680 account. */
1681 if (! retry_p)
1683 start_update_cost ();
1684 queue_update_cost (a, COST_HOP_DIVISOR);
1685 update_conflict_hard_regno_costs (full_costs, aclass, false);
1687 min_cost = min_full_cost = INT_MAX;
1689 /* We don't care about giving callee saved registers to allocnos no
1690 living through calls because call clobbered registers are
1691 allocated first (it is usual practice to put them first in
1692 REG_ALLOC_ORDER). */
1693 mode = ALLOCNO_MODE (a);
1694 for (i = 0; i < class_size; i++)
1696 hard_regno = ira_class_hard_regs[aclass][i];
1697 #ifdef STACK_REGS
1698 if (no_stack_reg_p
1699 && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
1700 continue;
1701 #endif
1702 if (! check_hard_reg_p (a, hard_regno,
1703 conflicting_regs, profitable_hard_regs))
1704 continue;
1705 cost = costs[i];
1706 full_cost = full_costs[i];
1707 #ifndef HONOR_REG_ALLOC_ORDER
1708 if (! allocated_hardreg_p[hard_regno]
1709 && ira_hard_reg_not_in_set_p (hard_regno, mode, call_used_reg_set)
1710 && !LOCAL_REGNO (hard_regno))
1711 /* We need to save/restore the hard register in
1712 epilogue/prologue. Therefore we increase the cost. */
1714 /* ??? If only part is call clobbered. */
1715 rclass = REGNO_REG_CLASS (hard_regno);
1716 add_cost = (ira_memory_move_cost[mode][rclass][0]
1717 + ira_memory_move_cost[mode][rclass][1] - 1);
1718 cost += add_cost;
1719 full_cost += add_cost;
1721 #endif
1722 if (min_cost > cost)
1723 min_cost = cost;
1724 if (min_full_cost > full_cost)
1726 min_full_cost = full_cost;
1727 best_hard_regno = hard_regno;
1728 ira_assert (hard_regno >= 0);
1731 if (min_full_cost > mem_cost)
1733 if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1734 fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
1735 mem_cost, min_full_cost);
1736 best_hard_regno = -1;
1738 fail:
1739 if (best_hard_regno >= 0)
1740 allocated_hardreg_p[best_hard_regno] = true;
1741 ALLOCNO_HARD_REGNO (a) = best_hard_regno;
1742 ALLOCNO_ASSIGNED_P (a) = true;
1743 if (best_hard_regno >= 0)
1744 update_copy_costs (a, true);
1745 ira_assert (ALLOCNO_CLASS (a) == aclass);
1746 /* We don't need updated costs anymore: */
1747 ira_free_allocno_updated_costs (a);
1748 return best_hard_regno >= 0;
1753 /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
1755 /* Bucket of allocnos that can colored currently without spilling. */
1756 static ira_allocno_t colorable_allocno_bucket;
1758 /* Bucket of allocnos that might be not colored currently without
1759 spilling. */
1760 static ira_allocno_t uncolorable_allocno_bucket;
1762 /* The current number of allocnos in the uncolorable_bucket. */
1763 static int uncolorable_allocnos_num;
1765 /* Return the current spill priority of allocno A. The less the
1766 number, the more preferable the allocno for spilling. */
1767 static inline int
1768 allocno_spill_priority (ira_allocno_t a)
1770 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
1772 return (data->temp
1773 / (ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
1774 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
1775 + 1));
1778 /* Add allocno A to bucket *BUCKET_PTR. A should be not in a bucket
1779 before the call. */
1780 static void
1781 add_allocno_to_bucket (ira_allocno_t a, ira_allocno_t *bucket_ptr)
1783 ira_allocno_t first_a;
1784 allocno_color_data_t data;
1786 if (bucket_ptr == &uncolorable_allocno_bucket
1787 && ALLOCNO_CLASS (a) != NO_REGS)
1789 uncolorable_allocnos_num++;
1790 ira_assert (uncolorable_allocnos_num > 0);
1792 first_a = *bucket_ptr;
1793 data = ALLOCNO_COLOR_DATA (a);
1794 data->next_bucket_allocno = first_a;
1795 data->prev_bucket_allocno = NULL;
1796 if (first_a != NULL)
1797 ALLOCNO_COLOR_DATA (first_a)->prev_bucket_allocno = a;
1798 *bucket_ptr = a;
1801 /* Compare two allocnos to define which allocno should be pushed first
1802 into the coloring stack. If the return is a negative number, the
1803 allocno given by the first parameter will be pushed first. In this
1804 case such allocno has less priority than the second one and the
1805 hard register will be assigned to it after assignment to the second
1806 one. As the result of such assignment order, the second allocno
1807 has a better chance to get the best hard register. */
1808 static int
1809 bucket_allocno_compare_func (const void *v1p, const void *v2p)
1811 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
1812 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
1813 int diff, a1_freq, a2_freq, a1_num, a2_num;
1815 if ((diff = (int) ALLOCNO_CLASS (a2) - ALLOCNO_CLASS (a1)) != 0)
1816 return diff;
1817 a1_freq = ALLOCNO_FREQ (a1);
1818 a2_freq = ALLOCNO_FREQ (a2);
1819 if ((diff = a1_freq - a2_freq) != 0)
1820 return diff;
1821 a1_num = ALLOCNO_COLOR_DATA (a1)->available_regs_num;
1822 a2_num = ALLOCNO_COLOR_DATA (a2)->available_regs_num;
1823 if ((diff = a2_num - a1_num) != 0)
1824 return diff;
1825 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
1828 /* Sort bucket *BUCKET_PTR and return the result through
1829 BUCKET_PTR. */
1830 static void
1831 sort_bucket (ira_allocno_t *bucket_ptr,
1832 int (*compare_func) (const void *, const void *))
1834 ira_allocno_t a, head;
1835 int n;
1837 for (n = 0, a = *bucket_ptr;
1838 a != NULL;
1839 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
1840 sorted_allocnos[n++] = a;
1841 if (n <= 1)
1842 return;
1843 qsort (sorted_allocnos, n, sizeof (ira_allocno_t), compare_func);
1844 head = NULL;
1845 for (n--; n >= 0; n--)
1847 a = sorted_allocnos[n];
1848 ALLOCNO_COLOR_DATA (a)->next_bucket_allocno = head;
1849 ALLOCNO_COLOR_DATA (a)->prev_bucket_allocno = NULL;
1850 if (head != NULL)
1851 ALLOCNO_COLOR_DATA (head)->prev_bucket_allocno = a;
1852 head = a;
1854 *bucket_ptr = head;
1857 /* Add ALLOCNO to bucket *BUCKET_PTR maintaining the order according
1858 their priority. ALLOCNO should be not in a bucket before the
1859 call. */
1860 static void
1861 add_allocno_to_ordered_bucket (ira_allocno_t allocno,
1862 ira_allocno_t *bucket_ptr)
1864 ira_allocno_t before, after;
1866 if (bucket_ptr == &uncolorable_allocno_bucket
1867 && ALLOCNO_CLASS (allocno) != NO_REGS)
1869 uncolorable_allocnos_num++;
1870 ira_assert (uncolorable_allocnos_num > 0);
1872 for (before = *bucket_ptr, after = NULL;
1873 before != NULL;
1874 after = before,
1875 before = ALLOCNO_COLOR_DATA (before)->next_bucket_allocno)
1876 if (bucket_allocno_compare_func (&allocno, &before) < 0)
1877 break;
1878 ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno = before;
1879 ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno = after;
1880 if (after == NULL)
1881 *bucket_ptr = allocno;
1882 else
1883 ALLOCNO_COLOR_DATA (after)->next_bucket_allocno = allocno;
1884 if (before != NULL)
1885 ALLOCNO_COLOR_DATA (before)->prev_bucket_allocno = allocno;
1888 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
1889 the call. */
1890 static void
1891 delete_allocno_from_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
1893 ira_allocno_t prev_allocno, next_allocno;
1895 if (bucket_ptr == &uncolorable_allocno_bucket
1896 && ALLOCNO_CLASS (allocno) != NO_REGS)
1898 uncolorable_allocnos_num--;
1899 ira_assert (uncolorable_allocnos_num >= 0);
1901 prev_allocno = ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno;
1902 next_allocno = ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno;
1903 if (prev_allocno != NULL)
1904 ALLOCNO_COLOR_DATA (prev_allocno)->next_bucket_allocno = next_allocno;
1905 else
1907 ira_assert (*bucket_ptr == allocno);
1908 *bucket_ptr = next_allocno;
1910 if (next_allocno != NULL)
1911 ALLOCNO_COLOR_DATA (next_allocno)->prev_bucket_allocno = prev_allocno;
1914 /* Put allocno A onto the coloring stack without removing it from its
1915 bucket. Pushing allocno to the coloring stack can result in moving
1916 conflicting allocnos from the uncolorable bucket to the colorable
1917 one. */
1918 static void
1919 push_allocno_to_stack (ira_allocno_t a)
1921 enum reg_class aclass;
1922 allocno_color_data_t data, conflict_data;
1923 int size, i, n = ALLOCNO_NUM_OBJECTS (a);
1925 data = ALLOCNO_COLOR_DATA (a);
1926 data->in_graph_p = false;
1927 VEC_safe_push (ira_allocno_t, heap, allocno_stack_vec, a);
1928 aclass = ALLOCNO_CLASS (a);
1929 if (aclass == NO_REGS)
1930 return;
1931 size = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
1932 if (n > 1)
1934 /* We will deal with the subwords individually. */
1935 gcc_assert (size == ALLOCNO_NUM_OBJECTS (a));
1936 size = 1;
1938 for (i = 0; i < n; i++)
1940 ira_object_t obj = ALLOCNO_OBJECT (a, i);
1941 ira_object_t conflict_obj;
1942 ira_object_conflict_iterator oci;
1944 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1946 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1948 conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
1949 if (conflict_data->colorable_p
1950 || ! conflict_data->in_graph_p
1951 || ALLOCNO_ASSIGNED_P (conflict_a)
1952 || !(hard_reg_set_intersect_p
1953 (OBJECT_COLOR_DATA (obj)->profitable_hard_regs,
1954 OBJECT_COLOR_DATA (conflict_obj)->profitable_hard_regs)))
1955 continue;
1956 ira_assert (bitmap_bit_p (coloring_allocno_bitmap,
1957 ALLOCNO_NUM (conflict_a)));
1958 if (update_left_conflict_sizes_p (conflict_a, obj, size))
1960 delete_allocno_from_bucket
1961 (conflict_a, &uncolorable_allocno_bucket);
1962 add_allocno_to_ordered_bucket
1963 (conflict_a, &colorable_allocno_bucket);
1964 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
1966 fprintf (ira_dump_file, " Making");
1967 ira_print_expanded_allocno (conflict_a);
1968 fprintf (ira_dump_file, " colorable\n");
1976 /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
1977 The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
1978 static void
1979 remove_allocno_from_bucket_and_push (ira_allocno_t allocno, bool colorable_p)
1981 if (colorable_p)
1982 delete_allocno_from_bucket (allocno, &colorable_allocno_bucket);
1983 else
1984 delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
1985 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1987 fprintf (ira_dump_file, " Pushing");
1988 ira_print_expanded_allocno (allocno);
1989 if (colorable_p)
1990 fprintf (ira_dump_file, "(cost %d)\n",
1991 ALLOCNO_COLOR_DATA (allocno)->temp);
1992 else
1993 fprintf (ira_dump_file, "(potential spill: %spri=%d, cost=%d)\n",
1994 ALLOCNO_BAD_SPILL_P (allocno) ? "bad spill, " : "",
1995 allocno_spill_priority (allocno),
1996 ALLOCNO_COLOR_DATA (allocno)->temp);
1998 if (! colorable_p)
1999 ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p = true;
2000 push_allocno_to_stack (allocno);
2003 /* Put all allocnos from colorable bucket onto the coloring stack. */
2004 static void
2005 push_only_colorable (void)
2007 sort_bucket (&colorable_allocno_bucket, bucket_allocno_compare_func);
2008 for (;colorable_allocno_bucket != NULL;)
2009 remove_allocno_from_bucket_and_push (colorable_allocno_bucket, true);
2012 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
2013 loop given by its LOOP_NODE. */
2015 ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
2017 int freq, i;
2018 edge_iterator ei;
2019 edge e;
2020 VEC (edge, heap) *edges;
2022 ira_assert (loop_node->loop != NULL
2023 && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
2024 freq = 0;
2025 if (! exit_p)
2027 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
2028 if (e->src != loop_node->loop->latch
2029 && (regno < 0
2030 || (bitmap_bit_p (DF_LR_OUT (e->src), regno)
2031 && bitmap_bit_p (DF_LR_IN (e->dest), regno))))
2032 freq += EDGE_FREQUENCY (e);
2034 else
2036 edges = get_loop_exit_edges (loop_node->loop);
2037 FOR_EACH_VEC_ELT (edge, edges, i, e)
2038 if (regno < 0
2039 || (bitmap_bit_p (DF_LR_OUT (e->src), regno)
2040 && bitmap_bit_p (DF_LR_IN (e->dest), regno)))
2041 freq += EDGE_FREQUENCY (e);
2042 VEC_free (edge, heap, edges);
2045 return REG_FREQ_FROM_EDGE_FREQ (freq);
2048 /* Calculate and return the cost of putting allocno A into memory. */
2049 static int
2050 calculate_allocno_spill_cost (ira_allocno_t a)
2052 int regno, cost;
2053 enum machine_mode mode;
2054 enum reg_class rclass;
2055 ira_allocno_t parent_allocno;
2056 ira_loop_tree_node_t parent_node, loop_node;
2058 regno = ALLOCNO_REGNO (a);
2059 cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_UPDATED_CLASS_COST (a);
2060 if (ALLOCNO_CAP (a) != NULL)
2061 return cost;
2062 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
2063 if ((parent_node = loop_node->parent) == NULL)
2064 return cost;
2065 if ((parent_allocno = parent_node->regno_allocno_map[regno]) == NULL)
2066 return cost;
2067 mode = ALLOCNO_MODE (a);
2068 rclass = ALLOCNO_CLASS (a);
2069 if (ALLOCNO_HARD_REGNO (parent_allocno) < 0)
2070 cost -= (ira_memory_move_cost[mode][rclass][0]
2071 * ira_loop_edge_freq (loop_node, regno, true)
2072 + ira_memory_move_cost[mode][rclass][1]
2073 * ira_loop_edge_freq (loop_node, regno, false));
2074 else
2076 ira_init_register_move_cost_if_necessary (mode);
2077 cost += ((ira_memory_move_cost[mode][rclass][1]
2078 * ira_loop_edge_freq (loop_node, regno, true)
2079 + ira_memory_move_cost[mode][rclass][0]
2080 * ira_loop_edge_freq (loop_node, regno, false))
2081 - (ira_register_move_cost[mode][rclass][rclass]
2082 * (ira_loop_edge_freq (loop_node, regno, false)
2083 + ira_loop_edge_freq (loop_node, regno, true))));
2085 return cost;
2088 /* Used for sorting allocnos for spilling. */
2089 static inline int
2090 allocno_spill_priority_compare (ira_allocno_t a1, ira_allocno_t a2)
2092 int pri1, pri2, diff;
2094 if (ALLOCNO_BAD_SPILL_P (a1) && ! ALLOCNO_BAD_SPILL_P (a2))
2095 return 1;
2096 if (ALLOCNO_BAD_SPILL_P (a2) && ! ALLOCNO_BAD_SPILL_P (a1))
2097 return -1;
2098 pri1 = allocno_spill_priority (a1);
2099 pri2 = allocno_spill_priority (a2);
2100 if ((diff = pri1 - pri2) != 0)
2101 return diff;
2102 if ((diff
2103 = ALLOCNO_COLOR_DATA (a1)->temp - ALLOCNO_COLOR_DATA (a2)->temp) != 0)
2104 return diff;
2105 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2108 /* Used for sorting allocnos for spilling. */
2109 static int
2110 allocno_spill_sort_compare (const void *v1p, const void *v2p)
2112 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2113 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2115 return allocno_spill_priority_compare (p1, p2);
2118 /* Push allocnos to the coloring stack. The order of allocnos in the
2119 stack defines the order for the subsequent coloring. */
2120 static void
2121 push_allocnos_to_stack (void)
2123 ira_allocno_t a;
2124 int cost;
2126 /* Calculate uncolorable allocno spill costs. */
2127 for (a = uncolorable_allocno_bucket;
2128 a != NULL;
2129 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2130 if (ALLOCNO_CLASS (a) != NO_REGS)
2132 cost = calculate_allocno_spill_cost (a);
2133 /* ??? Remove cost of copies between the coalesced
2134 allocnos. */
2135 ALLOCNO_COLOR_DATA (a)->temp = cost;
2137 sort_bucket (&uncolorable_allocno_bucket, allocno_spill_sort_compare);
2138 for (;;)
2140 push_only_colorable ();
2141 a = uncolorable_allocno_bucket;
2142 if (a == NULL)
2143 break;
2144 remove_allocno_from_bucket_and_push (a, false);
2146 ira_assert (colorable_allocno_bucket == NULL
2147 && uncolorable_allocno_bucket == NULL);
2148 ira_assert (uncolorable_allocnos_num == 0);
2151 /* Pop the coloring stack and assign hard registers to the popped
2152 allocnos. */
2153 static void
2154 pop_allocnos_from_stack (void)
2156 ira_allocno_t allocno;
2157 enum reg_class aclass;
2159 for (;VEC_length (ira_allocno_t, allocno_stack_vec) != 0;)
2161 allocno = VEC_pop (ira_allocno_t, allocno_stack_vec);
2162 aclass = ALLOCNO_CLASS (allocno);
2163 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2165 fprintf (ira_dump_file, " Popping");
2166 ira_print_expanded_allocno (allocno);
2167 fprintf (ira_dump_file, " -- ");
2169 if (aclass == NO_REGS)
2171 ALLOCNO_HARD_REGNO (allocno) = -1;
2172 ALLOCNO_ASSIGNED_P (allocno) = true;
2173 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
2174 ira_assert
2175 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
2176 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2177 fprintf (ira_dump_file, "assign memory\n");
2179 else if (assign_hard_reg (allocno, false))
2181 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2182 fprintf (ira_dump_file, "assign reg %d\n",
2183 ALLOCNO_HARD_REGNO (allocno));
2185 else if (ALLOCNO_ASSIGNED_P (allocno))
2187 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2188 fprintf (ira_dump_file, "spill\n");
2190 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2194 /* Set up number of available hard registers for allocno A. */
2195 static void
2196 setup_allocno_available_regs_num (ira_allocno_t a)
2198 int i, j, n, hard_regno, hard_regs_num, nwords, nregs;
2199 enum reg_class aclass;
2200 enum machine_mode mode;
2201 allocno_color_data_t data;
2203 aclass = ALLOCNO_CLASS (a);
2204 data = ALLOCNO_COLOR_DATA (a);
2205 data->available_regs_num = 0;
2206 if (aclass == NO_REGS)
2207 return;
2208 hard_regs_num = ira_class_hard_regs_num[aclass];
2209 mode = ALLOCNO_MODE (a);
2210 nwords = ALLOCNO_NUM_OBJECTS (a);
2211 for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
2213 hard_regno = ira_class_hard_regs[aclass][i];
2214 nregs = hard_regno_nregs[hard_regno][mode];
2215 for (j = 0; j < nregs; j++)
2217 int k;
2218 int set_to_test_start = 0, set_to_test_end = nwords;
2220 if (nregs == nwords)
2222 if (WORDS_BIG_ENDIAN)
2223 set_to_test_start = nwords - j - 1;
2224 else
2225 set_to_test_start = j;
2226 set_to_test_end = set_to_test_start + 1;
2228 for (k = set_to_test_start; k < set_to_test_end; k++)
2230 ira_object_t obj = ALLOCNO_OBJECT (a, k);
2231 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
2233 /* Checking only profitable hard regs. */
2234 if (TEST_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2235 hard_regno + j)
2236 || ! TEST_HARD_REG_BIT (obj_data->profitable_hard_regs,
2237 hard_regno + j))
2238 break;
2240 if (k != set_to_test_end)
2241 break;
2243 if (j == nregs)
2244 n++;
2246 data->available_regs_num = n;
2247 if (internal_flag_ira_verbose <= 2 || ira_dump_file == NULL)
2248 return;
2249 fprintf
2250 (ira_dump_file,
2251 " Allocno a%dr%d of %s(%d) has %d avail. regs",
2252 ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2253 reg_class_names[aclass], ira_class_hard_regs_num[aclass], n);
2254 for (i = 0; i < nwords; i++)
2256 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2257 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
2259 if (nwords != 1)
2261 if (i != 0)
2262 fprintf (ira_dump_file, ", ");
2263 fprintf (ira_dump_file, " obj %d", i);
2265 print_hard_reg_set (ira_dump_file, obj_data->profitable_hard_regs, false);
2266 fprintf (ira_dump_file, " (confl regs = ");
2267 print_hard_reg_set (ira_dump_file, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2268 false);
2269 fprintf (ira_dump_file, " ) %snode: ",
2270 hard_reg_set_equal_p (obj_data->profitable_hard_regs,
2271 obj_data->hard_regs_node->hard_regs->set)
2272 ? "" : "^");
2273 print_hard_reg_set (ira_dump_file,
2274 obj_data->hard_regs_node->hard_regs->set, false);
2277 fprintf (ira_dump_file, "\n");
2280 /* Put ALLOCNO in a bucket corresponding to its number and size of its
2281 conflicting allocnos and hard registers. */
2282 static void
2283 put_allocno_into_bucket (ira_allocno_t allocno)
2285 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2286 setup_allocno_available_regs_num (allocno);
2287 if (setup_left_conflict_sizes_p (allocno))
2288 add_allocno_to_bucket (allocno, &colorable_allocno_bucket);
2289 else
2290 add_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
2293 /* Map: allocno number -> allocno priority. */
2294 static int *allocno_priorities;
2296 /* Set up priorities for N allocnos in array
2297 CONSIDERATION_ALLOCNOS. */
2298 static void
2299 setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
2301 int i, length, nrefs, priority, max_priority, mult;
2302 ira_allocno_t a;
2304 max_priority = 0;
2305 for (i = 0; i < n; i++)
2307 a = consideration_allocnos[i];
2308 nrefs = ALLOCNO_NREFS (a);
2309 ira_assert (nrefs >= 0);
2310 mult = floor_log2 (ALLOCNO_NREFS (a)) + 1;
2311 ira_assert (mult >= 0);
2312 allocno_priorities[ALLOCNO_NUM (a)]
2313 = priority
2314 = (mult
2315 * (ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a))
2316 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
2317 if (priority < 0)
2318 priority = -priority;
2319 if (max_priority < priority)
2320 max_priority = priority;
2322 mult = max_priority == 0 ? 1 : INT_MAX / max_priority;
2323 for (i = 0; i < n; i++)
2325 a = consideration_allocnos[i];
2326 length = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2327 if (ALLOCNO_NUM_OBJECTS (a) > 1)
2328 length /= ALLOCNO_NUM_OBJECTS (a);
2329 if (length <= 0)
2330 length = 1;
2331 allocno_priorities[ALLOCNO_NUM (a)]
2332 = allocno_priorities[ALLOCNO_NUM (a)] * mult / length;
2336 /* Sort allocnos according to the profit of usage of a hard register
2337 instead of memory for them. */
2338 static int
2339 allocno_cost_compare_func (const void *v1p, const void *v2p)
2341 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2342 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2343 int c1, c2;
2345 c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_UPDATED_CLASS_COST (p1);
2346 c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_UPDATED_CLASS_COST (p2);
2347 if (c1 - c2)
2348 return c1 - c2;
2350 /* If regs are equally good, sort by allocno numbers, so that the
2351 results of qsort leave nothing to chance. */
2352 return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
2355 /* We used Chaitin-Briggs coloring to assign as many pseudos as
2356 possible to hard registers. Let us try to improve allocation with
2357 cost point of view. This function improves the allocation by
2358 spilling some allocnos and assigning the freed hard registers to
2359 other allocnos if it decreases the overall allocation cost. */
2360 static void
2361 improve_allocation (void)
2363 unsigned int i;
2364 int j, k, n, hregno, conflict_hregno, base_cost, class_size, word, nwords;
2365 int check, spill_cost, min_cost, nregs, conflict_nregs, r, best;
2366 bool try_p;
2367 enum reg_class aclass;
2368 enum machine_mode mode;
2369 int *allocno_costs;
2370 int costs[FIRST_PSEUDO_REGISTER];
2371 HARD_REG_SET conflicting_regs[2], profitable_hard_regs[2];
2372 ira_allocno_t a;
2373 bitmap_iterator bi;
2375 /* Clear counts used to process conflicting allocnos only once for
2376 each allocno. */
2377 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2378 ALLOCNO_COLOR_DATA (ira_allocnos[i])->temp = 0;
2379 check = n = 0;
2380 /* Process each allocno and try to assign a hard register to it by
2381 spilling some its conflicting allocnos. */
2382 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2384 a = ira_allocnos[i];
2385 ALLOCNO_COLOR_DATA (a)->temp = 0;
2386 if (empty_profitable_hard_regs (a))
2387 continue;
2388 check++;
2389 aclass = ALLOCNO_CLASS (a);
2390 allocno_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
2391 if (allocno_costs == NULL)
2392 allocno_costs = ALLOCNO_HARD_REG_COSTS (a);
2393 if ((hregno = ALLOCNO_HARD_REGNO (a)) < 0)
2394 base_cost = ALLOCNO_UPDATED_MEMORY_COST (a);
2395 else if (allocno_costs == NULL)
2396 /* It means that assigning a hard register is not profitable
2397 (we don't waste memory for hard register costs in this
2398 case). */
2399 continue;
2400 else
2401 base_cost = allocno_costs[ira_class_hard_reg_index[aclass][hregno]];
2402 try_p = false;
2403 setup_conflict_profitable_regs (a, false,
2404 conflicting_regs, profitable_hard_regs);
2405 class_size = ira_class_hard_regs_num[aclass];
2406 /* Set up cost improvement for usage of each profitable hard
2407 register for allocno A. */
2408 for (j = 0; j < class_size; j++)
2410 hregno = ira_class_hard_regs[aclass][j];
2411 if (! check_hard_reg_p (a, hregno,
2412 conflicting_regs, profitable_hard_regs))
2413 continue;
2414 ira_assert (ira_class_hard_reg_index[aclass][hregno] == j);
2415 k = allocno_costs == NULL ? 0 : j;
2416 costs[hregno] = (allocno_costs == NULL
2417 ? ALLOCNO_UPDATED_CLASS_COST (a) : allocno_costs[k]);
2418 costs[hregno] -= base_cost;
2419 if (costs[hregno] < 0)
2420 try_p = true;
2422 if (! try_p)
2423 /* There is no chance to improve the allocation cost by
2424 assigning hard register to allocno A even without spilling
2425 conflicting allocnos. */
2426 continue;
2427 mode = ALLOCNO_MODE (a);
2428 nwords = ALLOCNO_NUM_OBJECTS (a);
2429 /* Process each allocno conflicting with A and update the cost
2430 improvement for profitable hard registers of A. To use a
2431 hard register for A we need to spill some conflicting
2432 allocnos and that creates penalty for the cost
2433 improvement. */
2434 for (word = 0; word < nwords; word++)
2436 ira_object_t conflict_obj;
2437 ira_object_t obj = ALLOCNO_OBJECT (a, word);
2438 ira_object_conflict_iterator oci;
2440 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2442 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2444 if (ALLOCNO_COLOR_DATA (conflict_a)->temp == check)
2445 /* We already processed this conflicting allocno
2446 because we processed earlier another object of the
2447 conflicting allocno. */
2448 continue;
2449 ALLOCNO_COLOR_DATA (conflict_a)->temp = check;
2450 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
2451 continue;
2452 spill_cost = ALLOCNO_UPDATED_MEMORY_COST (conflict_a);
2453 k = (ira_class_hard_reg_index
2454 [ALLOCNO_CLASS (conflict_a)][conflict_hregno]);
2455 ira_assert (k >= 0);
2456 if ((allocno_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (conflict_a))
2457 != NULL)
2458 spill_cost -= allocno_costs[k];
2459 else if ((allocno_costs = ALLOCNO_HARD_REG_COSTS (conflict_a))
2460 != NULL)
2461 spill_cost -= allocno_costs[k];
2462 else
2463 spill_cost -= ALLOCNO_UPDATED_CLASS_COST (conflict_a);
2464 conflict_nregs
2465 = hard_regno_nregs[conflict_hregno][ALLOCNO_MODE (conflict_a)];
2466 for (r = conflict_hregno;
2467 r >= 0 && r + hard_regno_nregs[r][mode] > conflict_hregno;
2468 r--)
2469 if (check_hard_reg_p (a, r,
2470 conflicting_regs, profitable_hard_regs))
2471 costs[r] += spill_cost;
2472 for (r = conflict_hregno + 1;
2473 r < conflict_hregno + conflict_nregs;
2474 r++)
2475 if (check_hard_reg_p (a, r,
2476 conflicting_regs, profitable_hard_regs))
2477 costs[r] += spill_cost;
2480 min_cost = INT_MAX;
2481 best = -1;
2482 /* Now we choose hard register for A which results in highest
2483 allocation cost improvement. */
2484 for (j = 0; j < class_size; j++)
2486 hregno = ira_class_hard_regs[aclass][j];
2487 if (check_hard_reg_p (a, hregno,
2488 conflicting_regs, profitable_hard_regs)
2489 && min_cost > costs[hregno])
2491 best = hregno;
2492 min_cost = costs[hregno];
2495 if (min_cost >= 0)
2496 /* We are in a situation when assigning any hard register to A
2497 by spilling some conflicting allocnos does not improve the
2498 allocation cost. */
2499 continue;
2500 nregs = hard_regno_nregs[best][mode];
2501 /* Now spill conflicting allocnos which contain a hard register
2502 of A when we assign the best chosen hard register to it. */
2503 for (word = 0; word < nwords; word++)
2505 ira_object_t conflict_obj;
2506 ira_object_t obj = ALLOCNO_OBJECT (a, word);
2507 ira_object_conflict_iterator oci;
2509 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2511 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2513 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
2514 continue;
2515 conflict_nregs
2516 = hard_regno_nregs[conflict_hregno][ALLOCNO_MODE (conflict_a)];
2517 if (best + nregs <= conflict_hregno
2518 || conflict_hregno + conflict_nregs <= best)
2519 /* No intersection. */
2520 continue;
2521 ALLOCNO_HARD_REGNO (conflict_a) = -1;
2522 sorted_allocnos[n++] = conflict_a;
2523 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2524 fprintf (ira_dump_file, "Spilling a%dr%d for a%dr%d\n",
2525 ALLOCNO_NUM (conflict_a), ALLOCNO_REGNO (conflict_a),
2526 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2529 /* Assign the best chosen hard register to A. */
2530 ALLOCNO_HARD_REGNO (a) = best;
2531 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2532 fprintf (ira_dump_file, "Assigning %d to a%dr%d\n",
2533 best, ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2535 if (n == 0)
2536 return;
2537 /* We spilled some allocnos to assign their hard registers to other
2538 allocnos. The spilled allocnos are now in array
2539 'sorted_allocnos'. There is still a possibility that some of the
2540 spilled allocnos can get hard registers. So let us try assign
2541 them hard registers again (just a reminder -- function
2542 'assign_hard_reg' assigns hard registers only if it is possible
2543 and profitable). We process the spilled allocnos with biggest
2544 benefit to get hard register first -- see function
2545 'allocno_cost_compare_func'. */
2546 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
2547 allocno_cost_compare_func);
2548 for (j = 0; j < n; j++)
2550 a = sorted_allocnos[j];
2551 ALLOCNO_ASSIGNED_P (a) = false;
2552 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2554 fprintf (ira_dump_file, " ");
2555 ira_print_expanded_allocno (a);
2556 fprintf (ira_dump_file, " -- ");
2558 if (assign_hard_reg (a, false))
2560 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2561 fprintf (ira_dump_file, "assign hard reg %d\n",
2562 ALLOCNO_HARD_REGNO (a));
2564 else
2566 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2567 fprintf (ira_dump_file, "assign memory\n");
2572 /* Sort allocnos according to their priorities which are calculated
2573 analogous to ones in file `global.c'. */
2574 static int
2575 allocno_priority_compare_func (const void *v1p, const void *v2p)
2577 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2578 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2579 int pri1, pri2;
2581 pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
2582 pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
2583 if (pri2 != pri1)
2584 return SORTGT (pri2, pri1);
2586 /* If regs are equally good, sort by allocnos, so that the results of
2587 qsort leave nothing to chance. */
2588 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2591 /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
2592 taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
2593 static void
2594 color_allocnos (void)
2596 unsigned int i, n;
2597 bitmap_iterator bi;
2598 ira_allocno_t a;
2600 setup_profitable_hard_regs ();
2601 if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
2603 n = 0;
2604 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2606 a = ira_allocnos[i];
2607 if (ALLOCNO_CLASS (a) == NO_REGS)
2609 ALLOCNO_HARD_REGNO (a) = -1;
2610 ALLOCNO_ASSIGNED_P (a) = true;
2611 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
2612 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
2613 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2615 fprintf (ira_dump_file, " Spill");
2616 ira_print_expanded_allocno (a);
2617 fprintf (ira_dump_file, "\n");
2619 continue;
2621 sorted_allocnos[n++] = a;
2623 if (n != 0)
2625 setup_allocno_priorities (sorted_allocnos, n);
2626 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
2627 allocno_priority_compare_func);
2628 for (i = 0; i < n; i++)
2630 a = sorted_allocnos[i];
2631 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2633 fprintf (ira_dump_file, " ");
2634 ira_print_expanded_allocno (a);
2635 fprintf (ira_dump_file, " -- ");
2637 if (assign_hard_reg (a, false))
2639 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2640 fprintf (ira_dump_file, "assign hard reg %d\n",
2641 ALLOCNO_HARD_REGNO (a));
2643 else
2645 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2646 fprintf (ira_dump_file, "assign memory\n");
2651 else
2653 form_object_hard_regs_nodes_forest ();
2654 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2655 print_hard_regs_forest (ira_dump_file);
2656 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2658 a = ira_allocnos[i];
2659 if (ALLOCNO_CLASS (a) != NO_REGS && ! empty_profitable_hard_regs (a))
2660 ALLOCNO_COLOR_DATA (a)->in_graph_p = true;
2661 else
2663 ALLOCNO_HARD_REGNO (a) = -1;
2664 ALLOCNO_ASSIGNED_P (a) = true;
2665 /* We don't need updated costs anymore. */
2666 ira_free_allocno_updated_costs (a);
2667 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2669 fprintf (ira_dump_file, " Spill");
2670 ira_print_expanded_allocno (a);
2671 fprintf (ira_dump_file, "\n");
2675 /* Put the allocnos into the corresponding buckets. */
2676 colorable_allocno_bucket = NULL;
2677 uncolorable_allocno_bucket = NULL;
2678 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2680 a = ira_allocnos[i];
2681 if (ALLOCNO_COLOR_DATA (a)->in_graph_p)
2682 put_allocno_into_bucket (a);
2684 push_allocnos_to_stack ();
2685 pop_allocnos_from_stack ();
2686 finish_object_hard_regs_nodes_forest ();
2688 improve_allocation ();
2693 /* Output information about the loop given by its LOOP_TREE_NODE. */
2694 static void
2695 print_loop_title (ira_loop_tree_node_t loop_tree_node)
2697 unsigned int j;
2698 bitmap_iterator bi;
2699 ira_loop_tree_node_t subloop_node, dest_loop_node;
2700 edge e;
2701 edge_iterator ei;
2703 ira_assert (loop_tree_node->loop != NULL);
2704 fprintf (ira_dump_file,
2705 "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
2706 loop_tree_node->loop->num,
2707 (loop_tree_node->parent == NULL
2708 ? -1 : loop_tree_node->parent->loop->num),
2709 loop_tree_node->loop->header->index,
2710 loop_depth (loop_tree_node->loop));
2711 for (subloop_node = loop_tree_node->children;
2712 subloop_node != NULL;
2713 subloop_node = subloop_node->next)
2714 if (subloop_node->bb != NULL)
2716 fprintf (ira_dump_file, " %d", subloop_node->bb->index);
2717 FOR_EACH_EDGE (e, ei, subloop_node->bb->succs)
2718 if (e->dest != EXIT_BLOCK_PTR
2719 && ((dest_loop_node = IRA_BB_NODE (e->dest)->parent)
2720 != loop_tree_node))
2721 fprintf (ira_dump_file, "(->%d:l%d)",
2722 e->dest->index, dest_loop_node->loop->num);
2724 fprintf (ira_dump_file, "\n all:");
2725 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
2726 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
2727 fprintf (ira_dump_file, "\n modified regnos:");
2728 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
2729 fprintf (ira_dump_file, " %d", j);
2730 fprintf (ira_dump_file, "\n border:");
2731 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
2732 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
2733 fprintf (ira_dump_file, "\n Pressure:");
2734 for (j = 0; (int) j < ira_pressure_classes_num; j++)
2736 enum reg_class pclass;
2738 pclass = ira_pressure_classes[j];
2739 if (loop_tree_node->reg_pressure[pclass] == 0)
2740 continue;
2741 fprintf (ira_dump_file, " %s=%d", reg_class_names[pclass],
2742 loop_tree_node->reg_pressure[pclass]);
2744 fprintf (ira_dump_file, "\n");
2747 /* Color the allocnos inside loop (in the extreme case it can be all
2748 of the function) given the corresponding LOOP_TREE_NODE. The
2749 function is called for each loop during top-down traverse of the
2750 loop tree. */
2751 static void
2752 color_pass (ira_loop_tree_node_t loop_tree_node)
2754 int i, regno, hard_regno, index = -1, n, nobj;
2755 int cost, exit_freq, enter_freq;
2756 unsigned int j;
2757 bitmap_iterator bi;
2758 enum machine_mode mode;
2759 enum reg_class rclass, aclass, pclass;
2760 ira_allocno_t a, subloop_allocno;
2761 ira_loop_tree_node_t subloop_node;
2763 ira_assert (loop_tree_node->bb == NULL);
2764 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2765 print_loop_title (loop_tree_node);
2767 bitmap_copy (coloring_allocno_bitmap, loop_tree_node->all_allocnos);
2768 bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
2769 n = nobj = 0;
2770 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2772 a = ira_allocnos[j];
2773 n++;
2774 nobj += ALLOCNO_NUM_OBJECTS (a);
2775 if (! ALLOCNO_ASSIGNED_P (a))
2776 continue;
2777 bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
2779 allocno_color_data
2780 = (allocno_color_data_t) ira_allocate (sizeof (struct allocno_color_data)
2781 * n);
2782 memset (allocno_color_data, 0, sizeof (struct allocno_color_data) * n);
2783 object_color_data
2784 = (object_color_data_t) ira_allocate (sizeof (struct object_color_data)
2785 * nobj);
2786 memset (object_color_data, 0, sizeof (struct object_color_data) * nobj);
2787 n = nobj = 0;
2788 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2790 a = ira_allocnos[j];
2791 ALLOCNO_ADD_DATA (a) = allocno_color_data + n;
2792 n++;
2793 for (i = 0; i < ALLOCNO_NUM_OBJECTS (a); i++)
2795 OBJECT_ADD_DATA (ALLOCNO_OBJECT (a, i)) = object_color_data + nobj;
2796 nobj++;
2799 /* Color all mentioned allocnos including transparent ones. */
2800 color_allocnos ();
2801 /* Process caps. They are processed just once. */
2802 if (flag_ira_region == IRA_REGION_MIXED
2803 || flag_ira_region == IRA_REGION_ALL)
2804 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
2806 a = ira_allocnos[j];
2807 if (ALLOCNO_CAP_MEMBER (a) == NULL)
2808 continue;
2809 /* Remove from processing in the next loop. */
2810 bitmap_clear_bit (consideration_allocno_bitmap, j);
2811 rclass = ALLOCNO_CLASS (a);
2812 pclass = ira_pressure_class_translate[rclass];
2813 if (flag_ira_region == IRA_REGION_MIXED
2814 && (loop_tree_node->reg_pressure[pclass]
2815 <= ira_available_class_regs[pclass]))
2817 mode = ALLOCNO_MODE (a);
2818 hard_regno = ALLOCNO_HARD_REGNO (a);
2819 if (hard_regno >= 0)
2821 index = ira_class_hard_reg_index[rclass][hard_regno];
2822 ira_assert (index >= 0);
2824 regno = ALLOCNO_REGNO (a);
2825 subloop_allocno = ALLOCNO_CAP_MEMBER (a);
2826 subloop_node = ALLOCNO_LOOP_TREE_NODE (subloop_allocno);
2827 ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno));
2828 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
2829 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
2830 if (hard_regno >= 0)
2831 update_copy_costs (subloop_allocno, true);
2832 /* We don't need updated costs anymore: */
2833 ira_free_allocno_updated_costs (subloop_allocno);
2836 /* Update costs of the corresponding allocnos (not caps) in the
2837 subloops. */
2838 for (subloop_node = loop_tree_node->subloops;
2839 subloop_node != NULL;
2840 subloop_node = subloop_node->subloop_next)
2842 ira_assert (subloop_node->bb == NULL);
2843 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2845 a = ira_allocnos[j];
2846 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2847 mode = ALLOCNO_MODE (a);
2848 rclass = ALLOCNO_CLASS (a);
2849 pclass = ira_pressure_class_translate[rclass];
2850 hard_regno = ALLOCNO_HARD_REGNO (a);
2851 /* Use hard register class here. ??? */
2852 if (hard_regno >= 0)
2854 index = ira_class_hard_reg_index[rclass][hard_regno];
2855 ira_assert (index >= 0);
2857 regno = ALLOCNO_REGNO (a);
2858 /* ??? conflict costs */
2859 subloop_allocno = subloop_node->regno_allocno_map[regno];
2860 if (subloop_allocno == NULL
2861 || ALLOCNO_CAP (subloop_allocno) != NULL)
2862 continue;
2863 ira_assert (ALLOCNO_CLASS (subloop_allocno) == rclass);
2864 ira_assert (bitmap_bit_p (subloop_node->all_allocnos,
2865 ALLOCNO_NUM (subloop_allocno)));
2866 if ((flag_ira_region == IRA_REGION_MIXED)
2867 && (loop_tree_node->reg_pressure[pclass]
2868 <= ira_available_class_regs[pclass]))
2870 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
2872 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
2873 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
2874 if (hard_regno >= 0)
2875 update_copy_costs (subloop_allocno, true);
2876 /* We don't need updated costs anymore: */
2877 ira_free_allocno_updated_costs (subloop_allocno);
2879 continue;
2881 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
2882 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
2883 ira_assert (regno < ira_reg_equiv_len);
2884 if (ira_reg_equiv_invariant_p[regno]
2885 || ira_reg_equiv_const[regno] != NULL_RTX)
2887 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
2889 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
2890 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
2891 if (hard_regno >= 0)
2892 update_copy_costs (subloop_allocno, true);
2893 /* We don't need updated costs anymore: */
2894 ira_free_allocno_updated_costs (subloop_allocno);
2897 else if (hard_regno < 0)
2899 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
2900 -= ((ira_memory_move_cost[mode][rclass][1] * enter_freq)
2901 + (ira_memory_move_cost[mode][rclass][0] * exit_freq));
2903 else
2905 aclass = ALLOCNO_CLASS (subloop_allocno);
2906 ira_init_register_move_cost_if_necessary (mode);
2907 cost = (ira_register_move_cost[mode][rclass][rclass]
2908 * (exit_freq + enter_freq));
2909 ira_allocate_and_set_or_copy_costs
2910 (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno), aclass,
2911 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno),
2912 ALLOCNO_HARD_REG_COSTS (subloop_allocno));
2913 ira_allocate_and_set_or_copy_costs
2914 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno),
2915 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno));
2916 ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
2917 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
2918 -= cost;
2919 if (ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
2920 > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index])
2921 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
2922 = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index];
2923 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
2924 += (ira_memory_move_cost[mode][rclass][0] * enter_freq
2925 + ira_memory_move_cost[mode][rclass][1] * exit_freq);
2929 ira_free (object_color_data);
2930 ira_free (allocno_color_data);
2931 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
2933 a = ira_allocnos[j];
2934 ALLOCNO_ADD_DATA (a) = NULL;
2935 for (i = 0; i < ALLOCNO_NUM_OBJECTS (a); i++)
2936 OBJECT_ADD_DATA (a) = NULL;
2940 /* Initialize the common data for coloring and calls functions to do
2941 Chaitin-Briggs and regional coloring. */
2942 static void
2943 do_coloring (void)
2945 coloring_allocno_bitmap = ira_allocate_bitmap ();
2946 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2947 fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
2949 ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
2951 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2952 ira_print_disposition (ira_dump_file);
2954 ira_free_bitmap (coloring_allocno_bitmap);
2959 /* Move spill/restore code, which are to be generated in ira-emit.c,
2960 to less frequent points (if it is profitable) by reassigning some
2961 allocnos (in loop with subloops containing in another loop) to
2962 memory which results in longer live-range where the corresponding
2963 pseudo-registers will be in memory. */
2964 static void
2965 move_spill_restore (void)
2967 int cost, regno, hard_regno, hard_regno2, index;
2968 bool changed_p;
2969 int enter_freq, exit_freq;
2970 enum machine_mode mode;
2971 enum reg_class rclass;
2972 ira_allocno_t a, parent_allocno, subloop_allocno;
2973 ira_loop_tree_node_t parent, loop_node, subloop_node;
2974 ira_allocno_iterator ai;
2976 for (;;)
2978 changed_p = false;
2979 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2980 fprintf (ira_dump_file, "New iteration of spill/restore move\n");
2981 FOR_EACH_ALLOCNO (a, ai)
2983 regno = ALLOCNO_REGNO (a);
2984 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
2985 if (ALLOCNO_CAP_MEMBER (a) != NULL
2986 || ALLOCNO_CAP (a) != NULL
2987 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
2988 || loop_node->children == NULL
2989 /* don't do the optimization because it can create
2990 copies and the reload pass can spill the allocno set
2991 by copy although the allocno will not get memory
2992 slot. */
2993 || ira_reg_equiv_invariant_p[regno]
2994 || ira_reg_equiv_const[regno] != NULL_RTX
2995 || !bitmap_bit_p (loop_node->border_allocnos, ALLOCNO_NUM (a)))
2996 continue;
2997 mode = ALLOCNO_MODE (a);
2998 rclass = ALLOCNO_CLASS (a);
2999 index = ira_class_hard_reg_index[rclass][hard_regno];
3000 ira_assert (index >= 0);
3001 cost = (ALLOCNO_MEMORY_COST (a)
3002 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
3003 ? ALLOCNO_CLASS_COST (a)
3004 : ALLOCNO_HARD_REG_COSTS (a)[index]));
3005 ira_init_register_move_cost_if_necessary (mode);
3006 for (subloop_node = loop_node->subloops;
3007 subloop_node != NULL;
3008 subloop_node = subloop_node->subloop_next)
3010 ira_assert (subloop_node->bb == NULL);
3011 subloop_allocno = subloop_node->regno_allocno_map[regno];
3012 if (subloop_allocno == NULL)
3013 continue;
3014 ira_assert (rclass == ALLOCNO_CLASS (subloop_allocno));
3015 /* We have accumulated cost. To get the real cost of
3016 allocno usage in the loop we should subtract costs of
3017 the subloop allocnos. */
3018 cost -= (ALLOCNO_MEMORY_COST (subloop_allocno)
3019 - (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
3020 ? ALLOCNO_CLASS_COST (subloop_allocno)
3021 : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]));
3022 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
3023 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
3024 if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
3025 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3026 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3027 else
3029 cost
3030 += (ira_memory_move_cost[mode][rclass][0] * exit_freq
3031 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3032 if (hard_regno2 != hard_regno)
3033 cost -= (ira_register_move_cost[mode][rclass][rclass]
3034 * (exit_freq + enter_freq));
3037 if ((parent = loop_node->parent) != NULL
3038 && (parent_allocno = parent->regno_allocno_map[regno]) != NULL)
3040 ira_assert (rclass == ALLOCNO_CLASS (parent_allocno));
3041 exit_freq = ira_loop_edge_freq (loop_node, regno, true);
3042 enter_freq = ira_loop_edge_freq (loop_node, regno, false);
3043 if ((hard_regno2 = ALLOCNO_HARD_REGNO (parent_allocno)) < 0)
3044 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3045 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3046 else
3048 cost
3049 += (ira_memory_move_cost[mode][rclass][1] * exit_freq
3050 + ira_memory_move_cost[mode][rclass][0] * enter_freq);
3051 if (hard_regno2 != hard_regno)
3052 cost -= (ira_register_move_cost[mode][rclass][rclass]
3053 * (exit_freq + enter_freq));
3056 if (cost < 0)
3058 ALLOCNO_HARD_REGNO (a) = -1;
3059 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3061 fprintf
3062 (ira_dump_file,
3063 " Moving spill/restore for a%dr%d up from loop %d",
3064 ALLOCNO_NUM (a), regno, loop_node->loop->num);
3065 fprintf (ira_dump_file, " - profit %d\n", -cost);
3067 changed_p = true;
3070 if (! changed_p)
3071 break;
3077 /* Update current hard reg costs and current conflict hard reg costs
3078 for allocno A. It is done by processing its copies containing
3079 other allocnos already assigned. */
3080 static void
3081 update_curr_costs (ira_allocno_t a)
3083 int i, hard_regno, cost;
3084 enum machine_mode mode;
3085 enum reg_class aclass, rclass;
3086 ira_allocno_t another_a;
3087 ira_copy_t cp, next_cp;
3089 ira_free_allocno_updated_costs (a);
3090 ira_assert (! ALLOCNO_ASSIGNED_P (a));
3091 aclass = ALLOCNO_CLASS (a);
3092 if (aclass == NO_REGS)
3093 return;
3094 mode = ALLOCNO_MODE (a);
3095 ira_init_register_move_cost_if_necessary (mode);
3096 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3098 if (cp->first == a)
3100 next_cp = cp->next_first_allocno_copy;
3101 another_a = cp->second;
3103 else if (cp->second == a)
3105 next_cp = cp->next_second_allocno_copy;
3106 another_a = cp->first;
3108 else
3109 gcc_unreachable ();
3110 if (! ira_reg_classes_intersect_p[aclass][ALLOCNO_CLASS (another_a)]
3111 || ! ALLOCNO_ASSIGNED_P (another_a)
3112 || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
3113 continue;
3114 rclass = REGNO_REG_CLASS (hard_regno);
3115 i = ira_class_hard_reg_index[aclass][hard_regno];
3116 if (i < 0)
3117 continue;
3118 cost = (cp->first == a
3119 ? ira_register_move_cost[mode][rclass][aclass]
3120 : ira_register_move_cost[mode][aclass][rclass]);
3121 ira_allocate_and_set_or_copy_costs
3122 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a),
3123 ALLOCNO_HARD_REG_COSTS (a));
3124 ira_allocate_and_set_or_copy_costs
3125 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
3126 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
3127 ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3128 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3132 /* Try to assign hard registers to the unassigned allocnos and
3133 allocnos conflicting with them or conflicting with allocnos whose
3134 regno >= START_REGNO. The function is called after ira_flattening,
3135 so more allocnos (including ones created in ira-emit.c) will have a
3136 chance to get a hard register. We use simple assignment algorithm
3137 based on priorities. */
3138 void
3139 ira_reassign_conflict_allocnos (int start_regno)
3141 int i, allocnos_to_color_num;
3142 ira_allocno_t a;
3143 enum reg_class aclass;
3144 bitmap allocnos_to_color;
3145 ira_allocno_iterator ai;
3147 allocnos_to_color = ira_allocate_bitmap ();
3148 allocnos_to_color_num = 0;
3149 FOR_EACH_ALLOCNO (a, ai)
3151 int n = ALLOCNO_NUM_OBJECTS (a);
3153 if (! ALLOCNO_ASSIGNED_P (a)
3154 && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
3156 if (ALLOCNO_CLASS (a) != NO_REGS)
3157 sorted_allocnos[allocnos_to_color_num++] = a;
3158 else
3160 ALLOCNO_ASSIGNED_P (a) = true;
3161 ALLOCNO_HARD_REGNO (a) = -1;
3162 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3163 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3165 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
3167 if (ALLOCNO_REGNO (a) < start_regno
3168 || (aclass = ALLOCNO_CLASS (a)) == NO_REGS)
3169 continue;
3170 for (i = 0; i < n; i++)
3172 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3173 ira_object_t conflict_obj;
3174 ira_object_conflict_iterator oci;
3176 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3178 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3180 ira_assert (ira_reg_classes_intersect_p
3181 [aclass][ALLOCNO_CLASS (conflict_a)]);
3182 if (!bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
3183 continue;
3184 sorted_allocnos[allocnos_to_color_num++] = conflict_a;
3188 ira_free_bitmap (allocnos_to_color);
3189 if (allocnos_to_color_num > 1)
3191 setup_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
3192 qsort (sorted_allocnos, allocnos_to_color_num, sizeof (ira_allocno_t),
3193 allocno_priority_compare_func);
3195 for (i = 0; i < allocnos_to_color_num; i++)
3197 a = sorted_allocnos[i];
3198 ALLOCNO_ASSIGNED_P (a) = false;
3199 update_curr_costs (a);
3201 for (i = 0; i < allocnos_to_color_num; i++)
3203 a = sorted_allocnos[i];
3204 if (assign_hard_reg (a, true))
3206 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3207 fprintf
3208 (ira_dump_file,
3209 " Secondary allocation: assign hard reg %d to reg %d\n",
3210 ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
3217 /* This page contains functions used to find conflicts using allocno
3218 live ranges. */
3220 /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
3221 used to find a conflict for new allocnos or allocnos with the
3222 different allocno classes. */
3223 static bool
3224 allocnos_conflict_by_live_ranges_p (ira_allocno_t a1, ira_allocno_t a2)
3226 rtx reg1, reg2;
3227 int i, j;
3228 int n1 = ALLOCNO_NUM_OBJECTS (a1);
3229 int n2 = ALLOCNO_NUM_OBJECTS (a2);
3231 if (a1 == a2)
3232 return false;
3233 reg1 = regno_reg_rtx[ALLOCNO_REGNO (a1)];
3234 reg2 = regno_reg_rtx[ALLOCNO_REGNO (a2)];
3235 if (reg1 != NULL && reg2 != NULL
3236 && ORIGINAL_REGNO (reg1) == ORIGINAL_REGNO (reg2))
3237 return false;
3239 for (i = 0; i < n1; i++)
3241 ira_object_t c1 = ALLOCNO_OBJECT (a1, i);
3243 for (j = 0; j < n2; j++)
3245 ira_object_t c2 = ALLOCNO_OBJECT (a2, j);
3247 if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1),
3248 OBJECT_LIVE_RANGES (c2)))
3249 return true;
3252 return false;
3255 #ifdef ENABLE_IRA_CHECKING
3257 /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
3258 intersect. This should be used when there is only one region.
3259 Currently this is used during reload. */
3260 static bool
3261 conflict_by_live_ranges_p (int regno1, int regno2)
3263 ira_allocno_t a1, a2;
3265 ira_assert (regno1 >= FIRST_PSEUDO_REGISTER
3266 && regno2 >= FIRST_PSEUDO_REGISTER);
3267 /* Reg info caclulated by dataflow infrastructure can be different
3268 from one calculated by regclass. */
3269 if ((a1 = ira_loop_tree_root->regno_allocno_map[regno1]) == NULL
3270 || (a2 = ira_loop_tree_root->regno_allocno_map[regno2]) == NULL)
3271 return false;
3272 return allocnos_conflict_by_live_ranges_p (a1, a2);
3275 #endif
3279 /* This page contains code to coalesce memory stack slots used by
3280 spilled allocnos. This results in smaller stack frame, better data
3281 locality, and in smaller code for some architectures like
3282 x86/x86_64 where insn size depends on address displacement value.
3283 On the other hand, it can worsen insn scheduling after the RA but
3284 in practice it is less important than smaller stack frames. */
3286 /* TRUE if we coalesced some allocnos. In other words, if we got
3287 loops formed by members first_coalesced_allocno and
3288 next_coalesced_allocno containing more one allocno. */
3289 static bool allocno_coalesced_p;
3291 /* Bitmap used to prevent a repeated allocno processing because of
3292 coalescing. */
3293 static bitmap processed_coalesced_allocno_bitmap;
3295 /* See below. */
3296 typedef struct coalesce_data *coalesce_data_t;
3298 /* To decrease footprint of ira_allocno structure we store all data
3299 needed only for coalescing in the following structure. */
3300 struct coalesce_data
3302 /* Coalesced allocnos form a cyclic list. One allocno given by
3303 FIRST represents all coalesced allocnos. The
3304 list is chained by NEXT. */
3305 ira_allocno_t first;
3306 ira_allocno_t next;
3307 int temp;
3310 /* Container for storing allocno data concerning coalescing. */
3311 static coalesce_data_t allocno_coalesce_data;
3313 /* Macro to access the data concerning coalescing. */
3314 #define ALLOCNO_COALESCE_DATA(a) ((coalesce_data_t) ALLOCNO_ADD_DATA (a))
3316 /* The function is used to sort allocnos according to their execution
3317 frequencies. */
3318 static int
3319 copy_freq_compare_func (const void *v1p, const void *v2p)
3321 ira_copy_t cp1 = *(const ira_copy_t *) v1p, cp2 = *(const ira_copy_t *) v2p;
3322 int pri1, pri2;
3324 pri1 = cp1->freq;
3325 pri2 = cp2->freq;
3326 if (pri2 - pri1)
3327 return pri2 - pri1;
3329 /* If freqencies are equal, sort by copies, so that the results of
3330 qsort leave nothing to chance. */
3331 return cp1->num - cp2->num;
3334 /* Merge two sets of coalesced allocnos given correspondingly by
3335 allocnos A1 and A2 (more accurately merging A2 set into A1
3336 set). */
3337 static void
3338 merge_allocnos (ira_allocno_t a1, ira_allocno_t a2)
3340 ira_allocno_t a, first, last, next;
3342 first = ALLOCNO_COALESCE_DATA (a1)->first;
3343 a = ALLOCNO_COALESCE_DATA (a2)->first;
3344 if (first == a)
3345 return;
3346 for (last = a2, a = ALLOCNO_COALESCE_DATA (a2)->next;;
3347 a = ALLOCNO_COALESCE_DATA (a)->next)
3349 ALLOCNO_COALESCE_DATA (a)->first = first;
3350 if (a == a2)
3351 break;
3352 last = a;
3354 next = allocno_coalesce_data[ALLOCNO_NUM (first)].next;
3355 allocno_coalesce_data[ALLOCNO_NUM (first)].next = a2;
3356 allocno_coalesce_data[ALLOCNO_NUM (last)].next = next;
3359 /* Return TRUE if there are conflicting allocnos from two sets of
3360 coalesced allocnos given correspondingly by allocnos A1 and A2. We
3361 use live ranges to find conflicts because conflicts are represented
3362 only for allocnos of the same allocno class and during the reload
3363 pass we coalesce allocnos for sharing stack memory slots. */
3364 static bool
3365 coalesced_allocno_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
3367 ira_allocno_t a, conflict_a;
3369 if (allocno_coalesced_p)
3371 bitmap_clear (processed_coalesced_allocno_bitmap);
3372 for (a = ALLOCNO_COALESCE_DATA (a1)->next;;
3373 a = ALLOCNO_COALESCE_DATA (a)->next)
3375 bitmap_set_bit (processed_coalesced_allocno_bitmap, ALLOCNO_NUM (a));
3376 if (a == a1)
3377 break;
3380 for (a = ALLOCNO_COALESCE_DATA (a2)->next;;
3381 a = ALLOCNO_COALESCE_DATA (a)->next)
3383 for (conflict_a = ALLOCNO_COALESCE_DATA (a1)->next;;
3384 conflict_a = ALLOCNO_COALESCE_DATA (conflict_a)->next)
3386 if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
3387 return true;
3388 if (conflict_a == a1)
3389 break;
3391 if (a == a2)
3392 break;
3394 return false;
3397 /* The major function for aggressive allocno coalescing. We coalesce
3398 only spilled allocnos. If some allocnos have been coalesced, we
3399 set up flag allocno_coalesced_p. */
3400 static void
3401 coalesce_allocnos (void)
3403 ira_allocno_t a;
3404 ira_copy_t cp, next_cp, *sorted_copies;
3405 unsigned int j;
3406 int i, n, cp_num, regno;
3407 bitmap_iterator bi;
3409 sorted_copies = (ira_copy_t *) ira_allocate (ira_copies_num
3410 * sizeof (ira_copy_t));
3411 cp_num = 0;
3412 /* Collect copies. */
3413 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
3415 a = ira_allocnos[j];
3416 regno = ALLOCNO_REGNO (a);
3417 if (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
3418 || (regno < ira_reg_equiv_len
3419 && (ira_reg_equiv_const[regno] != NULL_RTX
3420 || ira_reg_equiv_invariant_p[regno])))
3421 continue;
3422 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3424 if (cp->first == a)
3426 next_cp = cp->next_first_allocno_copy;
3427 regno = ALLOCNO_REGNO (cp->second);
3428 /* For priority coloring we coalesce allocnos only with
3429 the same allocno class not with intersected allocno
3430 classes as it were possible. It is done for
3431 simplicity. */
3432 if ((cp->insn != NULL || cp->constraint_p)
3433 && ALLOCNO_ASSIGNED_P (cp->second)
3434 && ALLOCNO_HARD_REGNO (cp->second) < 0
3435 && (regno >= ira_reg_equiv_len
3436 || (! ira_reg_equiv_invariant_p[regno]
3437 && ira_reg_equiv_const[regno] == NULL_RTX)))
3438 sorted_copies[cp_num++] = cp;
3440 else if (cp->second == a)
3441 next_cp = cp->next_second_allocno_copy;
3442 else
3443 gcc_unreachable ();
3446 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
3447 /* Coalesced copies, most frequently executed first. */
3448 for (; cp_num != 0;)
3450 for (i = 0; i < cp_num; i++)
3452 cp = sorted_copies[i];
3453 if (! coalesced_allocno_conflict_p (cp->first, cp->second))
3455 allocno_coalesced_p = true;
3456 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3457 fprintf
3458 (ira_dump_file,
3459 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
3460 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
3461 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
3462 cp->freq);
3463 merge_allocnos (cp->first, cp->second);
3464 i++;
3465 break;
3468 /* Collect the rest of copies. */
3469 for (n = 0; i < cp_num; i++)
3471 cp = sorted_copies[i];
3472 if (allocno_coalesce_data[ALLOCNO_NUM (cp->first)].first
3473 != allocno_coalesce_data[ALLOCNO_NUM (cp->second)].first)
3474 sorted_copies[n++] = cp;
3476 cp_num = n;
3478 ira_free (sorted_copies);
3481 /* Usage cost and order number of coalesced allocno set to which
3482 given pseudo register belongs to. */
3483 static int *regno_coalesced_allocno_cost;
3484 static int *regno_coalesced_allocno_num;
3486 /* Sort pseudos according frequencies of coalesced allocno sets they
3487 belong to (putting most frequently ones first), and according to
3488 coalesced allocno set order numbers. */
3489 static int
3490 coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
3492 const int regno1 = *(const int *) v1p;
3493 const int regno2 = *(const int *) v2p;
3494 int diff;
3496 if ((diff = (regno_coalesced_allocno_cost[regno2]
3497 - regno_coalesced_allocno_cost[regno1])) != 0)
3498 return diff;
3499 if ((diff = (regno_coalesced_allocno_num[regno1]
3500 - regno_coalesced_allocno_num[regno2])) != 0)
3501 return diff;
3502 return regno1 - regno2;
3505 /* Widest width in which each pseudo reg is referred to (via subreg).
3506 It is used for sorting pseudo registers. */
3507 static unsigned int *regno_max_ref_width;
3509 /* Redefine STACK_GROWS_DOWNWARD in terms of 0 or 1. */
3510 #ifdef STACK_GROWS_DOWNWARD
3511 # undef STACK_GROWS_DOWNWARD
3512 # define STACK_GROWS_DOWNWARD 1
3513 #else
3514 # define STACK_GROWS_DOWNWARD 0
3515 #endif
3517 /* Sort pseudos according their slot numbers (putting ones with
3518 smaller numbers first, or last when the frame pointer is not
3519 needed). */
3520 static int
3521 coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
3523 const int regno1 = *(const int *) v1p;
3524 const int regno2 = *(const int *) v2p;
3525 ira_allocno_t a1 = ira_regno_allocno_map[regno1];
3526 ira_allocno_t a2 = ira_regno_allocno_map[regno2];
3527 int diff, slot_num1, slot_num2;
3528 int total_size1, total_size2;
3530 if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
3532 if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
3533 return regno1 - regno2;
3534 return 1;
3536 else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
3537 return -1;
3538 slot_num1 = -ALLOCNO_HARD_REGNO (a1);
3539 slot_num2 = -ALLOCNO_HARD_REGNO (a2);
3540 if ((diff = slot_num1 - slot_num2) != 0)
3541 return (frame_pointer_needed
3542 || !FRAME_GROWS_DOWNWARD == STACK_GROWS_DOWNWARD ? diff : -diff);
3543 total_size1 = MAX (PSEUDO_REGNO_BYTES (regno1),
3544 regno_max_ref_width[regno1]);
3545 total_size2 = MAX (PSEUDO_REGNO_BYTES (regno2),
3546 regno_max_ref_width[regno2]);
3547 if ((diff = total_size2 - total_size1) != 0)
3548 return diff;
3549 return regno1 - regno2;
3552 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
3553 for coalesced allocno sets containing allocnos with their regnos
3554 given in array PSEUDO_REGNOS of length N. */
3555 static void
3556 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
3558 int i, num, regno, cost;
3559 ira_allocno_t allocno, a;
3561 for (num = i = 0; i < n; i++)
3563 regno = pseudo_regnos[i];
3564 allocno = ira_regno_allocno_map[regno];
3565 if (allocno == NULL)
3567 regno_coalesced_allocno_cost[regno] = 0;
3568 regno_coalesced_allocno_num[regno] = ++num;
3569 continue;
3571 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
3572 continue;
3573 num++;
3574 for (cost = 0, a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3575 a = ALLOCNO_COALESCE_DATA (a)->next)
3577 cost += ALLOCNO_FREQ (a);
3578 if (a == allocno)
3579 break;
3581 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3582 a = ALLOCNO_COALESCE_DATA (a)->next)
3584 regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
3585 regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
3586 if (a == allocno)
3587 break;
3592 /* Collect spilled allocnos representing coalesced allocno sets (the
3593 first coalesced allocno). The collected allocnos are returned
3594 through array SPILLED_COALESCED_ALLOCNOS. The function returns the
3595 number of the collected allocnos. The allocnos are given by their
3596 regnos in array PSEUDO_REGNOS of length N. */
3597 static int
3598 collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
3599 ira_allocno_t *spilled_coalesced_allocnos)
3601 int i, num, regno;
3602 ira_allocno_t allocno;
3604 for (num = i = 0; i < n; i++)
3606 regno = pseudo_regnos[i];
3607 allocno = ira_regno_allocno_map[regno];
3608 if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
3609 || ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
3610 continue;
3611 spilled_coalesced_allocnos[num++] = allocno;
3613 return num;
3616 /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
3617 given slot contains live ranges of coalesced allocnos assigned to
3618 given slot. */
3619 static live_range_t *slot_coalesced_allocnos_live_ranges;
3621 /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
3622 ranges intersected with live ranges of coalesced allocnos assigned
3623 to slot with number N. */
3624 static bool
3625 slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno, int n)
3627 ira_allocno_t a;
3629 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3630 a = ALLOCNO_COALESCE_DATA (a)->next)
3632 int i;
3633 int nr = ALLOCNO_NUM_OBJECTS (a);
3635 for (i = 0; i < nr; i++)
3637 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3639 if (ira_live_ranges_intersect_p
3640 (slot_coalesced_allocnos_live_ranges[n],
3641 OBJECT_LIVE_RANGES (obj)))
3642 return true;
3644 if (a == allocno)
3645 break;
3647 return false;
3650 /* Update live ranges of slot to which coalesced allocnos represented
3651 by ALLOCNO were assigned. */
3652 static void
3653 setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno)
3655 int i, n;
3656 ira_allocno_t a;
3657 live_range_t r;
3659 n = ALLOCNO_COALESCE_DATA (allocno)->temp;
3660 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3661 a = ALLOCNO_COALESCE_DATA (a)->next)
3663 int nr = ALLOCNO_NUM_OBJECTS (a);
3664 for (i = 0; i < nr; i++)
3666 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3668 r = ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj));
3669 slot_coalesced_allocnos_live_ranges[n]
3670 = ira_merge_live_ranges
3671 (slot_coalesced_allocnos_live_ranges[n], r);
3673 if (a == allocno)
3674 break;
3678 /* We have coalesced allocnos involving in copies. Coalesce allocnos
3679 further in order to share the same memory stack slot. Allocnos
3680 representing sets of allocnos coalesced before the call are given
3681 in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
3682 some allocnos were coalesced in the function. */
3683 static bool
3684 coalesce_spill_slots (ira_allocno_t *spilled_coalesced_allocnos, int num)
3686 int i, j, n, last_coalesced_allocno_num;
3687 ira_allocno_t allocno, a;
3688 bool merged_p = false;
3689 bitmap set_jump_crosses = regstat_get_setjmp_crosses ();
3691 slot_coalesced_allocnos_live_ranges
3692 = (live_range_t *) ira_allocate (sizeof (live_range_t) * ira_allocnos_num);
3693 memset (slot_coalesced_allocnos_live_ranges, 0,
3694 sizeof (live_range_t) * ira_allocnos_num);
3695 last_coalesced_allocno_num = 0;
3696 /* Coalesce non-conflicting spilled allocnos preferring most
3697 frequently used. */
3698 for (i = 0; i < num; i++)
3700 allocno = spilled_coalesced_allocnos[i];
3701 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
3702 || bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (allocno))
3703 || (ALLOCNO_REGNO (allocno) < ira_reg_equiv_len
3704 && (ira_reg_equiv_const[ALLOCNO_REGNO (allocno)] != NULL_RTX
3705 || ira_reg_equiv_invariant_p[ALLOCNO_REGNO (allocno)])))
3706 continue;
3707 for (j = 0; j < i; j++)
3709 a = spilled_coalesced_allocnos[j];
3710 n = ALLOCNO_COALESCE_DATA (a)->temp;
3711 if (ALLOCNO_COALESCE_DATA (a)->first == a
3712 && ! bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (a))
3713 && (ALLOCNO_REGNO (a) >= ira_reg_equiv_len
3714 || (! ira_reg_equiv_invariant_p[ALLOCNO_REGNO (a)]
3715 && ira_reg_equiv_const[ALLOCNO_REGNO (a)] == NULL_RTX))
3716 && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno, n))
3717 break;
3719 if (j >= i)
3721 /* No coalescing: set up number for coalesced allocnos
3722 represented by ALLOCNO. */
3723 ALLOCNO_COALESCE_DATA (allocno)->temp = last_coalesced_allocno_num++;
3724 setup_slot_coalesced_allocno_live_ranges (allocno);
3726 else
3728 allocno_coalesced_p = true;
3729 merged_p = true;
3730 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3731 fprintf (ira_dump_file,
3732 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
3733 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
3734 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3735 ALLOCNO_COALESCE_DATA (allocno)->temp
3736 = ALLOCNO_COALESCE_DATA (a)->temp;
3737 setup_slot_coalesced_allocno_live_ranges (allocno);
3738 merge_allocnos (a, allocno);
3739 ira_assert (ALLOCNO_COALESCE_DATA (a)->first == a);
3742 for (i = 0; i < ira_allocnos_num; i++)
3743 ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges[i]);
3744 ira_free (slot_coalesced_allocnos_live_ranges);
3745 return merged_p;
3748 /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
3749 subsequent assigning stack slots to them in the reload pass. To do
3750 this we coalesce spilled allocnos first to decrease the number of
3751 memory-memory move insns. This function is called by the
3752 reload. */
3753 void
3754 ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
3755 unsigned int *reg_max_ref_width)
3757 int max_regno = max_reg_num ();
3758 int i, regno, num, slot_num;
3759 ira_allocno_t allocno, a;
3760 ira_allocno_iterator ai;
3761 ira_allocno_t *spilled_coalesced_allocnos;
3763 /* Set up allocnos can be coalesced. */
3764 coloring_allocno_bitmap = ira_allocate_bitmap ();
3765 for (i = 0; i < n; i++)
3767 regno = pseudo_regnos[i];
3768 allocno = ira_regno_allocno_map[regno];
3769 if (allocno != NULL)
3770 bitmap_set_bit (coloring_allocno_bitmap, ALLOCNO_NUM (allocno));
3772 allocno_coalesced_p = false;
3773 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
3774 allocno_coalesce_data
3775 = (coalesce_data_t) ira_allocate (sizeof (struct coalesce_data)
3776 * ira_allocnos_num);
3777 /* Initialize coalesce data for allocnos. */
3778 FOR_EACH_ALLOCNO (a, ai)
3780 ALLOCNO_ADD_DATA (a) = allocno_coalesce_data + ALLOCNO_NUM (a);
3781 ALLOCNO_COALESCE_DATA (a)->first = a;
3782 ALLOCNO_COALESCE_DATA (a)->next = a;
3784 coalesce_allocnos ();
3785 ira_free_bitmap (coloring_allocno_bitmap);
3786 regno_coalesced_allocno_cost
3787 = (int *) ira_allocate (max_regno * sizeof (int));
3788 regno_coalesced_allocno_num
3789 = (int *) ira_allocate (max_regno * sizeof (int));
3790 memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
3791 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
3792 /* Sort regnos according frequencies of the corresponding coalesced
3793 allocno sets. */
3794 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
3795 spilled_coalesced_allocnos
3796 = (ira_allocno_t *) ira_allocate (ira_allocnos_num
3797 * sizeof (ira_allocno_t));
3798 /* Collect allocnos representing the spilled coalesced allocno
3799 sets. */
3800 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
3801 spilled_coalesced_allocnos);
3802 if (flag_ira_share_spill_slots
3803 && coalesce_spill_slots (spilled_coalesced_allocnos, num))
3805 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
3806 qsort (pseudo_regnos, n, sizeof (int),
3807 coalesced_pseudo_reg_freq_compare);
3808 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
3809 spilled_coalesced_allocnos);
3811 ira_free_bitmap (processed_coalesced_allocno_bitmap);
3812 allocno_coalesced_p = false;
3813 /* Assign stack slot numbers to spilled allocno sets, use smaller
3814 numbers for most frequently used coalesced allocnos. -1 is
3815 reserved for dynamic search of stack slots for pseudos spilled by
3816 the reload. */
3817 slot_num = 1;
3818 for (i = 0; i < num; i++)
3820 allocno = spilled_coalesced_allocnos[i];
3821 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
3822 || ALLOCNO_HARD_REGNO (allocno) >= 0
3823 || (ALLOCNO_REGNO (allocno) < ira_reg_equiv_len
3824 && (ira_reg_equiv_const[ALLOCNO_REGNO (allocno)] != NULL_RTX
3825 || ira_reg_equiv_invariant_p[ALLOCNO_REGNO (allocno)])))
3826 continue;
3827 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3828 fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
3829 slot_num++;
3830 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3831 a = ALLOCNO_COALESCE_DATA (a)->next)
3833 ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
3834 ALLOCNO_HARD_REGNO (a) = -slot_num;
3835 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3836 fprintf (ira_dump_file, " a%dr%d(%d,%d)",
3837 ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a),
3838 MAX (PSEUDO_REGNO_BYTES (ALLOCNO_REGNO (a)),
3839 reg_max_ref_width[ALLOCNO_REGNO (a)]));
3841 if (a == allocno)
3842 break;
3844 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3845 fprintf (ira_dump_file, "\n");
3847 ira_spilled_reg_stack_slots_num = slot_num - 1;
3848 ira_free (spilled_coalesced_allocnos);
3849 /* Sort regnos according the slot numbers. */
3850 regno_max_ref_width = reg_max_ref_width;
3851 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
3852 FOR_EACH_ALLOCNO (a, ai)
3853 ALLOCNO_ADD_DATA (a) = NULL;
3854 ira_free (allocno_coalesce_data);
3855 ira_free (regno_coalesced_allocno_num);
3856 ira_free (regno_coalesced_allocno_cost);
3861 /* This page contains code used by the reload pass to improve the
3862 final code. */
3864 /* The function is called from reload to mark changes in the
3865 allocation of REGNO made by the reload. Remember that reg_renumber
3866 reflects the change result. */
3867 void
3868 ira_mark_allocation_change (int regno)
3870 ira_allocno_t a = ira_regno_allocno_map[regno];
3871 int old_hard_regno, hard_regno, cost;
3872 enum reg_class aclass = ALLOCNO_CLASS (a);
3874 ira_assert (a != NULL);
3875 hard_regno = reg_renumber[regno];
3876 if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
3877 return;
3878 if (old_hard_regno < 0)
3879 cost = -ALLOCNO_MEMORY_COST (a);
3880 else
3882 ira_assert (ira_class_hard_reg_index[aclass][old_hard_regno] >= 0);
3883 cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
3884 ? ALLOCNO_CLASS_COST (a)
3885 : ALLOCNO_HARD_REG_COSTS (a)
3886 [ira_class_hard_reg_index[aclass][old_hard_regno]]);
3887 update_copy_costs (a, false);
3889 ira_overall_cost -= cost;
3890 ALLOCNO_HARD_REGNO (a) = hard_regno;
3891 if (hard_regno < 0)
3893 ALLOCNO_HARD_REGNO (a) = -1;
3894 cost += ALLOCNO_MEMORY_COST (a);
3896 else if (ira_class_hard_reg_index[aclass][hard_regno] >= 0)
3898 cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
3899 ? ALLOCNO_CLASS_COST (a)
3900 : ALLOCNO_HARD_REG_COSTS (a)
3901 [ira_class_hard_reg_index[aclass][hard_regno]]);
3902 update_copy_costs (a, true);
3904 else
3905 /* Reload changed class of the allocno. */
3906 cost = 0;
3907 ira_overall_cost += cost;
3910 /* This function is called when reload deletes memory-memory move. In
3911 this case we marks that the allocation of the corresponding
3912 allocnos should be not changed in future. Otherwise we risk to get
3913 a wrong code. */
3914 void
3915 ira_mark_memory_move_deletion (int dst_regno, int src_regno)
3917 ira_allocno_t dst = ira_regno_allocno_map[dst_regno];
3918 ira_allocno_t src = ira_regno_allocno_map[src_regno];
3920 ira_assert (dst != NULL && src != NULL
3921 && ALLOCNO_HARD_REGNO (dst) < 0
3922 && ALLOCNO_HARD_REGNO (src) < 0);
3923 ALLOCNO_DONT_REASSIGN_P (dst) = true;
3924 ALLOCNO_DONT_REASSIGN_P (src) = true;
3927 /* Try to assign a hard register (except for FORBIDDEN_REGS) to
3928 allocno A and return TRUE in the case of success. */
3929 static bool
3930 allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
3932 int hard_regno;
3933 enum reg_class aclass;
3934 int regno = ALLOCNO_REGNO (a);
3935 HARD_REG_SET saved[2];
3936 int i, n;
3938 n = ALLOCNO_NUM_OBJECTS (a);
3939 for (i = 0; i < n; i++)
3941 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3942 COPY_HARD_REG_SET (saved[i], OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
3943 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), forbidden_regs);
3944 if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
3945 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
3946 call_used_reg_set);
3948 ALLOCNO_ASSIGNED_P (a) = false;
3949 aclass = ALLOCNO_CLASS (a);
3950 update_curr_costs (a);
3951 assign_hard_reg (a, true);
3952 hard_regno = ALLOCNO_HARD_REGNO (a);
3953 reg_renumber[regno] = hard_regno;
3954 if (hard_regno < 0)
3955 ALLOCNO_HARD_REGNO (a) = -1;
3956 else
3958 ira_assert (ira_class_hard_reg_index[aclass][hard_regno] >= 0);
3959 ira_overall_cost
3960 -= (ALLOCNO_MEMORY_COST (a)
3961 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
3962 ? ALLOCNO_CLASS_COST (a)
3963 : ALLOCNO_HARD_REG_COSTS (a)[ira_class_hard_reg_index
3964 [aclass][hard_regno]]));
3965 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
3966 && ! ira_hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
3967 call_used_reg_set))
3969 ira_assert (flag_caller_saves);
3970 caller_save_needed = 1;
3974 /* If we found a hard register, modify the RTL for the pseudo
3975 register to show the hard register, and mark the pseudo register
3976 live. */
3977 if (reg_renumber[regno] >= 0)
3979 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3980 fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
3981 SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
3982 mark_home_live (regno);
3984 else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3985 fprintf (ira_dump_file, "\n");
3986 for (i = 0; i < n; i++)
3988 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3989 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), saved[i]);
3991 return reg_renumber[regno] >= 0;
3994 /* Sort pseudos according their usage frequencies (putting most
3995 frequently ones first). */
3996 static int
3997 pseudo_reg_compare (const void *v1p, const void *v2p)
3999 int regno1 = *(const int *) v1p;
4000 int regno2 = *(const int *) v2p;
4001 int diff;
4003 if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
4004 return diff;
4005 return regno1 - regno2;
4008 /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
4009 NUM of them) or spilled pseudos conflicting with pseudos in
4010 SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
4011 allocation has been changed. The function doesn't use
4012 BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
4013 PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
4014 is called by the reload pass at the end of each reload
4015 iteration. */
4016 bool
4017 ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
4018 HARD_REG_SET bad_spill_regs,
4019 HARD_REG_SET *pseudo_forbidden_regs,
4020 HARD_REG_SET *pseudo_previous_regs,
4021 bitmap spilled)
4023 int i, n, regno;
4024 bool changed_p;
4025 ira_allocno_t a;
4026 HARD_REG_SET forbidden_regs;
4027 bitmap temp = BITMAP_ALLOC (NULL);
4029 /* Add pseudos which conflict with pseudos already in
4030 SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
4031 to allocating in two steps as some of the conflicts might have
4032 a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
4033 for (i = 0; i < num; i++)
4034 bitmap_set_bit (temp, spilled_pseudo_regs[i]);
4036 for (i = 0, n = num; i < n; i++)
4038 int nr, j;
4039 int regno = spilled_pseudo_regs[i];
4040 bitmap_set_bit (temp, regno);
4042 a = ira_regno_allocno_map[regno];
4043 nr = ALLOCNO_NUM_OBJECTS (a);
4044 for (j = 0; j < nr; j++)
4046 ira_object_t conflict_obj;
4047 ira_object_t obj = ALLOCNO_OBJECT (a, j);
4048 ira_object_conflict_iterator oci;
4050 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
4052 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
4053 if (ALLOCNO_HARD_REGNO (conflict_a) < 0
4054 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
4055 && bitmap_set_bit (temp, ALLOCNO_REGNO (conflict_a)))
4057 spilled_pseudo_regs[num++] = ALLOCNO_REGNO (conflict_a);
4058 /* ?!? This seems wrong. */
4059 bitmap_set_bit (consideration_allocno_bitmap,
4060 ALLOCNO_NUM (conflict_a));
4066 if (num > 1)
4067 qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
4068 changed_p = false;
4069 /* Try to assign hard registers to pseudos from
4070 SPILLED_PSEUDO_REGS. */
4071 for (i = 0; i < num; i++)
4073 regno = spilled_pseudo_regs[i];
4074 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
4075 IOR_HARD_REG_SET (forbidden_regs, pseudo_forbidden_regs[regno]);
4076 IOR_HARD_REG_SET (forbidden_regs, pseudo_previous_regs[regno]);
4077 gcc_assert (reg_renumber[regno] < 0);
4078 a = ira_regno_allocno_map[regno];
4079 ira_mark_allocation_change (regno);
4080 ira_assert (reg_renumber[regno] < 0);
4081 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4082 fprintf (ira_dump_file,
4083 " Try Assign %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
4084 ALLOCNO_MEMORY_COST (a)
4085 - ALLOCNO_CLASS_COST (a));
4086 allocno_reload_assign (a, forbidden_regs);
4087 if (reg_renumber[regno] >= 0)
4089 CLEAR_REGNO_REG_SET (spilled, regno);
4090 changed_p = true;
4093 BITMAP_FREE (temp);
4094 return changed_p;
4097 /* The function is called by reload and returns already allocated
4098 stack slot (if any) for REGNO with given INHERENT_SIZE and
4099 TOTAL_SIZE. In the case of failure to find a slot which can be
4100 used for REGNO, the function returns NULL. */
4102 ira_reuse_stack_slot (int regno, unsigned int inherent_size,
4103 unsigned int total_size)
4105 unsigned int i;
4106 int slot_num, best_slot_num;
4107 int cost, best_cost;
4108 ira_copy_t cp, next_cp;
4109 ira_allocno_t another_allocno, allocno = ira_regno_allocno_map[regno];
4110 rtx x;
4111 bitmap_iterator bi;
4112 struct ira_spilled_reg_stack_slot *slot = NULL;
4114 ira_assert (inherent_size == PSEUDO_REGNO_BYTES (regno)
4115 && inherent_size <= total_size
4116 && ALLOCNO_HARD_REGNO (allocno) < 0);
4117 if (! flag_ira_share_spill_slots)
4118 return NULL_RTX;
4119 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4120 if (slot_num != -1)
4122 slot = &ira_spilled_reg_stack_slots[slot_num];
4123 x = slot->mem;
4125 else
4127 best_cost = best_slot_num = -1;
4128 x = NULL_RTX;
4129 /* It means that the pseudo was spilled in the reload pass, try
4130 to reuse a slot. */
4131 for (slot_num = 0;
4132 slot_num < ira_spilled_reg_stack_slots_num;
4133 slot_num++)
4135 slot = &ira_spilled_reg_stack_slots[slot_num];
4136 if (slot->mem == NULL_RTX)
4137 continue;
4138 if (slot->width < total_size
4139 || GET_MODE_SIZE (GET_MODE (slot->mem)) < inherent_size)
4140 continue;
4142 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4143 FIRST_PSEUDO_REGISTER, i, bi)
4145 another_allocno = ira_regno_allocno_map[i];
4146 if (allocnos_conflict_by_live_ranges_p (allocno,
4147 another_allocno))
4148 goto cont;
4150 for (cost = 0, cp = ALLOCNO_COPIES (allocno);
4151 cp != NULL;
4152 cp = next_cp)
4154 if (cp->first == allocno)
4156 next_cp = cp->next_first_allocno_copy;
4157 another_allocno = cp->second;
4159 else if (cp->second == allocno)
4161 next_cp = cp->next_second_allocno_copy;
4162 another_allocno = cp->first;
4164 else
4165 gcc_unreachable ();
4166 if (cp->insn == NULL_RTX)
4167 continue;
4168 if (bitmap_bit_p (&slot->spilled_regs,
4169 ALLOCNO_REGNO (another_allocno)))
4170 cost += cp->freq;
4172 if (cost > best_cost)
4174 best_cost = cost;
4175 best_slot_num = slot_num;
4177 cont:
4180 if (best_cost >= 0)
4182 slot_num = best_slot_num;
4183 slot = &ira_spilled_reg_stack_slots[slot_num];
4184 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4185 x = slot->mem;
4186 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4189 if (x != NULL_RTX)
4191 ira_assert (slot->width >= total_size);
4192 #ifdef ENABLE_IRA_CHECKING
4193 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4194 FIRST_PSEUDO_REGISTER, i, bi)
4196 ira_assert (! conflict_by_live_ranges_p (regno, i));
4198 #endif
4199 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4200 if (internal_flag_ira_verbose > 3 && ira_dump_file)
4202 fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
4203 regno, REG_FREQ (regno), slot_num);
4204 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4205 FIRST_PSEUDO_REGISTER, i, bi)
4207 if ((unsigned) regno != i)
4208 fprintf (ira_dump_file, " %d", i);
4210 fprintf (ira_dump_file, "\n");
4213 return x;
4216 /* This is called by reload every time a new stack slot X with
4217 TOTAL_SIZE was allocated for REGNO. We store this info for
4218 subsequent ira_reuse_stack_slot calls. */
4219 void
4220 ira_mark_new_stack_slot (rtx x, int regno, unsigned int total_size)
4222 struct ira_spilled_reg_stack_slot *slot;
4223 int slot_num;
4224 ira_allocno_t allocno;
4226 ira_assert (PSEUDO_REGNO_BYTES (regno) <= total_size);
4227 allocno = ira_regno_allocno_map[regno];
4228 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4229 if (slot_num == -1)
4231 slot_num = ira_spilled_reg_stack_slots_num++;
4232 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4234 slot = &ira_spilled_reg_stack_slots[slot_num];
4235 INIT_REG_SET (&slot->spilled_regs);
4236 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4237 slot->mem = x;
4238 slot->width = total_size;
4239 if (internal_flag_ira_verbose > 3 && ira_dump_file)
4240 fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
4241 regno, REG_FREQ (regno), slot_num);
4245 /* Return spill cost for pseudo-registers whose numbers are in array
4246 REGNOS (with a negative number as an end marker) for reload with
4247 given IN and OUT for INSN. Return also number points (through
4248 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
4249 the register pressure is high, number of references of the
4250 pseudo-registers (through NREFS), number of callee-clobbered
4251 hard-registers occupied by the pseudo-registers (through
4252 CALL_USED_COUNT), and the first hard regno occupied by the
4253 pseudo-registers (through FIRST_HARD_REGNO). */
4254 static int
4255 calculate_spill_cost (int *regnos, rtx in, rtx out, rtx insn,
4256 int *excess_pressure_live_length,
4257 int *nrefs, int *call_used_count, int *first_hard_regno)
4259 int i, cost, regno, hard_regno, j, count, saved_cost, nregs;
4260 bool in_p, out_p;
4261 int length;
4262 ira_allocno_t a;
4264 *nrefs = 0;
4265 for (length = count = cost = i = 0;; i++)
4267 regno = regnos[i];
4268 if (regno < 0)
4269 break;
4270 *nrefs += REG_N_REFS (regno);
4271 hard_regno = reg_renumber[regno];
4272 ira_assert (hard_regno >= 0);
4273 a = ira_regno_allocno_map[regno];
4274 length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) / ALLOCNO_NUM_OBJECTS (a);
4275 cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
4276 nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
4277 for (j = 0; j < nregs; j++)
4278 if (! TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + j))
4279 break;
4280 if (j == nregs)
4281 count++;
4282 in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
4283 out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
4284 if ((in_p || out_p)
4285 && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
4287 saved_cost = 0;
4288 if (in_p)
4289 saved_cost += ira_memory_move_cost
4290 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][1];
4291 if (out_p)
4292 saved_cost
4293 += ira_memory_move_cost
4294 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][0];
4295 cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
4298 *excess_pressure_live_length = length;
4299 *call_used_count = count;
4300 hard_regno = -1;
4301 if (regnos[0] >= 0)
4303 hard_regno = reg_renumber[regnos[0]];
4305 *first_hard_regno = hard_regno;
4306 return cost;
4309 /* Return TRUE if spilling pseudo-registers whose numbers are in array
4310 REGNOS is better than spilling pseudo-registers with numbers in
4311 OTHER_REGNOS for reload with given IN and OUT for INSN. The
4312 function used by the reload pass to make better register spilling
4313 decisions. */
4314 bool
4315 ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
4316 rtx in, rtx out, rtx insn)
4318 int cost, other_cost;
4319 int length, other_length;
4320 int nrefs, other_nrefs;
4321 int call_used_count, other_call_used_count;
4322 int hard_regno, other_hard_regno;
4324 cost = calculate_spill_cost (regnos, in, out, insn,
4325 &length, &nrefs, &call_used_count, &hard_regno);
4326 other_cost = calculate_spill_cost (other_regnos, in, out, insn,
4327 &other_length, &other_nrefs,
4328 &other_call_used_count,
4329 &other_hard_regno);
4330 if (nrefs == 0 && other_nrefs != 0)
4331 return true;
4332 if (nrefs != 0 && other_nrefs == 0)
4333 return false;
4334 if (cost != other_cost)
4335 return cost < other_cost;
4336 if (length != other_length)
4337 return length > other_length;
4338 #ifdef REG_ALLOC_ORDER
4339 if (hard_regno >= 0 && other_hard_regno >= 0)
4340 return (inv_reg_alloc_order[hard_regno]
4341 < inv_reg_alloc_order[other_hard_regno]);
4342 #else
4343 if (call_used_count != other_call_used_count)
4344 return call_used_count > other_call_used_count;
4345 #endif
4346 return false;
4351 /* Allocate and initialize data necessary for assign_hard_reg. */
4352 void
4353 ira_initiate_assign (void)
4355 sorted_allocnos
4356 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4357 * ira_allocnos_num);
4358 consideration_allocno_bitmap = ira_allocate_bitmap ();
4359 initiate_cost_update ();
4360 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4363 /* Deallocate data used by assign_hard_reg. */
4364 void
4365 ira_finish_assign (void)
4367 ira_free (sorted_allocnos);
4368 ira_free_bitmap (consideration_allocno_bitmap);
4369 finish_cost_update ();
4370 ira_free (allocno_priorities);
4375 /* Entry function doing color-based register allocation. */
4376 static void
4377 color (void)
4379 allocno_stack_vec = VEC_alloc (ira_allocno_t, heap, ira_allocnos_num);
4380 memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
4381 ira_initiate_assign ();
4382 do_coloring ();
4383 ira_finish_assign ();
4384 VEC_free (ira_allocno_t, heap, allocno_stack_vec);
4385 move_spill_restore ();
4390 /* This page contains a simple register allocator without usage of
4391 allocno conflicts. This is used for fast allocation for -O0. */
4393 /* Do register allocation by not using allocno conflicts. It uses
4394 only allocno live ranges. The algorithm is close to Chow's
4395 priority coloring. */
4396 static void
4397 fast_allocation (void)
4399 int i, j, k, num, class_size, hard_regno;
4400 #ifdef STACK_REGS
4401 bool no_stack_reg_p;
4402 #endif
4403 enum reg_class aclass;
4404 enum machine_mode mode;
4405 ira_allocno_t a;
4406 ira_allocno_iterator ai;
4407 live_range_t r;
4408 HARD_REG_SET conflict_hard_regs, *used_hard_regs;
4410 sorted_allocnos = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4411 * ira_allocnos_num);
4412 num = 0;
4413 FOR_EACH_ALLOCNO (a, ai)
4414 sorted_allocnos[num++] = a;
4415 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4416 setup_allocno_priorities (sorted_allocnos, num);
4417 used_hard_regs = (HARD_REG_SET *) ira_allocate (sizeof (HARD_REG_SET)
4418 * ira_max_point);
4419 for (i = 0; i < ira_max_point; i++)
4420 CLEAR_HARD_REG_SET (used_hard_regs[i]);
4421 qsort (sorted_allocnos, num, sizeof (ira_allocno_t),
4422 allocno_priority_compare_func);
4423 for (i = 0; i < num; i++)
4425 int nr, l;
4427 a = sorted_allocnos[i];
4428 nr = ALLOCNO_NUM_OBJECTS (a);
4429 CLEAR_HARD_REG_SET (conflict_hard_regs);
4430 for (l = 0; l < nr; l++)
4432 ira_object_t obj = ALLOCNO_OBJECT (a, l);
4433 IOR_HARD_REG_SET (conflict_hard_regs,
4434 OBJECT_CONFLICT_HARD_REGS (obj));
4435 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4436 for (j = r->start; j <= r->finish; j++)
4437 IOR_HARD_REG_SET (conflict_hard_regs, used_hard_regs[j]);
4439 aclass = ALLOCNO_CLASS (a);
4440 ALLOCNO_ASSIGNED_P (a) = true;
4441 ALLOCNO_HARD_REGNO (a) = -1;
4442 if (hard_reg_set_subset_p (reg_class_contents[aclass],
4443 conflict_hard_regs))
4444 continue;
4445 mode = ALLOCNO_MODE (a);
4446 #ifdef STACK_REGS
4447 no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
4448 #endif
4449 class_size = ira_class_hard_regs_num[aclass];
4450 for (j = 0; j < class_size; j++)
4452 hard_regno = ira_class_hard_regs[aclass][j];
4453 #ifdef STACK_REGS
4454 if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
4455 && hard_regno <= LAST_STACK_REG)
4456 continue;
4457 #endif
4458 if (!ira_hard_reg_not_in_set_p (hard_regno, mode, conflict_hard_regs)
4459 || (TEST_HARD_REG_BIT
4460 (ira_prohibited_class_mode_regs[aclass][mode], hard_regno)))
4461 continue;
4462 ALLOCNO_HARD_REGNO (a) = hard_regno;
4463 for (l = 0; l < nr; l++)
4465 ira_object_t obj = ALLOCNO_OBJECT (a, l);
4466 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4467 for (k = r->start; k <= r->finish; k++)
4468 IOR_HARD_REG_SET (used_hard_regs[k],
4469 ira_reg_mode_hard_regset[hard_regno][mode]);
4471 break;
4474 ira_free (sorted_allocnos);
4475 ira_free (used_hard_regs);
4476 ira_free (allocno_priorities);
4477 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
4478 ira_print_disposition (ira_dump_file);
4483 /* Entry function doing coloring. */
4484 void
4485 ira_color (void)
4487 ira_allocno_t a;
4488 ira_allocno_iterator ai;
4490 /* Setup updated costs. */
4491 FOR_EACH_ALLOCNO (a, ai)
4493 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
4494 ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
4496 if (ira_conflicts_p)
4497 color ();
4498 else
4499 fast_allocation ();