* dwarf2out.c (dwarf_attr_name): Map DW_AT_GNAT_descriptive_type.
[official-gcc.git] / gcc / ira-color.c
blob8fa3393b367004d7a77797413734c6cdf4108861
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_hard_reg_set_intersection_p
1624 (hard_regno, ALLOCNO_MODE (conflict_a),
1625 reg_class_contents[aclass])))
1627 int n_objects = ALLOCNO_NUM_OBJECTS (conflict_a);
1628 int conflict_nregs;
1630 mode = ALLOCNO_MODE (conflict_a);
1631 conflict_nregs = hard_regno_nregs[hard_regno][mode];
1632 if (conflict_nregs == n_objects && conflict_nregs > 1)
1634 int num = OBJECT_SUBWORD (conflict_obj);
1636 if (WORDS_BIG_ENDIAN)
1637 SET_HARD_REG_BIT (conflicting_regs[word],
1638 hard_regno + n_objects - num - 1);
1639 else
1640 SET_HARD_REG_BIT (conflicting_regs[word],
1641 hard_regno + num);
1643 else
1644 IOR_HARD_REG_SET
1645 (conflicting_regs[word],
1646 ira_reg_mode_hard_regset[hard_regno][mode]);
1647 if (hard_reg_set_subset_p (profitable_hard_regs[word],
1648 conflicting_regs[word]))
1649 goto fail;
1652 else if (! retry_p
1653 && ! ALLOCNO_COLOR_DATA (conflict_a)->may_be_spilled_p)
1655 int k, *conflict_costs;
1657 ira_allocate_and_copy_costs
1658 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a),
1659 conflict_aclass,
1660 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_a));
1661 conflict_costs
1662 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a);
1663 if (conflict_costs != NULL)
1664 for (j = class_size - 1; j >= 0; j--)
1666 hard_regno = ira_class_hard_regs[aclass][j];
1667 ira_assert (hard_regno >= 0);
1668 k = ira_class_hard_reg_index[conflict_aclass][hard_regno];
1669 if (k < 0)
1670 continue;
1671 full_costs[j] -= conflict_costs[k];
1673 queue_update_cost (conflict_a, COST_HOP_DIVISOR);
1677 if (! retry_p)
1678 /* Take into account preferences of allocnos connected by copies to
1679 the conflict allocnos. */
1680 update_conflict_hard_regno_costs (full_costs, aclass, true);
1682 /* Take preferences of allocnos connected by copies into
1683 account. */
1684 if (! retry_p)
1686 start_update_cost ();
1687 queue_update_cost (a, COST_HOP_DIVISOR);
1688 update_conflict_hard_regno_costs (full_costs, aclass, false);
1690 min_cost = min_full_cost = INT_MAX;
1692 /* We don't care about giving callee saved registers to allocnos no
1693 living through calls because call clobbered registers are
1694 allocated first (it is usual practice to put them first in
1695 REG_ALLOC_ORDER). */
1696 mode = ALLOCNO_MODE (a);
1697 for (i = 0; i < class_size; i++)
1699 hard_regno = ira_class_hard_regs[aclass][i];
1700 #ifdef STACK_REGS
1701 if (no_stack_reg_p
1702 && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
1703 continue;
1704 #endif
1705 if (! check_hard_reg_p (a, hard_regno,
1706 conflicting_regs, profitable_hard_regs))
1707 continue;
1708 cost = costs[i];
1709 full_cost = full_costs[i];
1710 #ifndef HONOR_REG_ALLOC_ORDER
1711 if (! allocated_hardreg_p[hard_regno]
1712 && ira_hard_reg_not_in_set_p (hard_regno, mode, call_used_reg_set)
1713 && !LOCAL_REGNO (hard_regno))
1714 /* We need to save/restore the hard register in
1715 epilogue/prologue. Therefore we increase the cost. */
1717 /* ??? If only part is call clobbered. */
1718 rclass = REGNO_REG_CLASS (hard_regno);
1719 add_cost = (ira_memory_move_cost[mode][rclass][0]
1720 + ira_memory_move_cost[mode][rclass][1] - 1);
1721 cost += add_cost;
1722 full_cost += add_cost;
1724 #endif
1725 if (min_cost > cost)
1726 min_cost = cost;
1727 if (min_full_cost > full_cost)
1729 min_full_cost = full_cost;
1730 best_hard_regno = hard_regno;
1731 ira_assert (hard_regno >= 0);
1734 if (min_full_cost > mem_cost)
1736 if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1737 fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
1738 mem_cost, min_full_cost);
1739 best_hard_regno = -1;
1741 fail:
1742 if (best_hard_regno >= 0)
1743 allocated_hardreg_p[best_hard_regno] = true;
1744 ALLOCNO_HARD_REGNO (a) = best_hard_regno;
1745 ALLOCNO_ASSIGNED_P (a) = true;
1746 if (best_hard_regno >= 0)
1747 update_copy_costs (a, true);
1748 ira_assert (ALLOCNO_CLASS (a) == aclass);
1749 /* We don't need updated costs anymore: */
1750 ira_free_allocno_updated_costs (a);
1751 return best_hard_regno >= 0;
1756 /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
1758 /* Bucket of allocnos that can colored currently without spilling. */
1759 static ira_allocno_t colorable_allocno_bucket;
1761 /* Bucket of allocnos that might be not colored currently without
1762 spilling. */
1763 static ira_allocno_t uncolorable_allocno_bucket;
1765 /* The current number of allocnos in the uncolorable_bucket. */
1766 static int uncolorable_allocnos_num;
1768 /* Return the current spill priority of allocno A. The less the
1769 number, the more preferable the allocno for spilling. */
1770 static inline int
1771 allocno_spill_priority (ira_allocno_t a)
1773 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
1775 return (data->temp
1776 / (ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
1777 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
1778 + 1));
1781 /* Add allocno A to bucket *BUCKET_PTR. A should be not in a bucket
1782 before the call. */
1783 static void
1784 add_allocno_to_bucket (ira_allocno_t a, ira_allocno_t *bucket_ptr)
1786 ira_allocno_t first_a;
1787 allocno_color_data_t data;
1789 if (bucket_ptr == &uncolorable_allocno_bucket
1790 && ALLOCNO_CLASS (a) != NO_REGS)
1792 uncolorable_allocnos_num++;
1793 ira_assert (uncolorable_allocnos_num > 0);
1795 first_a = *bucket_ptr;
1796 data = ALLOCNO_COLOR_DATA (a);
1797 data->next_bucket_allocno = first_a;
1798 data->prev_bucket_allocno = NULL;
1799 if (first_a != NULL)
1800 ALLOCNO_COLOR_DATA (first_a)->prev_bucket_allocno = a;
1801 *bucket_ptr = a;
1804 /* Compare two allocnos to define which allocno should be pushed first
1805 into the coloring stack. If the return is a negative number, the
1806 allocno given by the first parameter will be pushed first. In this
1807 case such allocno has less priority than the second one and the
1808 hard register will be assigned to it after assignment to the second
1809 one. As the result of such assignment order, the second allocno
1810 has a better chance to get the best hard register. */
1811 static int
1812 bucket_allocno_compare_func (const void *v1p, const void *v2p)
1814 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
1815 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
1816 int diff, a1_freq, a2_freq, a1_num, a2_num;
1818 if ((diff = (int) ALLOCNO_CLASS (a2) - ALLOCNO_CLASS (a1)) != 0)
1819 return diff;
1820 a1_freq = ALLOCNO_FREQ (a1);
1821 a2_freq = ALLOCNO_FREQ (a2);
1822 if ((diff = a1_freq - a2_freq) != 0)
1823 return diff;
1824 a1_num = ALLOCNO_COLOR_DATA (a1)->available_regs_num;
1825 a2_num = ALLOCNO_COLOR_DATA (a2)->available_regs_num;
1826 if ((diff = a2_num - a1_num) != 0)
1827 return diff;
1828 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
1831 /* Sort bucket *BUCKET_PTR and return the result through
1832 BUCKET_PTR. */
1833 static void
1834 sort_bucket (ira_allocno_t *bucket_ptr,
1835 int (*compare_func) (const void *, const void *))
1837 ira_allocno_t a, head;
1838 int n;
1840 for (n = 0, a = *bucket_ptr;
1841 a != NULL;
1842 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
1843 sorted_allocnos[n++] = a;
1844 if (n <= 1)
1845 return;
1846 qsort (sorted_allocnos, n, sizeof (ira_allocno_t), compare_func);
1847 head = NULL;
1848 for (n--; n >= 0; n--)
1850 a = sorted_allocnos[n];
1851 ALLOCNO_COLOR_DATA (a)->next_bucket_allocno = head;
1852 ALLOCNO_COLOR_DATA (a)->prev_bucket_allocno = NULL;
1853 if (head != NULL)
1854 ALLOCNO_COLOR_DATA (head)->prev_bucket_allocno = a;
1855 head = a;
1857 *bucket_ptr = head;
1860 /* Add ALLOCNO to bucket *BUCKET_PTR maintaining the order according
1861 their priority. ALLOCNO should be not in a bucket before the
1862 call. */
1863 static void
1864 add_allocno_to_ordered_bucket (ira_allocno_t allocno,
1865 ira_allocno_t *bucket_ptr)
1867 ira_allocno_t before, after;
1869 if (bucket_ptr == &uncolorable_allocno_bucket
1870 && ALLOCNO_CLASS (allocno) != NO_REGS)
1872 uncolorable_allocnos_num++;
1873 ira_assert (uncolorable_allocnos_num > 0);
1875 for (before = *bucket_ptr, after = NULL;
1876 before != NULL;
1877 after = before,
1878 before = ALLOCNO_COLOR_DATA (before)->next_bucket_allocno)
1879 if (bucket_allocno_compare_func (&allocno, &before) < 0)
1880 break;
1881 ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno = before;
1882 ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno = after;
1883 if (after == NULL)
1884 *bucket_ptr = allocno;
1885 else
1886 ALLOCNO_COLOR_DATA (after)->next_bucket_allocno = allocno;
1887 if (before != NULL)
1888 ALLOCNO_COLOR_DATA (before)->prev_bucket_allocno = allocno;
1891 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
1892 the call. */
1893 static void
1894 delete_allocno_from_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
1896 ira_allocno_t prev_allocno, next_allocno;
1898 if (bucket_ptr == &uncolorable_allocno_bucket
1899 && ALLOCNO_CLASS (allocno) != NO_REGS)
1901 uncolorable_allocnos_num--;
1902 ira_assert (uncolorable_allocnos_num >= 0);
1904 prev_allocno = ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno;
1905 next_allocno = ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno;
1906 if (prev_allocno != NULL)
1907 ALLOCNO_COLOR_DATA (prev_allocno)->next_bucket_allocno = next_allocno;
1908 else
1910 ira_assert (*bucket_ptr == allocno);
1911 *bucket_ptr = next_allocno;
1913 if (next_allocno != NULL)
1914 ALLOCNO_COLOR_DATA (next_allocno)->prev_bucket_allocno = prev_allocno;
1917 /* Put allocno A onto the coloring stack without removing it from its
1918 bucket. Pushing allocno to the coloring stack can result in moving
1919 conflicting allocnos from the uncolorable bucket to the colorable
1920 one. */
1921 static void
1922 push_allocno_to_stack (ira_allocno_t a)
1924 enum reg_class aclass;
1925 allocno_color_data_t data, conflict_data;
1926 int size, i, n = ALLOCNO_NUM_OBJECTS (a);
1928 data = ALLOCNO_COLOR_DATA (a);
1929 data->in_graph_p = false;
1930 VEC_safe_push (ira_allocno_t, heap, allocno_stack_vec, a);
1931 aclass = ALLOCNO_CLASS (a);
1932 if (aclass == NO_REGS)
1933 return;
1934 size = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
1935 if (n > 1)
1937 /* We will deal with the subwords individually. */
1938 gcc_assert (size == ALLOCNO_NUM_OBJECTS (a));
1939 size = 1;
1941 for (i = 0; i < n; i++)
1943 ira_object_t obj = ALLOCNO_OBJECT (a, i);
1944 ira_object_t conflict_obj;
1945 ira_object_conflict_iterator oci;
1947 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1949 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1951 conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
1952 if (conflict_data->colorable_p
1953 || ! conflict_data->in_graph_p
1954 || ALLOCNO_ASSIGNED_P (conflict_a)
1955 || !(hard_reg_set_intersect_p
1956 (OBJECT_COLOR_DATA (obj)->profitable_hard_regs,
1957 OBJECT_COLOR_DATA (conflict_obj)->profitable_hard_regs)))
1958 continue;
1959 ira_assert (bitmap_bit_p (coloring_allocno_bitmap,
1960 ALLOCNO_NUM (conflict_a)));
1961 if (update_left_conflict_sizes_p (conflict_a, obj, size))
1963 delete_allocno_from_bucket
1964 (conflict_a, &uncolorable_allocno_bucket);
1965 add_allocno_to_ordered_bucket
1966 (conflict_a, &colorable_allocno_bucket);
1967 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
1969 fprintf (ira_dump_file, " Making");
1970 ira_print_expanded_allocno (conflict_a);
1971 fprintf (ira_dump_file, " colorable\n");
1979 /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
1980 The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
1981 static void
1982 remove_allocno_from_bucket_and_push (ira_allocno_t allocno, bool colorable_p)
1984 if (colorable_p)
1985 delete_allocno_from_bucket (allocno, &colorable_allocno_bucket);
1986 else
1987 delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
1988 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1990 fprintf (ira_dump_file, " Pushing");
1991 ira_print_expanded_allocno (allocno);
1992 if (colorable_p)
1993 fprintf (ira_dump_file, "(cost %d)\n",
1994 ALLOCNO_COLOR_DATA (allocno)->temp);
1995 else
1996 fprintf (ira_dump_file, "(potential spill: %spri=%d, cost=%d)\n",
1997 ALLOCNO_BAD_SPILL_P (allocno) ? "bad spill, " : "",
1998 allocno_spill_priority (allocno),
1999 ALLOCNO_COLOR_DATA (allocno)->temp);
2001 if (! colorable_p)
2002 ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p = true;
2003 push_allocno_to_stack (allocno);
2006 /* Put all allocnos from colorable bucket onto the coloring stack. */
2007 static void
2008 push_only_colorable (void)
2010 sort_bucket (&colorable_allocno_bucket, bucket_allocno_compare_func);
2011 for (;colorable_allocno_bucket != NULL;)
2012 remove_allocno_from_bucket_and_push (colorable_allocno_bucket, true);
2015 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
2016 loop given by its LOOP_NODE. */
2018 ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
2020 int freq, i;
2021 edge_iterator ei;
2022 edge e;
2023 VEC (edge, heap) *edges;
2025 ira_assert (loop_node->loop != NULL
2026 && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
2027 freq = 0;
2028 if (! exit_p)
2030 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
2031 if (e->src != loop_node->loop->latch
2032 && (regno < 0
2033 || (bitmap_bit_p (DF_LR_OUT (e->src), regno)
2034 && bitmap_bit_p (DF_LR_IN (e->dest), regno))))
2035 freq += EDGE_FREQUENCY (e);
2037 else
2039 edges = get_loop_exit_edges (loop_node->loop);
2040 FOR_EACH_VEC_ELT (edge, edges, i, e)
2041 if (regno < 0
2042 || (bitmap_bit_p (DF_LR_OUT (e->src), regno)
2043 && bitmap_bit_p (DF_LR_IN (e->dest), regno)))
2044 freq += EDGE_FREQUENCY (e);
2045 VEC_free (edge, heap, edges);
2048 return REG_FREQ_FROM_EDGE_FREQ (freq);
2051 /* Calculate and return the cost of putting allocno A into memory. */
2052 static int
2053 calculate_allocno_spill_cost (ira_allocno_t a)
2055 int regno, cost;
2056 enum machine_mode mode;
2057 enum reg_class rclass;
2058 ira_allocno_t parent_allocno;
2059 ira_loop_tree_node_t parent_node, loop_node;
2061 regno = ALLOCNO_REGNO (a);
2062 cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_UPDATED_CLASS_COST (a);
2063 if (ALLOCNO_CAP (a) != NULL)
2064 return cost;
2065 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
2066 if ((parent_node = loop_node->parent) == NULL)
2067 return cost;
2068 if ((parent_allocno = parent_node->regno_allocno_map[regno]) == NULL)
2069 return cost;
2070 mode = ALLOCNO_MODE (a);
2071 rclass = ALLOCNO_CLASS (a);
2072 if (ALLOCNO_HARD_REGNO (parent_allocno) < 0)
2073 cost -= (ira_memory_move_cost[mode][rclass][0]
2074 * ira_loop_edge_freq (loop_node, regno, true)
2075 + ira_memory_move_cost[mode][rclass][1]
2076 * ira_loop_edge_freq (loop_node, regno, false));
2077 else
2079 ira_init_register_move_cost_if_necessary (mode);
2080 cost += ((ira_memory_move_cost[mode][rclass][1]
2081 * ira_loop_edge_freq (loop_node, regno, true)
2082 + ira_memory_move_cost[mode][rclass][0]
2083 * ira_loop_edge_freq (loop_node, regno, false))
2084 - (ira_register_move_cost[mode][rclass][rclass]
2085 * (ira_loop_edge_freq (loop_node, regno, false)
2086 + ira_loop_edge_freq (loop_node, regno, true))));
2088 return cost;
2091 /* Used for sorting allocnos for spilling. */
2092 static inline int
2093 allocno_spill_priority_compare (ira_allocno_t a1, ira_allocno_t a2)
2095 int pri1, pri2, diff;
2097 if (ALLOCNO_BAD_SPILL_P (a1) && ! ALLOCNO_BAD_SPILL_P (a2))
2098 return 1;
2099 if (ALLOCNO_BAD_SPILL_P (a2) && ! ALLOCNO_BAD_SPILL_P (a1))
2100 return -1;
2101 pri1 = allocno_spill_priority (a1);
2102 pri2 = allocno_spill_priority (a2);
2103 if ((diff = pri1 - pri2) != 0)
2104 return diff;
2105 if ((diff
2106 = ALLOCNO_COLOR_DATA (a1)->temp - ALLOCNO_COLOR_DATA (a2)->temp) != 0)
2107 return diff;
2108 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2111 /* Used for sorting allocnos for spilling. */
2112 static int
2113 allocno_spill_sort_compare (const void *v1p, const void *v2p)
2115 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2116 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2118 return allocno_spill_priority_compare (p1, p2);
2121 /* Push allocnos to the coloring stack. The order of allocnos in the
2122 stack defines the order for the subsequent coloring. */
2123 static void
2124 push_allocnos_to_stack (void)
2126 ira_allocno_t a;
2127 int cost;
2129 /* Calculate uncolorable allocno spill costs. */
2130 for (a = uncolorable_allocno_bucket;
2131 a != NULL;
2132 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2133 if (ALLOCNO_CLASS (a) != NO_REGS)
2135 cost = calculate_allocno_spill_cost (a);
2136 /* ??? Remove cost of copies between the coalesced
2137 allocnos. */
2138 ALLOCNO_COLOR_DATA (a)->temp = cost;
2140 sort_bucket (&uncolorable_allocno_bucket, allocno_spill_sort_compare);
2141 for (;;)
2143 push_only_colorable ();
2144 a = uncolorable_allocno_bucket;
2145 if (a == NULL)
2146 break;
2147 remove_allocno_from_bucket_and_push (a, false);
2149 ira_assert (colorable_allocno_bucket == NULL
2150 && uncolorable_allocno_bucket == NULL);
2151 ira_assert (uncolorable_allocnos_num == 0);
2154 /* Pop the coloring stack and assign hard registers to the popped
2155 allocnos. */
2156 static void
2157 pop_allocnos_from_stack (void)
2159 ira_allocno_t allocno;
2160 enum reg_class aclass;
2162 for (;VEC_length (ira_allocno_t, allocno_stack_vec) != 0;)
2164 allocno = VEC_pop (ira_allocno_t, allocno_stack_vec);
2165 aclass = ALLOCNO_CLASS (allocno);
2166 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2168 fprintf (ira_dump_file, " Popping");
2169 ira_print_expanded_allocno (allocno);
2170 fprintf (ira_dump_file, " -- ");
2172 if (aclass == NO_REGS)
2174 ALLOCNO_HARD_REGNO (allocno) = -1;
2175 ALLOCNO_ASSIGNED_P (allocno) = true;
2176 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
2177 ira_assert
2178 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
2179 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2180 fprintf (ira_dump_file, "assign memory\n");
2182 else if (assign_hard_reg (allocno, false))
2184 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2185 fprintf (ira_dump_file, "assign reg %d\n",
2186 ALLOCNO_HARD_REGNO (allocno));
2188 else if (ALLOCNO_ASSIGNED_P (allocno))
2190 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2191 fprintf (ira_dump_file, "spill\n");
2193 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2197 /* Set up number of available hard registers for allocno A. */
2198 static void
2199 setup_allocno_available_regs_num (ira_allocno_t a)
2201 int i, j, n, hard_regno, hard_regs_num, nwords, nregs;
2202 enum reg_class aclass;
2203 enum machine_mode mode;
2204 allocno_color_data_t data;
2206 aclass = ALLOCNO_CLASS (a);
2207 data = ALLOCNO_COLOR_DATA (a);
2208 data->available_regs_num = 0;
2209 if (aclass == NO_REGS)
2210 return;
2211 hard_regs_num = ira_class_hard_regs_num[aclass];
2212 mode = ALLOCNO_MODE (a);
2213 nwords = ALLOCNO_NUM_OBJECTS (a);
2214 for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
2216 hard_regno = ira_class_hard_regs[aclass][i];
2217 nregs = hard_regno_nregs[hard_regno][mode];
2218 for (j = 0; j < nregs; j++)
2220 int k;
2221 int set_to_test_start = 0, set_to_test_end = nwords;
2223 if (nregs == nwords)
2225 if (WORDS_BIG_ENDIAN)
2226 set_to_test_start = nwords - j - 1;
2227 else
2228 set_to_test_start = j;
2229 set_to_test_end = set_to_test_start + 1;
2231 for (k = set_to_test_start; k < set_to_test_end; k++)
2233 ira_object_t obj = ALLOCNO_OBJECT (a, k);
2234 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
2236 /* Checking only profitable hard regs. */
2237 if (TEST_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2238 hard_regno + j)
2239 || ! TEST_HARD_REG_BIT (obj_data->profitable_hard_regs,
2240 hard_regno + j))
2241 break;
2243 if (k != set_to_test_end)
2244 break;
2246 if (j == nregs)
2247 n++;
2249 data->available_regs_num = n;
2250 if (internal_flag_ira_verbose <= 2 || ira_dump_file == NULL)
2251 return;
2252 fprintf
2253 (ira_dump_file,
2254 " Allocno a%dr%d of %s(%d) has %d avail. regs",
2255 ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2256 reg_class_names[aclass], ira_class_hard_regs_num[aclass], n);
2257 for (i = 0; i < nwords; i++)
2259 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2260 object_color_data_t obj_data = OBJECT_COLOR_DATA (obj);
2262 if (nwords != 1)
2264 if (i != 0)
2265 fprintf (ira_dump_file, ", ");
2266 fprintf (ira_dump_file, " obj %d", i);
2268 print_hard_reg_set (ira_dump_file, obj_data->profitable_hard_regs, false);
2269 fprintf (ira_dump_file, " (confl regs = ");
2270 print_hard_reg_set (ira_dump_file, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2271 false);
2272 fprintf (ira_dump_file, " ) %snode: ",
2273 hard_reg_set_equal_p (obj_data->profitable_hard_regs,
2274 obj_data->hard_regs_node->hard_regs->set)
2275 ? "" : "^");
2276 print_hard_reg_set (ira_dump_file,
2277 obj_data->hard_regs_node->hard_regs->set, false);
2280 fprintf (ira_dump_file, "\n");
2283 /* Put ALLOCNO in a bucket corresponding to its number and size of its
2284 conflicting allocnos and hard registers. */
2285 static void
2286 put_allocno_into_bucket (ira_allocno_t allocno)
2288 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2289 setup_allocno_available_regs_num (allocno);
2290 if (setup_left_conflict_sizes_p (allocno))
2291 add_allocno_to_bucket (allocno, &colorable_allocno_bucket);
2292 else
2293 add_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
2296 /* Map: allocno number -> allocno priority. */
2297 static int *allocno_priorities;
2299 /* Set up priorities for N allocnos in array
2300 CONSIDERATION_ALLOCNOS. */
2301 static void
2302 setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
2304 int i, length, nrefs, priority, max_priority, mult;
2305 ira_allocno_t a;
2307 max_priority = 0;
2308 for (i = 0; i < n; i++)
2310 a = consideration_allocnos[i];
2311 nrefs = ALLOCNO_NREFS (a);
2312 ira_assert (nrefs >= 0);
2313 mult = floor_log2 (ALLOCNO_NREFS (a)) + 1;
2314 ira_assert (mult >= 0);
2315 allocno_priorities[ALLOCNO_NUM (a)]
2316 = priority
2317 = (mult
2318 * (ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a))
2319 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
2320 if (priority < 0)
2321 priority = -priority;
2322 if (max_priority < priority)
2323 max_priority = priority;
2325 mult = max_priority == 0 ? 1 : INT_MAX / max_priority;
2326 for (i = 0; i < n; i++)
2328 a = consideration_allocnos[i];
2329 length = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2330 if (ALLOCNO_NUM_OBJECTS (a) > 1)
2331 length /= ALLOCNO_NUM_OBJECTS (a);
2332 if (length <= 0)
2333 length = 1;
2334 allocno_priorities[ALLOCNO_NUM (a)]
2335 = allocno_priorities[ALLOCNO_NUM (a)] * mult / length;
2339 /* Sort allocnos according to the profit of usage of a hard register
2340 instead of memory for them. */
2341 static int
2342 allocno_cost_compare_func (const void *v1p, const void *v2p)
2344 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2345 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2346 int c1, c2;
2348 c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_UPDATED_CLASS_COST (p1);
2349 c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_UPDATED_CLASS_COST (p2);
2350 if (c1 - c2)
2351 return c1 - c2;
2353 /* If regs are equally good, sort by allocno numbers, so that the
2354 results of qsort leave nothing to chance. */
2355 return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
2358 /* We used Chaitin-Briggs coloring to assign as many pseudos as
2359 possible to hard registers. Let us try to improve allocation with
2360 cost point of view. This function improves the allocation by
2361 spilling some allocnos and assigning the freed hard registers to
2362 other allocnos if it decreases the overall allocation cost. */
2363 static void
2364 improve_allocation (void)
2366 unsigned int i;
2367 int j, k, n, hregno, conflict_hregno, base_cost, class_size, word, nwords;
2368 int check, spill_cost, min_cost, nregs, conflict_nregs, r, best;
2369 bool try_p;
2370 enum reg_class aclass;
2371 enum machine_mode mode;
2372 int *allocno_costs;
2373 int costs[FIRST_PSEUDO_REGISTER];
2374 HARD_REG_SET conflicting_regs[2], profitable_hard_regs[2];
2375 ira_allocno_t a;
2376 bitmap_iterator bi;
2378 /* Clear counts used to process conflicting allocnos only once for
2379 each allocno. */
2380 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2381 ALLOCNO_COLOR_DATA (ira_allocnos[i])->temp = 0;
2382 check = n = 0;
2383 /* Process each allocno and try to assign a hard register to it by
2384 spilling some its conflicting allocnos. */
2385 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2387 a = ira_allocnos[i];
2388 ALLOCNO_COLOR_DATA (a)->temp = 0;
2389 if (empty_profitable_hard_regs (a))
2390 continue;
2391 check++;
2392 aclass = ALLOCNO_CLASS (a);
2393 allocno_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
2394 if (allocno_costs == NULL)
2395 allocno_costs = ALLOCNO_HARD_REG_COSTS (a);
2396 if ((hregno = ALLOCNO_HARD_REGNO (a)) < 0)
2397 base_cost = ALLOCNO_UPDATED_MEMORY_COST (a);
2398 else if (allocno_costs == NULL)
2399 /* It means that assigning a hard register is not profitable
2400 (we don't waste memory for hard register costs in this
2401 case). */
2402 continue;
2403 else
2404 base_cost = allocno_costs[ira_class_hard_reg_index[aclass][hregno]];
2405 try_p = false;
2406 setup_conflict_profitable_regs (a, false,
2407 conflicting_regs, profitable_hard_regs);
2408 class_size = ira_class_hard_regs_num[aclass];
2409 /* Set up cost improvement for usage of each profitable hard
2410 register for allocno A. */
2411 for (j = 0; j < class_size; j++)
2413 hregno = ira_class_hard_regs[aclass][j];
2414 if (! check_hard_reg_p (a, hregno,
2415 conflicting_regs, profitable_hard_regs))
2416 continue;
2417 ira_assert (ira_class_hard_reg_index[aclass][hregno] == j);
2418 k = allocno_costs == NULL ? 0 : j;
2419 costs[hregno] = (allocno_costs == NULL
2420 ? ALLOCNO_UPDATED_CLASS_COST (a) : allocno_costs[k]);
2421 costs[hregno] -= base_cost;
2422 if (costs[hregno] < 0)
2423 try_p = true;
2425 if (! try_p)
2426 /* There is no chance to improve the allocation cost by
2427 assigning hard register to allocno A even without spilling
2428 conflicting allocnos. */
2429 continue;
2430 mode = ALLOCNO_MODE (a);
2431 nwords = ALLOCNO_NUM_OBJECTS (a);
2432 /* Process each allocno conflicting with A and update the cost
2433 improvement for profitable hard registers of A. To use a
2434 hard register for A we need to spill some conflicting
2435 allocnos and that creates penalty for the cost
2436 improvement. */
2437 for (word = 0; word < nwords; word++)
2439 ira_object_t conflict_obj;
2440 ira_object_t obj = ALLOCNO_OBJECT (a, word);
2441 ira_object_conflict_iterator oci;
2443 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2445 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2447 if (ALLOCNO_COLOR_DATA (conflict_a)->temp == check)
2448 /* We already processed this conflicting allocno
2449 because we processed earlier another object of the
2450 conflicting allocno. */
2451 continue;
2452 ALLOCNO_COLOR_DATA (conflict_a)->temp = check;
2453 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
2454 continue;
2455 spill_cost = ALLOCNO_UPDATED_MEMORY_COST (conflict_a);
2456 k = (ira_class_hard_reg_index
2457 [ALLOCNO_CLASS (conflict_a)][conflict_hregno]);
2458 ira_assert (k >= 0);
2459 if ((allocno_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (conflict_a))
2460 != NULL)
2461 spill_cost -= allocno_costs[k];
2462 else if ((allocno_costs = ALLOCNO_HARD_REG_COSTS (conflict_a))
2463 != NULL)
2464 spill_cost -= allocno_costs[k];
2465 else
2466 spill_cost -= ALLOCNO_UPDATED_CLASS_COST (conflict_a);
2467 conflict_nregs
2468 = hard_regno_nregs[conflict_hregno][ALLOCNO_MODE (conflict_a)];
2469 for (r = conflict_hregno;
2470 r >= 0 && r + hard_regno_nregs[r][mode] > conflict_hregno;
2471 r--)
2472 if (check_hard_reg_p (a, r,
2473 conflicting_regs, profitable_hard_regs))
2474 costs[r] += spill_cost;
2475 for (r = conflict_hregno + 1;
2476 r < conflict_hregno + conflict_nregs;
2477 r++)
2478 if (check_hard_reg_p (a, r,
2479 conflicting_regs, profitable_hard_regs))
2480 costs[r] += spill_cost;
2483 min_cost = INT_MAX;
2484 best = -1;
2485 /* Now we choose hard register for A which results in highest
2486 allocation cost improvement. */
2487 for (j = 0; j < class_size; j++)
2489 hregno = ira_class_hard_regs[aclass][j];
2490 if (check_hard_reg_p (a, hregno,
2491 conflicting_regs, profitable_hard_regs)
2492 && min_cost > costs[hregno])
2494 best = hregno;
2495 min_cost = costs[hregno];
2498 if (min_cost >= 0)
2499 /* We are in a situation when assigning any hard register to A
2500 by spilling some conflicting allocnos does not improve the
2501 allocation cost. */
2502 continue;
2503 nregs = hard_regno_nregs[best][mode];
2504 /* Now spill conflicting allocnos which contain a hard register
2505 of A when we assign the best chosen hard register to it. */
2506 for (word = 0; word < nwords; word++)
2508 ira_object_t conflict_obj;
2509 ira_object_t obj = ALLOCNO_OBJECT (a, word);
2510 ira_object_conflict_iterator oci;
2512 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2514 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2516 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
2517 continue;
2518 conflict_nregs
2519 = hard_regno_nregs[conflict_hregno][ALLOCNO_MODE (conflict_a)];
2520 if (best + nregs <= conflict_hregno
2521 || conflict_hregno + conflict_nregs <= best)
2522 /* No intersection. */
2523 continue;
2524 ALLOCNO_HARD_REGNO (conflict_a) = -1;
2525 sorted_allocnos[n++] = conflict_a;
2526 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2527 fprintf (ira_dump_file, "Spilling a%dr%d for a%dr%d\n",
2528 ALLOCNO_NUM (conflict_a), ALLOCNO_REGNO (conflict_a),
2529 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2532 /* Assign the best chosen hard register to A. */
2533 ALLOCNO_HARD_REGNO (a) = best;
2534 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2535 fprintf (ira_dump_file, "Assigning %d to a%dr%d\n",
2536 best, ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2538 if (n == 0)
2539 return;
2540 /* We spilled some allocnos to assign their hard registers to other
2541 allocnos. The spilled allocnos are now in array
2542 'sorted_allocnos'. There is still a possibility that some of the
2543 spilled allocnos can get hard registers. So let us try assign
2544 them hard registers again (just a reminder -- function
2545 'assign_hard_reg' assigns hard registers only if it is possible
2546 and profitable). We process the spilled allocnos with biggest
2547 benefit to get hard register first -- see function
2548 'allocno_cost_compare_func'. */
2549 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
2550 allocno_cost_compare_func);
2551 for (j = 0; j < n; j++)
2553 a = sorted_allocnos[j];
2554 ALLOCNO_ASSIGNED_P (a) = false;
2555 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2557 fprintf (ira_dump_file, " ");
2558 ira_print_expanded_allocno (a);
2559 fprintf (ira_dump_file, " -- ");
2561 if (assign_hard_reg (a, false))
2563 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2564 fprintf (ira_dump_file, "assign hard reg %d\n",
2565 ALLOCNO_HARD_REGNO (a));
2567 else
2569 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2570 fprintf (ira_dump_file, "assign memory\n");
2575 /* Sort allocnos according to their priorities which are calculated
2576 analogous to ones in file `global.c'. */
2577 static int
2578 allocno_priority_compare_func (const void *v1p, const void *v2p)
2580 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2581 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2582 int pri1, pri2;
2584 pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
2585 pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
2586 if (pri2 != pri1)
2587 return SORTGT (pri2, pri1);
2589 /* If regs are equally good, sort by allocnos, so that the results of
2590 qsort leave nothing to chance. */
2591 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2594 /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
2595 taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
2596 static void
2597 color_allocnos (void)
2599 unsigned int i, n;
2600 bitmap_iterator bi;
2601 ira_allocno_t a;
2603 setup_profitable_hard_regs ();
2604 if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
2606 n = 0;
2607 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2609 a = ira_allocnos[i];
2610 if (ALLOCNO_CLASS (a) == NO_REGS)
2612 ALLOCNO_HARD_REGNO (a) = -1;
2613 ALLOCNO_ASSIGNED_P (a) = true;
2614 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
2615 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
2616 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2618 fprintf (ira_dump_file, " Spill");
2619 ira_print_expanded_allocno (a);
2620 fprintf (ira_dump_file, "\n");
2622 continue;
2624 sorted_allocnos[n++] = a;
2626 if (n != 0)
2628 setup_allocno_priorities (sorted_allocnos, n);
2629 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
2630 allocno_priority_compare_func);
2631 for (i = 0; i < n; i++)
2633 a = sorted_allocnos[i];
2634 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2636 fprintf (ira_dump_file, " ");
2637 ira_print_expanded_allocno (a);
2638 fprintf (ira_dump_file, " -- ");
2640 if (assign_hard_reg (a, false))
2642 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2643 fprintf (ira_dump_file, "assign hard reg %d\n",
2644 ALLOCNO_HARD_REGNO (a));
2646 else
2648 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2649 fprintf (ira_dump_file, "assign memory\n");
2654 else
2656 form_object_hard_regs_nodes_forest ();
2657 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2658 print_hard_regs_forest (ira_dump_file);
2659 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2661 a = ira_allocnos[i];
2662 if (ALLOCNO_CLASS (a) != NO_REGS && ! empty_profitable_hard_regs (a))
2663 ALLOCNO_COLOR_DATA (a)->in_graph_p = true;
2664 else
2666 ALLOCNO_HARD_REGNO (a) = -1;
2667 ALLOCNO_ASSIGNED_P (a) = true;
2668 /* We don't need updated costs anymore. */
2669 ira_free_allocno_updated_costs (a);
2670 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2672 fprintf (ira_dump_file, " Spill");
2673 ira_print_expanded_allocno (a);
2674 fprintf (ira_dump_file, "\n");
2678 /* Put the allocnos into the corresponding buckets. */
2679 colorable_allocno_bucket = NULL;
2680 uncolorable_allocno_bucket = NULL;
2681 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2683 a = ira_allocnos[i];
2684 if (ALLOCNO_COLOR_DATA (a)->in_graph_p)
2685 put_allocno_into_bucket (a);
2687 push_allocnos_to_stack ();
2688 pop_allocnos_from_stack ();
2689 finish_object_hard_regs_nodes_forest ();
2691 improve_allocation ();
2696 /* Output information about the loop given by its LOOP_TREE_NODE. */
2697 static void
2698 print_loop_title (ira_loop_tree_node_t loop_tree_node)
2700 unsigned int j;
2701 bitmap_iterator bi;
2702 ira_loop_tree_node_t subloop_node, dest_loop_node;
2703 edge e;
2704 edge_iterator ei;
2706 ira_assert (loop_tree_node->loop != NULL);
2707 fprintf (ira_dump_file,
2708 "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
2709 loop_tree_node->loop->num,
2710 (loop_tree_node->parent == NULL
2711 ? -1 : loop_tree_node->parent->loop->num),
2712 loop_tree_node->loop->header->index,
2713 loop_depth (loop_tree_node->loop));
2714 for (subloop_node = loop_tree_node->children;
2715 subloop_node != NULL;
2716 subloop_node = subloop_node->next)
2717 if (subloop_node->bb != NULL)
2719 fprintf (ira_dump_file, " %d", subloop_node->bb->index);
2720 FOR_EACH_EDGE (e, ei, subloop_node->bb->succs)
2721 if (e->dest != EXIT_BLOCK_PTR
2722 && ((dest_loop_node = IRA_BB_NODE (e->dest)->parent)
2723 != loop_tree_node))
2724 fprintf (ira_dump_file, "(->%d:l%d)",
2725 e->dest->index, dest_loop_node->loop->num);
2727 fprintf (ira_dump_file, "\n all:");
2728 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
2729 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
2730 fprintf (ira_dump_file, "\n modified regnos:");
2731 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
2732 fprintf (ira_dump_file, " %d", j);
2733 fprintf (ira_dump_file, "\n border:");
2734 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
2735 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
2736 fprintf (ira_dump_file, "\n Pressure:");
2737 for (j = 0; (int) j < ira_pressure_classes_num; j++)
2739 enum reg_class pclass;
2741 pclass = ira_pressure_classes[j];
2742 if (loop_tree_node->reg_pressure[pclass] == 0)
2743 continue;
2744 fprintf (ira_dump_file, " %s=%d", reg_class_names[pclass],
2745 loop_tree_node->reg_pressure[pclass]);
2747 fprintf (ira_dump_file, "\n");
2750 /* Color the allocnos inside loop (in the extreme case it can be all
2751 of the function) given the corresponding LOOP_TREE_NODE. The
2752 function is called for each loop during top-down traverse of the
2753 loop tree. */
2754 static void
2755 color_pass (ira_loop_tree_node_t loop_tree_node)
2757 int i, regno, hard_regno, index = -1, n, nobj;
2758 int cost, exit_freq, enter_freq;
2759 unsigned int j;
2760 bitmap_iterator bi;
2761 enum machine_mode mode;
2762 enum reg_class rclass, aclass, pclass;
2763 ira_allocno_t a, subloop_allocno;
2764 ira_loop_tree_node_t subloop_node;
2766 ira_assert (loop_tree_node->bb == NULL);
2767 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2768 print_loop_title (loop_tree_node);
2770 bitmap_copy (coloring_allocno_bitmap, loop_tree_node->all_allocnos);
2771 bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
2772 n = nobj = 0;
2773 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2775 a = ira_allocnos[j];
2776 n++;
2777 nobj += ALLOCNO_NUM_OBJECTS (a);
2778 if (! ALLOCNO_ASSIGNED_P (a))
2779 continue;
2780 bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
2782 allocno_color_data
2783 = (allocno_color_data_t) ira_allocate (sizeof (struct allocno_color_data)
2784 * n);
2785 memset (allocno_color_data, 0, sizeof (struct allocno_color_data) * n);
2786 object_color_data
2787 = (object_color_data_t) ira_allocate (sizeof (struct object_color_data)
2788 * nobj);
2789 memset (object_color_data, 0, sizeof (struct object_color_data) * nobj);
2790 n = nobj = 0;
2791 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2793 a = ira_allocnos[j];
2794 ALLOCNO_ADD_DATA (a) = allocno_color_data + n;
2795 n++;
2796 for (i = 0; i < ALLOCNO_NUM_OBJECTS (a); i++)
2798 OBJECT_ADD_DATA (ALLOCNO_OBJECT (a, i)) = object_color_data + nobj;
2799 nobj++;
2802 /* Color all mentioned allocnos including transparent ones. */
2803 color_allocnos ();
2804 /* Process caps. They are processed just once. */
2805 if (flag_ira_region == IRA_REGION_MIXED
2806 || flag_ira_region == IRA_REGION_ALL)
2807 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
2809 a = ira_allocnos[j];
2810 if (ALLOCNO_CAP_MEMBER (a) == NULL)
2811 continue;
2812 /* Remove from processing in the next loop. */
2813 bitmap_clear_bit (consideration_allocno_bitmap, j);
2814 rclass = ALLOCNO_CLASS (a);
2815 pclass = ira_pressure_class_translate[rclass];
2816 if (flag_ira_region == IRA_REGION_MIXED
2817 && (loop_tree_node->reg_pressure[pclass]
2818 <= ira_available_class_regs[pclass]))
2820 mode = ALLOCNO_MODE (a);
2821 hard_regno = ALLOCNO_HARD_REGNO (a);
2822 if (hard_regno >= 0)
2824 index = ira_class_hard_reg_index[rclass][hard_regno];
2825 ira_assert (index >= 0);
2827 regno = ALLOCNO_REGNO (a);
2828 subloop_allocno = ALLOCNO_CAP_MEMBER (a);
2829 subloop_node = ALLOCNO_LOOP_TREE_NODE (subloop_allocno);
2830 ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno));
2831 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
2832 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
2833 if (hard_regno >= 0)
2834 update_copy_costs (subloop_allocno, true);
2835 /* We don't need updated costs anymore: */
2836 ira_free_allocno_updated_costs (subloop_allocno);
2839 /* Update costs of the corresponding allocnos (not caps) in the
2840 subloops. */
2841 for (subloop_node = loop_tree_node->subloops;
2842 subloop_node != NULL;
2843 subloop_node = subloop_node->subloop_next)
2845 ira_assert (subloop_node->bb == NULL);
2846 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2848 a = ira_allocnos[j];
2849 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2850 mode = ALLOCNO_MODE (a);
2851 rclass = ALLOCNO_CLASS (a);
2852 pclass = ira_pressure_class_translate[rclass];
2853 hard_regno = ALLOCNO_HARD_REGNO (a);
2854 /* Use hard register class here. ??? */
2855 if (hard_regno >= 0)
2857 index = ira_class_hard_reg_index[rclass][hard_regno];
2858 ira_assert (index >= 0);
2860 regno = ALLOCNO_REGNO (a);
2861 /* ??? conflict costs */
2862 subloop_allocno = subloop_node->regno_allocno_map[regno];
2863 if (subloop_allocno == NULL
2864 || ALLOCNO_CAP (subloop_allocno) != NULL)
2865 continue;
2866 ira_assert (ALLOCNO_CLASS (subloop_allocno) == rclass);
2867 ira_assert (bitmap_bit_p (subloop_node->all_allocnos,
2868 ALLOCNO_NUM (subloop_allocno)));
2869 if ((flag_ira_region == IRA_REGION_MIXED)
2870 && (loop_tree_node->reg_pressure[pclass]
2871 <= ira_available_class_regs[pclass]))
2873 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
2875 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
2876 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
2877 if (hard_regno >= 0)
2878 update_copy_costs (subloop_allocno, true);
2879 /* We don't need updated costs anymore: */
2880 ira_free_allocno_updated_costs (subloop_allocno);
2882 continue;
2884 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
2885 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
2886 ira_assert (regno < ira_reg_equiv_len);
2887 if (ira_reg_equiv_invariant_p[regno]
2888 || ira_reg_equiv_const[regno] != NULL_RTX)
2890 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
2892 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
2893 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
2894 if (hard_regno >= 0)
2895 update_copy_costs (subloop_allocno, true);
2896 /* We don't need updated costs anymore: */
2897 ira_free_allocno_updated_costs (subloop_allocno);
2900 else if (hard_regno < 0)
2902 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
2903 -= ((ira_memory_move_cost[mode][rclass][1] * enter_freq)
2904 + (ira_memory_move_cost[mode][rclass][0] * exit_freq));
2906 else
2908 aclass = ALLOCNO_CLASS (subloop_allocno);
2909 ira_init_register_move_cost_if_necessary (mode);
2910 cost = (ira_register_move_cost[mode][rclass][rclass]
2911 * (exit_freq + enter_freq));
2912 ira_allocate_and_set_or_copy_costs
2913 (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno), aclass,
2914 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno),
2915 ALLOCNO_HARD_REG_COSTS (subloop_allocno));
2916 ira_allocate_and_set_or_copy_costs
2917 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno),
2918 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno));
2919 ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
2920 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
2921 -= cost;
2922 if (ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
2923 > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index])
2924 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
2925 = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index];
2926 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
2927 += (ira_memory_move_cost[mode][rclass][0] * enter_freq
2928 + ira_memory_move_cost[mode][rclass][1] * exit_freq);
2932 ira_free (object_color_data);
2933 ira_free (allocno_color_data);
2934 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
2936 a = ira_allocnos[j];
2937 ALLOCNO_ADD_DATA (a) = NULL;
2938 for (i = 0; i < ALLOCNO_NUM_OBJECTS (a); i++)
2939 OBJECT_ADD_DATA (a) = NULL;
2943 /* Initialize the common data for coloring and calls functions to do
2944 Chaitin-Briggs and regional coloring. */
2945 static void
2946 do_coloring (void)
2948 coloring_allocno_bitmap = ira_allocate_bitmap ();
2949 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2950 fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
2952 ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
2954 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2955 ira_print_disposition (ira_dump_file);
2957 ira_free_bitmap (coloring_allocno_bitmap);
2962 /* Move spill/restore code, which are to be generated in ira-emit.c,
2963 to less frequent points (if it is profitable) by reassigning some
2964 allocnos (in loop with subloops containing in another loop) to
2965 memory which results in longer live-range where the corresponding
2966 pseudo-registers will be in memory. */
2967 static void
2968 move_spill_restore (void)
2970 int cost, regno, hard_regno, hard_regno2, index;
2971 bool changed_p;
2972 int enter_freq, exit_freq;
2973 enum machine_mode mode;
2974 enum reg_class rclass;
2975 ira_allocno_t a, parent_allocno, subloop_allocno;
2976 ira_loop_tree_node_t parent, loop_node, subloop_node;
2977 ira_allocno_iterator ai;
2979 for (;;)
2981 changed_p = false;
2982 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2983 fprintf (ira_dump_file, "New iteration of spill/restore move\n");
2984 FOR_EACH_ALLOCNO (a, ai)
2986 regno = ALLOCNO_REGNO (a);
2987 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
2988 if (ALLOCNO_CAP_MEMBER (a) != NULL
2989 || ALLOCNO_CAP (a) != NULL
2990 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
2991 || loop_node->children == NULL
2992 /* don't do the optimization because it can create
2993 copies and the reload pass can spill the allocno set
2994 by copy although the allocno will not get memory
2995 slot. */
2996 || ira_reg_equiv_invariant_p[regno]
2997 || ira_reg_equiv_const[regno] != NULL_RTX
2998 || !bitmap_bit_p (loop_node->border_allocnos, ALLOCNO_NUM (a)))
2999 continue;
3000 mode = ALLOCNO_MODE (a);
3001 rclass = ALLOCNO_CLASS (a);
3002 index = ira_class_hard_reg_index[rclass][hard_regno];
3003 ira_assert (index >= 0);
3004 cost = (ALLOCNO_MEMORY_COST (a)
3005 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
3006 ? ALLOCNO_CLASS_COST (a)
3007 : ALLOCNO_HARD_REG_COSTS (a)[index]));
3008 ira_init_register_move_cost_if_necessary (mode);
3009 for (subloop_node = loop_node->subloops;
3010 subloop_node != NULL;
3011 subloop_node = subloop_node->subloop_next)
3013 ira_assert (subloop_node->bb == NULL);
3014 subloop_allocno = subloop_node->regno_allocno_map[regno];
3015 if (subloop_allocno == NULL)
3016 continue;
3017 ira_assert (rclass == ALLOCNO_CLASS (subloop_allocno));
3018 /* We have accumulated cost. To get the real cost of
3019 allocno usage in the loop we should subtract costs of
3020 the subloop allocnos. */
3021 cost -= (ALLOCNO_MEMORY_COST (subloop_allocno)
3022 - (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
3023 ? ALLOCNO_CLASS_COST (subloop_allocno)
3024 : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]));
3025 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
3026 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
3027 if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
3028 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3029 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3030 else
3032 cost
3033 += (ira_memory_move_cost[mode][rclass][0] * exit_freq
3034 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3035 if (hard_regno2 != hard_regno)
3036 cost -= (ira_register_move_cost[mode][rclass][rclass]
3037 * (exit_freq + enter_freq));
3040 if ((parent = loop_node->parent) != NULL
3041 && (parent_allocno = parent->regno_allocno_map[regno]) != NULL)
3043 ira_assert (rclass == ALLOCNO_CLASS (parent_allocno));
3044 exit_freq = ira_loop_edge_freq (loop_node, regno, true);
3045 enter_freq = ira_loop_edge_freq (loop_node, regno, false);
3046 if ((hard_regno2 = ALLOCNO_HARD_REGNO (parent_allocno)) < 0)
3047 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3048 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3049 else
3051 cost
3052 += (ira_memory_move_cost[mode][rclass][1] * exit_freq
3053 + ira_memory_move_cost[mode][rclass][0] * enter_freq);
3054 if (hard_regno2 != hard_regno)
3055 cost -= (ira_register_move_cost[mode][rclass][rclass]
3056 * (exit_freq + enter_freq));
3059 if (cost < 0)
3061 ALLOCNO_HARD_REGNO (a) = -1;
3062 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3064 fprintf
3065 (ira_dump_file,
3066 " Moving spill/restore for a%dr%d up from loop %d",
3067 ALLOCNO_NUM (a), regno, loop_node->loop->num);
3068 fprintf (ira_dump_file, " - profit %d\n", -cost);
3070 changed_p = true;
3073 if (! changed_p)
3074 break;
3080 /* Update current hard reg costs and current conflict hard reg costs
3081 for allocno A. It is done by processing its copies containing
3082 other allocnos already assigned. */
3083 static void
3084 update_curr_costs (ira_allocno_t a)
3086 int i, hard_regno, cost;
3087 enum machine_mode mode;
3088 enum reg_class aclass, rclass;
3089 ira_allocno_t another_a;
3090 ira_copy_t cp, next_cp;
3092 ira_free_allocno_updated_costs (a);
3093 ira_assert (! ALLOCNO_ASSIGNED_P (a));
3094 aclass = ALLOCNO_CLASS (a);
3095 if (aclass == NO_REGS)
3096 return;
3097 mode = ALLOCNO_MODE (a);
3098 ira_init_register_move_cost_if_necessary (mode);
3099 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3101 if (cp->first == a)
3103 next_cp = cp->next_first_allocno_copy;
3104 another_a = cp->second;
3106 else if (cp->second == a)
3108 next_cp = cp->next_second_allocno_copy;
3109 another_a = cp->first;
3111 else
3112 gcc_unreachable ();
3113 if (! ira_reg_classes_intersect_p[aclass][ALLOCNO_CLASS (another_a)]
3114 || ! ALLOCNO_ASSIGNED_P (another_a)
3115 || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
3116 continue;
3117 rclass = REGNO_REG_CLASS (hard_regno);
3118 i = ira_class_hard_reg_index[aclass][hard_regno];
3119 if (i < 0)
3120 continue;
3121 cost = (cp->first == a
3122 ? ira_register_move_cost[mode][rclass][aclass]
3123 : ira_register_move_cost[mode][aclass][rclass]);
3124 ira_allocate_and_set_or_copy_costs
3125 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a),
3126 ALLOCNO_HARD_REG_COSTS (a));
3127 ira_allocate_and_set_or_copy_costs
3128 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
3129 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
3130 ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3131 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3135 /* Try to assign hard registers to the unassigned allocnos and
3136 allocnos conflicting with them or conflicting with allocnos whose
3137 regno >= START_REGNO. The function is called after ira_flattening,
3138 so more allocnos (including ones created in ira-emit.c) will have a
3139 chance to get a hard register. We use simple assignment algorithm
3140 based on priorities. */
3141 void
3142 ira_reassign_conflict_allocnos (int start_regno)
3144 int i, allocnos_to_color_num;
3145 ira_allocno_t a;
3146 enum reg_class aclass;
3147 bitmap allocnos_to_color;
3148 ira_allocno_iterator ai;
3150 allocnos_to_color = ira_allocate_bitmap ();
3151 allocnos_to_color_num = 0;
3152 FOR_EACH_ALLOCNO (a, ai)
3154 int n = ALLOCNO_NUM_OBJECTS (a);
3156 if (! ALLOCNO_ASSIGNED_P (a)
3157 && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
3159 if (ALLOCNO_CLASS (a) != NO_REGS)
3160 sorted_allocnos[allocnos_to_color_num++] = a;
3161 else
3163 ALLOCNO_ASSIGNED_P (a) = true;
3164 ALLOCNO_HARD_REGNO (a) = -1;
3165 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3166 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3168 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
3170 if (ALLOCNO_REGNO (a) < start_regno
3171 || (aclass = ALLOCNO_CLASS (a)) == NO_REGS)
3172 continue;
3173 for (i = 0; i < n; i++)
3175 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3176 ira_object_t conflict_obj;
3177 ira_object_conflict_iterator oci;
3179 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3181 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3183 ira_assert (ira_reg_classes_intersect_p
3184 [aclass][ALLOCNO_CLASS (conflict_a)]);
3185 if (!bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
3186 continue;
3187 sorted_allocnos[allocnos_to_color_num++] = conflict_a;
3191 ira_free_bitmap (allocnos_to_color);
3192 if (allocnos_to_color_num > 1)
3194 setup_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
3195 qsort (sorted_allocnos, allocnos_to_color_num, sizeof (ira_allocno_t),
3196 allocno_priority_compare_func);
3198 for (i = 0; i < allocnos_to_color_num; i++)
3200 a = sorted_allocnos[i];
3201 ALLOCNO_ASSIGNED_P (a) = false;
3202 update_curr_costs (a);
3204 for (i = 0; i < allocnos_to_color_num; i++)
3206 a = sorted_allocnos[i];
3207 if (assign_hard_reg (a, true))
3209 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3210 fprintf
3211 (ira_dump_file,
3212 " Secondary allocation: assign hard reg %d to reg %d\n",
3213 ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
3220 /* This page contains functions used to find conflicts using allocno
3221 live ranges. */
3223 /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
3224 used to find a conflict for new allocnos or allocnos with the
3225 different allocno classes. */
3226 static bool
3227 allocnos_conflict_by_live_ranges_p (ira_allocno_t a1, ira_allocno_t a2)
3229 rtx reg1, reg2;
3230 int i, j;
3231 int n1 = ALLOCNO_NUM_OBJECTS (a1);
3232 int n2 = ALLOCNO_NUM_OBJECTS (a2);
3234 if (a1 == a2)
3235 return false;
3236 reg1 = regno_reg_rtx[ALLOCNO_REGNO (a1)];
3237 reg2 = regno_reg_rtx[ALLOCNO_REGNO (a2)];
3238 if (reg1 != NULL && reg2 != NULL
3239 && ORIGINAL_REGNO (reg1) == ORIGINAL_REGNO (reg2))
3240 return false;
3242 for (i = 0; i < n1; i++)
3244 ira_object_t c1 = ALLOCNO_OBJECT (a1, i);
3246 for (j = 0; j < n2; j++)
3248 ira_object_t c2 = ALLOCNO_OBJECT (a2, j);
3250 if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1),
3251 OBJECT_LIVE_RANGES (c2)))
3252 return true;
3255 return false;
3258 #ifdef ENABLE_IRA_CHECKING
3260 /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
3261 intersect. This should be used when there is only one region.
3262 Currently this is used during reload. */
3263 static bool
3264 conflict_by_live_ranges_p (int regno1, int regno2)
3266 ira_allocno_t a1, a2;
3268 ira_assert (regno1 >= FIRST_PSEUDO_REGISTER
3269 && regno2 >= FIRST_PSEUDO_REGISTER);
3270 /* Reg info caclulated by dataflow infrastructure can be different
3271 from one calculated by regclass. */
3272 if ((a1 = ira_loop_tree_root->regno_allocno_map[regno1]) == NULL
3273 || (a2 = ira_loop_tree_root->regno_allocno_map[regno2]) == NULL)
3274 return false;
3275 return allocnos_conflict_by_live_ranges_p (a1, a2);
3278 #endif
3282 /* This page contains code to coalesce memory stack slots used by
3283 spilled allocnos. This results in smaller stack frame, better data
3284 locality, and in smaller code for some architectures like
3285 x86/x86_64 where insn size depends on address displacement value.
3286 On the other hand, it can worsen insn scheduling after the RA but
3287 in practice it is less important than smaller stack frames. */
3289 /* TRUE if we coalesced some allocnos. In other words, if we got
3290 loops formed by members first_coalesced_allocno and
3291 next_coalesced_allocno containing more one allocno. */
3292 static bool allocno_coalesced_p;
3294 /* Bitmap used to prevent a repeated allocno processing because of
3295 coalescing. */
3296 static bitmap processed_coalesced_allocno_bitmap;
3298 /* See below. */
3299 typedef struct coalesce_data *coalesce_data_t;
3301 /* To decrease footprint of ira_allocno structure we store all data
3302 needed only for coalescing in the following structure. */
3303 struct coalesce_data
3305 /* Coalesced allocnos form a cyclic list. One allocno given by
3306 FIRST represents all coalesced allocnos. The
3307 list is chained by NEXT. */
3308 ira_allocno_t first;
3309 ira_allocno_t next;
3310 int temp;
3313 /* Container for storing allocno data concerning coalescing. */
3314 static coalesce_data_t allocno_coalesce_data;
3316 /* Macro to access the data concerning coalescing. */
3317 #define ALLOCNO_COALESCE_DATA(a) ((coalesce_data_t) ALLOCNO_ADD_DATA (a))
3319 /* The function is used to sort allocnos according to their execution
3320 frequencies. */
3321 static int
3322 copy_freq_compare_func (const void *v1p, const void *v2p)
3324 ira_copy_t cp1 = *(const ira_copy_t *) v1p, cp2 = *(const ira_copy_t *) v2p;
3325 int pri1, pri2;
3327 pri1 = cp1->freq;
3328 pri2 = cp2->freq;
3329 if (pri2 - pri1)
3330 return pri2 - pri1;
3332 /* If freqencies are equal, sort by copies, so that the results of
3333 qsort leave nothing to chance. */
3334 return cp1->num - cp2->num;
3337 /* Merge two sets of coalesced allocnos given correspondingly by
3338 allocnos A1 and A2 (more accurately merging A2 set into A1
3339 set). */
3340 static void
3341 merge_allocnos (ira_allocno_t a1, ira_allocno_t a2)
3343 ira_allocno_t a, first, last, next;
3345 first = ALLOCNO_COALESCE_DATA (a1)->first;
3346 a = ALLOCNO_COALESCE_DATA (a2)->first;
3347 if (first == a)
3348 return;
3349 for (last = a2, a = ALLOCNO_COALESCE_DATA (a2)->next;;
3350 a = ALLOCNO_COALESCE_DATA (a)->next)
3352 ALLOCNO_COALESCE_DATA (a)->first = first;
3353 if (a == a2)
3354 break;
3355 last = a;
3357 next = allocno_coalesce_data[ALLOCNO_NUM (first)].next;
3358 allocno_coalesce_data[ALLOCNO_NUM (first)].next = a2;
3359 allocno_coalesce_data[ALLOCNO_NUM (last)].next = next;
3362 /* Return TRUE if there are conflicting allocnos from two sets of
3363 coalesced allocnos given correspondingly by allocnos A1 and A2. We
3364 use live ranges to find conflicts because conflicts are represented
3365 only for allocnos of the same allocno class and during the reload
3366 pass we coalesce allocnos for sharing stack memory slots. */
3367 static bool
3368 coalesced_allocno_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
3370 ira_allocno_t a, conflict_a;
3372 if (allocno_coalesced_p)
3374 bitmap_clear (processed_coalesced_allocno_bitmap);
3375 for (a = ALLOCNO_COALESCE_DATA (a1)->next;;
3376 a = ALLOCNO_COALESCE_DATA (a)->next)
3378 bitmap_set_bit (processed_coalesced_allocno_bitmap, ALLOCNO_NUM (a));
3379 if (a == a1)
3380 break;
3383 for (a = ALLOCNO_COALESCE_DATA (a2)->next;;
3384 a = ALLOCNO_COALESCE_DATA (a)->next)
3386 for (conflict_a = ALLOCNO_COALESCE_DATA (a1)->next;;
3387 conflict_a = ALLOCNO_COALESCE_DATA (conflict_a)->next)
3389 if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
3390 return true;
3391 if (conflict_a == a1)
3392 break;
3394 if (a == a2)
3395 break;
3397 return false;
3400 /* The major function for aggressive allocno coalescing. We coalesce
3401 only spilled allocnos. If some allocnos have been coalesced, we
3402 set up flag allocno_coalesced_p. */
3403 static void
3404 coalesce_allocnos (void)
3406 ira_allocno_t a;
3407 ira_copy_t cp, next_cp, *sorted_copies;
3408 unsigned int j;
3409 int i, n, cp_num, regno;
3410 bitmap_iterator bi;
3412 sorted_copies = (ira_copy_t *) ira_allocate (ira_copies_num
3413 * sizeof (ira_copy_t));
3414 cp_num = 0;
3415 /* Collect copies. */
3416 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
3418 a = ira_allocnos[j];
3419 regno = ALLOCNO_REGNO (a);
3420 if (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
3421 || (regno < ira_reg_equiv_len
3422 && (ira_reg_equiv_const[regno] != NULL_RTX
3423 || ira_reg_equiv_invariant_p[regno])))
3424 continue;
3425 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3427 if (cp->first == a)
3429 next_cp = cp->next_first_allocno_copy;
3430 regno = ALLOCNO_REGNO (cp->second);
3431 /* For priority coloring we coalesce allocnos only with
3432 the same allocno class not with intersected allocno
3433 classes as it were possible. It is done for
3434 simplicity. */
3435 if ((cp->insn != NULL || cp->constraint_p)
3436 && ALLOCNO_ASSIGNED_P (cp->second)
3437 && ALLOCNO_HARD_REGNO (cp->second) < 0
3438 && (regno >= ira_reg_equiv_len
3439 || (! ira_reg_equiv_invariant_p[regno]
3440 && ira_reg_equiv_const[regno] == NULL_RTX)))
3441 sorted_copies[cp_num++] = cp;
3443 else if (cp->second == a)
3444 next_cp = cp->next_second_allocno_copy;
3445 else
3446 gcc_unreachable ();
3449 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
3450 /* Coalesced copies, most frequently executed first. */
3451 for (; cp_num != 0;)
3453 for (i = 0; i < cp_num; i++)
3455 cp = sorted_copies[i];
3456 if (! coalesced_allocno_conflict_p (cp->first, cp->second))
3458 allocno_coalesced_p = true;
3459 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3460 fprintf
3461 (ira_dump_file,
3462 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
3463 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
3464 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
3465 cp->freq);
3466 merge_allocnos (cp->first, cp->second);
3467 i++;
3468 break;
3471 /* Collect the rest of copies. */
3472 for (n = 0; i < cp_num; i++)
3474 cp = sorted_copies[i];
3475 if (allocno_coalesce_data[ALLOCNO_NUM (cp->first)].first
3476 != allocno_coalesce_data[ALLOCNO_NUM (cp->second)].first)
3477 sorted_copies[n++] = cp;
3479 cp_num = n;
3481 ira_free (sorted_copies);
3484 /* Usage cost and order number of coalesced allocno set to which
3485 given pseudo register belongs to. */
3486 static int *regno_coalesced_allocno_cost;
3487 static int *regno_coalesced_allocno_num;
3489 /* Sort pseudos according frequencies of coalesced allocno sets they
3490 belong to (putting most frequently ones first), and according to
3491 coalesced allocno set order numbers. */
3492 static int
3493 coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
3495 const int regno1 = *(const int *) v1p;
3496 const int regno2 = *(const int *) v2p;
3497 int diff;
3499 if ((diff = (regno_coalesced_allocno_cost[regno2]
3500 - regno_coalesced_allocno_cost[regno1])) != 0)
3501 return diff;
3502 if ((diff = (regno_coalesced_allocno_num[regno1]
3503 - regno_coalesced_allocno_num[regno2])) != 0)
3504 return diff;
3505 return regno1 - regno2;
3508 /* Widest width in which each pseudo reg is referred to (via subreg).
3509 It is used for sorting pseudo registers. */
3510 static unsigned int *regno_max_ref_width;
3512 /* Redefine STACK_GROWS_DOWNWARD in terms of 0 or 1. */
3513 #ifdef STACK_GROWS_DOWNWARD
3514 # undef STACK_GROWS_DOWNWARD
3515 # define STACK_GROWS_DOWNWARD 1
3516 #else
3517 # define STACK_GROWS_DOWNWARD 0
3518 #endif
3520 /* Sort pseudos according their slot numbers (putting ones with
3521 smaller numbers first, or last when the frame pointer is not
3522 needed). */
3523 static int
3524 coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
3526 const int regno1 = *(const int *) v1p;
3527 const int regno2 = *(const int *) v2p;
3528 ira_allocno_t a1 = ira_regno_allocno_map[regno1];
3529 ira_allocno_t a2 = ira_regno_allocno_map[regno2];
3530 int diff, slot_num1, slot_num2;
3531 int total_size1, total_size2;
3533 if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
3535 if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
3536 return regno1 - regno2;
3537 return 1;
3539 else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
3540 return -1;
3541 slot_num1 = -ALLOCNO_HARD_REGNO (a1);
3542 slot_num2 = -ALLOCNO_HARD_REGNO (a2);
3543 if ((diff = slot_num1 - slot_num2) != 0)
3544 return (frame_pointer_needed
3545 || !FRAME_GROWS_DOWNWARD == STACK_GROWS_DOWNWARD ? diff : -diff);
3546 total_size1 = MAX (PSEUDO_REGNO_BYTES (regno1),
3547 regno_max_ref_width[regno1]);
3548 total_size2 = MAX (PSEUDO_REGNO_BYTES (regno2),
3549 regno_max_ref_width[regno2]);
3550 if ((diff = total_size2 - total_size1) != 0)
3551 return diff;
3552 return regno1 - regno2;
3555 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
3556 for coalesced allocno sets containing allocnos with their regnos
3557 given in array PSEUDO_REGNOS of length N. */
3558 static void
3559 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
3561 int i, num, regno, cost;
3562 ira_allocno_t allocno, a;
3564 for (num = i = 0; i < n; i++)
3566 regno = pseudo_regnos[i];
3567 allocno = ira_regno_allocno_map[regno];
3568 if (allocno == NULL)
3570 regno_coalesced_allocno_cost[regno] = 0;
3571 regno_coalesced_allocno_num[regno] = ++num;
3572 continue;
3574 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
3575 continue;
3576 num++;
3577 for (cost = 0, a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3578 a = ALLOCNO_COALESCE_DATA (a)->next)
3580 cost += ALLOCNO_FREQ (a);
3581 if (a == allocno)
3582 break;
3584 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3585 a = ALLOCNO_COALESCE_DATA (a)->next)
3587 regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
3588 regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
3589 if (a == allocno)
3590 break;
3595 /* Collect spilled allocnos representing coalesced allocno sets (the
3596 first coalesced allocno). The collected allocnos are returned
3597 through array SPILLED_COALESCED_ALLOCNOS. The function returns the
3598 number of the collected allocnos. The allocnos are given by their
3599 regnos in array PSEUDO_REGNOS of length N. */
3600 static int
3601 collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
3602 ira_allocno_t *spilled_coalesced_allocnos)
3604 int i, num, regno;
3605 ira_allocno_t allocno;
3607 for (num = i = 0; i < n; i++)
3609 regno = pseudo_regnos[i];
3610 allocno = ira_regno_allocno_map[regno];
3611 if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
3612 || ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
3613 continue;
3614 spilled_coalesced_allocnos[num++] = allocno;
3616 return num;
3619 /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
3620 given slot contains live ranges of coalesced allocnos assigned to
3621 given slot. */
3622 static live_range_t *slot_coalesced_allocnos_live_ranges;
3624 /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
3625 ranges intersected with live ranges of coalesced allocnos assigned
3626 to slot with number N. */
3627 static bool
3628 slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno, int n)
3630 ira_allocno_t a;
3632 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3633 a = ALLOCNO_COALESCE_DATA (a)->next)
3635 int i;
3636 int nr = ALLOCNO_NUM_OBJECTS (a);
3638 for (i = 0; i < nr; i++)
3640 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3642 if (ira_live_ranges_intersect_p
3643 (slot_coalesced_allocnos_live_ranges[n],
3644 OBJECT_LIVE_RANGES (obj)))
3645 return true;
3647 if (a == allocno)
3648 break;
3650 return false;
3653 /* Update live ranges of slot to which coalesced allocnos represented
3654 by ALLOCNO were assigned. */
3655 static void
3656 setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno)
3658 int i, n;
3659 ira_allocno_t a;
3660 live_range_t r;
3662 n = ALLOCNO_COALESCE_DATA (allocno)->temp;
3663 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3664 a = ALLOCNO_COALESCE_DATA (a)->next)
3666 int nr = ALLOCNO_NUM_OBJECTS (a);
3667 for (i = 0; i < nr; i++)
3669 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3671 r = ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj));
3672 slot_coalesced_allocnos_live_ranges[n]
3673 = ira_merge_live_ranges
3674 (slot_coalesced_allocnos_live_ranges[n], r);
3676 if (a == allocno)
3677 break;
3681 /* We have coalesced allocnos involving in copies. Coalesce allocnos
3682 further in order to share the same memory stack slot. Allocnos
3683 representing sets of allocnos coalesced before the call are given
3684 in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
3685 some allocnos were coalesced in the function. */
3686 static bool
3687 coalesce_spill_slots (ira_allocno_t *spilled_coalesced_allocnos, int num)
3689 int i, j, n, last_coalesced_allocno_num;
3690 ira_allocno_t allocno, a;
3691 bool merged_p = false;
3692 bitmap set_jump_crosses = regstat_get_setjmp_crosses ();
3694 slot_coalesced_allocnos_live_ranges
3695 = (live_range_t *) ira_allocate (sizeof (live_range_t) * ira_allocnos_num);
3696 memset (slot_coalesced_allocnos_live_ranges, 0,
3697 sizeof (live_range_t) * ira_allocnos_num);
3698 last_coalesced_allocno_num = 0;
3699 /* Coalesce non-conflicting spilled allocnos preferring most
3700 frequently used. */
3701 for (i = 0; i < num; i++)
3703 allocno = spilled_coalesced_allocnos[i];
3704 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
3705 || bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (allocno))
3706 || (ALLOCNO_REGNO (allocno) < ira_reg_equiv_len
3707 && (ira_reg_equiv_const[ALLOCNO_REGNO (allocno)] != NULL_RTX
3708 || ira_reg_equiv_invariant_p[ALLOCNO_REGNO (allocno)])))
3709 continue;
3710 for (j = 0; j < i; j++)
3712 a = spilled_coalesced_allocnos[j];
3713 n = ALLOCNO_COALESCE_DATA (a)->temp;
3714 if (ALLOCNO_COALESCE_DATA (a)->first == a
3715 && ! bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (a))
3716 && (ALLOCNO_REGNO (a) >= ira_reg_equiv_len
3717 || (! ira_reg_equiv_invariant_p[ALLOCNO_REGNO (a)]
3718 && ira_reg_equiv_const[ALLOCNO_REGNO (a)] == NULL_RTX))
3719 && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno, n))
3720 break;
3722 if (j >= i)
3724 /* No coalescing: set up number for coalesced allocnos
3725 represented by ALLOCNO. */
3726 ALLOCNO_COALESCE_DATA (allocno)->temp = last_coalesced_allocno_num++;
3727 setup_slot_coalesced_allocno_live_ranges (allocno);
3729 else
3731 allocno_coalesced_p = true;
3732 merged_p = true;
3733 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3734 fprintf (ira_dump_file,
3735 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
3736 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
3737 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3738 ALLOCNO_COALESCE_DATA (allocno)->temp
3739 = ALLOCNO_COALESCE_DATA (a)->temp;
3740 setup_slot_coalesced_allocno_live_ranges (allocno);
3741 merge_allocnos (a, allocno);
3742 ira_assert (ALLOCNO_COALESCE_DATA (a)->first == a);
3745 for (i = 0; i < ira_allocnos_num; i++)
3746 ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges[i]);
3747 ira_free (slot_coalesced_allocnos_live_ranges);
3748 return merged_p;
3751 /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
3752 subsequent assigning stack slots to them in the reload pass. To do
3753 this we coalesce spilled allocnos first to decrease the number of
3754 memory-memory move insns. This function is called by the
3755 reload. */
3756 void
3757 ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
3758 unsigned int *reg_max_ref_width)
3760 int max_regno = max_reg_num ();
3761 int i, regno, num, slot_num;
3762 ira_allocno_t allocno, a;
3763 ira_allocno_iterator ai;
3764 ira_allocno_t *spilled_coalesced_allocnos;
3766 /* Set up allocnos can be coalesced. */
3767 coloring_allocno_bitmap = ira_allocate_bitmap ();
3768 for (i = 0; i < n; i++)
3770 regno = pseudo_regnos[i];
3771 allocno = ira_regno_allocno_map[regno];
3772 if (allocno != NULL)
3773 bitmap_set_bit (coloring_allocno_bitmap, ALLOCNO_NUM (allocno));
3775 allocno_coalesced_p = false;
3776 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
3777 allocno_coalesce_data
3778 = (coalesce_data_t) ira_allocate (sizeof (struct coalesce_data)
3779 * ira_allocnos_num);
3780 /* Initialize coalesce data for allocnos. */
3781 FOR_EACH_ALLOCNO (a, ai)
3783 ALLOCNO_ADD_DATA (a) = allocno_coalesce_data + ALLOCNO_NUM (a);
3784 ALLOCNO_COALESCE_DATA (a)->first = a;
3785 ALLOCNO_COALESCE_DATA (a)->next = a;
3787 coalesce_allocnos ();
3788 ira_free_bitmap (coloring_allocno_bitmap);
3789 regno_coalesced_allocno_cost
3790 = (int *) ira_allocate (max_regno * sizeof (int));
3791 regno_coalesced_allocno_num
3792 = (int *) ira_allocate (max_regno * sizeof (int));
3793 memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
3794 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
3795 /* Sort regnos according frequencies of the corresponding coalesced
3796 allocno sets. */
3797 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
3798 spilled_coalesced_allocnos
3799 = (ira_allocno_t *) ira_allocate (ira_allocnos_num
3800 * sizeof (ira_allocno_t));
3801 /* Collect allocnos representing the spilled coalesced allocno
3802 sets. */
3803 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
3804 spilled_coalesced_allocnos);
3805 if (flag_ira_share_spill_slots
3806 && coalesce_spill_slots (spilled_coalesced_allocnos, num))
3808 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
3809 qsort (pseudo_regnos, n, sizeof (int),
3810 coalesced_pseudo_reg_freq_compare);
3811 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
3812 spilled_coalesced_allocnos);
3814 ira_free_bitmap (processed_coalesced_allocno_bitmap);
3815 allocno_coalesced_p = false;
3816 /* Assign stack slot numbers to spilled allocno sets, use smaller
3817 numbers for most frequently used coalesced allocnos. -1 is
3818 reserved for dynamic search of stack slots for pseudos spilled by
3819 the reload. */
3820 slot_num = 1;
3821 for (i = 0; i < num; i++)
3823 allocno = spilled_coalesced_allocnos[i];
3824 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
3825 || ALLOCNO_HARD_REGNO (allocno) >= 0
3826 || (ALLOCNO_REGNO (allocno) < ira_reg_equiv_len
3827 && (ira_reg_equiv_const[ALLOCNO_REGNO (allocno)] != NULL_RTX
3828 || ira_reg_equiv_invariant_p[ALLOCNO_REGNO (allocno)])))
3829 continue;
3830 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3831 fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
3832 slot_num++;
3833 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3834 a = ALLOCNO_COALESCE_DATA (a)->next)
3836 ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
3837 ALLOCNO_HARD_REGNO (a) = -slot_num;
3838 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3839 fprintf (ira_dump_file, " a%dr%d(%d,%d)",
3840 ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a),
3841 MAX (PSEUDO_REGNO_BYTES (ALLOCNO_REGNO (a)),
3842 reg_max_ref_width[ALLOCNO_REGNO (a)]));
3844 if (a == allocno)
3845 break;
3847 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3848 fprintf (ira_dump_file, "\n");
3850 ira_spilled_reg_stack_slots_num = slot_num - 1;
3851 ira_free (spilled_coalesced_allocnos);
3852 /* Sort regnos according the slot numbers. */
3853 regno_max_ref_width = reg_max_ref_width;
3854 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
3855 FOR_EACH_ALLOCNO (a, ai)
3856 ALLOCNO_ADD_DATA (a) = NULL;
3857 ira_free (allocno_coalesce_data);
3858 ira_free (regno_coalesced_allocno_num);
3859 ira_free (regno_coalesced_allocno_cost);
3864 /* This page contains code used by the reload pass to improve the
3865 final code. */
3867 /* The function is called from reload to mark changes in the
3868 allocation of REGNO made by the reload. Remember that reg_renumber
3869 reflects the change result. */
3870 void
3871 ira_mark_allocation_change (int regno)
3873 ira_allocno_t a = ira_regno_allocno_map[regno];
3874 int old_hard_regno, hard_regno, cost;
3875 enum reg_class aclass = ALLOCNO_CLASS (a);
3877 ira_assert (a != NULL);
3878 hard_regno = reg_renumber[regno];
3879 if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
3880 return;
3881 if (old_hard_regno < 0)
3882 cost = -ALLOCNO_MEMORY_COST (a);
3883 else
3885 ira_assert (ira_class_hard_reg_index[aclass][old_hard_regno] >= 0);
3886 cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
3887 ? ALLOCNO_CLASS_COST (a)
3888 : ALLOCNO_HARD_REG_COSTS (a)
3889 [ira_class_hard_reg_index[aclass][old_hard_regno]]);
3890 update_copy_costs (a, false);
3892 ira_overall_cost -= cost;
3893 ALLOCNO_HARD_REGNO (a) = hard_regno;
3894 if (hard_regno < 0)
3896 ALLOCNO_HARD_REGNO (a) = -1;
3897 cost += ALLOCNO_MEMORY_COST (a);
3899 else if (ira_class_hard_reg_index[aclass][hard_regno] >= 0)
3901 cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
3902 ? ALLOCNO_CLASS_COST (a)
3903 : ALLOCNO_HARD_REG_COSTS (a)
3904 [ira_class_hard_reg_index[aclass][hard_regno]]);
3905 update_copy_costs (a, true);
3907 else
3908 /* Reload changed class of the allocno. */
3909 cost = 0;
3910 ira_overall_cost += cost;
3913 /* This function is called when reload deletes memory-memory move. In
3914 this case we marks that the allocation of the corresponding
3915 allocnos should be not changed in future. Otherwise we risk to get
3916 a wrong code. */
3917 void
3918 ira_mark_memory_move_deletion (int dst_regno, int src_regno)
3920 ira_allocno_t dst = ira_regno_allocno_map[dst_regno];
3921 ira_allocno_t src = ira_regno_allocno_map[src_regno];
3923 ira_assert (dst != NULL && src != NULL
3924 && ALLOCNO_HARD_REGNO (dst) < 0
3925 && ALLOCNO_HARD_REGNO (src) < 0);
3926 ALLOCNO_DONT_REASSIGN_P (dst) = true;
3927 ALLOCNO_DONT_REASSIGN_P (src) = true;
3930 /* Try to assign a hard register (except for FORBIDDEN_REGS) to
3931 allocno A and return TRUE in the case of success. */
3932 static bool
3933 allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
3935 int hard_regno;
3936 enum reg_class aclass;
3937 int regno = ALLOCNO_REGNO (a);
3938 HARD_REG_SET saved[2];
3939 int i, n;
3941 n = ALLOCNO_NUM_OBJECTS (a);
3942 for (i = 0; i < n; i++)
3944 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3945 COPY_HARD_REG_SET (saved[i], OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
3946 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), forbidden_regs);
3947 if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
3948 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
3949 call_used_reg_set);
3951 ALLOCNO_ASSIGNED_P (a) = false;
3952 aclass = ALLOCNO_CLASS (a);
3953 update_curr_costs (a);
3954 assign_hard_reg (a, true);
3955 hard_regno = ALLOCNO_HARD_REGNO (a);
3956 reg_renumber[regno] = hard_regno;
3957 if (hard_regno < 0)
3958 ALLOCNO_HARD_REGNO (a) = -1;
3959 else
3961 ira_assert (ira_class_hard_reg_index[aclass][hard_regno] >= 0);
3962 ira_overall_cost
3963 -= (ALLOCNO_MEMORY_COST (a)
3964 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
3965 ? ALLOCNO_CLASS_COST (a)
3966 : ALLOCNO_HARD_REG_COSTS (a)[ira_class_hard_reg_index
3967 [aclass][hard_regno]]));
3968 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
3969 && ! ira_hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
3970 call_used_reg_set))
3972 ira_assert (flag_caller_saves);
3973 caller_save_needed = 1;
3977 /* If we found a hard register, modify the RTL for the pseudo
3978 register to show the hard register, and mark the pseudo register
3979 live. */
3980 if (reg_renumber[regno] >= 0)
3982 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3983 fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
3984 SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
3985 mark_home_live (regno);
3987 else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3988 fprintf (ira_dump_file, "\n");
3989 for (i = 0; i < n; i++)
3991 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3992 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), saved[i]);
3994 return reg_renumber[regno] >= 0;
3997 /* Sort pseudos according their usage frequencies (putting most
3998 frequently ones first). */
3999 static int
4000 pseudo_reg_compare (const void *v1p, const void *v2p)
4002 int regno1 = *(const int *) v1p;
4003 int regno2 = *(const int *) v2p;
4004 int diff;
4006 if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
4007 return diff;
4008 return regno1 - regno2;
4011 /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
4012 NUM of them) or spilled pseudos conflicting with pseudos in
4013 SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
4014 allocation has been changed. The function doesn't use
4015 BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
4016 PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
4017 is called by the reload pass at the end of each reload
4018 iteration. */
4019 bool
4020 ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
4021 HARD_REG_SET bad_spill_regs,
4022 HARD_REG_SET *pseudo_forbidden_regs,
4023 HARD_REG_SET *pseudo_previous_regs,
4024 bitmap spilled)
4026 int i, n, regno;
4027 bool changed_p;
4028 ira_allocno_t a;
4029 HARD_REG_SET forbidden_regs;
4030 bitmap temp = BITMAP_ALLOC (NULL);
4032 /* Add pseudos which conflict with pseudos already in
4033 SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
4034 to allocating in two steps as some of the conflicts might have
4035 a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
4036 for (i = 0; i < num; i++)
4037 bitmap_set_bit (temp, spilled_pseudo_regs[i]);
4039 for (i = 0, n = num; i < n; i++)
4041 int nr, j;
4042 int regno = spilled_pseudo_regs[i];
4043 bitmap_set_bit (temp, regno);
4045 a = ira_regno_allocno_map[regno];
4046 nr = ALLOCNO_NUM_OBJECTS (a);
4047 for (j = 0; j < nr; j++)
4049 ira_object_t conflict_obj;
4050 ira_object_t obj = ALLOCNO_OBJECT (a, j);
4051 ira_object_conflict_iterator oci;
4053 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
4055 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
4056 if (ALLOCNO_HARD_REGNO (conflict_a) < 0
4057 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
4058 && bitmap_set_bit (temp, ALLOCNO_REGNO (conflict_a)))
4060 spilled_pseudo_regs[num++] = ALLOCNO_REGNO (conflict_a);
4061 /* ?!? This seems wrong. */
4062 bitmap_set_bit (consideration_allocno_bitmap,
4063 ALLOCNO_NUM (conflict_a));
4069 if (num > 1)
4070 qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
4071 changed_p = false;
4072 /* Try to assign hard registers to pseudos from
4073 SPILLED_PSEUDO_REGS. */
4074 for (i = 0; i < num; i++)
4076 regno = spilled_pseudo_regs[i];
4077 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
4078 IOR_HARD_REG_SET (forbidden_regs, pseudo_forbidden_regs[regno]);
4079 IOR_HARD_REG_SET (forbidden_regs, pseudo_previous_regs[regno]);
4080 gcc_assert (reg_renumber[regno] < 0);
4081 a = ira_regno_allocno_map[regno];
4082 ira_mark_allocation_change (regno);
4083 ira_assert (reg_renumber[regno] < 0);
4084 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4085 fprintf (ira_dump_file,
4086 " Try Assign %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
4087 ALLOCNO_MEMORY_COST (a)
4088 - ALLOCNO_CLASS_COST (a));
4089 allocno_reload_assign (a, forbidden_regs);
4090 if (reg_renumber[regno] >= 0)
4092 CLEAR_REGNO_REG_SET (spilled, regno);
4093 changed_p = true;
4096 BITMAP_FREE (temp);
4097 return changed_p;
4100 /* The function is called by reload and returns already allocated
4101 stack slot (if any) for REGNO with given INHERENT_SIZE and
4102 TOTAL_SIZE. In the case of failure to find a slot which can be
4103 used for REGNO, the function returns NULL. */
4105 ira_reuse_stack_slot (int regno, unsigned int inherent_size,
4106 unsigned int total_size)
4108 unsigned int i;
4109 int slot_num, best_slot_num;
4110 int cost, best_cost;
4111 ira_copy_t cp, next_cp;
4112 ira_allocno_t another_allocno, allocno = ira_regno_allocno_map[regno];
4113 rtx x;
4114 bitmap_iterator bi;
4115 struct ira_spilled_reg_stack_slot *slot = NULL;
4117 ira_assert (inherent_size == PSEUDO_REGNO_BYTES (regno)
4118 && inherent_size <= total_size
4119 && ALLOCNO_HARD_REGNO (allocno) < 0);
4120 if (! flag_ira_share_spill_slots)
4121 return NULL_RTX;
4122 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4123 if (slot_num != -1)
4125 slot = &ira_spilled_reg_stack_slots[slot_num];
4126 x = slot->mem;
4128 else
4130 best_cost = best_slot_num = -1;
4131 x = NULL_RTX;
4132 /* It means that the pseudo was spilled in the reload pass, try
4133 to reuse a slot. */
4134 for (slot_num = 0;
4135 slot_num < ira_spilled_reg_stack_slots_num;
4136 slot_num++)
4138 slot = &ira_spilled_reg_stack_slots[slot_num];
4139 if (slot->mem == NULL_RTX)
4140 continue;
4141 if (slot->width < total_size
4142 || GET_MODE_SIZE (GET_MODE (slot->mem)) < inherent_size)
4143 continue;
4145 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4146 FIRST_PSEUDO_REGISTER, i, bi)
4148 another_allocno = ira_regno_allocno_map[i];
4149 if (allocnos_conflict_by_live_ranges_p (allocno,
4150 another_allocno))
4151 goto cont;
4153 for (cost = 0, cp = ALLOCNO_COPIES (allocno);
4154 cp != NULL;
4155 cp = next_cp)
4157 if (cp->first == allocno)
4159 next_cp = cp->next_first_allocno_copy;
4160 another_allocno = cp->second;
4162 else if (cp->second == allocno)
4164 next_cp = cp->next_second_allocno_copy;
4165 another_allocno = cp->first;
4167 else
4168 gcc_unreachable ();
4169 if (cp->insn == NULL_RTX)
4170 continue;
4171 if (bitmap_bit_p (&slot->spilled_regs,
4172 ALLOCNO_REGNO (another_allocno)))
4173 cost += cp->freq;
4175 if (cost > best_cost)
4177 best_cost = cost;
4178 best_slot_num = slot_num;
4180 cont:
4183 if (best_cost >= 0)
4185 slot_num = best_slot_num;
4186 slot = &ira_spilled_reg_stack_slots[slot_num];
4187 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4188 x = slot->mem;
4189 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4192 if (x != NULL_RTX)
4194 ira_assert (slot->width >= total_size);
4195 #ifdef ENABLE_IRA_CHECKING
4196 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4197 FIRST_PSEUDO_REGISTER, i, bi)
4199 ira_assert (! conflict_by_live_ranges_p (regno, i));
4201 #endif
4202 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4203 if (internal_flag_ira_verbose > 3 && ira_dump_file)
4205 fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
4206 regno, REG_FREQ (regno), slot_num);
4207 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4208 FIRST_PSEUDO_REGISTER, i, bi)
4210 if ((unsigned) regno != i)
4211 fprintf (ira_dump_file, " %d", i);
4213 fprintf (ira_dump_file, "\n");
4216 return x;
4219 /* This is called by reload every time a new stack slot X with
4220 TOTAL_SIZE was allocated for REGNO. We store this info for
4221 subsequent ira_reuse_stack_slot calls. */
4222 void
4223 ira_mark_new_stack_slot (rtx x, int regno, unsigned int total_size)
4225 struct ira_spilled_reg_stack_slot *slot;
4226 int slot_num;
4227 ira_allocno_t allocno;
4229 ira_assert (PSEUDO_REGNO_BYTES (regno) <= total_size);
4230 allocno = ira_regno_allocno_map[regno];
4231 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4232 if (slot_num == -1)
4234 slot_num = ira_spilled_reg_stack_slots_num++;
4235 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4237 slot = &ira_spilled_reg_stack_slots[slot_num];
4238 INIT_REG_SET (&slot->spilled_regs);
4239 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4240 slot->mem = x;
4241 slot->width = total_size;
4242 if (internal_flag_ira_verbose > 3 && ira_dump_file)
4243 fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
4244 regno, REG_FREQ (regno), slot_num);
4248 /* Return spill cost for pseudo-registers whose numbers are in array
4249 REGNOS (with a negative number as an end marker) for reload with
4250 given IN and OUT for INSN. Return also number points (through
4251 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
4252 the register pressure is high, number of references of the
4253 pseudo-registers (through NREFS), number of callee-clobbered
4254 hard-registers occupied by the pseudo-registers (through
4255 CALL_USED_COUNT), and the first hard regno occupied by the
4256 pseudo-registers (through FIRST_HARD_REGNO). */
4257 static int
4258 calculate_spill_cost (int *regnos, rtx in, rtx out, rtx insn,
4259 int *excess_pressure_live_length,
4260 int *nrefs, int *call_used_count, int *first_hard_regno)
4262 int i, cost, regno, hard_regno, j, count, saved_cost, nregs;
4263 bool in_p, out_p;
4264 int length;
4265 ira_allocno_t a;
4267 *nrefs = 0;
4268 for (length = count = cost = i = 0;; i++)
4270 regno = regnos[i];
4271 if (regno < 0)
4272 break;
4273 *nrefs += REG_N_REFS (regno);
4274 hard_regno = reg_renumber[regno];
4275 ira_assert (hard_regno >= 0);
4276 a = ira_regno_allocno_map[regno];
4277 length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) / ALLOCNO_NUM_OBJECTS (a);
4278 cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
4279 nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
4280 for (j = 0; j < nregs; j++)
4281 if (! TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + j))
4282 break;
4283 if (j == nregs)
4284 count++;
4285 in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
4286 out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
4287 if ((in_p || out_p)
4288 && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
4290 saved_cost = 0;
4291 if (in_p)
4292 saved_cost += ira_memory_move_cost
4293 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][1];
4294 if (out_p)
4295 saved_cost
4296 += ira_memory_move_cost
4297 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][0];
4298 cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
4301 *excess_pressure_live_length = length;
4302 *call_used_count = count;
4303 hard_regno = -1;
4304 if (regnos[0] >= 0)
4306 hard_regno = reg_renumber[regnos[0]];
4308 *first_hard_regno = hard_regno;
4309 return cost;
4312 /* Return TRUE if spilling pseudo-registers whose numbers are in array
4313 REGNOS is better than spilling pseudo-registers with numbers in
4314 OTHER_REGNOS for reload with given IN and OUT for INSN. The
4315 function used by the reload pass to make better register spilling
4316 decisions. */
4317 bool
4318 ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
4319 rtx in, rtx out, rtx insn)
4321 int cost, other_cost;
4322 int length, other_length;
4323 int nrefs, other_nrefs;
4324 int call_used_count, other_call_used_count;
4325 int hard_regno, other_hard_regno;
4327 cost = calculate_spill_cost (regnos, in, out, insn,
4328 &length, &nrefs, &call_used_count, &hard_regno);
4329 other_cost = calculate_spill_cost (other_regnos, in, out, insn,
4330 &other_length, &other_nrefs,
4331 &other_call_used_count,
4332 &other_hard_regno);
4333 if (nrefs == 0 && other_nrefs != 0)
4334 return true;
4335 if (nrefs != 0 && other_nrefs == 0)
4336 return false;
4337 if (cost != other_cost)
4338 return cost < other_cost;
4339 if (length != other_length)
4340 return length > other_length;
4341 #ifdef REG_ALLOC_ORDER
4342 if (hard_regno >= 0 && other_hard_regno >= 0)
4343 return (inv_reg_alloc_order[hard_regno]
4344 < inv_reg_alloc_order[other_hard_regno]);
4345 #else
4346 if (call_used_count != other_call_used_count)
4347 return call_used_count > other_call_used_count;
4348 #endif
4349 return false;
4354 /* Allocate and initialize data necessary for assign_hard_reg. */
4355 void
4356 ira_initiate_assign (void)
4358 sorted_allocnos
4359 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4360 * ira_allocnos_num);
4361 consideration_allocno_bitmap = ira_allocate_bitmap ();
4362 initiate_cost_update ();
4363 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4366 /* Deallocate data used by assign_hard_reg. */
4367 void
4368 ira_finish_assign (void)
4370 ira_free (sorted_allocnos);
4371 ira_free_bitmap (consideration_allocno_bitmap);
4372 finish_cost_update ();
4373 ira_free (allocno_priorities);
4378 /* Entry function doing color-based register allocation. */
4379 static void
4380 color (void)
4382 allocno_stack_vec = VEC_alloc (ira_allocno_t, heap, ira_allocnos_num);
4383 memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
4384 ira_initiate_assign ();
4385 do_coloring ();
4386 ira_finish_assign ();
4387 VEC_free (ira_allocno_t, heap, allocno_stack_vec);
4388 move_spill_restore ();
4393 /* This page contains a simple register allocator without usage of
4394 allocno conflicts. This is used for fast allocation for -O0. */
4396 /* Do register allocation by not using allocno conflicts. It uses
4397 only allocno live ranges. The algorithm is close to Chow's
4398 priority coloring. */
4399 static void
4400 fast_allocation (void)
4402 int i, j, k, num, class_size, hard_regno;
4403 #ifdef STACK_REGS
4404 bool no_stack_reg_p;
4405 #endif
4406 enum reg_class aclass;
4407 enum machine_mode mode;
4408 ira_allocno_t a;
4409 ira_allocno_iterator ai;
4410 live_range_t r;
4411 HARD_REG_SET conflict_hard_regs, *used_hard_regs;
4413 sorted_allocnos = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4414 * ira_allocnos_num);
4415 num = 0;
4416 FOR_EACH_ALLOCNO (a, ai)
4417 sorted_allocnos[num++] = a;
4418 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4419 setup_allocno_priorities (sorted_allocnos, num);
4420 used_hard_regs = (HARD_REG_SET *) ira_allocate (sizeof (HARD_REG_SET)
4421 * ira_max_point);
4422 for (i = 0; i < ira_max_point; i++)
4423 CLEAR_HARD_REG_SET (used_hard_regs[i]);
4424 qsort (sorted_allocnos, num, sizeof (ira_allocno_t),
4425 allocno_priority_compare_func);
4426 for (i = 0; i < num; i++)
4428 int nr, l;
4430 a = sorted_allocnos[i];
4431 nr = ALLOCNO_NUM_OBJECTS (a);
4432 CLEAR_HARD_REG_SET (conflict_hard_regs);
4433 for (l = 0; l < nr; l++)
4435 ira_object_t obj = ALLOCNO_OBJECT (a, l);
4436 IOR_HARD_REG_SET (conflict_hard_regs,
4437 OBJECT_CONFLICT_HARD_REGS (obj));
4438 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4439 for (j = r->start; j <= r->finish; j++)
4440 IOR_HARD_REG_SET (conflict_hard_regs, used_hard_regs[j]);
4442 aclass = ALLOCNO_CLASS (a);
4443 ALLOCNO_ASSIGNED_P (a) = true;
4444 ALLOCNO_HARD_REGNO (a) = -1;
4445 if (hard_reg_set_subset_p (reg_class_contents[aclass],
4446 conflict_hard_regs))
4447 continue;
4448 mode = ALLOCNO_MODE (a);
4449 #ifdef STACK_REGS
4450 no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
4451 #endif
4452 class_size = ira_class_hard_regs_num[aclass];
4453 for (j = 0; j < class_size; j++)
4455 hard_regno = ira_class_hard_regs[aclass][j];
4456 #ifdef STACK_REGS
4457 if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
4458 && hard_regno <= LAST_STACK_REG)
4459 continue;
4460 #endif
4461 if (!ira_hard_reg_not_in_set_p (hard_regno, mode, conflict_hard_regs)
4462 || (TEST_HARD_REG_BIT
4463 (ira_prohibited_class_mode_regs[aclass][mode], hard_regno)))
4464 continue;
4465 ALLOCNO_HARD_REGNO (a) = hard_regno;
4466 for (l = 0; l < nr; l++)
4468 ira_object_t obj = ALLOCNO_OBJECT (a, l);
4469 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4470 for (k = r->start; k <= r->finish; k++)
4471 IOR_HARD_REG_SET (used_hard_regs[k],
4472 ira_reg_mode_hard_regset[hard_regno][mode]);
4474 break;
4477 ira_free (sorted_allocnos);
4478 ira_free (used_hard_regs);
4479 ira_free (allocno_priorities);
4480 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
4481 ira_print_disposition (ira_dump_file);
4486 /* Entry function doing coloring. */
4487 void
4488 ira_color (void)
4490 ira_allocno_t a;
4491 ira_allocno_iterator ai;
4493 /* Setup updated costs. */
4494 FOR_EACH_ALLOCNO (a, ai)
4496 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
4497 ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
4499 if (ira_conflicts_p)
4500 color ();
4501 else
4502 fast_allocation ();