* include/bits/basic_string.h (getline): Qualify call to prevent ADL
[official-gcc.git] / gcc / ira-color.c
blob36c3c879f5757de701b743ce651e068176389af2
1 /* IRA allocation based on graph coloring.
2 Copyright (C) 2006-2014 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "target.h"
28 #include "regs.h"
29 #include "flags.h"
30 #include "sbitmap.h"
31 #include "bitmap.h"
32 #include "hash-table.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 allocno_hard_regs *allocno_hard_regs_t;
44 /* The structure contains information about hard registers can be
45 assigned to allocnos. 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 allocno_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 int64_t cost;
59 typedef struct allocno_hard_regs_node *allocno_hard_regs_node_t;
61 /* A node representing allocno 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 allocno profitable hard
64 register set) which is a subset of one referred from given
65 node. */
66 struct allocno_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 allocno
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 allocno_hard_regs_t hard_regs;
86 /* Parent, first subnode, previous and next node with the same
87 parent in the forest. */
88 allocno_hard_regs_node_t parent, first, prev, next;
91 /* Info about changing hard reg costs of an allocno. */
92 struct update_cost_record
94 /* Hard regno for which we changed the cost. */
95 int hard_regno;
96 /* Divisor used when we changed the cost of HARD_REGNO. */
97 int divisor;
98 /* Next record for given allocno. */
99 struct update_cost_record *next;
102 /* To decrease footprint of ira_allocno structure we store all data
103 needed only for coloring in the following structure. */
104 struct allocno_color_data
106 /* TRUE value means that the allocno was not removed yet from the
107 conflicting graph during colouring. */
108 unsigned int in_graph_p : 1;
109 /* TRUE if it is put on the stack to make other allocnos
110 colorable. */
111 unsigned int may_be_spilled_p : 1;
112 /* TRUE if the allocno is trivially colorable. */
113 unsigned int colorable_p : 1;
114 /* Number of hard registers of the allocno class really
115 available for the allocno allocation. It is number of the
116 profitable hard regs. */
117 int available_regs_num;
118 /* Allocnos in a bucket (used in coloring) chained by the following
119 two members. */
120 ira_allocno_t next_bucket_allocno;
121 ira_allocno_t prev_bucket_allocno;
122 /* Used for temporary purposes. */
123 int temp;
124 /* Used to exclude repeated processing. */
125 int last_process;
126 /* Profitable hard regs available for this pseudo allocation. It
127 means that the set excludes unavailable hard regs and hard regs
128 conflicting with given pseudo. They should be of the allocno
129 class. */
130 HARD_REG_SET profitable_hard_regs;
131 /* The allocno hard registers node. */
132 allocno_hard_regs_node_t hard_regs_node;
133 /* Array of structures allocno_hard_regs_subnode representing
134 given allocno hard registers node (the 1st element in the array)
135 and all its subnodes in the tree (forest) of allocno hard
136 register nodes (see comments above). */
137 int hard_regs_subnodes_start;
138 /* The length of the previous array. */
139 int hard_regs_subnodes_num;
140 /* Records about updating allocno hard reg costs from copies. If
141 the allocno did not get expected hard register, these records are
142 used to restore original hard reg costs of allocnos connected to
143 this allocno by copies. */
144 struct update_cost_record *update_cost_records;
145 /* Threads. We collect allocnos connected by copies into threads
146 and try to assign hard regs to allocnos by threads. */
147 /* Allocno representing all thread. */
148 ira_allocno_t first_thread_allocno;
149 /* Allocnos in thread forms a cycle list through the following
150 member. */
151 ira_allocno_t next_thread_allocno;
152 /* All thread frequency. Defined only for first thread allocno. */
153 int thread_freq;
156 /* See above. */
157 typedef struct allocno_color_data *allocno_color_data_t;
159 /* Container for storing allocno data concerning coloring. */
160 static allocno_color_data_t allocno_color_data;
162 /* Macro to access the data concerning coloring. */
163 #define ALLOCNO_COLOR_DATA(a) ((allocno_color_data_t) ALLOCNO_ADD_DATA (a))
165 /* Used for finding allocno colorability to exclude repeated allocno
166 processing and for updating preferencing to exclude repeated
167 allocno processing during assignment. */
168 static int curr_allocno_process;
170 /* This file contains code for regional graph coloring, spill/restore
171 code placement optimization, and code helping the reload pass to do
172 a better job. */
174 /* Bitmap of allocnos which should be colored. */
175 static bitmap coloring_allocno_bitmap;
177 /* Bitmap of allocnos which should be taken into account during
178 coloring. In general case it contains allocnos from
179 coloring_allocno_bitmap plus other already colored conflicting
180 allocnos. */
181 static bitmap consideration_allocno_bitmap;
183 /* All allocnos sorted according their priorities. */
184 static ira_allocno_t *sorted_allocnos;
186 /* Vec representing the stack of allocnos used during coloring. */
187 static vec<ira_allocno_t> allocno_stack_vec;
189 /* Helper for qsort comparison callbacks - return a positive integer if
190 X > Y, or a negative value otherwise. Use a conditional expression
191 instead of a difference computation to insulate from possible overflow
192 issues, e.g. X - Y < 0 for some X > 0 and Y < 0. */
193 #define SORTGT(x,y) (((x) > (y)) ? 1 : -1)
197 /* Definition of vector of allocno hard registers. */
199 /* Vector of unique allocno hard registers. */
200 static vec<allocno_hard_regs_t> allocno_hard_regs_vec;
202 struct allocno_hard_regs_hasher : typed_noop_remove <allocno_hard_regs>
204 typedef allocno_hard_regs value_type;
205 typedef allocno_hard_regs compare_type;
206 static inline hashval_t hash (const value_type *);
207 static inline bool equal (const value_type *, const compare_type *);
210 /* Returns hash value for allocno hard registers V. */
211 inline hashval_t
212 allocno_hard_regs_hasher::hash (const value_type *hv)
214 return iterative_hash (&hv->set, sizeof (HARD_REG_SET), 0);
217 /* Compares allocno hard registers V1 and V2. */
218 inline bool
219 allocno_hard_regs_hasher::equal (const value_type *hv1, const compare_type *hv2)
221 return hard_reg_set_equal_p (hv1->set, hv2->set);
224 /* Hash table of unique allocno hard registers. */
225 static hash_table<allocno_hard_regs_hasher> *allocno_hard_regs_htab;
227 /* Return allocno hard registers in the hash table equal to HV. */
228 static allocno_hard_regs_t
229 find_hard_regs (allocno_hard_regs_t hv)
231 return allocno_hard_regs_htab->find (hv);
234 /* Insert allocno hard registers HV in the hash table (if it is not
235 there yet) and return the value which in the table. */
236 static allocno_hard_regs_t
237 insert_hard_regs (allocno_hard_regs_t hv)
239 allocno_hard_regs **slot = allocno_hard_regs_htab->find_slot (hv, INSERT);
241 if (*slot == NULL)
242 *slot = hv;
243 return *slot;
246 /* Initialize data concerning allocno hard registers. */
247 static void
248 init_allocno_hard_regs (void)
250 allocno_hard_regs_vec.create (200);
251 allocno_hard_regs_htab
252 = new hash_table<allocno_hard_regs_hasher> (200);
255 /* Add (or update info about) allocno hard registers with SET and
256 COST. */
257 static allocno_hard_regs_t
258 add_allocno_hard_regs (HARD_REG_SET set, int64_t cost)
260 struct allocno_hard_regs temp;
261 allocno_hard_regs_t hv;
263 gcc_assert (! hard_reg_set_empty_p (set));
264 COPY_HARD_REG_SET (temp.set, set);
265 if ((hv = find_hard_regs (&temp)) != NULL)
266 hv->cost += cost;
267 else
269 hv = ((struct allocno_hard_regs *)
270 ira_allocate (sizeof (struct allocno_hard_regs)));
271 COPY_HARD_REG_SET (hv->set, set);
272 hv->cost = cost;
273 allocno_hard_regs_vec.safe_push (hv);
274 insert_hard_regs (hv);
276 return hv;
279 /* Finalize data concerning allocno hard registers. */
280 static void
281 finish_allocno_hard_regs (void)
283 int i;
284 allocno_hard_regs_t hv;
286 for (i = 0;
287 allocno_hard_regs_vec.iterate (i, &hv);
288 i++)
289 ira_free (hv);
290 delete allocno_hard_regs_htab;
291 allocno_hard_regs_htab = NULL;
292 allocno_hard_regs_vec.release ();
295 /* Sort hard regs according to their frequency of usage. */
296 static int
297 allocno_hard_regs_compare (const void *v1p, const void *v2p)
299 allocno_hard_regs_t hv1 = *(const allocno_hard_regs_t *) v1p;
300 allocno_hard_regs_t hv2 = *(const allocno_hard_regs_t *) v2p;
302 if (hv2->cost > hv1->cost)
303 return 1;
304 else if (hv2->cost < hv1->cost)
305 return -1;
306 else
307 return 0;
312 /* Used for finding a common ancestor of two allocno hard registers
313 nodes in the forest. We use the current value of
314 'node_check_tick' to mark all nodes from one node to the top and
315 then walking up from another node until we find a marked node.
317 It is also used to figure out allocno colorability as a mark that
318 we already reset value of member 'conflict_size' for the forest
319 node corresponding to the processed allocno. */
320 static int node_check_tick;
322 /* Roots of the forest containing hard register sets can be assigned
323 to allocnos. */
324 static allocno_hard_regs_node_t hard_regs_roots;
326 /* Definition of vector of allocno hard register nodes. */
328 /* Vector used to create the forest. */
329 static vec<allocno_hard_regs_node_t> hard_regs_node_vec;
331 /* Create and return allocno hard registers node containing allocno
332 hard registers HV. */
333 static allocno_hard_regs_node_t
334 create_new_allocno_hard_regs_node (allocno_hard_regs_t hv)
336 allocno_hard_regs_node_t new_node;
338 new_node = ((struct allocno_hard_regs_node *)
339 ira_allocate (sizeof (struct allocno_hard_regs_node)));
340 new_node->check = 0;
341 new_node->hard_regs = hv;
342 new_node->hard_regs_num = hard_reg_set_size (hv->set);
343 new_node->first = NULL;
344 new_node->used_p = false;
345 return new_node;
348 /* Add allocno hard registers node NEW_NODE to the forest on its level
349 given by ROOTS. */
350 static void
351 add_new_allocno_hard_regs_node_to_forest (allocno_hard_regs_node_t *roots,
352 allocno_hard_regs_node_t new_node)
354 new_node->next = *roots;
355 if (new_node->next != NULL)
356 new_node->next->prev = new_node;
357 new_node->prev = NULL;
358 *roots = new_node;
361 /* Add allocno hard registers HV (or its best approximation if it is
362 not possible) to the forest on its level given by ROOTS. */
363 static void
364 add_allocno_hard_regs_to_forest (allocno_hard_regs_node_t *roots,
365 allocno_hard_regs_t hv)
367 unsigned int i, start;
368 allocno_hard_regs_node_t node, prev, new_node;
369 HARD_REG_SET temp_set;
370 allocno_hard_regs_t hv2;
372 start = hard_regs_node_vec.length ();
373 for (node = *roots; node != NULL; node = node->next)
375 if (hard_reg_set_equal_p (hv->set, node->hard_regs->set))
376 return;
377 if (hard_reg_set_subset_p (hv->set, node->hard_regs->set))
379 add_allocno_hard_regs_to_forest (&node->first, hv);
380 return;
382 if (hard_reg_set_subset_p (node->hard_regs->set, hv->set))
383 hard_regs_node_vec.safe_push (node);
384 else if (hard_reg_set_intersect_p (hv->set, node->hard_regs->set))
386 COPY_HARD_REG_SET (temp_set, hv->set);
387 AND_HARD_REG_SET (temp_set, node->hard_regs->set);
388 hv2 = add_allocno_hard_regs (temp_set, hv->cost);
389 add_allocno_hard_regs_to_forest (&node->first, hv2);
392 if (hard_regs_node_vec.length ()
393 > start + 1)
395 /* Create a new node which contains nodes in hard_regs_node_vec. */
396 CLEAR_HARD_REG_SET (temp_set);
397 for (i = start;
398 i < hard_regs_node_vec.length ();
399 i++)
401 node = hard_regs_node_vec[i];
402 IOR_HARD_REG_SET (temp_set, node->hard_regs->set);
404 hv = add_allocno_hard_regs (temp_set, hv->cost);
405 new_node = create_new_allocno_hard_regs_node (hv);
406 prev = NULL;
407 for (i = start;
408 i < hard_regs_node_vec.length ();
409 i++)
411 node = hard_regs_node_vec[i];
412 if (node->prev == NULL)
413 *roots = node->next;
414 else
415 node->prev->next = node->next;
416 if (node->next != NULL)
417 node->next->prev = node->prev;
418 if (prev == NULL)
419 new_node->first = node;
420 else
421 prev->next = node;
422 node->prev = prev;
423 node->next = NULL;
424 prev = node;
426 add_new_allocno_hard_regs_node_to_forest (roots, new_node);
428 hard_regs_node_vec.truncate (start);
431 /* Add allocno hard registers nodes starting with the forest level
432 given by FIRST which contains biggest set inside SET. */
433 static void
434 collect_allocno_hard_regs_cover (allocno_hard_regs_node_t first,
435 HARD_REG_SET set)
437 allocno_hard_regs_node_t node;
439 ira_assert (first != NULL);
440 for (node = first; node != NULL; node = node->next)
441 if (hard_reg_set_subset_p (node->hard_regs->set, set))
442 hard_regs_node_vec.safe_push (node);
443 else if (hard_reg_set_intersect_p (set, node->hard_regs->set))
444 collect_allocno_hard_regs_cover (node->first, set);
447 /* Set up field parent as PARENT in all allocno hard registers nodes
448 in forest given by FIRST. */
449 static void
450 setup_allocno_hard_regs_nodes_parent (allocno_hard_regs_node_t first,
451 allocno_hard_regs_node_t parent)
453 allocno_hard_regs_node_t node;
455 for (node = first; node != NULL; node = node->next)
457 node->parent = parent;
458 setup_allocno_hard_regs_nodes_parent (node->first, node);
462 /* Return allocno hard registers node which is a first common ancestor
463 node of FIRST and SECOND in the forest. */
464 static allocno_hard_regs_node_t
465 first_common_ancestor_node (allocno_hard_regs_node_t first,
466 allocno_hard_regs_node_t second)
468 allocno_hard_regs_node_t node;
470 node_check_tick++;
471 for (node = first; node != NULL; node = node->parent)
472 node->check = node_check_tick;
473 for (node = second; node != NULL; node = node->parent)
474 if (node->check == node_check_tick)
475 return node;
476 return first_common_ancestor_node (second, first);
479 /* Print hard reg set SET to F. */
480 static void
481 print_hard_reg_set (FILE *f, HARD_REG_SET set, bool new_line_p)
483 int i, start;
485 for (start = -1, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
487 if (TEST_HARD_REG_BIT (set, i))
489 if (i == 0 || ! TEST_HARD_REG_BIT (set, i - 1))
490 start = i;
492 if (start >= 0
493 && (i == FIRST_PSEUDO_REGISTER - 1 || ! TEST_HARD_REG_BIT (set, i)))
495 if (start == i - 1)
496 fprintf (f, " %d", start);
497 else if (start == i - 2)
498 fprintf (f, " %d %d", start, start + 1);
499 else
500 fprintf (f, " %d-%d", start, i - 1);
501 start = -1;
504 if (new_line_p)
505 fprintf (f, "\n");
508 /* Print allocno hard register subforest given by ROOTS and its LEVEL
509 to F. */
510 static void
511 print_hard_regs_subforest (FILE *f, allocno_hard_regs_node_t roots,
512 int level)
514 int i;
515 allocno_hard_regs_node_t node;
517 for (node = roots; node != NULL; node = node->next)
519 fprintf (f, " ");
520 for (i = 0; i < level * 2; i++)
521 fprintf (f, " ");
522 fprintf (f, "%d:(", node->preorder_num);
523 print_hard_reg_set (f, node->hard_regs->set, false);
524 fprintf (f, ")@%"PRId64"\n", node->hard_regs->cost);
525 print_hard_regs_subforest (f, node->first, level + 1);
529 /* Print the allocno hard register forest to F. */
530 static void
531 print_hard_regs_forest (FILE *f)
533 fprintf (f, " Hard reg set forest:\n");
534 print_hard_regs_subforest (f, hard_regs_roots, 1);
537 /* Print the allocno hard register forest to stderr. */
538 void
539 ira_debug_hard_regs_forest (void)
541 print_hard_regs_forest (stderr);
544 /* Remove unused allocno hard registers nodes from forest given by its
545 *ROOTS. */
546 static void
547 remove_unused_allocno_hard_regs_nodes (allocno_hard_regs_node_t *roots)
549 allocno_hard_regs_node_t node, prev, next, last;
551 for (prev = NULL, node = *roots; node != NULL; node = next)
553 next = node->next;
554 if (node->used_p)
556 remove_unused_allocno_hard_regs_nodes (&node->first);
557 prev = node;
559 else
561 for (last = node->first;
562 last != NULL && last->next != NULL;
563 last = last->next)
565 if (last != NULL)
567 if (prev == NULL)
568 *roots = node->first;
569 else
570 prev->next = node->first;
571 if (next != NULL)
572 next->prev = last;
573 last->next = next;
574 next = node->first;
576 else
578 if (prev == NULL)
579 *roots = next;
580 else
581 prev->next = next;
582 if (next != NULL)
583 next->prev = prev;
585 ira_free (node);
590 /* Set up fields preorder_num starting with START_NUM in all allocno
591 hard registers nodes in forest given by FIRST. Return biggest set
592 PREORDER_NUM increased by 1. */
593 static int
594 enumerate_allocno_hard_regs_nodes (allocno_hard_regs_node_t first,
595 allocno_hard_regs_node_t parent,
596 int start_num)
598 allocno_hard_regs_node_t node;
600 for (node = first; node != NULL; node = node->next)
602 node->preorder_num = start_num++;
603 node->parent = parent;
604 start_num = enumerate_allocno_hard_regs_nodes (node->first, node,
605 start_num);
607 return start_num;
610 /* Number of allocno hard registers nodes in the forest. */
611 static int allocno_hard_regs_nodes_num;
613 /* Table preorder number of allocno hard registers node in the forest
614 -> the allocno hard registers node. */
615 static allocno_hard_regs_node_t *allocno_hard_regs_nodes;
617 /* See below. */
618 typedef struct allocno_hard_regs_subnode *allocno_hard_regs_subnode_t;
620 /* The structure is used to describes all subnodes (not only immediate
621 ones) in the mentioned above tree for given allocno hard register
622 node. The usage of such data accelerates calculation of
623 colorability of given allocno. */
624 struct allocno_hard_regs_subnode
626 /* The conflict size of conflicting allocnos whose hard register
627 sets are equal sets (plus supersets if given node is given
628 allocno hard registers node) of one in the given node. */
629 int left_conflict_size;
630 /* The summary conflict size of conflicting allocnos whose hard
631 register sets are strict subsets of one in the given node.
632 Overall conflict size is
633 left_conflict_subnodes_size
634 + MIN (max_node_impact - left_conflict_subnodes_size,
635 left_conflict_size)
637 short left_conflict_subnodes_size;
638 short max_node_impact;
641 /* Container for hard regs subnodes of all allocnos. */
642 static allocno_hard_regs_subnode_t allocno_hard_regs_subnodes;
644 /* Table (preorder number of allocno hard registers node in the
645 forest, preorder number of allocno hard registers subnode) -> index
646 of the subnode relative to the node. -1 if it is not a
647 subnode. */
648 static int *allocno_hard_regs_subnode_index;
650 /* Setup arrays ALLOCNO_HARD_REGS_NODES and
651 ALLOCNO_HARD_REGS_SUBNODE_INDEX. */
652 static void
653 setup_allocno_hard_regs_subnode_index (allocno_hard_regs_node_t first)
655 allocno_hard_regs_node_t node, parent;
656 int index;
658 for (node = first; node != NULL; node = node->next)
660 allocno_hard_regs_nodes[node->preorder_num] = node;
661 for (parent = node; parent != NULL; parent = parent->parent)
663 index = parent->preorder_num * allocno_hard_regs_nodes_num;
664 allocno_hard_regs_subnode_index[index + node->preorder_num]
665 = node->preorder_num - parent->preorder_num;
667 setup_allocno_hard_regs_subnode_index (node->first);
671 /* Count all allocno hard registers nodes in tree ROOT. */
672 static int
673 get_allocno_hard_regs_subnodes_num (allocno_hard_regs_node_t root)
675 int len = 1;
677 for (root = root->first; root != NULL; root = root->next)
678 len += get_allocno_hard_regs_subnodes_num (root);
679 return len;
682 /* Build the forest of allocno hard registers nodes and assign each
683 allocno a node from the forest. */
684 static void
685 form_allocno_hard_regs_nodes_forest (void)
687 unsigned int i, j, size, len;
688 int start;
689 ira_allocno_t a;
690 allocno_hard_regs_t hv;
691 bitmap_iterator bi;
692 HARD_REG_SET temp;
693 allocno_hard_regs_node_t node, allocno_hard_regs_node;
694 allocno_color_data_t allocno_data;
696 node_check_tick = 0;
697 init_allocno_hard_regs ();
698 hard_regs_roots = NULL;
699 hard_regs_node_vec.create (100);
700 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
701 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
703 CLEAR_HARD_REG_SET (temp);
704 SET_HARD_REG_BIT (temp, i);
705 hv = add_allocno_hard_regs (temp, 0);
706 node = create_new_allocno_hard_regs_node (hv);
707 add_new_allocno_hard_regs_node_to_forest (&hard_regs_roots, node);
709 start = allocno_hard_regs_vec.length ();
710 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
712 a = ira_allocnos[i];
713 allocno_data = ALLOCNO_COLOR_DATA (a);
715 if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
716 continue;
717 hv = (add_allocno_hard_regs
718 (allocno_data->profitable_hard_regs,
719 ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a)));
721 SET_HARD_REG_SET (temp);
722 AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
723 add_allocno_hard_regs (temp, 0);
724 qsort (allocno_hard_regs_vec.address () + start,
725 allocno_hard_regs_vec.length () - start,
726 sizeof (allocno_hard_regs_t), allocno_hard_regs_compare);
727 for (i = start;
728 allocno_hard_regs_vec.iterate (i, &hv);
729 i++)
731 add_allocno_hard_regs_to_forest (&hard_regs_roots, hv);
732 ira_assert (hard_regs_node_vec.length () == 0);
734 /* We need to set up parent fields for right work of
735 first_common_ancestor_node. */
736 setup_allocno_hard_regs_nodes_parent (hard_regs_roots, NULL);
737 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
739 a = ira_allocnos[i];
740 allocno_data = ALLOCNO_COLOR_DATA (a);
741 if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
742 continue;
743 hard_regs_node_vec.truncate (0);
744 collect_allocno_hard_regs_cover (hard_regs_roots,
745 allocno_data->profitable_hard_regs);
746 allocno_hard_regs_node = NULL;
747 for (j = 0; hard_regs_node_vec.iterate (j, &node); j++)
748 allocno_hard_regs_node
749 = (j == 0
750 ? node
751 : first_common_ancestor_node (node, allocno_hard_regs_node));
752 /* That is a temporary storage. */
753 allocno_hard_regs_node->used_p = true;
754 allocno_data->hard_regs_node = allocno_hard_regs_node;
756 ira_assert (hard_regs_roots->next == NULL);
757 hard_regs_roots->used_p = true;
758 remove_unused_allocno_hard_regs_nodes (&hard_regs_roots);
759 allocno_hard_regs_nodes_num
760 = enumerate_allocno_hard_regs_nodes (hard_regs_roots, NULL, 0);
761 allocno_hard_regs_nodes
762 = ((allocno_hard_regs_node_t *)
763 ira_allocate (allocno_hard_regs_nodes_num
764 * sizeof (allocno_hard_regs_node_t)));
765 size = allocno_hard_regs_nodes_num * allocno_hard_regs_nodes_num;
766 allocno_hard_regs_subnode_index
767 = (int *) ira_allocate (size * sizeof (int));
768 for (i = 0; i < size; i++)
769 allocno_hard_regs_subnode_index[i] = -1;
770 setup_allocno_hard_regs_subnode_index (hard_regs_roots);
771 start = 0;
772 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
774 a = ira_allocnos[i];
775 allocno_data = ALLOCNO_COLOR_DATA (a);
776 if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
777 continue;
778 len = get_allocno_hard_regs_subnodes_num (allocno_data->hard_regs_node);
779 allocno_data->hard_regs_subnodes_start = start;
780 allocno_data->hard_regs_subnodes_num = len;
781 start += len;
783 allocno_hard_regs_subnodes
784 = ((allocno_hard_regs_subnode_t)
785 ira_allocate (sizeof (struct allocno_hard_regs_subnode) * start));
786 hard_regs_node_vec.release ();
789 /* Free tree of allocno hard registers nodes given by its ROOT. */
790 static void
791 finish_allocno_hard_regs_nodes_tree (allocno_hard_regs_node_t root)
793 allocno_hard_regs_node_t child, next;
795 for (child = root->first; child != NULL; child = next)
797 next = child->next;
798 finish_allocno_hard_regs_nodes_tree (child);
800 ira_free (root);
803 /* Finish work with the forest of allocno hard registers nodes. */
804 static void
805 finish_allocno_hard_regs_nodes_forest (void)
807 allocno_hard_regs_node_t node, next;
809 ira_free (allocno_hard_regs_subnodes);
810 for (node = hard_regs_roots; node != NULL; node = next)
812 next = node->next;
813 finish_allocno_hard_regs_nodes_tree (node);
815 ira_free (allocno_hard_regs_nodes);
816 ira_free (allocno_hard_regs_subnode_index);
817 finish_allocno_hard_regs ();
820 /* Set up left conflict sizes and left conflict subnodes sizes of hard
821 registers subnodes of allocno A. Return TRUE if allocno A is
822 trivially colorable. */
823 static bool
824 setup_left_conflict_sizes_p (ira_allocno_t a)
826 int i, k, nobj, start;
827 int conflict_size, left_conflict_subnodes_size, node_preorder_num;
828 allocno_color_data_t data;
829 HARD_REG_SET profitable_hard_regs;
830 allocno_hard_regs_subnode_t subnodes;
831 allocno_hard_regs_node_t node;
832 HARD_REG_SET node_set;
834 nobj = ALLOCNO_NUM_OBJECTS (a);
835 conflict_size = 0;
836 data = ALLOCNO_COLOR_DATA (a);
837 subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
838 COPY_HARD_REG_SET (profitable_hard_regs, data->profitable_hard_regs);
839 node = data->hard_regs_node;
840 node_preorder_num = node->preorder_num;
841 COPY_HARD_REG_SET (node_set, node->hard_regs->set);
842 node_check_tick++;
843 for (k = 0; k < nobj; k++)
845 ira_object_t obj = ALLOCNO_OBJECT (a, k);
846 ira_object_t conflict_obj;
847 ira_object_conflict_iterator oci;
849 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
851 int size;
852 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
853 allocno_hard_regs_node_t conflict_node, temp_node;
854 HARD_REG_SET conflict_node_set;
855 allocno_color_data_t conflict_data;
857 conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
858 if (! ALLOCNO_COLOR_DATA (conflict_a)->in_graph_p
859 || ! hard_reg_set_intersect_p (profitable_hard_regs,
860 conflict_data
861 ->profitable_hard_regs))
862 continue;
863 conflict_node = conflict_data->hard_regs_node;
864 COPY_HARD_REG_SET (conflict_node_set, conflict_node->hard_regs->set);
865 if (hard_reg_set_subset_p (node_set, conflict_node_set))
866 temp_node = node;
867 else
869 ira_assert (hard_reg_set_subset_p (conflict_node_set, node_set));
870 temp_node = conflict_node;
872 if (temp_node->check != node_check_tick)
874 temp_node->check = node_check_tick;
875 temp_node->conflict_size = 0;
877 size = (ira_reg_class_max_nregs
878 [ALLOCNO_CLASS (conflict_a)][ALLOCNO_MODE (conflict_a)]);
879 if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1)
880 /* We will deal with the subwords individually. */
881 size = 1;
882 temp_node->conflict_size += size;
885 for (i = 0; i < data->hard_regs_subnodes_num; i++)
887 allocno_hard_regs_node_t temp_node;
889 temp_node = allocno_hard_regs_nodes[i + node_preorder_num];
890 ira_assert (temp_node->preorder_num == i + node_preorder_num);
891 subnodes[i].left_conflict_size = (temp_node->check != node_check_tick
892 ? 0 : temp_node->conflict_size);
893 if (hard_reg_set_subset_p (temp_node->hard_regs->set,
894 profitable_hard_regs))
895 subnodes[i].max_node_impact = temp_node->hard_regs_num;
896 else
898 HARD_REG_SET temp_set;
899 int j, n, hard_regno;
900 enum reg_class aclass;
902 COPY_HARD_REG_SET (temp_set, temp_node->hard_regs->set);
903 AND_HARD_REG_SET (temp_set, profitable_hard_regs);
904 aclass = ALLOCNO_CLASS (a);
905 for (n = 0, j = ira_class_hard_regs_num[aclass] - 1; j >= 0; j--)
907 hard_regno = ira_class_hard_regs[aclass][j];
908 if (TEST_HARD_REG_BIT (temp_set, hard_regno))
909 n++;
911 subnodes[i].max_node_impact = n;
913 subnodes[i].left_conflict_subnodes_size = 0;
915 start = node_preorder_num * allocno_hard_regs_nodes_num;
916 for (i = data->hard_regs_subnodes_num - 1; i >= 0; i--)
918 int size, parent_i;
919 allocno_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 = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
926 if (parent == NULL)
927 continue;
928 parent_i
929 = allocno_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));
939 conflict_size += ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
940 data->colorable_p = conflict_size <= data->available_regs_num;
941 return data->colorable_p;
944 /* Update left conflict sizes of hard registers subnodes of allocno A
945 after removing allocno REMOVED_A with SIZE from the conflict graph.
946 Return TRUE if A is trivially colorable. */
947 static bool
948 update_left_conflict_sizes_p (ira_allocno_t a,
949 ira_allocno_t removed_a, int size)
951 int i, conflict_size, before_conflict_size, diff, start;
952 int node_preorder_num, parent_i;
953 allocno_hard_regs_node_t node, removed_node, parent;
954 allocno_hard_regs_subnode_t subnodes;
955 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
957 ira_assert (! data->colorable_p);
958 node = data->hard_regs_node;
959 node_preorder_num = node->preorder_num;
960 removed_node = ALLOCNO_COLOR_DATA (removed_a)->hard_regs_node;
961 ira_assert (hard_reg_set_subset_p (removed_node->hard_regs->set,
962 node->hard_regs->set)
963 || hard_reg_set_subset_p (node->hard_regs->set,
964 removed_node->hard_regs->set));
965 start = node_preorder_num * allocno_hard_regs_nodes_num;
966 i = allocno_hard_regs_subnode_index[start + removed_node->preorder_num];
967 if (i < 0)
968 i = 0;
969 subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
970 before_conflict_size
971 = (subnodes[i].left_conflict_subnodes_size
972 + MIN (subnodes[i].max_node_impact
973 - subnodes[i].left_conflict_subnodes_size,
974 subnodes[i].left_conflict_size));
975 subnodes[i].left_conflict_size -= size;
976 for (;;)
978 conflict_size
979 = (subnodes[i].left_conflict_subnodes_size
980 + MIN (subnodes[i].max_node_impact
981 - subnodes[i].left_conflict_subnodes_size,
982 subnodes[i].left_conflict_size));
983 if ((diff = before_conflict_size - conflict_size) == 0)
984 break;
985 ira_assert (conflict_size < before_conflict_size);
986 parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
987 if (parent == NULL)
988 break;
989 parent_i
990 = allocno_hard_regs_subnode_index[start + parent->preorder_num];
991 if (parent_i < 0)
992 break;
993 i = parent_i;
994 before_conflict_size
995 = (subnodes[i].left_conflict_subnodes_size
996 + MIN (subnodes[i].max_node_impact
997 - subnodes[i].left_conflict_subnodes_size,
998 subnodes[i].left_conflict_size));
999 subnodes[i].left_conflict_subnodes_size -= diff;
1001 if (i != 0
1002 || (conflict_size
1003 + ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
1004 > data->available_regs_num))
1005 return false;
1006 data->colorable_p = true;
1007 return true;
1010 /* Return true if allocno A has empty profitable hard regs. */
1011 static bool
1012 empty_profitable_hard_regs (ira_allocno_t a)
1014 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
1016 return hard_reg_set_empty_p (data->profitable_hard_regs);
1019 /* Set up profitable hard registers for each allocno being
1020 colored. */
1021 static void
1022 setup_profitable_hard_regs (void)
1024 unsigned int i;
1025 int j, k, nobj, hard_regno, nregs, class_size;
1026 ira_allocno_t a;
1027 bitmap_iterator bi;
1028 enum reg_class aclass;
1029 enum machine_mode mode;
1030 allocno_color_data_t data;
1032 /* Initial set up from allocno classes and explicitly conflicting
1033 hard regs. */
1034 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1036 a = ira_allocnos[i];
1037 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS)
1038 continue;
1039 data = ALLOCNO_COLOR_DATA (a);
1040 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL
1041 && ALLOCNO_CLASS_COST (a) > ALLOCNO_MEMORY_COST (a))
1042 CLEAR_HARD_REG_SET (data->profitable_hard_regs);
1043 else
1045 mode = ALLOCNO_MODE (a);
1046 COPY_HARD_REG_SET (data->profitable_hard_regs,
1047 ira_useful_class_mode_regs[aclass][mode]);
1048 nobj = ALLOCNO_NUM_OBJECTS (a);
1049 for (k = 0; k < nobj; k++)
1051 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1053 AND_COMPL_HARD_REG_SET (data->profitable_hard_regs,
1054 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
1058 /* Exclude hard regs already assigned for conflicting objects. */
1059 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, i, bi)
1061 a = ira_allocnos[i];
1062 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1063 || ! ALLOCNO_ASSIGNED_P (a)
1064 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1065 continue;
1066 mode = ALLOCNO_MODE (a);
1067 nregs = hard_regno_nregs[hard_regno][mode];
1068 nobj = ALLOCNO_NUM_OBJECTS (a);
1069 for (k = 0; k < nobj; k++)
1071 ira_object_t obj = ALLOCNO_OBJECT (a, k);
1072 ira_object_t conflict_obj;
1073 ira_object_conflict_iterator oci;
1075 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1077 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1079 /* We can process the conflict allocno repeatedly with
1080 the same result. */
1081 if (nregs == nobj && nregs > 1)
1083 int num = OBJECT_SUBWORD (conflict_obj);
1085 if (REG_WORDS_BIG_ENDIAN)
1086 CLEAR_HARD_REG_BIT
1087 (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1088 hard_regno + nobj - num - 1);
1089 else
1090 CLEAR_HARD_REG_BIT
1091 (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1092 hard_regno + num);
1094 else
1095 AND_COMPL_HARD_REG_SET
1096 (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1097 ira_reg_mode_hard_regset[hard_regno][mode]);
1101 /* Exclude too costly hard regs. */
1102 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1104 int min_cost = INT_MAX;
1105 int *costs;
1107 a = ira_allocnos[i];
1108 if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1109 || empty_profitable_hard_regs (a))
1110 continue;
1111 data = ALLOCNO_COLOR_DATA (a);
1112 mode = ALLOCNO_MODE (a);
1113 if ((costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a)) != NULL
1114 || (costs = ALLOCNO_HARD_REG_COSTS (a)) != NULL)
1116 class_size = ira_class_hard_regs_num[aclass];
1117 for (j = 0; j < class_size; j++)
1119 hard_regno = ira_class_hard_regs[aclass][j];
1120 if (! TEST_HARD_REG_BIT (data->profitable_hard_regs,
1121 hard_regno))
1122 continue;
1123 if (ALLOCNO_UPDATED_MEMORY_COST (a) < costs[j])
1124 CLEAR_HARD_REG_BIT (data->profitable_hard_regs,
1125 hard_regno);
1126 else if (min_cost > costs[j])
1127 min_cost = costs[j];
1130 else if (ALLOCNO_UPDATED_MEMORY_COST (a)
1131 < ALLOCNO_UPDATED_CLASS_COST (a))
1132 CLEAR_HARD_REG_SET (data->profitable_hard_regs);
1133 if (ALLOCNO_UPDATED_CLASS_COST (a) > min_cost)
1134 ALLOCNO_UPDATED_CLASS_COST (a) = min_cost;
1140 /* This page contains functions used to choose hard registers for
1141 allocnos. */
1143 /* Pool for update cost records. */
1144 static alloc_pool update_cost_record_pool;
1146 /* Initiate update cost records. */
1147 static void
1148 init_update_cost_records (void)
1150 update_cost_record_pool
1151 = create_alloc_pool ("update cost records",
1152 sizeof (struct update_cost_record), 100);
1155 /* Return new update cost record with given params. */
1156 static struct update_cost_record *
1157 get_update_cost_record (int hard_regno, int divisor,
1158 struct update_cost_record *next)
1160 struct update_cost_record *record;
1162 record = (struct update_cost_record *) pool_alloc (update_cost_record_pool);
1163 record->hard_regno = hard_regno;
1164 record->divisor = divisor;
1165 record->next = next;
1166 return record;
1169 /* Free memory for all records in LIST. */
1170 static void
1171 free_update_cost_record_list (struct update_cost_record *list)
1173 struct update_cost_record *next;
1175 while (list != NULL)
1177 next = list->next;
1178 pool_free (update_cost_record_pool, list);
1179 list = next;
1183 /* Free memory allocated for all update cost records. */
1184 static void
1185 finish_update_cost_records (void)
1187 free_alloc_pool (update_cost_record_pool);
1190 /* Array whose element value is TRUE if the corresponding hard
1191 register was already allocated for an allocno. */
1192 static bool allocated_hardreg_p[FIRST_PSEUDO_REGISTER];
1194 /* Describes one element in a queue of allocnos whose costs need to be
1195 updated. Each allocno in the queue is known to have an allocno
1196 class. */
1197 struct update_cost_queue_elem
1199 /* This element is in the queue iff CHECK == update_cost_check. */
1200 int check;
1202 /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
1203 connecting this allocno to the one being allocated. */
1204 int divisor;
1206 /* Allocno from which we are chaning costs of connected allocnos.
1207 It is used not go back in graph of allocnos connected by
1208 copies. */
1209 ira_allocno_t from;
1211 /* The next allocno in the queue, or null if this is the last element. */
1212 ira_allocno_t next;
1215 /* The first element in a queue of allocnos whose copy costs need to be
1216 updated. Null if the queue is empty. */
1217 static ira_allocno_t update_cost_queue;
1219 /* The last element in the queue described by update_cost_queue.
1220 Not valid if update_cost_queue is null. */
1221 static struct update_cost_queue_elem *update_cost_queue_tail;
1223 /* A pool of elements in the queue described by update_cost_queue.
1224 Elements are indexed by ALLOCNO_NUM. */
1225 static struct update_cost_queue_elem *update_cost_queue_elems;
1227 /* The current value of update_costs_from_copies call count. */
1228 static int update_cost_check;
1230 /* Allocate and initialize data necessary for function
1231 update_costs_from_copies. */
1232 static void
1233 initiate_cost_update (void)
1235 size_t size;
1237 size = ira_allocnos_num * sizeof (struct update_cost_queue_elem);
1238 update_cost_queue_elems
1239 = (struct update_cost_queue_elem *) ira_allocate (size);
1240 memset (update_cost_queue_elems, 0, size);
1241 update_cost_check = 0;
1242 init_update_cost_records ();
1245 /* Deallocate data used by function update_costs_from_copies. */
1246 static void
1247 finish_cost_update (void)
1249 ira_free (update_cost_queue_elems);
1250 finish_update_cost_records ();
1253 /* When we traverse allocnos to update hard register costs, the cost
1254 divisor will be multiplied by the following macro value for each
1255 hop from given allocno to directly connected allocnos. */
1256 #define COST_HOP_DIVISOR 4
1258 /* Start a new cost-updating pass. */
1259 static void
1260 start_update_cost (void)
1262 update_cost_check++;
1263 update_cost_queue = NULL;
1266 /* Add (ALLOCNO, FROM, DIVISOR) to the end of update_cost_queue, unless
1267 ALLOCNO is already in the queue, or has NO_REGS class. */
1268 static inline void
1269 queue_update_cost (ira_allocno_t allocno, ira_allocno_t from, int divisor)
1271 struct update_cost_queue_elem *elem;
1273 elem = &update_cost_queue_elems[ALLOCNO_NUM (allocno)];
1274 if (elem->check != update_cost_check
1275 && ALLOCNO_CLASS (allocno) != NO_REGS)
1277 elem->check = update_cost_check;
1278 elem->from = from;
1279 elem->divisor = divisor;
1280 elem->next = NULL;
1281 if (update_cost_queue == NULL)
1282 update_cost_queue = allocno;
1283 else
1284 update_cost_queue_tail->next = allocno;
1285 update_cost_queue_tail = elem;
1289 /* Try to remove the first element from update_cost_queue. Return
1290 false if the queue was empty, otherwise make (*ALLOCNO, *FROM,
1291 *DIVISOR) describe the removed element. */
1292 static inline bool
1293 get_next_update_cost (ira_allocno_t *allocno, ira_allocno_t *from, int *divisor)
1295 struct update_cost_queue_elem *elem;
1297 if (update_cost_queue == NULL)
1298 return false;
1300 *allocno = update_cost_queue;
1301 elem = &update_cost_queue_elems[ALLOCNO_NUM (*allocno)];
1302 *from = elem->from;
1303 *divisor = elem->divisor;
1304 update_cost_queue = elem->next;
1305 return true;
1308 /* Increase costs of HARD_REGNO by UPDATE_COST for ALLOCNO. Return
1309 true if we really modified the cost. */
1310 static bool
1311 update_allocno_cost (ira_allocno_t allocno, int hard_regno, int update_cost)
1313 int i;
1314 enum reg_class aclass = ALLOCNO_CLASS (allocno);
1316 i = ira_class_hard_reg_index[aclass][hard_regno];
1317 if (i < 0)
1318 return false;
1319 ira_allocate_and_set_or_copy_costs
1320 (&ALLOCNO_UPDATED_HARD_REG_COSTS (allocno), aclass,
1321 ALLOCNO_UPDATED_CLASS_COST (allocno),
1322 ALLOCNO_HARD_REG_COSTS (allocno));
1323 ira_allocate_and_set_or_copy_costs
1324 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno),
1325 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (allocno));
1326 ALLOCNO_UPDATED_HARD_REG_COSTS (allocno)[i] += update_cost;
1327 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno)[i] += update_cost;
1328 return true;
1331 /* Update (decrease if DECR_P) HARD_REGNO cost of allocnos connected
1332 by copies to ALLOCNO to increase chances to remove some copies as
1333 the result of subsequent assignment. Record cost updates if
1334 RECORD_P is true. */
1335 static void
1336 update_costs_from_allocno (ira_allocno_t allocno, int hard_regno,
1337 int divisor, bool decr_p, bool record_p)
1339 int cost, update_cost;
1340 enum machine_mode mode;
1341 enum reg_class rclass, aclass;
1342 ira_allocno_t another_allocno, from = NULL;
1343 ira_copy_t cp, next_cp;
1345 rclass = REGNO_REG_CLASS (hard_regno);
1348 mode = ALLOCNO_MODE (allocno);
1349 ira_init_register_move_cost_if_necessary (mode);
1350 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1352 if (cp->first == allocno)
1354 next_cp = cp->next_first_allocno_copy;
1355 another_allocno = cp->second;
1357 else if (cp->second == allocno)
1359 next_cp = cp->next_second_allocno_copy;
1360 another_allocno = cp->first;
1362 else
1363 gcc_unreachable ();
1365 if (another_allocno == from)
1366 continue;
1368 aclass = ALLOCNO_CLASS (another_allocno);
1369 if (! TEST_HARD_REG_BIT (reg_class_contents[aclass],
1370 hard_regno)
1371 || ALLOCNO_ASSIGNED_P (another_allocno))
1372 continue;
1374 cost = (cp->second == allocno
1375 ? ira_register_move_cost[mode][rclass][aclass]
1376 : ira_register_move_cost[mode][aclass][rclass]);
1377 if (decr_p)
1378 cost = -cost;
1380 update_cost = cp->freq * cost / divisor;
1381 if (update_cost == 0)
1382 continue;
1384 if (! update_allocno_cost (another_allocno, hard_regno, update_cost))
1385 continue;
1386 queue_update_cost (another_allocno, allocno, divisor * COST_HOP_DIVISOR);
1387 if (record_p && ALLOCNO_COLOR_DATA (another_allocno) != NULL)
1388 ALLOCNO_COLOR_DATA (another_allocno)->update_cost_records
1389 = get_update_cost_record (hard_regno, divisor,
1390 ALLOCNO_COLOR_DATA (another_allocno)
1391 ->update_cost_records);
1394 while (get_next_update_cost (&allocno, &from, &divisor));
1397 /* Decrease preferred ALLOCNO hard register costs and costs of
1398 allocnos connected to ALLOCNO through copy. */
1399 static void
1400 update_costs_from_prefs (ira_allocno_t allocno)
1402 ira_pref_t pref;
1404 start_update_cost ();
1405 for (pref = ALLOCNO_PREFS (allocno); pref != NULL; pref = pref->next_pref)
1406 update_costs_from_allocno (allocno, pref->hard_regno,
1407 COST_HOP_DIVISOR, true, true);
1410 /* Update (decrease if DECR_P) the cost of allocnos connected to
1411 ALLOCNO through copies to increase chances to remove some copies as
1412 the result of subsequent assignment. ALLOCNO was just assigned to
1413 a hard register. Record cost updates if RECORD_P is true. */
1414 static void
1415 update_costs_from_copies (ira_allocno_t allocno, bool decr_p, bool record_p)
1417 int hard_regno;
1419 hard_regno = ALLOCNO_HARD_REGNO (allocno);
1420 ira_assert (hard_regno >= 0 && ALLOCNO_CLASS (allocno) != NO_REGS);
1421 start_update_cost ();
1422 update_costs_from_allocno (allocno, hard_regno, 1, decr_p, record_p);
1425 /* Restore costs of allocnos connected to ALLOCNO by copies as it was
1426 before updating costs of these allocnos from given allocno. This
1427 is a wise thing to do as if given allocno did not get an expected
1428 hard reg, using smaller cost of the hard reg for allocnos connected
1429 by copies to given allocno becomes actually misleading. Free all
1430 update cost records for ALLOCNO as we don't need them anymore. */
1431 static void
1432 restore_costs_from_copies (ira_allocno_t allocno)
1434 struct update_cost_record *records, *curr;
1436 if (ALLOCNO_COLOR_DATA (allocno) == NULL)
1437 return;
1438 records = ALLOCNO_COLOR_DATA (allocno)->update_cost_records;
1439 start_update_cost ();
1440 for (curr = records; curr != NULL; curr = curr->next)
1441 update_costs_from_allocno (allocno, curr->hard_regno,
1442 curr->divisor, true, false);
1443 free_update_cost_record_list (records);
1444 ALLOCNO_COLOR_DATA (allocno)->update_cost_records = NULL;
1447 /* This function updates COSTS (decrease if DECR_P) for hard_registers
1448 of ACLASS by conflict costs of the unassigned allocnos
1449 connected by copies with allocnos in update_cost_queue. This
1450 update increases chances to remove some copies. */
1451 static void
1452 update_conflict_hard_regno_costs (int *costs, enum reg_class aclass,
1453 bool decr_p)
1455 int i, cost, class_size, freq, mult, div, divisor;
1456 int index, hard_regno;
1457 int *conflict_costs;
1458 bool cont_p;
1459 enum reg_class another_aclass;
1460 ira_allocno_t allocno, another_allocno, from;
1461 ira_copy_t cp, next_cp;
1463 while (get_next_update_cost (&allocno, &from, &divisor))
1464 for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1466 if (cp->first == allocno)
1468 next_cp = cp->next_first_allocno_copy;
1469 another_allocno = cp->second;
1471 else if (cp->second == allocno)
1473 next_cp = cp->next_second_allocno_copy;
1474 another_allocno = cp->first;
1476 else
1477 gcc_unreachable ();
1479 if (another_allocno == from)
1480 continue;
1482 another_aclass = ALLOCNO_CLASS (another_allocno);
1483 if (! ira_reg_classes_intersect_p[aclass][another_aclass]
1484 || ALLOCNO_ASSIGNED_P (another_allocno)
1485 || ALLOCNO_COLOR_DATA (another_allocno)->may_be_spilled_p)
1486 continue;
1487 class_size = ira_class_hard_regs_num[another_aclass];
1488 ira_allocate_and_copy_costs
1489 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
1490 another_aclass, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
1491 conflict_costs
1492 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno);
1493 if (conflict_costs == NULL)
1494 cont_p = true;
1495 else
1497 mult = cp->freq;
1498 freq = ALLOCNO_FREQ (another_allocno);
1499 if (freq == 0)
1500 freq = 1;
1501 div = freq * divisor;
1502 cont_p = false;
1503 for (i = class_size - 1; i >= 0; i--)
1505 hard_regno = ira_class_hard_regs[another_aclass][i];
1506 ira_assert (hard_regno >= 0);
1507 index = ira_class_hard_reg_index[aclass][hard_regno];
1508 if (index < 0)
1509 continue;
1510 cost = (int) ((unsigned) conflict_costs [i] * mult) / div;
1511 if (cost == 0)
1512 continue;
1513 cont_p = true;
1514 if (decr_p)
1515 cost = -cost;
1516 costs[index] += cost;
1519 /* Probably 5 hops will be enough. */
1520 if (cont_p
1521 && divisor <= (COST_HOP_DIVISOR
1522 * COST_HOP_DIVISOR
1523 * COST_HOP_DIVISOR
1524 * COST_HOP_DIVISOR))
1525 queue_update_cost (another_allocno, allocno, divisor * COST_HOP_DIVISOR);
1529 /* Set up conflicting (through CONFLICT_REGS) for each object of
1530 allocno A and the start allocno profitable regs (through
1531 START_PROFITABLE_REGS). Remember that the start profitable regs
1532 exclude hard regs which can not hold value of mode of allocno A.
1533 This covers mostly cases when multi-register value should be
1534 aligned. */
1535 static inline void
1536 get_conflict_and_start_profitable_regs (ira_allocno_t a, bool retry_p,
1537 HARD_REG_SET *conflict_regs,
1538 HARD_REG_SET *start_profitable_regs)
1540 int i, nwords;
1541 ira_object_t obj;
1543 nwords = ALLOCNO_NUM_OBJECTS (a);
1544 for (i = 0; i < nwords; i++)
1546 obj = ALLOCNO_OBJECT (a, i);
1547 COPY_HARD_REG_SET (conflict_regs[i],
1548 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
1550 if (retry_p)
1552 COPY_HARD_REG_SET (*start_profitable_regs,
1553 reg_class_contents[ALLOCNO_CLASS (a)]);
1554 AND_COMPL_HARD_REG_SET (*start_profitable_regs,
1555 ira_prohibited_class_mode_regs
1556 [ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
1558 else
1559 COPY_HARD_REG_SET (*start_profitable_regs,
1560 ALLOCNO_COLOR_DATA (a)->profitable_hard_regs);
1563 /* Return true if HARD_REGNO is ok for assigning to allocno A with
1564 PROFITABLE_REGS and whose objects have CONFLICT_REGS. */
1565 static inline bool
1566 check_hard_reg_p (ira_allocno_t a, int hard_regno,
1567 HARD_REG_SET *conflict_regs, HARD_REG_SET profitable_regs)
1569 int j, nwords, nregs;
1570 enum reg_class aclass;
1571 enum machine_mode mode;
1573 aclass = ALLOCNO_CLASS (a);
1574 mode = ALLOCNO_MODE (a);
1575 if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[aclass][mode],
1576 hard_regno))
1577 return false;
1578 /* Checking only profitable hard regs. */
1579 if (! TEST_HARD_REG_BIT (profitable_regs, hard_regno))
1580 return false;
1581 nregs = hard_regno_nregs[hard_regno][mode];
1582 nwords = ALLOCNO_NUM_OBJECTS (a);
1583 for (j = 0; j < nregs; j++)
1585 int k;
1586 int set_to_test_start = 0, set_to_test_end = nwords;
1588 if (nregs == nwords)
1590 if (REG_WORDS_BIG_ENDIAN)
1591 set_to_test_start = nwords - j - 1;
1592 else
1593 set_to_test_start = j;
1594 set_to_test_end = set_to_test_start + 1;
1596 for (k = set_to_test_start; k < set_to_test_end; k++)
1597 if (TEST_HARD_REG_BIT (conflict_regs[k], hard_regno + j))
1598 break;
1599 if (k != set_to_test_end)
1600 break;
1602 return j == nregs;
1605 /* Return number of registers needed to be saved and restored at
1606 function prologue/epilogue if we allocate HARD_REGNO to hold value
1607 of MODE. */
1608 static int
1609 calculate_saved_nregs (int hard_regno, enum machine_mode mode)
1611 int i;
1612 int nregs = 0;
1614 ira_assert (hard_regno >= 0);
1615 for (i = hard_regno_nregs[hard_regno][mode] - 1; i >= 0; i--)
1616 if (!allocated_hardreg_p[hard_regno + i]
1617 && !TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + i)
1618 && !LOCAL_REGNO (hard_regno + i))
1619 nregs++;
1620 return nregs;
1623 /* Choose a hard register for allocno A. If RETRY_P is TRUE, it means
1624 that the function called from function
1625 `ira_reassign_conflict_allocnos' and `allocno_reload_assign'. In
1626 this case some allocno data are not defined or updated and we
1627 should not touch these data. The function returns true if we
1628 managed to assign a hard register to the allocno.
1630 To assign a hard register, first of all we calculate all conflict
1631 hard registers which can come from conflicting allocnos with
1632 already assigned hard registers. After that we find first free
1633 hard register with the minimal cost. During hard register cost
1634 calculation we take conflict hard register costs into account to
1635 give a chance for conflicting allocnos to get a better hard
1636 register in the future.
1638 If the best hard register cost is bigger than cost of memory usage
1639 for the allocno, we don't assign a hard register to given allocno
1640 at all.
1642 If we assign a hard register to the allocno, we update costs of the
1643 hard register for allocnos connected by copies to improve a chance
1644 to coalesce insns represented by the copies when we assign hard
1645 registers to the allocnos connected by the copies. */
1646 static bool
1647 assign_hard_reg (ira_allocno_t a, bool retry_p)
1649 HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
1650 int i, j, hard_regno, best_hard_regno, class_size;
1651 int cost, mem_cost, min_cost, full_cost, min_full_cost, nwords, word;
1652 int *a_costs;
1653 enum reg_class aclass;
1654 enum machine_mode mode;
1655 static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
1656 int saved_nregs;
1657 enum reg_class rclass;
1658 int add_cost;
1659 #ifdef STACK_REGS
1660 bool no_stack_reg_p;
1661 #endif
1663 ira_assert (! ALLOCNO_ASSIGNED_P (a));
1664 get_conflict_and_start_profitable_regs (a, retry_p,
1665 conflicting_regs,
1666 &profitable_hard_regs);
1667 aclass = ALLOCNO_CLASS (a);
1668 class_size = ira_class_hard_regs_num[aclass];
1669 best_hard_regno = -1;
1670 memset (full_costs, 0, sizeof (int) * class_size);
1671 mem_cost = 0;
1672 memset (costs, 0, sizeof (int) * class_size);
1673 memset (full_costs, 0, sizeof (int) * class_size);
1674 #ifdef STACK_REGS
1675 no_stack_reg_p = false;
1676 #endif
1677 if (! retry_p)
1678 start_update_cost ();
1679 mem_cost += ALLOCNO_UPDATED_MEMORY_COST (a);
1681 ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
1682 aclass, ALLOCNO_HARD_REG_COSTS (a));
1683 a_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
1684 #ifdef STACK_REGS
1685 no_stack_reg_p = no_stack_reg_p || ALLOCNO_TOTAL_NO_STACK_REG_P (a);
1686 #endif
1687 cost = ALLOCNO_UPDATED_CLASS_COST (a);
1688 for (i = 0; i < class_size; i++)
1689 if (a_costs != NULL)
1691 costs[i] += a_costs[i];
1692 full_costs[i] += a_costs[i];
1694 else
1696 costs[i] += cost;
1697 full_costs[i] += cost;
1699 nwords = ALLOCNO_NUM_OBJECTS (a);
1700 curr_allocno_process++;
1701 for (word = 0; word < nwords; word++)
1703 ira_object_t conflict_obj;
1704 ira_object_t obj = ALLOCNO_OBJECT (a, word);
1705 ira_object_conflict_iterator oci;
1707 /* Take preferences of conflicting allocnos into account. */
1708 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1710 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1711 enum reg_class conflict_aclass;
1713 /* Reload can give another class so we need to check all
1714 allocnos. */
1715 if (!retry_p
1716 && (!bitmap_bit_p (consideration_allocno_bitmap,
1717 ALLOCNO_NUM (conflict_a))
1718 || ((!ALLOCNO_ASSIGNED_P (conflict_a)
1719 || ALLOCNO_HARD_REGNO (conflict_a) < 0)
1720 && !(hard_reg_set_intersect_p
1721 (profitable_hard_regs,
1722 ALLOCNO_COLOR_DATA
1723 (conflict_a)->profitable_hard_regs)))))
1724 continue;
1725 conflict_aclass = ALLOCNO_CLASS (conflict_a);
1726 ira_assert (ira_reg_classes_intersect_p
1727 [aclass][conflict_aclass]);
1728 if (ALLOCNO_ASSIGNED_P (conflict_a))
1730 hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
1731 if (hard_regno >= 0
1732 && (ira_hard_reg_set_intersection_p
1733 (hard_regno, ALLOCNO_MODE (conflict_a),
1734 reg_class_contents[aclass])))
1736 int n_objects = ALLOCNO_NUM_OBJECTS (conflict_a);
1737 int conflict_nregs;
1739 mode = ALLOCNO_MODE (conflict_a);
1740 conflict_nregs = hard_regno_nregs[hard_regno][mode];
1741 if (conflict_nregs == n_objects && conflict_nregs > 1)
1743 int num = OBJECT_SUBWORD (conflict_obj);
1745 if (REG_WORDS_BIG_ENDIAN)
1746 SET_HARD_REG_BIT (conflicting_regs[word],
1747 hard_regno + n_objects - num - 1);
1748 else
1749 SET_HARD_REG_BIT (conflicting_regs[word],
1750 hard_regno + num);
1752 else
1753 IOR_HARD_REG_SET
1754 (conflicting_regs[word],
1755 ira_reg_mode_hard_regset[hard_regno][mode]);
1756 if (hard_reg_set_subset_p (profitable_hard_regs,
1757 conflicting_regs[word]))
1758 goto fail;
1761 else if (! retry_p
1762 && ! ALLOCNO_COLOR_DATA (conflict_a)->may_be_spilled_p
1763 /* Don't process the conflict allocno twice. */
1764 && (ALLOCNO_COLOR_DATA (conflict_a)->last_process
1765 != curr_allocno_process))
1767 int k, *conflict_costs;
1769 ALLOCNO_COLOR_DATA (conflict_a)->last_process
1770 = curr_allocno_process;
1771 ira_allocate_and_copy_costs
1772 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a),
1773 conflict_aclass,
1774 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_a));
1775 conflict_costs
1776 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a);
1777 if (conflict_costs != NULL)
1778 for (j = class_size - 1; j >= 0; j--)
1780 hard_regno = ira_class_hard_regs[aclass][j];
1781 ira_assert (hard_regno >= 0);
1782 k = ira_class_hard_reg_index[conflict_aclass][hard_regno];
1783 if (k < 0)
1784 continue;
1785 full_costs[j] -= conflict_costs[k];
1787 queue_update_cost (conflict_a, NULL, COST_HOP_DIVISOR);
1792 if (! retry_p)
1793 /* Take into account preferences of allocnos connected by copies to
1794 the conflict allocnos. */
1795 update_conflict_hard_regno_costs (full_costs, aclass, true);
1797 /* Take preferences of allocnos connected by copies into
1798 account. */
1799 if (! retry_p)
1801 start_update_cost ();
1802 queue_update_cost (a, NULL, COST_HOP_DIVISOR);
1803 update_conflict_hard_regno_costs (full_costs, aclass, false);
1805 min_cost = min_full_cost = INT_MAX;
1806 /* We don't care about giving callee saved registers to allocnos no
1807 living through calls because call clobbered registers are
1808 allocated first (it is usual practice to put them first in
1809 REG_ALLOC_ORDER). */
1810 mode = ALLOCNO_MODE (a);
1811 for (i = 0; i < class_size; i++)
1813 hard_regno = ira_class_hard_regs[aclass][i];
1814 #ifdef STACK_REGS
1815 if (no_stack_reg_p
1816 && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
1817 continue;
1818 #endif
1819 if (! check_hard_reg_p (a, hard_regno,
1820 conflicting_regs, profitable_hard_regs))
1821 continue;
1822 cost = costs[i];
1823 full_cost = full_costs[i];
1824 if (!HONOR_REG_ALLOC_ORDER)
1826 if ((saved_nregs = calculate_saved_nregs (hard_regno, mode)) != 0)
1827 /* We need to save/restore the hard register in
1828 epilogue/prologue. Therefore we increase the cost. */
1830 rclass = REGNO_REG_CLASS (hard_regno);
1831 add_cost = ((ira_memory_move_cost[mode][rclass][0]
1832 + ira_memory_move_cost[mode][rclass][1])
1833 * saved_nregs / hard_regno_nregs[hard_regno][mode] - 1);
1834 cost += add_cost;
1835 full_cost += add_cost;
1838 if (min_cost > cost)
1839 min_cost = cost;
1840 if (min_full_cost > full_cost)
1842 min_full_cost = full_cost;
1843 best_hard_regno = hard_regno;
1844 ira_assert (hard_regno >= 0);
1847 if (min_full_cost > mem_cost)
1849 if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1850 fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
1851 mem_cost, min_full_cost);
1852 best_hard_regno = -1;
1854 fail:
1855 if (best_hard_regno >= 0)
1857 for (i = hard_regno_nregs[best_hard_regno][mode] - 1; i >= 0; i--)
1858 allocated_hardreg_p[best_hard_regno + i] = true;
1860 if (! retry_p)
1861 restore_costs_from_copies (a);
1862 ALLOCNO_HARD_REGNO (a) = best_hard_regno;
1863 ALLOCNO_ASSIGNED_P (a) = true;
1864 if (best_hard_regno >= 0)
1865 update_costs_from_copies (a, true, ! retry_p);
1866 ira_assert (ALLOCNO_CLASS (a) == aclass);
1867 /* We don't need updated costs anymore: */
1868 ira_free_allocno_updated_costs (a);
1869 return best_hard_regno >= 0;
1874 /* An array used to sort copies. */
1875 static ira_copy_t *sorted_copies;
1877 /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
1878 used to find a conflict for new allocnos or allocnos with the
1879 different allocno classes. */
1880 static bool
1881 allocnos_conflict_by_live_ranges_p (ira_allocno_t a1, ira_allocno_t a2)
1883 rtx reg1, reg2;
1884 int i, j;
1885 int n1 = ALLOCNO_NUM_OBJECTS (a1);
1886 int n2 = ALLOCNO_NUM_OBJECTS (a2);
1888 if (a1 == a2)
1889 return false;
1890 reg1 = regno_reg_rtx[ALLOCNO_REGNO (a1)];
1891 reg2 = regno_reg_rtx[ALLOCNO_REGNO (a2)];
1892 if (reg1 != NULL && reg2 != NULL
1893 && ORIGINAL_REGNO (reg1) == ORIGINAL_REGNO (reg2))
1894 return false;
1896 for (i = 0; i < n1; i++)
1898 ira_object_t c1 = ALLOCNO_OBJECT (a1, i);
1900 for (j = 0; j < n2; j++)
1902 ira_object_t c2 = ALLOCNO_OBJECT (a2, j);
1904 if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1),
1905 OBJECT_LIVE_RANGES (c2)))
1906 return true;
1909 return false;
1912 /* The function is used to sort copies according to their execution
1913 frequencies. */
1914 static int
1915 copy_freq_compare_func (const void *v1p, const void *v2p)
1917 ira_copy_t cp1 = *(const ira_copy_t *) v1p, cp2 = *(const ira_copy_t *) v2p;
1918 int pri1, pri2;
1920 pri1 = cp1->freq;
1921 pri2 = cp2->freq;
1922 if (pri2 - pri1)
1923 return pri2 - pri1;
1925 /* If freqencies are equal, sort by copies, so that the results of
1926 qsort leave nothing to chance. */
1927 return cp1->num - cp2->num;
1932 /* Return true if any allocno from thread of A1 conflicts with any
1933 allocno from thread A2. */
1934 static bool
1935 allocno_thread_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
1937 ira_allocno_t a, conflict_a;
1939 for (a = ALLOCNO_COLOR_DATA (a2)->next_thread_allocno;;
1940 a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
1942 for (conflict_a = ALLOCNO_COLOR_DATA (a1)->next_thread_allocno;;
1943 conflict_a = ALLOCNO_COLOR_DATA (conflict_a)->next_thread_allocno)
1945 if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
1946 return true;
1947 if (conflict_a == a1)
1948 break;
1950 if (a == a2)
1951 break;
1953 return false;
1956 /* Merge two threads given correspondingly by their first allocnos T1
1957 and T2 (more accurately merging T2 into T1). */
1958 static void
1959 merge_threads (ira_allocno_t t1, ira_allocno_t t2)
1961 ira_allocno_t a, next, last;
1963 gcc_assert (t1 != t2
1964 && ALLOCNO_COLOR_DATA (t1)->first_thread_allocno == t1
1965 && ALLOCNO_COLOR_DATA (t2)->first_thread_allocno == t2);
1966 for (last = t2, a = ALLOCNO_COLOR_DATA (t2)->next_thread_allocno;;
1967 a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
1969 ALLOCNO_COLOR_DATA (a)->first_thread_allocno = t1;
1970 if (a == t2)
1971 break;
1972 last = a;
1974 next = ALLOCNO_COLOR_DATA (t1)->next_thread_allocno;
1975 ALLOCNO_COLOR_DATA (t1)->next_thread_allocno = t2;
1976 ALLOCNO_COLOR_DATA (last)->next_thread_allocno = next;
1977 ALLOCNO_COLOR_DATA (t1)->thread_freq += ALLOCNO_COLOR_DATA (t2)->thread_freq;
1980 /* Create threads by processing CP_NUM copies from sorted)ciopeis. We
1981 process the most expensive copies first. */
1982 static void
1983 form_threads_from_copies (int cp_num)
1985 ira_allocno_t a, thread1, thread2;
1986 ira_copy_t cp;
1987 int i, n;
1989 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
1990 /* Form threads processing copies, most frequently executed
1991 first. */
1992 for (; cp_num != 0;)
1994 for (i = 0; i < cp_num; i++)
1996 cp = sorted_copies[i];
1997 thread1 = ALLOCNO_COLOR_DATA (cp->first)->first_thread_allocno;
1998 thread2 = ALLOCNO_COLOR_DATA (cp->second)->first_thread_allocno;
1999 if (thread1 == thread2)
2000 continue;
2001 if (! allocno_thread_conflict_p (thread1, thread2))
2003 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2004 fprintf
2005 (ira_dump_file,
2006 " Forming thread by copy %d:a%dr%d-a%dr%d (freq=%d):\n",
2007 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
2008 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
2009 cp->freq);
2010 merge_threads (thread1, thread2);
2011 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2013 thread1 = ALLOCNO_COLOR_DATA (thread1)->first_thread_allocno;
2014 fprintf (ira_dump_file, " Result (freq=%d): a%dr%d(%d)",
2015 ALLOCNO_COLOR_DATA (thread1)->thread_freq,
2016 ALLOCNO_NUM (thread1), ALLOCNO_REGNO (thread1),
2017 ALLOCNO_FREQ (thread1));
2018 for (a = ALLOCNO_COLOR_DATA (thread1)->next_thread_allocno;
2019 a != thread1;
2020 a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2021 fprintf (ira_dump_file, " a%dr%d(%d)",
2022 ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2023 ALLOCNO_FREQ (a));
2024 fprintf (ira_dump_file, "\n");
2026 i++;
2027 break;
2030 /* Collect the rest of copies. */
2031 for (n = 0; i < cp_num; i++)
2033 cp = sorted_copies[i];
2034 if (ALLOCNO_COLOR_DATA (cp->first)->first_thread_allocno
2035 != ALLOCNO_COLOR_DATA (cp->second)->first_thread_allocno)
2036 sorted_copies[n++] = cp;
2038 cp_num = n;
2042 /* Create threads by processing copies of all alocnos from BUCKET. We
2043 process the most expensive copies first. */
2044 static void
2045 form_threads_from_bucket (ira_allocno_t bucket)
2047 ira_allocno_t a;
2048 ira_copy_t cp, next_cp;
2049 int cp_num = 0;
2051 for (a = bucket; a != NULL; a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2053 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2055 if (cp->first == a)
2057 next_cp = cp->next_first_allocno_copy;
2058 sorted_copies[cp_num++] = cp;
2060 else if (cp->second == a)
2061 next_cp = cp->next_second_allocno_copy;
2062 else
2063 gcc_unreachable ();
2066 form_threads_from_copies (cp_num);
2069 /* Create threads by processing copies of colorable allocno A. We
2070 process most expensive copies first. */
2071 static void
2072 form_threads_from_colorable_allocno (ira_allocno_t a)
2074 ira_allocno_t another_a;
2075 ira_copy_t cp, next_cp;
2076 int cp_num = 0;
2078 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2080 if (cp->first == a)
2082 next_cp = cp->next_first_allocno_copy;
2083 another_a = cp->second;
2085 else if (cp->second == a)
2087 next_cp = cp->next_second_allocno_copy;
2088 another_a = cp->first;
2090 else
2091 gcc_unreachable ();
2092 if ((! ALLOCNO_COLOR_DATA (another_a)->in_graph_p
2093 && !ALLOCNO_COLOR_DATA (another_a)->may_be_spilled_p)
2094 || ALLOCNO_COLOR_DATA (another_a)->colorable_p)
2095 sorted_copies[cp_num++] = cp;
2097 form_threads_from_copies (cp_num);
2100 /* Form initial threads which contain only one allocno. */
2101 static void
2102 init_allocno_threads (void)
2104 ira_allocno_t a;
2105 unsigned int j;
2106 bitmap_iterator bi;
2108 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2110 a = ira_allocnos[j];
2111 /* Set up initial thread data: */
2112 ALLOCNO_COLOR_DATA (a)->first_thread_allocno
2113 = ALLOCNO_COLOR_DATA (a)->next_thread_allocno = a;
2114 ALLOCNO_COLOR_DATA (a)->thread_freq = ALLOCNO_FREQ (a);
2120 /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
2122 /* Bucket of allocnos that can colored currently without spilling. */
2123 static ira_allocno_t colorable_allocno_bucket;
2125 /* Bucket of allocnos that might be not colored currently without
2126 spilling. */
2127 static ira_allocno_t uncolorable_allocno_bucket;
2129 /* The current number of allocnos in the uncolorable_bucket. */
2130 static int uncolorable_allocnos_num;
2132 /* Return the current spill priority of allocno A. The less the
2133 number, the more preferable the allocno for spilling. */
2134 static inline int
2135 allocno_spill_priority (ira_allocno_t a)
2137 allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
2139 return (data->temp
2140 / (ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
2141 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
2142 + 1));
2145 /* Add allocno A to bucket *BUCKET_PTR. A should be not in a bucket
2146 before the call. */
2147 static void
2148 add_allocno_to_bucket (ira_allocno_t a, ira_allocno_t *bucket_ptr)
2150 ira_allocno_t first_a;
2151 allocno_color_data_t data;
2153 if (bucket_ptr == &uncolorable_allocno_bucket
2154 && ALLOCNO_CLASS (a) != NO_REGS)
2156 uncolorable_allocnos_num++;
2157 ira_assert (uncolorable_allocnos_num > 0);
2159 first_a = *bucket_ptr;
2160 data = ALLOCNO_COLOR_DATA (a);
2161 data->next_bucket_allocno = first_a;
2162 data->prev_bucket_allocno = NULL;
2163 if (first_a != NULL)
2164 ALLOCNO_COLOR_DATA (first_a)->prev_bucket_allocno = a;
2165 *bucket_ptr = a;
2168 /* Compare two allocnos to define which allocno should be pushed first
2169 into the coloring stack. If the return is a negative number, the
2170 allocno given by the first parameter will be pushed first. In this
2171 case such allocno has less priority than the second one and the
2172 hard register will be assigned to it after assignment to the second
2173 one. As the result of such assignment order, the second allocno
2174 has a better chance to get the best hard register. */
2175 static int
2176 bucket_allocno_compare_func (const void *v1p, const void *v2p)
2178 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2179 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2180 int diff, freq1, freq2, a1_num, a2_num;
2181 ira_allocno_t t1 = ALLOCNO_COLOR_DATA (a1)->first_thread_allocno;
2182 ira_allocno_t t2 = ALLOCNO_COLOR_DATA (a2)->first_thread_allocno;
2183 int cl1 = ALLOCNO_CLASS (a1), cl2 = ALLOCNO_CLASS (a2);
2185 freq1 = ALLOCNO_COLOR_DATA (t1)->thread_freq;
2186 freq2 = ALLOCNO_COLOR_DATA (t2)->thread_freq;
2187 if ((diff = freq1 - freq2) != 0)
2188 return diff;
2190 if ((diff = ALLOCNO_NUM (t2) - ALLOCNO_NUM (t1)) != 0)
2191 return diff;
2193 /* Push pseudos requiring less hard registers first. It means that
2194 we will assign pseudos requiring more hard registers first
2195 avoiding creation small holes in free hard register file into
2196 which the pseudos requiring more hard registers can not fit. */
2197 if ((diff = (ira_reg_class_max_nregs[cl1][ALLOCNO_MODE (a1)]
2198 - ira_reg_class_max_nregs[cl2][ALLOCNO_MODE (a2)])) != 0)
2199 return diff;
2201 freq1 = ALLOCNO_FREQ (a1);
2202 freq2 = ALLOCNO_FREQ (a2);
2203 if ((diff = freq1 - freq2) != 0)
2204 return diff;
2206 a1_num = ALLOCNO_COLOR_DATA (a1)->available_regs_num;
2207 a2_num = ALLOCNO_COLOR_DATA (a2)->available_regs_num;
2208 if ((diff = a2_num - a1_num) != 0)
2209 return diff;
2210 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
2213 /* Sort bucket *BUCKET_PTR and return the result through
2214 BUCKET_PTR. */
2215 static void
2216 sort_bucket (ira_allocno_t *bucket_ptr,
2217 int (*compare_func) (const void *, const void *))
2219 ira_allocno_t a, head;
2220 int n;
2222 for (n = 0, a = *bucket_ptr;
2223 a != NULL;
2224 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2225 sorted_allocnos[n++] = a;
2226 if (n <= 1)
2227 return;
2228 qsort (sorted_allocnos, n, sizeof (ira_allocno_t), compare_func);
2229 head = NULL;
2230 for (n--; n >= 0; n--)
2232 a = sorted_allocnos[n];
2233 ALLOCNO_COLOR_DATA (a)->next_bucket_allocno = head;
2234 ALLOCNO_COLOR_DATA (a)->prev_bucket_allocno = NULL;
2235 if (head != NULL)
2236 ALLOCNO_COLOR_DATA (head)->prev_bucket_allocno = a;
2237 head = a;
2239 *bucket_ptr = head;
2242 /* Add ALLOCNO to colorable bucket maintaining the order according
2243 their priority. ALLOCNO should be not in a bucket before the
2244 call. */
2245 static void
2246 add_allocno_to_ordered_colorable_bucket (ira_allocno_t allocno)
2248 ira_allocno_t before, after;
2250 form_threads_from_colorable_allocno (allocno);
2251 for (before = colorable_allocno_bucket, after = NULL;
2252 before != NULL;
2253 after = before,
2254 before = ALLOCNO_COLOR_DATA (before)->next_bucket_allocno)
2255 if (bucket_allocno_compare_func (&allocno, &before) < 0)
2256 break;
2257 ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno = before;
2258 ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno = after;
2259 if (after == NULL)
2260 colorable_allocno_bucket = allocno;
2261 else
2262 ALLOCNO_COLOR_DATA (after)->next_bucket_allocno = allocno;
2263 if (before != NULL)
2264 ALLOCNO_COLOR_DATA (before)->prev_bucket_allocno = allocno;
2267 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
2268 the call. */
2269 static void
2270 delete_allocno_from_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
2272 ira_allocno_t prev_allocno, next_allocno;
2274 if (bucket_ptr == &uncolorable_allocno_bucket
2275 && ALLOCNO_CLASS (allocno) != NO_REGS)
2277 uncolorable_allocnos_num--;
2278 ira_assert (uncolorable_allocnos_num >= 0);
2280 prev_allocno = ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno;
2281 next_allocno = ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno;
2282 if (prev_allocno != NULL)
2283 ALLOCNO_COLOR_DATA (prev_allocno)->next_bucket_allocno = next_allocno;
2284 else
2286 ira_assert (*bucket_ptr == allocno);
2287 *bucket_ptr = next_allocno;
2289 if (next_allocno != NULL)
2290 ALLOCNO_COLOR_DATA (next_allocno)->prev_bucket_allocno = prev_allocno;
2293 /* Put allocno A onto the coloring stack without removing it from its
2294 bucket. Pushing allocno to the coloring stack can result in moving
2295 conflicting allocnos from the uncolorable bucket to the colorable
2296 one. */
2297 static void
2298 push_allocno_to_stack (ira_allocno_t a)
2300 enum reg_class aclass;
2301 allocno_color_data_t data, conflict_data;
2302 int size, i, n = ALLOCNO_NUM_OBJECTS (a);
2304 data = ALLOCNO_COLOR_DATA (a);
2305 data->in_graph_p = false;
2306 allocno_stack_vec.safe_push (a);
2307 aclass = ALLOCNO_CLASS (a);
2308 if (aclass == NO_REGS)
2309 return;
2310 size = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
2311 if (n > 1)
2313 /* We will deal with the subwords individually. */
2314 gcc_assert (size == ALLOCNO_NUM_OBJECTS (a));
2315 size = 1;
2317 for (i = 0; i < n; i++)
2319 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2320 ira_object_t conflict_obj;
2321 ira_object_conflict_iterator oci;
2323 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2325 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2327 conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
2328 if (conflict_data->colorable_p
2329 || ! conflict_data->in_graph_p
2330 || ALLOCNO_ASSIGNED_P (conflict_a)
2331 || !(hard_reg_set_intersect_p
2332 (ALLOCNO_COLOR_DATA (a)->profitable_hard_regs,
2333 conflict_data->profitable_hard_regs)))
2334 continue;
2335 ira_assert (bitmap_bit_p (coloring_allocno_bitmap,
2336 ALLOCNO_NUM (conflict_a)));
2337 if (update_left_conflict_sizes_p (conflict_a, a, size))
2339 delete_allocno_from_bucket
2340 (conflict_a, &uncolorable_allocno_bucket);
2341 add_allocno_to_ordered_colorable_bucket (conflict_a);
2342 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2344 fprintf (ira_dump_file, " Making");
2345 ira_print_expanded_allocno (conflict_a);
2346 fprintf (ira_dump_file, " colorable\n");
2354 /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
2355 The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
2356 static void
2357 remove_allocno_from_bucket_and_push (ira_allocno_t allocno, bool colorable_p)
2359 if (colorable_p)
2360 delete_allocno_from_bucket (allocno, &colorable_allocno_bucket);
2361 else
2362 delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
2363 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2365 fprintf (ira_dump_file, " Pushing");
2366 ira_print_expanded_allocno (allocno);
2367 if (colorable_p)
2368 fprintf (ira_dump_file, "(cost %d)\n",
2369 ALLOCNO_COLOR_DATA (allocno)->temp);
2370 else
2371 fprintf (ira_dump_file, "(potential spill: %spri=%d, cost=%d)\n",
2372 ALLOCNO_BAD_SPILL_P (allocno) ? "bad spill, " : "",
2373 allocno_spill_priority (allocno),
2374 ALLOCNO_COLOR_DATA (allocno)->temp);
2376 if (! colorable_p)
2377 ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p = true;
2378 push_allocno_to_stack (allocno);
2381 /* Put all allocnos from colorable bucket onto the coloring stack. */
2382 static void
2383 push_only_colorable (void)
2385 form_threads_from_bucket (colorable_allocno_bucket);
2386 sort_bucket (&colorable_allocno_bucket, bucket_allocno_compare_func);
2387 for (;colorable_allocno_bucket != NULL;)
2388 remove_allocno_from_bucket_and_push (colorable_allocno_bucket, true);
2391 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
2392 loop given by its LOOP_NODE. */
2394 ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
2396 int freq, i;
2397 edge_iterator ei;
2398 edge e;
2399 vec<edge> edges;
2401 ira_assert (current_loops != NULL && loop_node->loop != NULL
2402 && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
2403 freq = 0;
2404 if (! exit_p)
2406 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
2407 if (e->src != loop_node->loop->latch
2408 && (regno < 0
2409 || (bitmap_bit_p (df_get_live_out (e->src), regno)
2410 && bitmap_bit_p (df_get_live_in (e->dest), regno))))
2411 freq += EDGE_FREQUENCY (e);
2413 else
2415 edges = get_loop_exit_edges (loop_node->loop);
2416 FOR_EACH_VEC_ELT (edges, i, e)
2417 if (regno < 0
2418 || (bitmap_bit_p (df_get_live_out (e->src), regno)
2419 && bitmap_bit_p (df_get_live_in (e->dest), regno)))
2420 freq += EDGE_FREQUENCY (e);
2421 edges.release ();
2424 return REG_FREQ_FROM_EDGE_FREQ (freq);
2427 /* Calculate and return the cost of putting allocno A into memory. */
2428 static int
2429 calculate_allocno_spill_cost (ira_allocno_t a)
2431 int regno, cost;
2432 enum machine_mode mode;
2433 enum reg_class rclass;
2434 ira_allocno_t parent_allocno;
2435 ira_loop_tree_node_t parent_node, loop_node;
2437 regno = ALLOCNO_REGNO (a);
2438 cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_UPDATED_CLASS_COST (a);
2439 if (ALLOCNO_CAP (a) != NULL)
2440 return cost;
2441 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
2442 if ((parent_node = loop_node->parent) == NULL)
2443 return cost;
2444 if ((parent_allocno = parent_node->regno_allocno_map[regno]) == NULL)
2445 return cost;
2446 mode = ALLOCNO_MODE (a);
2447 rclass = ALLOCNO_CLASS (a);
2448 if (ALLOCNO_HARD_REGNO (parent_allocno) < 0)
2449 cost -= (ira_memory_move_cost[mode][rclass][0]
2450 * ira_loop_edge_freq (loop_node, regno, true)
2451 + ira_memory_move_cost[mode][rclass][1]
2452 * ira_loop_edge_freq (loop_node, regno, false));
2453 else
2455 ira_init_register_move_cost_if_necessary (mode);
2456 cost += ((ira_memory_move_cost[mode][rclass][1]
2457 * ira_loop_edge_freq (loop_node, regno, true)
2458 + ira_memory_move_cost[mode][rclass][0]
2459 * ira_loop_edge_freq (loop_node, regno, false))
2460 - (ira_register_move_cost[mode][rclass][rclass]
2461 * (ira_loop_edge_freq (loop_node, regno, false)
2462 + ira_loop_edge_freq (loop_node, regno, true))));
2464 return cost;
2467 /* Used for sorting allocnos for spilling. */
2468 static inline int
2469 allocno_spill_priority_compare (ira_allocno_t a1, ira_allocno_t a2)
2471 int pri1, pri2, diff;
2473 if (ALLOCNO_BAD_SPILL_P (a1) && ! ALLOCNO_BAD_SPILL_P (a2))
2474 return 1;
2475 if (ALLOCNO_BAD_SPILL_P (a2) && ! ALLOCNO_BAD_SPILL_P (a1))
2476 return -1;
2477 pri1 = allocno_spill_priority (a1);
2478 pri2 = allocno_spill_priority (a2);
2479 if ((diff = pri1 - pri2) != 0)
2480 return diff;
2481 if ((diff
2482 = ALLOCNO_COLOR_DATA (a1)->temp - ALLOCNO_COLOR_DATA (a2)->temp) != 0)
2483 return diff;
2484 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2487 /* Used for sorting allocnos for spilling. */
2488 static int
2489 allocno_spill_sort_compare (const void *v1p, const void *v2p)
2491 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2492 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2494 return allocno_spill_priority_compare (p1, p2);
2497 /* Push allocnos to the coloring stack. The order of allocnos in the
2498 stack defines the order for the subsequent coloring. */
2499 static void
2500 push_allocnos_to_stack (void)
2502 ira_allocno_t a;
2503 int cost;
2505 /* Calculate uncolorable allocno spill costs. */
2506 for (a = uncolorable_allocno_bucket;
2507 a != NULL;
2508 a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2509 if (ALLOCNO_CLASS (a) != NO_REGS)
2511 cost = calculate_allocno_spill_cost (a);
2512 /* ??? Remove cost of copies between the coalesced
2513 allocnos. */
2514 ALLOCNO_COLOR_DATA (a)->temp = cost;
2516 sort_bucket (&uncolorable_allocno_bucket, allocno_spill_sort_compare);
2517 for (;;)
2519 push_only_colorable ();
2520 a = uncolorable_allocno_bucket;
2521 if (a == NULL)
2522 break;
2523 remove_allocno_from_bucket_and_push (a, false);
2525 ira_assert (colorable_allocno_bucket == NULL
2526 && uncolorable_allocno_bucket == NULL);
2527 ira_assert (uncolorable_allocnos_num == 0);
2530 /* Pop the coloring stack and assign hard registers to the popped
2531 allocnos. */
2532 static void
2533 pop_allocnos_from_stack (void)
2535 ira_allocno_t allocno;
2536 enum reg_class aclass;
2538 for (;allocno_stack_vec.length () != 0;)
2540 allocno = allocno_stack_vec.pop ();
2541 aclass = ALLOCNO_CLASS (allocno);
2542 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2544 fprintf (ira_dump_file, " Popping");
2545 ira_print_expanded_allocno (allocno);
2546 fprintf (ira_dump_file, " -- ");
2548 if (aclass == NO_REGS)
2550 ALLOCNO_HARD_REGNO (allocno) = -1;
2551 ALLOCNO_ASSIGNED_P (allocno) = true;
2552 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
2553 ira_assert
2554 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
2555 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2556 fprintf (ira_dump_file, "assign memory\n");
2558 else if (assign_hard_reg (allocno, false))
2560 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2561 fprintf (ira_dump_file, "assign reg %d\n",
2562 ALLOCNO_HARD_REGNO (allocno));
2564 else if (ALLOCNO_ASSIGNED_P (allocno))
2566 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2567 fprintf (ira_dump_file, "spill%s\n",
2568 ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p
2569 ? "" : "!");
2571 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2575 /* Set up number of available hard registers for allocno A. */
2576 static void
2577 setup_allocno_available_regs_num (ira_allocno_t a)
2579 int i, n, hard_regno, hard_regs_num, nwords;
2580 enum reg_class aclass;
2581 allocno_color_data_t data;
2583 aclass = ALLOCNO_CLASS (a);
2584 data = ALLOCNO_COLOR_DATA (a);
2585 data->available_regs_num = 0;
2586 if (aclass == NO_REGS)
2587 return;
2588 hard_regs_num = ira_class_hard_regs_num[aclass];
2589 nwords = ALLOCNO_NUM_OBJECTS (a);
2590 for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
2592 hard_regno = ira_class_hard_regs[aclass][i];
2593 /* Checking only profitable hard regs. */
2594 if (TEST_HARD_REG_BIT (data->profitable_hard_regs, hard_regno))
2595 n++;
2597 data->available_regs_num = n;
2598 if (internal_flag_ira_verbose <= 2 || ira_dump_file == NULL)
2599 return;
2600 fprintf
2601 (ira_dump_file,
2602 " Allocno a%dr%d of %s(%d) has %d avail. regs ",
2603 ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2604 reg_class_names[aclass], ira_class_hard_regs_num[aclass], n);
2605 print_hard_reg_set (ira_dump_file, data->profitable_hard_regs, false);
2606 fprintf (ira_dump_file, ", %snode: ",
2607 hard_reg_set_equal_p (data->profitable_hard_regs,
2608 data->hard_regs_node->hard_regs->set)
2609 ? "" : "^");
2610 print_hard_reg_set (ira_dump_file,
2611 data->hard_regs_node->hard_regs->set, false);
2612 for (i = 0; i < nwords; i++)
2614 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2616 if (nwords != 1)
2618 if (i != 0)
2619 fprintf (ira_dump_file, ", ");
2620 fprintf (ira_dump_file, " obj %d", i);
2622 fprintf (ira_dump_file, " (confl regs = ");
2623 print_hard_reg_set (ira_dump_file, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2624 false);
2625 fprintf (ira_dump_file, ")");
2627 fprintf (ira_dump_file, "\n");
2630 /* Put ALLOCNO in a bucket corresponding to its number and size of its
2631 conflicting allocnos and hard registers. */
2632 static void
2633 put_allocno_into_bucket (ira_allocno_t allocno)
2635 ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2636 setup_allocno_available_regs_num (allocno);
2637 if (setup_left_conflict_sizes_p (allocno))
2638 add_allocno_to_bucket (allocno, &colorable_allocno_bucket);
2639 else
2640 add_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
2643 /* Map: allocno number -> allocno priority. */
2644 static int *allocno_priorities;
2646 /* Set up priorities for N allocnos in array
2647 CONSIDERATION_ALLOCNOS. */
2648 static void
2649 setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
2651 int i, length, nrefs, priority, max_priority, mult;
2652 ira_allocno_t a;
2654 max_priority = 0;
2655 for (i = 0; i < n; i++)
2657 a = consideration_allocnos[i];
2658 nrefs = ALLOCNO_NREFS (a);
2659 ira_assert (nrefs >= 0);
2660 mult = floor_log2 (ALLOCNO_NREFS (a)) + 1;
2661 ira_assert (mult >= 0);
2662 allocno_priorities[ALLOCNO_NUM (a)]
2663 = priority
2664 = (mult
2665 * (ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a))
2666 * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
2667 if (priority < 0)
2668 priority = -priority;
2669 if (max_priority < priority)
2670 max_priority = priority;
2672 mult = max_priority == 0 ? 1 : INT_MAX / max_priority;
2673 for (i = 0; i < n; i++)
2675 a = consideration_allocnos[i];
2676 length = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2677 if (ALLOCNO_NUM_OBJECTS (a) > 1)
2678 length /= ALLOCNO_NUM_OBJECTS (a);
2679 if (length <= 0)
2680 length = 1;
2681 allocno_priorities[ALLOCNO_NUM (a)]
2682 = allocno_priorities[ALLOCNO_NUM (a)] * mult / length;
2686 /* Sort allocnos according to the profit of usage of a hard register
2687 instead of memory for them. */
2688 static int
2689 allocno_cost_compare_func (const void *v1p, const void *v2p)
2691 ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2692 ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2693 int c1, c2;
2695 c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_UPDATED_CLASS_COST (p1);
2696 c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_UPDATED_CLASS_COST (p2);
2697 if (c1 - c2)
2698 return c1 - c2;
2700 /* If regs are equally good, sort by allocno numbers, so that the
2701 results of qsort leave nothing to chance. */
2702 return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
2705 /* We used Chaitin-Briggs coloring to assign as many pseudos as
2706 possible to hard registers. Let us try to improve allocation with
2707 cost point of view. This function improves the allocation by
2708 spilling some allocnos and assigning the freed hard registers to
2709 other allocnos if it decreases the overall allocation cost. */
2710 static void
2711 improve_allocation (void)
2713 unsigned int i;
2714 int j, k, n, hregno, conflict_hregno, base_cost, class_size, word, nwords;
2715 int check, spill_cost, min_cost, nregs, conflict_nregs, r, best;
2716 bool try_p;
2717 enum reg_class aclass;
2718 enum machine_mode mode;
2719 int *allocno_costs;
2720 int costs[FIRST_PSEUDO_REGISTER];
2721 HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
2722 ira_allocno_t a;
2723 bitmap_iterator bi;
2725 /* Clear counts used to process conflicting allocnos only once for
2726 each allocno. */
2727 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2728 ALLOCNO_COLOR_DATA (ira_allocnos[i])->temp = 0;
2729 check = n = 0;
2730 /* Process each allocno and try to assign a hard register to it by
2731 spilling some its conflicting allocnos. */
2732 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2734 a = ira_allocnos[i];
2735 ALLOCNO_COLOR_DATA (a)->temp = 0;
2736 if (empty_profitable_hard_regs (a))
2737 continue;
2738 check++;
2739 aclass = ALLOCNO_CLASS (a);
2740 allocno_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
2741 if (allocno_costs == NULL)
2742 allocno_costs = ALLOCNO_HARD_REG_COSTS (a);
2743 if ((hregno = ALLOCNO_HARD_REGNO (a)) < 0)
2744 base_cost = ALLOCNO_UPDATED_MEMORY_COST (a);
2745 else if (allocno_costs == NULL)
2746 /* It means that assigning a hard register is not profitable
2747 (we don't waste memory for hard register costs in this
2748 case). */
2749 continue;
2750 else
2751 base_cost = allocno_costs[ira_class_hard_reg_index[aclass][hregno]];
2752 try_p = false;
2753 get_conflict_and_start_profitable_regs (a, false,
2754 conflicting_regs,
2755 &profitable_hard_regs);
2756 class_size = ira_class_hard_regs_num[aclass];
2757 /* Set up cost improvement for usage of each profitable hard
2758 register for allocno A. */
2759 for (j = 0; j < class_size; j++)
2761 hregno = ira_class_hard_regs[aclass][j];
2762 if (! check_hard_reg_p (a, hregno,
2763 conflicting_regs, profitable_hard_regs))
2764 continue;
2765 ira_assert (ira_class_hard_reg_index[aclass][hregno] == j);
2766 k = allocno_costs == NULL ? 0 : j;
2767 costs[hregno] = (allocno_costs == NULL
2768 ? ALLOCNO_UPDATED_CLASS_COST (a) : allocno_costs[k]);
2769 costs[hregno] -= base_cost;
2770 if (costs[hregno] < 0)
2771 try_p = true;
2773 if (! try_p)
2774 /* There is no chance to improve the allocation cost by
2775 assigning hard register to allocno A even without spilling
2776 conflicting allocnos. */
2777 continue;
2778 mode = ALLOCNO_MODE (a);
2779 nwords = ALLOCNO_NUM_OBJECTS (a);
2780 /* Process each allocno conflicting with A and update the cost
2781 improvement for profitable hard registers of A. To use a
2782 hard register for A we need to spill some conflicting
2783 allocnos and that creates penalty for the cost
2784 improvement. */
2785 for (word = 0; word < nwords; word++)
2787 ira_object_t conflict_obj;
2788 ira_object_t obj = ALLOCNO_OBJECT (a, word);
2789 ira_object_conflict_iterator oci;
2791 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2793 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2795 if (ALLOCNO_COLOR_DATA (conflict_a)->temp == check)
2796 /* We already processed this conflicting allocno
2797 because we processed earlier another object of the
2798 conflicting allocno. */
2799 continue;
2800 ALLOCNO_COLOR_DATA (conflict_a)->temp = check;
2801 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
2802 continue;
2803 spill_cost = ALLOCNO_UPDATED_MEMORY_COST (conflict_a);
2804 k = (ira_class_hard_reg_index
2805 [ALLOCNO_CLASS (conflict_a)][conflict_hregno]);
2806 ira_assert (k >= 0);
2807 if ((allocno_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (conflict_a))
2808 != NULL)
2809 spill_cost -= allocno_costs[k];
2810 else if ((allocno_costs = ALLOCNO_HARD_REG_COSTS (conflict_a))
2811 != NULL)
2812 spill_cost -= allocno_costs[k];
2813 else
2814 spill_cost -= ALLOCNO_UPDATED_CLASS_COST (conflict_a);
2815 conflict_nregs
2816 = hard_regno_nregs[conflict_hregno][ALLOCNO_MODE (conflict_a)];
2817 for (r = conflict_hregno;
2818 r >= 0 && r + hard_regno_nregs[r][mode] > conflict_hregno;
2819 r--)
2820 if (check_hard_reg_p (a, r,
2821 conflicting_regs, profitable_hard_regs))
2822 costs[r] += spill_cost;
2823 for (r = conflict_hregno + 1;
2824 r < conflict_hregno + conflict_nregs;
2825 r++)
2826 if (check_hard_reg_p (a, r,
2827 conflicting_regs, profitable_hard_regs))
2828 costs[r] += spill_cost;
2831 min_cost = INT_MAX;
2832 best = -1;
2833 /* Now we choose hard register for A which results in highest
2834 allocation cost improvement. */
2835 for (j = 0; j < class_size; j++)
2837 hregno = ira_class_hard_regs[aclass][j];
2838 if (check_hard_reg_p (a, hregno,
2839 conflicting_regs, profitable_hard_regs)
2840 && min_cost > costs[hregno])
2842 best = hregno;
2843 min_cost = costs[hregno];
2846 if (min_cost >= 0)
2847 /* We are in a situation when assigning any hard register to A
2848 by spilling some conflicting allocnos does not improve the
2849 allocation cost. */
2850 continue;
2851 nregs = hard_regno_nregs[best][mode];
2852 /* Now spill conflicting allocnos which contain a hard register
2853 of A when we assign the best chosen hard register to it. */
2854 for (word = 0; word < nwords; word++)
2856 ira_object_t conflict_obj;
2857 ira_object_t obj = ALLOCNO_OBJECT (a, word);
2858 ira_object_conflict_iterator oci;
2860 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2862 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2864 if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
2865 continue;
2866 conflict_nregs
2867 = hard_regno_nregs[conflict_hregno][ALLOCNO_MODE (conflict_a)];
2868 if (best + nregs <= conflict_hregno
2869 || conflict_hregno + conflict_nregs <= best)
2870 /* No intersection. */
2871 continue;
2872 ALLOCNO_HARD_REGNO (conflict_a) = -1;
2873 sorted_allocnos[n++] = conflict_a;
2874 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2875 fprintf (ira_dump_file, "Spilling a%dr%d for a%dr%d\n",
2876 ALLOCNO_NUM (conflict_a), ALLOCNO_REGNO (conflict_a),
2877 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2880 /* Assign the best chosen hard register to A. */
2881 ALLOCNO_HARD_REGNO (a) = best;
2882 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2883 fprintf (ira_dump_file, "Assigning %d to a%dr%d\n",
2884 best, ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
2886 if (n == 0)
2887 return;
2888 /* We spilled some allocnos to assign their hard registers to other
2889 allocnos. The spilled allocnos are now in array
2890 'sorted_allocnos'. There is still a possibility that some of the
2891 spilled allocnos can get hard registers. So let us try assign
2892 them hard registers again (just a reminder -- function
2893 'assign_hard_reg' assigns hard registers only if it is possible
2894 and profitable). We process the spilled allocnos with biggest
2895 benefit to get hard register first -- see function
2896 'allocno_cost_compare_func'. */
2897 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
2898 allocno_cost_compare_func);
2899 for (j = 0; j < n; j++)
2901 a = sorted_allocnos[j];
2902 ALLOCNO_ASSIGNED_P (a) = false;
2903 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2905 fprintf (ira_dump_file, " ");
2906 ira_print_expanded_allocno (a);
2907 fprintf (ira_dump_file, " -- ");
2909 if (assign_hard_reg (a, false))
2911 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2912 fprintf (ira_dump_file, "assign hard reg %d\n",
2913 ALLOCNO_HARD_REGNO (a));
2915 else
2917 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2918 fprintf (ira_dump_file, "assign memory\n");
2923 /* Sort allocnos according to their priorities. */
2924 static int
2925 allocno_priority_compare_func (const void *v1p, const void *v2p)
2927 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2928 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2929 int pri1, pri2;
2931 pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
2932 pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
2933 if (pri2 != pri1)
2934 return SORTGT (pri2, pri1);
2936 /* If regs are equally good, sort by allocnos, so that the results of
2937 qsort leave nothing to chance. */
2938 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2941 /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
2942 taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
2943 static void
2944 color_allocnos (void)
2946 unsigned int i, n;
2947 bitmap_iterator bi;
2948 ira_allocno_t a;
2950 setup_profitable_hard_regs ();
2951 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2953 int l, nr;
2954 HARD_REG_SET conflict_hard_regs;
2955 allocno_color_data_t data;
2956 ira_pref_t pref, next_pref;
2958 a = ira_allocnos[i];
2959 nr = ALLOCNO_NUM_OBJECTS (a);
2960 CLEAR_HARD_REG_SET (conflict_hard_regs);
2961 for (l = 0; l < nr; l++)
2963 ira_object_t obj = ALLOCNO_OBJECT (a, l);
2964 IOR_HARD_REG_SET (conflict_hard_regs,
2965 OBJECT_CONFLICT_HARD_REGS (obj));
2967 data = ALLOCNO_COLOR_DATA (a);
2968 for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = next_pref)
2970 next_pref = pref->next_pref;
2971 if (! ira_hard_reg_in_set_p (pref->hard_regno,
2972 ALLOCNO_MODE (a),
2973 data->profitable_hard_regs))
2974 ira_remove_pref (pref);
2977 if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
2979 n = 0;
2980 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2982 a = ira_allocnos[i];
2983 if (ALLOCNO_CLASS (a) == NO_REGS)
2985 ALLOCNO_HARD_REGNO (a) = -1;
2986 ALLOCNO_ASSIGNED_P (a) = true;
2987 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
2988 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
2989 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2991 fprintf (ira_dump_file, " Spill");
2992 ira_print_expanded_allocno (a);
2993 fprintf (ira_dump_file, "\n");
2995 continue;
2997 sorted_allocnos[n++] = a;
2999 if (n != 0)
3001 setup_allocno_priorities (sorted_allocnos, n);
3002 qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
3003 allocno_priority_compare_func);
3004 for (i = 0; i < n; i++)
3006 a = sorted_allocnos[i];
3007 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3009 fprintf (ira_dump_file, " ");
3010 ira_print_expanded_allocno (a);
3011 fprintf (ira_dump_file, " -- ");
3013 if (assign_hard_reg (a, false))
3015 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3016 fprintf (ira_dump_file, "assign hard reg %d\n",
3017 ALLOCNO_HARD_REGNO (a));
3019 else
3021 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3022 fprintf (ira_dump_file, "assign memory\n");
3027 else
3029 form_allocno_hard_regs_nodes_forest ();
3030 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3031 print_hard_regs_forest (ira_dump_file);
3032 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3034 a = ira_allocnos[i];
3035 if (ALLOCNO_CLASS (a) != NO_REGS && ! empty_profitable_hard_regs (a))
3037 ALLOCNO_COLOR_DATA (a)->in_graph_p = true;
3038 update_costs_from_prefs (a);
3040 else
3042 ALLOCNO_HARD_REGNO (a) = -1;
3043 ALLOCNO_ASSIGNED_P (a) = true;
3044 /* We don't need updated costs anymore. */
3045 ira_free_allocno_updated_costs (a);
3046 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3048 fprintf (ira_dump_file, " Spill");
3049 ira_print_expanded_allocno (a);
3050 fprintf (ira_dump_file, "\n");
3054 /* Put the allocnos into the corresponding buckets. */
3055 colorable_allocno_bucket = NULL;
3056 uncolorable_allocno_bucket = NULL;
3057 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3059 a = ira_allocnos[i];
3060 if (ALLOCNO_COLOR_DATA (a)->in_graph_p)
3061 put_allocno_into_bucket (a);
3063 push_allocnos_to_stack ();
3064 pop_allocnos_from_stack ();
3065 finish_allocno_hard_regs_nodes_forest ();
3067 improve_allocation ();
3072 /* Output information about the loop given by its LOOP_TREE_NODE. */
3073 static void
3074 print_loop_title (ira_loop_tree_node_t loop_tree_node)
3076 unsigned int j;
3077 bitmap_iterator bi;
3078 ira_loop_tree_node_t subloop_node, dest_loop_node;
3079 edge e;
3080 edge_iterator ei;
3082 if (loop_tree_node->parent == NULL)
3083 fprintf (ira_dump_file,
3084 "\n Loop 0 (parent -1, header bb%d, depth 0)\n bbs:",
3085 NUM_FIXED_BLOCKS);
3086 else
3088 ira_assert (current_loops != NULL && loop_tree_node->loop != NULL);
3089 fprintf (ira_dump_file,
3090 "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
3091 loop_tree_node->loop_num, loop_tree_node->parent->loop_num,
3092 loop_tree_node->loop->header->index,
3093 loop_depth (loop_tree_node->loop));
3095 for (subloop_node = loop_tree_node->children;
3096 subloop_node != NULL;
3097 subloop_node = subloop_node->next)
3098 if (subloop_node->bb != NULL)
3100 fprintf (ira_dump_file, " %d", subloop_node->bb->index);
3101 FOR_EACH_EDGE (e, ei, subloop_node->bb->succs)
3102 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
3103 && ((dest_loop_node = IRA_BB_NODE (e->dest)->parent)
3104 != loop_tree_node))
3105 fprintf (ira_dump_file, "(->%d:l%d)",
3106 e->dest->index, dest_loop_node->loop_num);
3108 fprintf (ira_dump_file, "\n all:");
3109 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
3110 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
3111 fprintf (ira_dump_file, "\n modified regnos:");
3112 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
3113 fprintf (ira_dump_file, " %d", j);
3114 fprintf (ira_dump_file, "\n border:");
3115 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
3116 fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
3117 fprintf (ira_dump_file, "\n Pressure:");
3118 for (j = 0; (int) j < ira_pressure_classes_num; j++)
3120 enum reg_class pclass;
3122 pclass = ira_pressure_classes[j];
3123 if (loop_tree_node->reg_pressure[pclass] == 0)
3124 continue;
3125 fprintf (ira_dump_file, " %s=%d", reg_class_names[pclass],
3126 loop_tree_node->reg_pressure[pclass]);
3128 fprintf (ira_dump_file, "\n");
3131 /* Color the allocnos inside loop (in the extreme case it can be all
3132 of the function) given the corresponding LOOP_TREE_NODE. The
3133 function is called for each loop during top-down traverse of the
3134 loop tree. */
3135 static void
3136 color_pass (ira_loop_tree_node_t loop_tree_node)
3138 int regno, hard_regno, index = -1, n;
3139 int cost, exit_freq, enter_freq;
3140 unsigned int j;
3141 bitmap_iterator bi;
3142 enum machine_mode mode;
3143 enum reg_class rclass, aclass, pclass;
3144 ira_allocno_t a, subloop_allocno;
3145 ira_loop_tree_node_t subloop_node;
3147 ira_assert (loop_tree_node->bb == NULL);
3148 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3149 print_loop_title (loop_tree_node);
3151 bitmap_copy (coloring_allocno_bitmap, loop_tree_node->all_allocnos);
3152 bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
3153 n = 0;
3154 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3156 a = ira_allocnos[j];
3157 n++;
3158 if (! ALLOCNO_ASSIGNED_P (a))
3159 continue;
3160 bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
3162 allocno_color_data
3163 = (allocno_color_data_t) ira_allocate (sizeof (struct allocno_color_data)
3164 * n);
3165 memset (allocno_color_data, 0, sizeof (struct allocno_color_data) * n);
3166 curr_allocno_process = 0;
3167 n = 0;
3168 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3170 a = ira_allocnos[j];
3171 ALLOCNO_ADD_DATA (a) = allocno_color_data + n;
3172 n++;
3174 init_allocno_threads ();
3175 /* Color all mentioned allocnos including transparent ones. */
3176 color_allocnos ();
3177 /* Process caps. They are processed just once. */
3178 if (flag_ira_region == IRA_REGION_MIXED
3179 || flag_ira_region == IRA_REGION_ALL)
3180 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
3182 a = ira_allocnos[j];
3183 if (ALLOCNO_CAP_MEMBER (a) == NULL)
3184 continue;
3185 /* Remove from processing in the next loop. */
3186 bitmap_clear_bit (consideration_allocno_bitmap, j);
3187 rclass = ALLOCNO_CLASS (a);
3188 pclass = ira_pressure_class_translate[rclass];
3189 if (flag_ira_region == IRA_REGION_MIXED
3190 && (loop_tree_node->reg_pressure[pclass]
3191 <= ira_class_hard_regs_num[pclass]))
3193 mode = ALLOCNO_MODE (a);
3194 hard_regno = ALLOCNO_HARD_REGNO (a);
3195 if (hard_regno >= 0)
3197 index = ira_class_hard_reg_index[rclass][hard_regno];
3198 ira_assert (index >= 0);
3200 regno = ALLOCNO_REGNO (a);
3201 subloop_allocno = ALLOCNO_CAP_MEMBER (a);
3202 subloop_node = ALLOCNO_LOOP_TREE_NODE (subloop_allocno);
3203 ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno));
3204 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3205 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3206 if (hard_regno >= 0)
3207 update_costs_from_copies (subloop_allocno, true, true);
3208 /* We don't need updated costs anymore: */
3209 ira_free_allocno_updated_costs (subloop_allocno);
3212 /* Update costs of the corresponding allocnos (not caps) in the
3213 subloops. */
3214 for (subloop_node = loop_tree_node->subloops;
3215 subloop_node != NULL;
3216 subloop_node = subloop_node->subloop_next)
3218 ira_assert (subloop_node->bb == NULL);
3219 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3221 a = ira_allocnos[j];
3222 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3223 mode = ALLOCNO_MODE (a);
3224 rclass = ALLOCNO_CLASS (a);
3225 pclass = ira_pressure_class_translate[rclass];
3226 hard_regno = ALLOCNO_HARD_REGNO (a);
3227 /* Use hard register class here. ??? */
3228 if (hard_regno >= 0)
3230 index = ira_class_hard_reg_index[rclass][hard_regno];
3231 ira_assert (index >= 0);
3233 regno = ALLOCNO_REGNO (a);
3234 /* ??? conflict costs */
3235 subloop_allocno = subloop_node->regno_allocno_map[regno];
3236 if (subloop_allocno == NULL
3237 || ALLOCNO_CAP (subloop_allocno) != NULL)
3238 continue;
3239 ira_assert (ALLOCNO_CLASS (subloop_allocno) == rclass);
3240 ira_assert (bitmap_bit_p (subloop_node->all_allocnos,
3241 ALLOCNO_NUM (subloop_allocno)));
3242 if ((flag_ira_region == IRA_REGION_MIXED)
3243 && (loop_tree_node->reg_pressure[pclass]
3244 <= ira_class_hard_regs_num[pclass]))
3246 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
3248 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3249 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3250 if (hard_regno >= 0)
3251 update_costs_from_copies (subloop_allocno, true, true);
3252 /* We don't need updated costs anymore: */
3253 ira_free_allocno_updated_costs (subloop_allocno);
3255 continue;
3257 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
3258 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
3259 ira_assert (regno < ira_reg_equiv_len);
3260 if (ira_equiv_no_lvalue_p (regno))
3262 if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
3264 ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3265 ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3266 if (hard_regno >= 0)
3267 update_costs_from_copies (subloop_allocno, true, true);
3268 /* We don't need updated costs anymore: */
3269 ira_free_allocno_updated_costs (subloop_allocno);
3272 else if (hard_regno < 0)
3274 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
3275 -= ((ira_memory_move_cost[mode][rclass][1] * enter_freq)
3276 + (ira_memory_move_cost[mode][rclass][0] * exit_freq));
3278 else
3280 aclass = ALLOCNO_CLASS (subloop_allocno);
3281 ira_init_register_move_cost_if_necessary (mode);
3282 cost = (ira_register_move_cost[mode][rclass][rclass]
3283 * (exit_freq + enter_freq));
3284 ira_allocate_and_set_or_copy_costs
3285 (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno), aclass,
3286 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno),
3287 ALLOCNO_HARD_REG_COSTS (subloop_allocno));
3288 ira_allocate_and_set_or_copy_costs
3289 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno),
3290 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno));
3291 ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
3292 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
3293 -= cost;
3294 if (ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
3295 > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index])
3296 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
3297 = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index];
3298 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
3299 += (ira_memory_move_cost[mode][rclass][0] * enter_freq
3300 + ira_memory_move_cost[mode][rclass][1] * exit_freq);
3304 ira_free (allocno_color_data);
3305 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3307 a = ira_allocnos[j];
3308 ALLOCNO_ADD_DATA (a) = NULL;
3312 /* Initialize the common data for coloring and calls functions to do
3313 Chaitin-Briggs and regional coloring. */
3314 static void
3315 do_coloring (void)
3317 coloring_allocno_bitmap = ira_allocate_bitmap ();
3318 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3319 fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
3321 ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
3323 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3324 ira_print_disposition (ira_dump_file);
3326 ira_free_bitmap (coloring_allocno_bitmap);
3331 /* Move spill/restore code, which are to be generated in ira-emit.c,
3332 to less frequent points (if it is profitable) by reassigning some
3333 allocnos (in loop with subloops containing in another loop) to
3334 memory which results in longer live-range where the corresponding
3335 pseudo-registers will be in memory. */
3336 static void
3337 move_spill_restore (void)
3339 int cost, regno, hard_regno, hard_regno2, index;
3340 bool changed_p;
3341 int enter_freq, exit_freq;
3342 enum machine_mode mode;
3343 enum reg_class rclass;
3344 ira_allocno_t a, parent_allocno, subloop_allocno;
3345 ira_loop_tree_node_t parent, loop_node, subloop_node;
3346 ira_allocno_iterator ai;
3348 for (;;)
3350 changed_p = false;
3351 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3352 fprintf (ira_dump_file, "New iteration of spill/restore move\n");
3353 FOR_EACH_ALLOCNO (a, ai)
3355 regno = ALLOCNO_REGNO (a);
3356 loop_node = ALLOCNO_LOOP_TREE_NODE (a);
3357 if (ALLOCNO_CAP_MEMBER (a) != NULL
3358 || ALLOCNO_CAP (a) != NULL
3359 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
3360 || loop_node->children == NULL
3361 /* don't do the optimization because it can create
3362 copies and the reload pass can spill the allocno set
3363 by copy although the allocno will not get memory
3364 slot. */
3365 || ira_equiv_no_lvalue_p (regno)
3366 || !bitmap_bit_p (loop_node->border_allocnos, ALLOCNO_NUM (a)))
3367 continue;
3368 mode = ALLOCNO_MODE (a);
3369 rclass = ALLOCNO_CLASS (a);
3370 index = ira_class_hard_reg_index[rclass][hard_regno];
3371 ira_assert (index >= 0);
3372 cost = (ALLOCNO_MEMORY_COST (a)
3373 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
3374 ? ALLOCNO_CLASS_COST (a)
3375 : ALLOCNO_HARD_REG_COSTS (a)[index]));
3376 ira_init_register_move_cost_if_necessary (mode);
3377 for (subloop_node = loop_node->subloops;
3378 subloop_node != NULL;
3379 subloop_node = subloop_node->subloop_next)
3381 ira_assert (subloop_node->bb == NULL);
3382 subloop_allocno = subloop_node->regno_allocno_map[regno];
3383 if (subloop_allocno == NULL)
3384 continue;
3385 ira_assert (rclass == ALLOCNO_CLASS (subloop_allocno));
3386 /* We have accumulated cost. To get the real cost of
3387 allocno usage in the loop we should subtract costs of
3388 the subloop allocnos. */
3389 cost -= (ALLOCNO_MEMORY_COST (subloop_allocno)
3390 - (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
3391 ? ALLOCNO_CLASS_COST (subloop_allocno)
3392 : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]));
3393 exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
3394 enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
3395 if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
3396 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3397 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3398 else
3400 cost
3401 += (ira_memory_move_cost[mode][rclass][0] * exit_freq
3402 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3403 if (hard_regno2 != hard_regno)
3404 cost -= (ira_register_move_cost[mode][rclass][rclass]
3405 * (exit_freq + enter_freq));
3408 if ((parent = loop_node->parent) != NULL
3409 && (parent_allocno = parent->regno_allocno_map[regno]) != NULL)
3411 ira_assert (rclass == ALLOCNO_CLASS (parent_allocno));
3412 exit_freq = ira_loop_edge_freq (loop_node, regno, true);
3413 enter_freq = ira_loop_edge_freq (loop_node, regno, false);
3414 if ((hard_regno2 = ALLOCNO_HARD_REGNO (parent_allocno)) < 0)
3415 cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3416 + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3417 else
3419 cost
3420 += (ira_memory_move_cost[mode][rclass][1] * exit_freq
3421 + ira_memory_move_cost[mode][rclass][0] * enter_freq);
3422 if (hard_regno2 != hard_regno)
3423 cost -= (ira_register_move_cost[mode][rclass][rclass]
3424 * (exit_freq + enter_freq));
3427 if (cost < 0)
3429 ALLOCNO_HARD_REGNO (a) = -1;
3430 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3432 fprintf
3433 (ira_dump_file,
3434 " Moving spill/restore for a%dr%d up from loop %d",
3435 ALLOCNO_NUM (a), regno, loop_node->loop_num);
3436 fprintf (ira_dump_file, " - profit %d\n", -cost);
3438 changed_p = true;
3441 if (! changed_p)
3442 break;
3448 /* Update current hard reg costs and current conflict hard reg costs
3449 for allocno A. It is done by processing its copies containing
3450 other allocnos already assigned. */
3451 static void
3452 update_curr_costs (ira_allocno_t a)
3454 int i, hard_regno, cost;
3455 enum machine_mode mode;
3456 enum reg_class aclass, rclass;
3457 ira_allocno_t another_a;
3458 ira_copy_t cp, next_cp;
3460 ira_free_allocno_updated_costs (a);
3461 ira_assert (! ALLOCNO_ASSIGNED_P (a));
3462 aclass = ALLOCNO_CLASS (a);
3463 if (aclass == NO_REGS)
3464 return;
3465 mode = ALLOCNO_MODE (a);
3466 ira_init_register_move_cost_if_necessary (mode);
3467 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3469 if (cp->first == a)
3471 next_cp = cp->next_first_allocno_copy;
3472 another_a = cp->second;
3474 else if (cp->second == a)
3476 next_cp = cp->next_second_allocno_copy;
3477 another_a = cp->first;
3479 else
3480 gcc_unreachable ();
3481 if (! ira_reg_classes_intersect_p[aclass][ALLOCNO_CLASS (another_a)]
3482 || ! ALLOCNO_ASSIGNED_P (another_a)
3483 || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
3484 continue;
3485 rclass = REGNO_REG_CLASS (hard_regno);
3486 i = ira_class_hard_reg_index[aclass][hard_regno];
3487 if (i < 0)
3488 continue;
3489 cost = (cp->first == a
3490 ? ira_register_move_cost[mode][rclass][aclass]
3491 : ira_register_move_cost[mode][aclass][rclass]);
3492 ira_allocate_and_set_or_copy_costs
3493 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a),
3494 ALLOCNO_HARD_REG_COSTS (a));
3495 ira_allocate_and_set_or_copy_costs
3496 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
3497 aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
3498 ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3499 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3503 /* Try to assign hard registers to the unassigned allocnos and
3504 allocnos conflicting with them or conflicting with allocnos whose
3505 regno >= START_REGNO. The function is called after ira_flattening,
3506 so more allocnos (including ones created in ira-emit.c) will have a
3507 chance to get a hard register. We use simple assignment algorithm
3508 based on priorities. */
3509 void
3510 ira_reassign_conflict_allocnos (int start_regno)
3512 int i, allocnos_to_color_num;
3513 ira_allocno_t a;
3514 enum reg_class aclass;
3515 bitmap allocnos_to_color;
3516 ira_allocno_iterator ai;
3518 allocnos_to_color = ira_allocate_bitmap ();
3519 allocnos_to_color_num = 0;
3520 FOR_EACH_ALLOCNO (a, ai)
3522 int n = ALLOCNO_NUM_OBJECTS (a);
3524 if (! ALLOCNO_ASSIGNED_P (a)
3525 && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
3527 if (ALLOCNO_CLASS (a) != NO_REGS)
3528 sorted_allocnos[allocnos_to_color_num++] = a;
3529 else
3531 ALLOCNO_ASSIGNED_P (a) = true;
3532 ALLOCNO_HARD_REGNO (a) = -1;
3533 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3534 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3536 bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
3538 if (ALLOCNO_REGNO (a) < start_regno
3539 || (aclass = ALLOCNO_CLASS (a)) == NO_REGS)
3540 continue;
3541 for (i = 0; i < n; i++)
3543 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3544 ira_object_t conflict_obj;
3545 ira_object_conflict_iterator oci;
3547 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3549 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3551 ira_assert (ira_reg_classes_intersect_p
3552 [aclass][ALLOCNO_CLASS (conflict_a)]);
3553 if (!bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
3554 continue;
3555 sorted_allocnos[allocnos_to_color_num++] = conflict_a;
3559 ira_free_bitmap (allocnos_to_color);
3560 if (allocnos_to_color_num > 1)
3562 setup_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
3563 qsort (sorted_allocnos, allocnos_to_color_num, sizeof (ira_allocno_t),
3564 allocno_priority_compare_func);
3566 for (i = 0; i < allocnos_to_color_num; i++)
3568 a = sorted_allocnos[i];
3569 ALLOCNO_ASSIGNED_P (a) = false;
3570 update_curr_costs (a);
3572 for (i = 0; i < allocnos_to_color_num; i++)
3574 a = sorted_allocnos[i];
3575 if (assign_hard_reg (a, true))
3577 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3578 fprintf
3579 (ira_dump_file,
3580 " Secondary allocation: assign hard reg %d to reg %d\n",
3581 ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
3588 /* This page contains functions used to find conflicts using allocno
3589 live ranges. */
3591 #ifdef ENABLE_IRA_CHECKING
3593 /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
3594 intersect. This should be used when there is only one region.
3595 Currently this is used during reload. */
3596 static bool
3597 conflict_by_live_ranges_p (int regno1, int regno2)
3599 ira_allocno_t a1, a2;
3601 ira_assert (regno1 >= FIRST_PSEUDO_REGISTER
3602 && regno2 >= FIRST_PSEUDO_REGISTER);
3603 /* Reg info caclulated by dataflow infrastructure can be different
3604 from one calculated by regclass. */
3605 if ((a1 = ira_loop_tree_root->regno_allocno_map[regno1]) == NULL
3606 || (a2 = ira_loop_tree_root->regno_allocno_map[regno2]) == NULL)
3607 return false;
3608 return allocnos_conflict_by_live_ranges_p (a1, a2);
3611 #endif
3615 /* This page contains code to coalesce memory stack slots used by
3616 spilled allocnos. This results in smaller stack frame, better data
3617 locality, and in smaller code for some architectures like
3618 x86/x86_64 where insn size depends on address displacement value.
3619 On the other hand, it can worsen insn scheduling after the RA but
3620 in practice it is less important than smaller stack frames. */
3622 /* TRUE if we coalesced some allocnos. In other words, if we got
3623 loops formed by members first_coalesced_allocno and
3624 next_coalesced_allocno containing more one allocno. */
3625 static bool allocno_coalesced_p;
3627 /* Bitmap used to prevent a repeated allocno processing because of
3628 coalescing. */
3629 static bitmap processed_coalesced_allocno_bitmap;
3631 /* See below. */
3632 typedef struct coalesce_data *coalesce_data_t;
3634 /* To decrease footprint of ira_allocno structure we store all data
3635 needed only for coalescing in the following structure. */
3636 struct coalesce_data
3638 /* Coalesced allocnos form a cyclic list. One allocno given by
3639 FIRST represents all coalesced allocnos. The
3640 list is chained by NEXT. */
3641 ira_allocno_t first;
3642 ira_allocno_t next;
3643 int temp;
3646 /* Container for storing allocno data concerning coalescing. */
3647 static coalesce_data_t allocno_coalesce_data;
3649 /* Macro to access the data concerning coalescing. */
3650 #define ALLOCNO_COALESCE_DATA(a) ((coalesce_data_t) ALLOCNO_ADD_DATA (a))
3652 /* Merge two sets of coalesced allocnos given correspondingly by
3653 allocnos A1 and A2 (more accurately merging A2 set into A1
3654 set). */
3655 static void
3656 merge_allocnos (ira_allocno_t a1, ira_allocno_t a2)
3658 ira_allocno_t a, first, last, next;
3660 first = ALLOCNO_COALESCE_DATA (a1)->first;
3661 a = ALLOCNO_COALESCE_DATA (a2)->first;
3662 if (first == a)
3663 return;
3664 for (last = a2, a = ALLOCNO_COALESCE_DATA (a2)->next;;
3665 a = ALLOCNO_COALESCE_DATA (a)->next)
3667 ALLOCNO_COALESCE_DATA (a)->first = first;
3668 if (a == a2)
3669 break;
3670 last = a;
3672 next = allocno_coalesce_data[ALLOCNO_NUM (first)].next;
3673 allocno_coalesce_data[ALLOCNO_NUM (first)].next = a2;
3674 allocno_coalesce_data[ALLOCNO_NUM (last)].next = next;
3677 /* Return TRUE if there are conflicting allocnos from two sets of
3678 coalesced allocnos given correspondingly by allocnos A1 and A2. We
3679 use live ranges to find conflicts because conflicts are represented
3680 only for allocnos of the same allocno class and during the reload
3681 pass we coalesce allocnos for sharing stack memory slots. */
3682 static bool
3683 coalesced_allocno_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
3685 ira_allocno_t a, conflict_a;
3687 if (allocno_coalesced_p)
3689 bitmap_clear (processed_coalesced_allocno_bitmap);
3690 for (a = ALLOCNO_COALESCE_DATA (a1)->next;;
3691 a = ALLOCNO_COALESCE_DATA (a)->next)
3693 bitmap_set_bit (processed_coalesced_allocno_bitmap, ALLOCNO_NUM (a));
3694 if (a == a1)
3695 break;
3698 for (a = ALLOCNO_COALESCE_DATA (a2)->next;;
3699 a = ALLOCNO_COALESCE_DATA (a)->next)
3701 for (conflict_a = ALLOCNO_COALESCE_DATA (a1)->next;;
3702 conflict_a = ALLOCNO_COALESCE_DATA (conflict_a)->next)
3704 if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
3705 return true;
3706 if (conflict_a == a1)
3707 break;
3709 if (a == a2)
3710 break;
3712 return false;
3715 /* The major function for aggressive allocno coalescing. We coalesce
3716 only spilled allocnos. If some allocnos have been coalesced, we
3717 set up flag allocno_coalesced_p. */
3718 static void
3719 coalesce_allocnos (void)
3721 ira_allocno_t a;
3722 ira_copy_t cp, next_cp;
3723 unsigned int j;
3724 int i, n, cp_num, regno;
3725 bitmap_iterator bi;
3727 cp_num = 0;
3728 /* Collect copies. */
3729 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
3731 a = ira_allocnos[j];
3732 regno = ALLOCNO_REGNO (a);
3733 if (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
3734 || ira_equiv_no_lvalue_p (regno))
3735 continue;
3736 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3738 if (cp->first == a)
3740 next_cp = cp->next_first_allocno_copy;
3741 regno = ALLOCNO_REGNO (cp->second);
3742 /* For priority coloring we coalesce allocnos only with
3743 the same allocno class not with intersected allocno
3744 classes as it were possible. It is done for
3745 simplicity. */
3746 if ((cp->insn != NULL || cp->constraint_p)
3747 && ALLOCNO_ASSIGNED_P (cp->second)
3748 && ALLOCNO_HARD_REGNO (cp->second) < 0
3749 && ! ira_equiv_no_lvalue_p (regno))
3750 sorted_copies[cp_num++] = cp;
3752 else if (cp->second == a)
3753 next_cp = cp->next_second_allocno_copy;
3754 else
3755 gcc_unreachable ();
3758 qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
3759 /* Coalesced copies, most frequently executed first. */
3760 for (; cp_num != 0;)
3762 for (i = 0; i < cp_num; i++)
3764 cp = sorted_copies[i];
3765 if (! coalesced_allocno_conflict_p (cp->first, cp->second))
3767 allocno_coalesced_p = true;
3768 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3769 fprintf
3770 (ira_dump_file,
3771 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
3772 cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
3773 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
3774 cp->freq);
3775 merge_allocnos (cp->first, cp->second);
3776 i++;
3777 break;
3780 /* Collect the rest of copies. */
3781 for (n = 0; i < cp_num; i++)
3783 cp = sorted_copies[i];
3784 if (allocno_coalesce_data[ALLOCNO_NUM (cp->first)].first
3785 != allocno_coalesce_data[ALLOCNO_NUM (cp->second)].first)
3786 sorted_copies[n++] = cp;
3788 cp_num = n;
3792 /* Usage cost and order number of coalesced allocno set to which
3793 given pseudo register belongs to. */
3794 static int *regno_coalesced_allocno_cost;
3795 static int *regno_coalesced_allocno_num;
3797 /* Sort pseudos according frequencies of coalesced allocno sets they
3798 belong to (putting most frequently ones first), and according to
3799 coalesced allocno set order numbers. */
3800 static int
3801 coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
3803 const int regno1 = *(const int *) v1p;
3804 const int regno2 = *(const int *) v2p;
3805 int diff;
3807 if ((diff = (regno_coalesced_allocno_cost[regno2]
3808 - regno_coalesced_allocno_cost[regno1])) != 0)
3809 return diff;
3810 if ((diff = (regno_coalesced_allocno_num[regno1]
3811 - regno_coalesced_allocno_num[regno2])) != 0)
3812 return diff;
3813 return regno1 - regno2;
3816 /* Widest width in which each pseudo reg is referred to (via subreg).
3817 It is used for sorting pseudo registers. */
3818 static unsigned int *regno_max_ref_width;
3820 /* Redefine STACK_GROWS_DOWNWARD in terms of 0 or 1. */
3821 #ifdef STACK_GROWS_DOWNWARD
3822 # undef STACK_GROWS_DOWNWARD
3823 # define STACK_GROWS_DOWNWARD 1
3824 #else
3825 # define STACK_GROWS_DOWNWARD 0
3826 #endif
3828 /* Sort pseudos according their slot numbers (putting ones with
3829 smaller numbers first, or last when the frame pointer is not
3830 needed). */
3831 static int
3832 coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
3834 const int regno1 = *(const int *) v1p;
3835 const int regno2 = *(const int *) v2p;
3836 ira_allocno_t a1 = ira_regno_allocno_map[regno1];
3837 ira_allocno_t a2 = ira_regno_allocno_map[regno2];
3838 int diff, slot_num1, slot_num2;
3839 int total_size1, total_size2;
3841 if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
3843 if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
3844 return regno1 - regno2;
3845 return 1;
3847 else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
3848 return -1;
3849 slot_num1 = -ALLOCNO_HARD_REGNO (a1);
3850 slot_num2 = -ALLOCNO_HARD_REGNO (a2);
3851 if ((diff = slot_num1 - slot_num2) != 0)
3852 return (frame_pointer_needed
3853 || !FRAME_GROWS_DOWNWARD == STACK_GROWS_DOWNWARD ? diff : -diff);
3854 total_size1 = MAX (PSEUDO_REGNO_BYTES (regno1),
3855 regno_max_ref_width[regno1]);
3856 total_size2 = MAX (PSEUDO_REGNO_BYTES (regno2),
3857 regno_max_ref_width[regno2]);
3858 if ((diff = total_size2 - total_size1) != 0)
3859 return diff;
3860 return regno1 - regno2;
3863 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
3864 for coalesced allocno sets containing allocnos with their regnos
3865 given in array PSEUDO_REGNOS of length N. */
3866 static void
3867 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
3869 int i, num, regno, cost;
3870 ira_allocno_t allocno, a;
3872 for (num = i = 0; i < n; i++)
3874 regno = pseudo_regnos[i];
3875 allocno = ira_regno_allocno_map[regno];
3876 if (allocno == NULL)
3878 regno_coalesced_allocno_cost[regno] = 0;
3879 regno_coalesced_allocno_num[regno] = ++num;
3880 continue;
3882 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
3883 continue;
3884 num++;
3885 for (cost = 0, a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3886 a = ALLOCNO_COALESCE_DATA (a)->next)
3888 cost += ALLOCNO_FREQ (a);
3889 if (a == allocno)
3890 break;
3892 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3893 a = ALLOCNO_COALESCE_DATA (a)->next)
3895 regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
3896 regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
3897 if (a == allocno)
3898 break;
3903 /* Collect spilled allocnos representing coalesced allocno sets (the
3904 first coalesced allocno). The collected allocnos are returned
3905 through array SPILLED_COALESCED_ALLOCNOS. The function returns the
3906 number of the collected allocnos. The allocnos are given by their
3907 regnos in array PSEUDO_REGNOS of length N. */
3908 static int
3909 collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
3910 ira_allocno_t *spilled_coalesced_allocnos)
3912 int i, num, regno;
3913 ira_allocno_t allocno;
3915 for (num = i = 0; i < n; i++)
3917 regno = pseudo_regnos[i];
3918 allocno = ira_regno_allocno_map[regno];
3919 if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
3920 || ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
3921 continue;
3922 spilled_coalesced_allocnos[num++] = allocno;
3924 return num;
3927 /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
3928 given slot contains live ranges of coalesced allocnos assigned to
3929 given slot. */
3930 static live_range_t *slot_coalesced_allocnos_live_ranges;
3932 /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
3933 ranges intersected with live ranges of coalesced allocnos assigned
3934 to slot with number N. */
3935 static bool
3936 slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno, int n)
3938 ira_allocno_t a;
3940 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3941 a = ALLOCNO_COALESCE_DATA (a)->next)
3943 int i;
3944 int nr = ALLOCNO_NUM_OBJECTS (a);
3946 for (i = 0; i < nr; i++)
3948 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3950 if (ira_live_ranges_intersect_p
3951 (slot_coalesced_allocnos_live_ranges[n],
3952 OBJECT_LIVE_RANGES (obj)))
3953 return true;
3955 if (a == allocno)
3956 break;
3958 return false;
3961 /* Update live ranges of slot to which coalesced allocnos represented
3962 by ALLOCNO were assigned. */
3963 static void
3964 setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno)
3966 int i, n;
3967 ira_allocno_t a;
3968 live_range_t r;
3970 n = ALLOCNO_COALESCE_DATA (allocno)->temp;
3971 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
3972 a = ALLOCNO_COALESCE_DATA (a)->next)
3974 int nr = ALLOCNO_NUM_OBJECTS (a);
3975 for (i = 0; i < nr; i++)
3977 ira_object_t obj = ALLOCNO_OBJECT (a, i);
3979 r = ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj));
3980 slot_coalesced_allocnos_live_ranges[n]
3981 = ira_merge_live_ranges
3982 (slot_coalesced_allocnos_live_ranges[n], r);
3984 if (a == allocno)
3985 break;
3989 /* We have coalesced allocnos involving in copies. Coalesce allocnos
3990 further in order to share the same memory stack slot. Allocnos
3991 representing sets of allocnos coalesced before the call are given
3992 in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
3993 some allocnos were coalesced in the function. */
3994 static bool
3995 coalesce_spill_slots (ira_allocno_t *spilled_coalesced_allocnos, int num)
3997 int i, j, n, last_coalesced_allocno_num;
3998 ira_allocno_t allocno, a;
3999 bool merged_p = false;
4000 bitmap set_jump_crosses = regstat_get_setjmp_crosses ();
4002 slot_coalesced_allocnos_live_ranges
4003 = (live_range_t *) ira_allocate (sizeof (live_range_t) * ira_allocnos_num);
4004 memset (slot_coalesced_allocnos_live_ranges, 0,
4005 sizeof (live_range_t) * ira_allocnos_num);
4006 last_coalesced_allocno_num = 0;
4007 /* Coalesce non-conflicting spilled allocnos preferring most
4008 frequently used. */
4009 for (i = 0; i < num; i++)
4011 allocno = spilled_coalesced_allocnos[i];
4012 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
4013 || bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (allocno))
4014 || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
4015 continue;
4016 for (j = 0; j < i; j++)
4018 a = spilled_coalesced_allocnos[j];
4019 n = ALLOCNO_COALESCE_DATA (a)->temp;
4020 if (ALLOCNO_COALESCE_DATA (a)->first == a
4021 && ! bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (a))
4022 && ! ira_equiv_no_lvalue_p (ALLOCNO_REGNO (a))
4023 && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno, n))
4024 break;
4026 if (j >= i)
4028 /* No coalescing: set up number for coalesced allocnos
4029 represented by ALLOCNO. */
4030 ALLOCNO_COALESCE_DATA (allocno)->temp = last_coalesced_allocno_num++;
4031 setup_slot_coalesced_allocno_live_ranges (allocno);
4033 else
4035 allocno_coalesced_p = true;
4036 merged_p = true;
4037 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4038 fprintf (ira_dump_file,
4039 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
4040 ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
4041 ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
4042 ALLOCNO_COALESCE_DATA (allocno)->temp
4043 = ALLOCNO_COALESCE_DATA (a)->temp;
4044 setup_slot_coalesced_allocno_live_ranges (allocno);
4045 merge_allocnos (a, allocno);
4046 ira_assert (ALLOCNO_COALESCE_DATA (a)->first == a);
4049 for (i = 0; i < ira_allocnos_num; i++)
4050 ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges[i]);
4051 ira_free (slot_coalesced_allocnos_live_ranges);
4052 return merged_p;
4055 /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
4056 subsequent assigning stack slots to them in the reload pass. To do
4057 this we coalesce spilled allocnos first to decrease the number of
4058 memory-memory move insns. This function is called by the
4059 reload. */
4060 void
4061 ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
4062 unsigned int *reg_max_ref_width)
4064 int max_regno = max_reg_num ();
4065 int i, regno, num, slot_num;
4066 ira_allocno_t allocno, a;
4067 ira_allocno_iterator ai;
4068 ira_allocno_t *spilled_coalesced_allocnos;
4070 /* Set up allocnos can be coalesced. */
4071 coloring_allocno_bitmap = ira_allocate_bitmap ();
4072 for (i = 0; i < n; i++)
4074 regno = pseudo_regnos[i];
4075 allocno = ira_regno_allocno_map[regno];
4076 if (allocno != NULL)
4077 bitmap_set_bit (coloring_allocno_bitmap, ALLOCNO_NUM (allocno));
4079 allocno_coalesced_p = false;
4080 processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
4081 allocno_coalesce_data
4082 = (coalesce_data_t) ira_allocate (sizeof (struct coalesce_data)
4083 * ira_allocnos_num);
4084 /* Initialize coalesce data for allocnos. */
4085 FOR_EACH_ALLOCNO (a, ai)
4087 ALLOCNO_ADD_DATA (a) = allocno_coalesce_data + ALLOCNO_NUM (a);
4088 ALLOCNO_COALESCE_DATA (a)->first = a;
4089 ALLOCNO_COALESCE_DATA (a)->next = a;
4091 coalesce_allocnos ();
4092 ira_free_bitmap (coloring_allocno_bitmap);
4093 regno_coalesced_allocno_cost
4094 = (int *) ira_allocate (max_regno * sizeof (int));
4095 regno_coalesced_allocno_num
4096 = (int *) ira_allocate (max_regno * sizeof (int));
4097 memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
4098 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
4099 /* Sort regnos according frequencies of the corresponding coalesced
4100 allocno sets. */
4101 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
4102 spilled_coalesced_allocnos
4103 = (ira_allocno_t *) ira_allocate (ira_allocnos_num
4104 * sizeof (ira_allocno_t));
4105 /* Collect allocnos representing the spilled coalesced allocno
4106 sets. */
4107 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
4108 spilled_coalesced_allocnos);
4109 if (flag_ira_share_spill_slots
4110 && coalesce_spill_slots (spilled_coalesced_allocnos, num))
4112 setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
4113 qsort (pseudo_regnos, n, sizeof (int),
4114 coalesced_pseudo_reg_freq_compare);
4115 num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
4116 spilled_coalesced_allocnos);
4118 ira_free_bitmap (processed_coalesced_allocno_bitmap);
4119 allocno_coalesced_p = false;
4120 /* Assign stack slot numbers to spilled allocno sets, use smaller
4121 numbers for most frequently used coalesced allocnos. -1 is
4122 reserved for dynamic search of stack slots for pseudos spilled by
4123 the reload. */
4124 slot_num = 1;
4125 for (i = 0; i < num; i++)
4127 allocno = spilled_coalesced_allocnos[i];
4128 if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
4129 || ALLOCNO_HARD_REGNO (allocno) >= 0
4130 || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
4131 continue;
4132 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4133 fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
4134 slot_num++;
4135 for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4136 a = ALLOCNO_COALESCE_DATA (a)->next)
4138 ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
4139 ALLOCNO_HARD_REGNO (a) = -slot_num;
4140 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4141 fprintf (ira_dump_file, " a%dr%d(%d,%d)",
4142 ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a),
4143 MAX (PSEUDO_REGNO_BYTES (ALLOCNO_REGNO (a)),
4144 reg_max_ref_width[ALLOCNO_REGNO (a)]));
4146 if (a == allocno)
4147 break;
4149 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4150 fprintf (ira_dump_file, "\n");
4152 ira_spilled_reg_stack_slots_num = slot_num - 1;
4153 ira_free (spilled_coalesced_allocnos);
4154 /* Sort regnos according the slot numbers. */
4155 regno_max_ref_width = reg_max_ref_width;
4156 qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
4157 FOR_EACH_ALLOCNO (a, ai)
4158 ALLOCNO_ADD_DATA (a) = NULL;
4159 ira_free (allocno_coalesce_data);
4160 ira_free (regno_coalesced_allocno_num);
4161 ira_free (regno_coalesced_allocno_cost);
4166 /* This page contains code used by the reload pass to improve the
4167 final code. */
4169 /* The function is called from reload to mark changes in the
4170 allocation of REGNO made by the reload. Remember that reg_renumber
4171 reflects the change result. */
4172 void
4173 ira_mark_allocation_change (int regno)
4175 ira_allocno_t a = ira_regno_allocno_map[regno];
4176 int old_hard_regno, hard_regno, cost;
4177 enum reg_class aclass = ALLOCNO_CLASS (a);
4179 ira_assert (a != NULL);
4180 hard_regno = reg_renumber[regno];
4181 if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
4182 return;
4183 if (old_hard_regno < 0)
4184 cost = -ALLOCNO_MEMORY_COST (a);
4185 else
4187 ira_assert (ira_class_hard_reg_index[aclass][old_hard_regno] >= 0);
4188 cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
4189 ? ALLOCNO_CLASS_COST (a)
4190 : ALLOCNO_HARD_REG_COSTS (a)
4191 [ira_class_hard_reg_index[aclass][old_hard_regno]]);
4192 update_costs_from_copies (a, false, false);
4194 ira_overall_cost -= cost;
4195 ALLOCNO_HARD_REGNO (a) = hard_regno;
4196 if (hard_regno < 0)
4198 ALLOCNO_HARD_REGNO (a) = -1;
4199 cost += ALLOCNO_MEMORY_COST (a);
4201 else if (ira_class_hard_reg_index[aclass][hard_regno] >= 0)
4203 cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
4204 ? ALLOCNO_CLASS_COST (a)
4205 : ALLOCNO_HARD_REG_COSTS (a)
4206 [ira_class_hard_reg_index[aclass][hard_regno]]);
4207 update_costs_from_copies (a, true, false);
4209 else
4210 /* Reload changed class of the allocno. */
4211 cost = 0;
4212 ira_overall_cost += cost;
4215 /* This function is called when reload deletes memory-memory move. In
4216 this case we marks that the allocation of the corresponding
4217 allocnos should be not changed in future. Otherwise we risk to get
4218 a wrong code. */
4219 void
4220 ira_mark_memory_move_deletion (int dst_regno, int src_regno)
4222 ira_allocno_t dst = ira_regno_allocno_map[dst_regno];
4223 ira_allocno_t src = ira_regno_allocno_map[src_regno];
4225 ira_assert (dst != NULL && src != NULL
4226 && ALLOCNO_HARD_REGNO (dst) < 0
4227 && ALLOCNO_HARD_REGNO (src) < 0);
4228 ALLOCNO_DONT_REASSIGN_P (dst) = true;
4229 ALLOCNO_DONT_REASSIGN_P (src) = true;
4232 /* Try to assign a hard register (except for FORBIDDEN_REGS) to
4233 allocno A and return TRUE in the case of success. */
4234 static bool
4235 allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
4237 int hard_regno;
4238 enum reg_class aclass;
4239 int regno = ALLOCNO_REGNO (a);
4240 HARD_REG_SET saved[2];
4241 int i, n;
4243 n = ALLOCNO_NUM_OBJECTS (a);
4244 for (i = 0; i < n; i++)
4246 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4247 COPY_HARD_REG_SET (saved[i], OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
4248 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), forbidden_regs);
4249 if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
4250 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
4251 call_used_reg_set);
4253 ALLOCNO_ASSIGNED_P (a) = false;
4254 aclass = ALLOCNO_CLASS (a);
4255 update_curr_costs (a);
4256 assign_hard_reg (a, true);
4257 hard_regno = ALLOCNO_HARD_REGNO (a);
4258 reg_renumber[regno] = hard_regno;
4259 if (hard_regno < 0)
4260 ALLOCNO_HARD_REGNO (a) = -1;
4261 else
4263 ira_assert (ira_class_hard_reg_index[aclass][hard_regno] >= 0);
4264 ira_overall_cost
4265 -= (ALLOCNO_MEMORY_COST (a)
4266 - (ALLOCNO_HARD_REG_COSTS (a) == NULL
4267 ? ALLOCNO_CLASS_COST (a)
4268 : ALLOCNO_HARD_REG_COSTS (a)[ira_class_hard_reg_index
4269 [aclass][hard_regno]]));
4270 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
4271 && ira_hard_reg_set_intersection_p (hard_regno, ALLOCNO_MODE (a),
4272 call_used_reg_set))
4274 ira_assert (flag_caller_saves);
4275 caller_save_needed = 1;
4279 /* If we found a hard register, modify the RTL for the pseudo
4280 register to show the hard register, and mark the pseudo register
4281 live. */
4282 if (reg_renumber[regno] >= 0)
4284 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4285 fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
4286 SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
4287 mark_home_live (regno);
4289 else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4290 fprintf (ira_dump_file, "\n");
4291 for (i = 0; i < n; i++)
4293 ira_object_t obj = ALLOCNO_OBJECT (a, i);
4294 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), saved[i]);
4296 return reg_renumber[regno] >= 0;
4299 /* Sort pseudos according their usage frequencies (putting most
4300 frequently ones first). */
4301 static int
4302 pseudo_reg_compare (const void *v1p, const void *v2p)
4304 int regno1 = *(const int *) v1p;
4305 int regno2 = *(const int *) v2p;
4306 int diff;
4308 if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
4309 return diff;
4310 return regno1 - regno2;
4313 /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
4314 NUM of them) or spilled pseudos conflicting with pseudos in
4315 SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
4316 allocation has been changed. The function doesn't use
4317 BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
4318 PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
4319 is called by the reload pass at the end of each reload
4320 iteration. */
4321 bool
4322 ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
4323 HARD_REG_SET bad_spill_regs,
4324 HARD_REG_SET *pseudo_forbidden_regs,
4325 HARD_REG_SET *pseudo_previous_regs,
4326 bitmap spilled)
4328 int i, n, regno;
4329 bool changed_p;
4330 ira_allocno_t a;
4331 HARD_REG_SET forbidden_regs;
4332 bitmap temp = BITMAP_ALLOC (NULL);
4334 /* Add pseudos which conflict with pseudos already in
4335 SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
4336 to allocating in two steps as some of the conflicts might have
4337 a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
4338 for (i = 0; i < num; i++)
4339 bitmap_set_bit (temp, spilled_pseudo_regs[i]);
4341 for (i = 0, n = num; i < n; i++)
4343 int nr, j;
4344 int regno = spilled_pseudo_regs[i];
4345 bitmap_set_bit (temp, regno);
4347 a = ira_regno_allocno_map[regno];
4348 nr = ALLOCNO_NUM_OBJECTS (a);
4349 for (j = 0; j < nr; j++)
4351 ira_object_t conflict_obj;
4352 ira_object_t obj = ALLOCNO_OBJECT (a, j);
4353 ira_object_conflict_iterator oci;
4355 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
4357 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
4358 if (ALLOCNO_HARD_REGNO (conflict_a) < 0
4359 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
4360 && bitmap_set_bit (temp, ALLOCNO_REGNO (conflict_a)))
4362 spilled_pseudo_regs[num++] = ALLOCNO_REGNO (conflict_a);
4363 /* ?!? This seems wrong. */
4364 bitmap_set_bit (consideration_allocno_bitmap,
4365 ALLOCNO_NUM (conflict_a));
4371 if (num > 1)
4372 qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
4373 changed_p = false;
4374 /* Try to assign hard registers to pseudos from
4375 SPILLED_PSEUDO_REGS. */
4376 for (i = 0; i < num; i++)
4378 regno = spilled_pseudo_regs[i];
4379 COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
4380 IOR_HARD_REG_SET (forbidden_regs, pseudo_forbidden_regs[regno]);
4381 IOR_HARD_REG_SET (forbidden_regs, pseudo_previous_regs[regno]);
4382 gcc_assert (reg_renumber[regno] < 0);
4383 a = ira_regno_allocno_map[regno];
4384 ira_mark_allocation_change (regno);
4385 ira_assert (reg_renumber[regno] < 0);
4386 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4387 fprintf (ira_dump_file,
4388 " Try Assign %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
4389 ALLOCNO_MEMORY_COST (a)
4390 - ALLOCNO_CLASS_COST (a));
4391 allocno_reload_assign (a, forbidden_regs);
4392 if (reg_renumber[regno] >= 0)
4394 CLEAR_REGNO_REG_SET (spilled, regno);
4395 changed_p = true;
4398 BITMAP_FREE (temp);
4399 return changed_p;
4402 /* The function is called by reload and returns already allocated
4403 stack slot (if any) for REGNO with given INHERENT_SIZE and
4404 TOTAL_SIZE. In the case of failure to find a slot which can be
4405 used for REGNO, the function returns NULL. */
4407 ira_reuse_stack_slot (int regno, unsigned int inherent_size,
4408 unsigned int total_size)
4410 unsigned int i;
4411 int slot_num, best_slot_num;
4412 int cost, best_cost;
4413 ira_copy_t cp, next_cp;
4414 ira_allocno_t another_allocno, allocno = ira_regno_allocno_map[regno];
4415 rtx x;
4416 bitmap_iterator bi;
4417 struct ira_spilled_reg_stack_slot *slot = NULL;
4419 ira_assert (inherent_size == PSEUDO_REGNO_BYTES (regno)
4420 && inherent_size <= total_size
4421 && ALLOCNO_HARD_REGNO (allocno) < 0);
4422 if (! flag_ira_share_spill_slots)
4423 return NULL_RTX;
4424 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4425 if (slot_num != -1)
4427 slot = &ira_spilled_reg_stack_slots[slot_num];
4428 x = slot->mem;
4430 else
4432 best_cost = best_slot_num = -1;
4433 x = NULL_RTX;
4434 /* It means that the pseudo was spilled in the reload pass, try
4435 to reuse a slot. */
4436 for (slot_num = 0;
4437 slot_num < ira_spilled_reg_stack_slots_num;
4438 slot_num++)
4440 slot = &ira_spilled_reg_stack_slots[slot_num];
4441 if (slot->mem == NULL_RTX)
4442 continue;
4443 if (slot->width < total_size
4444 || GET_MODE_SIZE (GET_MODE (slot->mem)) < inherent_size)
4445 continue;
4447 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4448 FIRST_PSEUDO_REGISTER, i, bi)
4450 another_allocno = ira_regno_allocno_map[i];
4451 if (allocnos_conflict_by_live_ranges_p (allocno,
4452 another_allocno))
4453 goto cont;
4455 for (cost = 0, cp = ALLOCNO_COPIES (allocno);
4456 cp != NULL;
4457 cp = next_cp)
4459 if (cp->first == allocno)
4461 next_cp = cp->next_first_allocno_copy;
4462 another_allocno = cp->second;
4464 else if (cp->second == allocno)
4466 next_cp = cp->next_second_allocno_copy;
4467 another_allocno = cp->first;
4469 else
4470 gcc_unreachable ();
4471 if (cp->insn == NULL_RTX)
4472 continue;
4473 if (bitmap_bit_p (&slot->spilled_regs,
4474 ALLOCNO_REGNO (another_allocno)))
4475 cost += cp->freq;
4477 if (cost > best_cost)
4479 best_cost = cost;
4480 best_slot_num = slot_num;
4482 cont:
4485 if (best_cost >= 0)
4487 slot_num = best_slot_num;
4488 slot = &ira_spilled_reg_stack_slots[slot_num];
4489 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4490 x = slot->mem;
4491 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4494 if (x != NULL_RTX)
4496 ira_assert (slot->width >= total_size);
4497 #ifdef ENABLE_IRA_CHECKING
4498 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4499 FIRST_PSEUDO_REGISTER, i, bi)
4501 ira_assert (! conflict_by_live_ranges_p (regno, i));
4503 #endif
4504 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4505 if (internal_flag_ira_verbose > 3 && ira_dump_file)
4507 fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
4508 regno, REG_FREQ (regno), slot_num);
4509 EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4510 FIRST_PSEUDO_REGISTER, i, bi)
4512 if ((unsigned) regno != i)
4513 fprintf (ira_dump_file, " %d", i);
4515 fprintf (ira_dump_file, "\n");
4518 return x;
4521 /* This is called by reload every time a new stack slot X with
4522 TOTAL_SIZE was allocated for REGNO. We store this info for
4523 subsequent ira_reuse_stack_slot calls. */
4524 void
4525 ira_mark_new_stack_slot (rtx x, int regno, unsigned int total_size)
4527 struct ira_spilled_reg_stack_slot *slot;
4528 int slot_num;
4529 ira_allocno_t allocno;
4531 ira_assert (PSEUDO_REGNO_BYTES (regno) <= total_size);
4532 allocno = ira_regno_allocno_map[regno];
4533 slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4534 if (slot_num == -1)
4536 slot_num = ira_spilled_reg_stack_slots_num++;
4537 ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4539 slot = &ira_spilled_reg_stack_slots[slot_num];
4540 INIT_REG_SET (&slot->spilled_regs);
4541 SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4542 slot->mem = x;
4543 slot->width = total_size;
4544 if (internal_flag_ira_verbose > 3 && ira_dump_file)
4545 fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
4546 regno, REG_FREQ (regno), slot_num);
4550 /* Return spill cost for pseudo-registers whose numbers are in array
4551 REGNOS (with a negative number as an end marker) for reload with
4552 given IN and OUT for INSN. Return also number points (through
4553 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
4554 the register pressure is high, number of references of the
4555 pseudo-registers (through NREFS), number of callee-clobbered
4556 hard-registers occupied by the pseudo-registers (through
4557 CALL_USED_COUNT), and the first hard regno occupied by the
4558 pseudo-registers (through FIRST_HARD_REGNO). */
4559 static int
4560 calculate_spill_cost (int *regnos, rtx in, rtx out, rtx insn,
4561 int *excess_pressure_live_length,
4562 int *nrefs, int *call_used_count, int *first_hard_regno)
4564 int i, cost, regno, hard_regno, j, count, saved_cost, nregs;
4565 bool in_p, out_p;
4566 int length;
4567 ira_allocno_t a;
4569 *nrefs = 0;
4570 for (length = count = cost = i = 0;; i++)
4572 regno = regnos[i];
4573 if (regno < 0)
4574 break;
4575 *nrefs += REG_N_REFS (regno);
4576 hard_regno = reg_renumber[regno];
4577 ira_assert (hard_regno >= 0);
4578 a = ira_regno_allocno_map[regno];
4579 length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) / ALLOCNO_NUM_OBJECTS (a);
4580 cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
4581 nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
4582 for (j = 0; j < nregs; j++)
4583 if (! TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + j))
4584 break;
4585 if (j == nregs)
4586 count++;
4587 in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
4588 out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
4589 if ((in_p || out_p)
4590 && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
4592 saved_cost = 0;
4593 if (in_p)
4594 saved_cost += ira_memory_move_cost
4595 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][1];
4596 if (out_p)
4597 saved_cost
4598 += ira_memory_move_cost
4599 [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][0];
4600 cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
4603 *excess_pressure_live_length = length;
4604 *call_used_count = count;
4605 hard_regno = -1;
4606 if (regnos[0] >= 0)
4608 hard_regno = reg_renumber[regnos[0]];
4610 *first_hard_regno = hard_regno;
4611 return cost;
4614 /* Return TRUE if spilling pseudo-registers whose numbers are in array
4615 REGNOS is better than spilling pseudo-registers with numbers in
4616 OTHER_REGNOS for reload with given IN and OUT for INSN. The
4617 function used by the reload pass to make better register spilling
4618 decisions. */
4619 bool
4620 ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
4621 rtx in, rtx out, rtx insn)
4623 int cost, other_cost;
4624 int length, other_length;
4625 int nrefs, other_nrefs;
4626 int call_used_count, other_call_used_count;
4627 int hard_regno, other_hard_regno;
4629 cost = calculate_spill_cost (regnos, in, out, insn,
4630 &length, &nrefs, &call_used_count, &hard_regno);
4631 other_cost = calculate_spill_cost (other_regnos, in, out, insn,
4632 &other_length, &other_nrefs,
4633 &other_call_used_count,
4634 &other_hard_regno);
4635 if (nrefs == 0 && other_nrefs != 0)
4636 return true;
4637 if (nrefs != 0 && other_nrefs == 0)
4638 return false;
4639 if (cost != other_cost)
4640 return cost < other_cost;
4641 if (length != other_length)
4642 return length > other_length;
4643 #ifdef REG_ALLOC_ORDER
4644 if (hard_regno >= 0 && other_hard_regno >= 0)
4645 return (inv_reg_alloc_order[hard_regno]
4646 < inv_reg_alloc_order[other_hard_regno]);
4647 #else
4648 if (call_used_count != other_call_used_count)
4649 return call_used_count > other_call_used_count;
4650 #endif
4651 return false;
4656 /* Allocate and initialize data necessary for assign_hard_reg. */
4657 void
4658 ira_initiate_assign (void)
4660 sorted_allocnos
4661 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4662 * ira_allocnos_num);
4663 consideration_allocno_bitmap = ira_allocate_bitmap ();
4664 initiate_cost_update ();
4665 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4666 sorted_copies = (ira_copy_t *) ira_allocate (ira_copies_num
4667 * sizeof (ira_copy_t));
4670 /* Deallocate data used by assign_hard_reg. */
4671 void
4672 ira_finish_assign (void)
4674 ira_free (sorted_allocnos);
4675 ira_free_bitmap (consideration_allocno_bitmap);
4676 finish_cost_update ();
4677 ira_free (allocno_priorities);
4678 ira_free (sorted_copies);
4683 /* Entry function doing color-based register allocation. */
4684 static void
4685 color (void)
4687 allocno_stack_vec.create (ira_allocnos_num);
4688 memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
4689 ira_initiate_assign ();
4690 do_coloring ();
4691 ira_finish_assign ();
4692 allocno_stack_vec.release ();
4693 move_spill_restore ();
4698 /* This page contains a simple register allocator without usage of
4699 allocno conflicts. This is used for fast allocation for -O0. */
4701 /* Do register allocation by not using allocno conflicts. It uses
4702 only allocno live ranges. The algorithm is close to Chow's
4703 priority coloring. */
4704 static void
4705 fast_allocation (void)
4707 int i, j, k, num, class_size, hard_regno;
4708 #ifdef STACK_REGS
4709 bool no_stack_reg_p;
4710 #endif
4711 enum reg_class aclass;
4712 enum machine_mode mode;
4713 ira_allocno_t a;
4714 ira_allocno_iterator ai;
4715 live_range_t r;
4716 HARD_REG_SET conflict_hard_regs, *used_hard_regs;
4718 sorted_allocnos = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4719 * ira_allocnos_num);
4720 num = 0;
4721 FOR_EACH_ALLOCNO (a, ai)
4722 sorted_allocnos[num++] = a;
4723 allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4724 setup_allocno_priorities (sorted_allocnos, num);
4725 used_hard_regs = (HARD_REG_SET *) ira_allocate (sizeof (HARD_REG_SET)
4726 * ira_max_point);
4727 for (i = 0; i < ira_max_point; i++)
4728 CLEAR_HARD_REG_SET (used_hard_regs[i]);
4729 qsort (sorted_allocnos, num, sizeof (ira_allocno_t),
4730 allocno_priority_compare_func);
4731 for (i = 0; i < num; i++)
4733 int nr, l;
4735 a = sorted_allocnos[i];
4736 nr = ALLOCNO_NUM_OBJECTS (a);
4737 CLEAR_HARD_REG_SET (conflict_hard_regs);
4738 for (l = 0; l < nr; l++)
4740 ira_object_t obj = ALLOCNO_OBJECT (a, l);
4741 IOR_HARD_REG_SET (conflict_hard_regs,
4742 OBJECT_CONFLICT_HARD_REGS (obj));
4743 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4744 for (j = r->start; j <= r->finish; j++)
4745 IOR_HARD_REG_SET (conflict_hard_regs, used_hard_regs[j]);
4747 aclass = ALLOCNO_CLASS (a);
4748 ALLOCNO_ASSIGNED_P (a) = true;
4749 ALLOCNO_HARD_REGNO (a) = -1;
4750 if (hard_reg_set_subset_p (reg_class_contents[aclass],
4751 conflict_hard_regs))
4752 continue;
4753 mode = ALLOCNO_MODE (a);
4754 #ifdef STACK_REGS
4755 no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
4756 #endif
4757 class_size = ira_class_hard_regs_num[aclass];
4758 for (j = 0; j < class_size; j++)
4760 hard_regno = ira_class_hard_regs[aclass][j];
4761 #ifdef STACK_REGS
4762 if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
4763 && hard_regno <= LAST_STACK_REG)
4764 continue;
4765 #endif
4766 if (ira_hard_reg_set_intersection_p (hard_regno, mode, conflict_hard_regs)
4767 || (TEST_HARD_REG_BIT
4768 (ira_prohibited_class_mode_regs[aclass][mode], hard_regno)))
4769 continue;
4770 ALLOCNO_HARD_REGNO (a) = hard_regno;
4771 for (l = 0; l < nr; l++)
4773 ira_object_t obj = ALLOCNO_OBJECT (a, l);
4774 for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4775 for (k = r->start; k <= r->finish; k++)
4776 IOR_HARD_REG_SET (used_hard_regs[k],
4777 ira_reg_mode_hard_regset[hard_regno][mode]);
4779 break;
4782 ira_free (sorted_allocnos);
4783 ira_free (used_hard_regs);
4784 ira_free (allocno_priorities);
4785 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
4786 ira_print_disposition (ira_dump_file);
4791 /* Entry function doing coloring. */
4792 void
4793 ira_color (void)
4795 ira_allocno_t a;
4796 ira_allocno_iterator ai;
4798 /* Setup updated costs. */
4799 FOR_EACH_ALLOCNO (a, ai)
4801 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
4802 ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
4804 if (ira_conflicts_p)
4805 color ();
4806 else
4807 fast_allocation ();