1 /* IRA allocation based on graph coloring.
2 Copyright (C) 2006-2017 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
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
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/>. */
23 #include "coretypes.h"
32 #include "insn-config.h"
39 typedef struct allocno_hard_regs
*allocno_hard_regs_t
;
41 /* The structure contains information about hard registers can be
42 assigned to allocnos. Usually it is allocno profitable hard
43 registers but in some cases this set can be a bit different. Major
44 reason of the difference is a requirement to use hard register sets
45 that form a tree or a forest (set of trees), i.e. hard register set
46 of a node should contain hard register sets of its subnodes. */
47 struct allocno_hard_regs
49 /* Hard registers can be assigned to an allocno. */
51 /* Overall (spilling) cost of all allocnos with given register
56 typedef struct allocno_hard_regs_node
*allocno_hard_regs_node_t
;
58 /* A node representing allocno hard registers. Such nodes form a
59 forest (set of trees). Each subnode of given node in the forest
60 refers for hard register set (usually allocno profitable hard
61 register set) which is a subset of one referred from given
63 struct allocno_hard_regs_node
65 /* Set up number of the node in preorder traversing of the forest. */
67 /* Used for different calculation like finding conflict size of an
70 /* Used for calculation of conflict size of an allocno. The
71 conflict size of the allocno is maximal number of given allocno
72 hard registers needed for allocation of the conflicting allocnos.
73 Given allocno is trivially colored if this number plus the number
74 of hard registers needed for given allocno is not greater than
75 the number of given allocno hard register set. */
77 /* The number of hard registers given by member hard_regs. */
79 /* The following member is used to form the final forest. */
81 /* Pointer to the corresponding profitable hard registers. */
82 allocno_hard_regs_t hard_regs
;
83 /* Parent, first subnode, previous and next node with the same
84 parent in the forest. */
85 allocno_hard_regs_node_t parent
, first
, prev
, next
;
88 /* Info about changing hard reg costs of an allocno. */
89 struct update_cost_record
91 /* Hard regno for which we changed the cost. */
93 /* Divisor used when we changed the cost of HARD_REGNO. */
95 /* Next record for given allocno. */
96 struct update_cost_record
*next
;
99 /* To decrease footprint of ira_allocno structure we store all data
100 needed only for coloring in the following structure. */
101 struct allocno_color_data
103 /* TRUE value means that the allocno was not removed yet from the
104 conflicting graph during coloring. */
105 unsigned int in_graph_p
: 1;
106 /* TRUE if it is put on the stack to make other allocnos
108 unsigned int may_be_spilled_p
: 1;
109 /* TRUE if the allocno is trivially colorable. */
110 unsigned int colorable_p
: 1;
111 /* Number of hard registers of the allocno class really
112 available for the allocno allocation. It is number of the
113 profitable hard regs. */
114 int available_regs_num
;
115 /* Allocnos in a bucket (used in coloring) chained by the following
117 ira_allocno_t next_bucket_allocno
;
118 ira_allocno_t prev_bucket_allocno
;
119 /* Used for temporary purposes. */
121 /* Used to exclude repeated processing. */
123 /* Profitable hard regs available for this pseudo allocation. It
124 means that the set excludes unavailable hard regs and hard regs
125 conflicting with given pseudo. They should be of the allocno
127 HARD_REG_SET profitable_hard_regs
;
128 /* The allocno hard registers node. */
129 allocno_hard_regs_node_t hard_regs_node
;
130 /* Array of structures allocno_hard_regs_subnode representing
131 given allocno hard registers node (the 1st element in the array)
132 and all its subnodes in the tree (forest) of allocno hard
133 register nodes (see comments above). */
134 int hard_regs_subnodes_start
;
135 /* The length of the previous array. */
136 int hard_regs_subnodes_num
;
137 /* Records about updating allocno hard reg costs from copies. If
138 the allocno did not get expected hard register, these records are
139 used to restore original hard reg costs of allocnos connected to
140 this allocno by copies. */
141 struct update_cost_record
*update_cost_records
;
142 /* Threads. We collect allocnos connected by copies into threads
143 and try to assign hard regs to allocnos by threads. */
144 /* Allocno representing all thread. */
145 ira_allocno_t first_thread_allocno
;
146 /* Allocnos in thread forms a cycle list through the following
148 ira_allocno_t next_thread_allocno
;
149 /* All thread frequency. Defined only for first thread allocno. */
154 typedef struct allocno_color_data
*allocno_color_data_t
;
156 /* Container for storing allocno data concerning coloring. */
157 static allocno_color_data_t allocno_color_data
;
159 /* Macro to access the data concerning coloring. */
160 #define ALLOCNO_COLOR_DATA(a) ((allocno_color_data_t) ALLOCNO_ADD_DATA (a))
162 /* Used for finding allocno colorability to exclude repeated allocno
163 processing and for updating preferencing to exclude repeated
164 allocno processing during assignment. */
165 static int curr_allocno_process
;
167 /* This file contains code for regional graph coloring, spill/restore
168 code placement optimization, and code helping the reload pass to do
171 /* Bitmap of allocnos which should be colored. */
172 static bitmap coloring_allocno_bitmap
;
174 /* Bitmap of allocnos which should be taken into account during
175 coloring. In general case it contains allocnos from
176 coloring_allocno_bitmap plus other already colored conflicting
178 static bitmap consideration_allocno_bitmap
;
180 /* All allocnos sorted according their priorities. */
181 static ira_allocno_t
*sorted_allocnos
;
183 /* Vec representing the stack of allocnos used during coloring. */
184 static vec
<ira_allocno_t
> allocno_stack_vec
;
186 /* Helper for qsort comparison callbacks - return a positive integer if
187 X > Y, or a negative value otherwise. Use a conditional expression
188 instead of a difference computation to insulate from possible overflow
189 issues, e.g. X - Y < 0 for some X > 0 and Y < 0. */
190 #define SORTGT(x,y) (((x) > (y)) ? 1 : -1)
194 /* Definition of vector of allocno hard registers. */
196 /* Vector of unique allocno hard registers. */
197 static vec
<allocno_hard_regs_t
> allocno_hard_regs_vec
;
199 struct allocno_hard_regs_hasher
: nofree_ptr_hash
<allocno_hard_regs
>
201 static inline hashval_t
hash (const allocno_hard_regs
*);
202 static inline bool equal (const allocno_hard_regs
*,
203 const allocno_hard_regs
*);
206 /* Returns hash value for allocno hard registers V. */
208 allocno_hard_regs_hasher::hash (const allocno_hard_regs
*hv
)
210 return iterative_hash (&hv
->set
, sizeof (HARD_REG_SET
), 0);
213 /* Compares allocno hard registers V1 and V2. */
215 allocno_hard_regs_hasher::equal (const allocno_hard_regs
*hv1
,
216 const allocno_hard_regs
*hv2
)
218 return hard_reg_set_equal_p (hv1
->set
, hv2
->set
);
221 /* Hash table of unique allocno hard registers. */
222 static hash_table
<allocno_hard_regs_hasher
> *allocno_hard_regs_htab
;
224 /* Return allocno hard registers in the hash table equal to HV. */
225 static allocno_hard_regs_t
226 find_hard_regs (allocno_hard_regs_t hv
)
228 return allocno_hard_regs_htab
->find (hv
);
231 /* Insert allocno hard registers HV in the hash table (if it is not
232 there yet) and return the value which in the table. */
233 static allocno_hard_regs_t
234 insert_hard_regs (allocno_hard_regs_t hv
)
236 allocno_hard_regs
**slot
= allocno_hard_regs_htab
->find_slot (hv
, INSERT
);
243 /* Initialize data concerning allocno hard registers. */
245 init_allocno_hard_regs (void)
247 allocno_hard_regs_vec
.create (200);
248 allocno_hard_regs_htab
249 = new hash_table
<allocno_hard_regs_hasher
> (200);
252 /* Add (or update info about) allocno hard registers with SET and
254 static allocno_hard_regs_t
255 add_allocno_hard_regs (HARD_REG_SET set
, int64_t cost
)
257 struct allocno_hard_regs temp
;
258 allocno_hard_regs_t hv
;
260 gcc_assert (! hard_reg_set_empty_p (set
));
261 COPY_HARD_REG_SET (temp
.set
, set
);
262 if ((hv
= find_hard_regs (&temp
)) != NULL
)
266 hv
= ((struct allocno_hard_regs
*)
267 ira_allocate (sizeof (struct allocno_hard_regs
)));
268 COPY_HARD_REG_SET (hv
->set
, set
);
270 allocno_hard_regs_vec
.safe_push (hv
);
271 insert_hard_regs (hv
);
276 /* Finalize data concerning allocno hard registers. */
278 finish_allocno_hard_regs (void)
281 allocno_hard_regs_t hv
;
284 allocno_hard_regs_vec
.iterate (i
, &hv
);
287 delete allocno_hard_regs_htab
;
288 allocno_hard_regs_htab
= NULL
;
289 allocno_hard_regs_vec
.release ();
292 /* Sort hard regs according to their frequency of usage. */
294 allocno_hard_regs_compare (const void *v1p
, const void *v2p
)
296 allocno_hard_regs_t hv1
= *(const allocno_hard_regs_t
*) v1p
;
297 allocno_hard_regs_t hv2
= *(const allocno_hard_regs_t
*) v2p
;
299 if (hv2
->cost
> hv1
->cost
)
301 else if (hv2
->cost
< hv1
->cost
)
309 /* Used for finding a common ancestor of two allocno hard registers
310 nodes in the forest. We use the current value of
311 'node_check_tick' to mark all nodes from one node to the top and
312 then walking up from another node until we find a marked node.
314 It is also used to figure out allocno colorability as a mark that
315 we already reset value of member 'conflict_size' for the forest
316 node corresponding to the processed allocno. */
317 static int node_check_tick
;
319 /* Roots of the forest containing hard register sets can be assigned
321 static allocno_hard_regs_node_t hard_regs_roots
;
323 /* Definition of vector of allocno hard register nodes. */
325 /* Vector used to create the forest. */
326 static vec
<allocno_hard_regs_node_t
> hard_regs_node_vec
;
328 /* Create and return allocno hard registers node containing allocno
329 hard registers HV. */
330 static allocno_hard_regs_node_t
331 create_new_allocno_hard_regs_node (allocno_hard_regs_t hv
)
333 allocno_hard_regs_node_t new_node
;
335 new_node
= ((struct allocno_hard_regs_node
*)
336 ira_allocate (sizeof (struct allocno_hard_regs_node
)));
338 new_node
->hard_regs
= hv
;
339 new_node
->hard_regs_num
= hard_reg_set_size (hv
->set
);
340 new_node
->first
= NULL
;
341 new_node
->used_p
= false;
345 /* Add allocno hard registers node NEW_NODE to the forest on its level
348 add_new_allocno_hard_regs_node_to_forest (allocno_hard_regs_node_t
*roots
,
349 allocno_hard_regs_node_t new_node
)
351 new_node
->next
= *roots
;
352 if (new_node
->next
!= NULL
)
353 new_node
->next
->prev
= new_node
;
354 new_node
->prev
= NULL
;
358 /* Add allocno hard registers HV (or its best approximation if it is
359 not possible) to the forest on its level given by ROOTS. */
361 add_allocno_hard_regs_to_forest (allocno_hard_regs_node_t
*roots
,
362 allocno_hard_regs_t hv
)
364 unsigned int i
, start
;
365 allocno_hard_regs_node_t node
, prev
, new_node
;
366 HARD_REG_SET temp_set
;
367 allocno_hard_regs_t hv2
;
369 start
= hard_regs_node_vec
.length ();
370 for (node
= *roots
; node
!= NULL
; node
= node
->next
)
372 if (hard_reg_set_equal_p (hv
->set
, node
->hard_regs
->set
))
374 if (hard_reg_set_subset_p (hv
->set
, node
->hard_regs
->set
))
376 add_allocno_hard_regs_to_forest (&node
->first
, hv
);
379 if (hard_reg_set_subset_p (node
->hard_regs
->set
, hv
->set
))
380 hard_regs_node_vec
.safe_push (node
);
381 else if (hard_reg_set_intersect_p (hv
->set
, node
->hard_regs
->set
))
383 COPY_HARD_REG_SET (temp_set
, hv
->set
);
384 AND_HARD_REG_SET (temp_set
, node
->hard_regs
->set
);
385 hv2
= add_allocno_hard_regs (temp_set
, hv
->cost
);
386 add_allocno_hard_regs_to_forest (&node
->first
, hv2
);
389 if (hard_regs_node_vec
.length ()
392 /* Create a new node which contains nodes in hard_regs_node_vec. */
393 CLEAR_HARD_REG_SET (temp_set
);
395 i
< hard_regs_node_vec
.length ();
398 node
= hard_regs_node_vec
[i
];
399 IOR_HARD_REG_SET (temp_set
, node
->hard_regs
->set
);
401 hv
= add_allocno_hard_regs (temp_set
, hv
->cost
);
402 new_node
= create_new_allocno_hard_regs_node (hv
);
405 i
< hard_regs_node_vec
.length ();
408 node
= hard_regs_node_vec
[i
];
409 if (node
->prev
== NULL
)
412 node
->prev
->next
= node
->next
;
413 if (node
->next
!= NULL
)
414 node
->next
->prev
= node
->prev
;
416 new_node
->first
= node
;
423 add_new_allocno_hard_regs_node_to_forest (roots
, new_node
);
425 hard_regs_node_vec
.truncate (start
);
428 /* Add allocno hard registers nodes starting with the forest level
429 given by FIRST which contains biggest set inside SET. */
431 collect_allocno_hard_regs_cover (allocno_hard_regs_node_t first
,
434 allocno_hard_regs_node_t node
;
436 ira_assert (first
!= NULL
);
437 for (node
= first
; node
!= NULL
; node
= node
->next
)
438 if (hard_reg_set_subset_p (node
->hard_regs
->set
, set
))
439 hard_regs_node_vec
.safe_push (node
);
440 else if (hard_reg_set_intersect_p (set
, node
->hard_regs
->set
))
441 collect_allocno_hard_regs_cover (node
->first
, set
);
444 /* Set up field parent as PARENT in all allocno hard registers nodes
445 in forest given by FIRST. */
447 setup_allocno_hard_regs_nodes_parent (allocno_hard_regs_node_t first
,
448 allocno_hard_regs_node_t parent
)
450 allocno_hard_regs_node_t node
;
452 for (node
= first
; node
!= NULL
; node
= node
->next
)
454 node
->parent
= parent
;
455 setup_allocno_hard_regs_nodes_parent (node
->first
, node
);
459 /* Return allocno hard registers node which is a first common ancestor
460 node of FIRST and SECOND in the forest. */
461 static allocno_hard_regs_node_t
462 first_common_ancestor_node (allocno_hard_regs_node_t first
,
463 allocno_hard_regs_node_t second
)
465 allocno_hard_regs_node_t node
;
468 for (node
= first
; node
!= NULL
; node
= node
->parent
)
469 node
->check
= node_check_tick
;
470 for (node
= second
; node
!= NULL
; node
= node
->parent
)
471 if (node
->check
== node_check_tick
)
473 return first_common_ancestor_node (second
, first
);
476 /* Print hard reg set SET to F. */
478 print_hard_reg_set (FILE *f
, HARD_REG_SET set
, bool new_line_p
)
482 for (start
= -1, i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
484 if (TEST_HARD_REG_BIT (set
, i
))
486 if (i
== 0 || ! TEST_HARD_REG_BIT (set
, i
- 1))
490 && (i
== FIRST_PSEUDO_REGISTER
- 1 || ! TEST_HARD_REG_BIT (set
, i
)))
493 fprintf (f
, " %d", start
);
494 else if (start
== i
- 2)
495 fprintf (f
, " %d %d", start
, start
+ 1);
497 fprintf (f
, " %d-%d", start
, i
- 1);
505 /* Print allocno hard register subforest given by ROOTS and its LEVEL
508 print_hard_regs_subforest (FILE *f
, allocno_hard_regs_node_t roots
,
512 allocno_hard_regs_node_t node
;
514 for (node
= roots
; node
!= NULL
; node
= node
->next
)
517 for (i
= 0; i
< level
* 2; i
++)
519 fprintf (f
, "%d:(", node
->preorder_num
);
520 print_hard_reg_set (f
, node
->hard_regs
->set
, false);
521 fprintf (f
, ")@%" PRId64
"\n", node
->hard_regs
->cost
);
522 print_hard_regs_subforest (f
, node
->first
, level
+ 1);
526 /* Print the allocno hard register forest to F. */
528 print_hard_regs_forest (FILE *f
)
530 fprintf (f
, " Hard reg set forest:\n");
531 print_hard_regs_subforest (f
, hard_regs_roots
, 1);
534 /* Print the allocno hard register forest to stderr. */
536 ira_debug_hard_regs_forest (void)
538 print_hard_regs_forest (stderr
);
541 /* Remove unused allocno hard registers nodes from forest given by its
544 remove_unused_allocno_hard_regs_nodes (allocno_hard_regs_node_t
*roots
)
546 allocno_hard_regs_node_t node
, prev
, next
, last
;
548 for (prev
= NULL
, node
= *roots
; node
!= NULL
; node
= next
)
553 remove_unused_allocno_hard_regs_nodes (&node
->first
);
558 for (last
= node
->first
;
559 last
!= NULL
&& last
->next
!= NULL
;
565 *roots
= node
->first
;
567 prev
->next
= node
->first
;
587 /* Set up fields preorder_num starting with START_NUM in all allocno
588 hard registers nodes in forest given by FIRST. Return biggest set
589 PREORDER_NUM increased by 1. */
591 enumerate_allocno_hard_regs_nodes (allocno_hard_regs_node_t first
,
592 allocno_hard_regs_node_t parent
,
595 allocno_hard_regs_node_t node
;
597 for (node
= first
; node
!= NULL
; node
= node
->next
)
599 node
->preorder_num
= start_num
++;
600 node
->parent
= parent
;
601 start_num
= enumerate_allocno_hard_regs_nodes (node
->first
, node
,
607 /* Number of allocno hard registers nodes in the forest. */
608 static int allocno_hard_regs_nodes_num
;
610 /* Table preorder number of allocno hard registers node in the forest
611 -> the allocno hard registers node. */
612 static allocno_hard_regs_node_t
*allocno_hard_regs_nodes
;
615 typedef struct allocno_hard_regs_subnode
*allocno_hard_regs_subnode_t
;
617 /* The structure is used to describes all subnodes (not only immediate
618 ones) in the mentioned above tree for given allocno hard register
619 node. The usage of such data accelerates calculation of
620 colorability of given allocno. */
621 struct allocno_hard_regs_subnode
623 /* The conflict size of conflicting allocnos whose hard register
624 sets are equal sets (plus supersets if given node is given
625 allocno hard registers node) of one in the given node. */
626 int left_conflict_size
;
627 /* The summary conflict size of conflicting allocnos whose hard
628 register sets are strict subsets of one in the given node.
629 Overall conflict size is
630 left_conflict_subnodes_size
631 + MIN (max_node_impact - left_conflict_subnodes_size,
634 short left_conflict_subnodes_size
;
635 short max_node_impact
;
638 /* Container for hard regs subnodes of all allocnos. */
639 static allocno_hard_regs_subnode_t allocno_hard_regs_subnodes
;
641 /* Table (preorder number of allocno hard registers node in the
642 forest, preorder number of allocno hard registers subnode) -> index
643 of the subnode relative to the node. -1 if it is not a
645 static int *allocno_hard_regs_subnode_index
;
647 /* Setup arrays ALLOCNO_HARD_REGS_NODES and
648 ALLOCNO_HARD_REGS_SUBNODE_INDEX. */
650 setup_allocno_hard_regs_subnode_index (allocno_hard_regs_node_t first
)
652 allocno_hard_regs_node_t node
, parent
;
655 for (node
= first
; node
!= NULL
; node
= node
->next
)
657 allocno_hard_regs_nodes
[node
->preorder_num
] = node
;
658 for (parent
= node
; parent
!= NULL
; parent
= parent
->parent
)
660 index
= parent
->preorder_num
* allocno_hard_regs_nodes_num
;
661 allocno_hard_regs_subnode_index
[index
+ node
->preorder_num
]
662 = node
->preorder_num
- parent
->preorder_num
;
664 setup_allocno_hard_regs_subnode_index (node
->first
);
668 /* Count all allocno hard registers nodes in tree ROOT. */
670 get_allocno_hard_regs_subnodes_num (allocno_hard_regs_node_t root
)
674 for (root
= root
->first
; root
!= NULL
; root
= root
->next
)
675 len
+= get_allocno_hard_regs_subnodes_num (root
);
679 /* Build the forest of allocno hard registers nodes and assign each
680 allocno a node from the forest. */
682 form_allocno_hard_regs_nodes_forest (void)
684 unsigned int i
, j
, size
, len
;
687 allocno_hard_regs_t hv
;
690 allocno_hard_regs_node_t node
, allocno_hard_regs_node
;
691 allocno_color_data_t allocno_data
;
694 init_allocno_hard_regs ();
695 hard_regs_roots
= NULL
;
696 hard_regs_node_vec
.create (100);
697 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
698 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs
, i
))
700 CLEAR_HARD_REG_SET (temp
);
701 SET_HARD_REG_BIT (temp
, i
);
702 hv
= add_allocno_hard_regs (temp
, 0);
703 node
= create_new_allocno_hard_regs_node (hv
);
704 add_new_allocno_hard_regs_node_to_forest (&hard_regs_roots
, node
);
706 start
= allocno_hard_regs_vec
.length ();
707 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
710 allocno_data
= ALLOCNO_COLOR_DATA (a
);
712 if (hard_reg_set_empty_p (allocno_data
->profitable_hard_regs
))
714 hv
= (add_allocno_hard_regs
715 (allocno_data
->profitable_hard_regs
,
716 ALLOCNO_MEMORY_COST (a
) - ALLOCNO_CLASS_COST (a
)));
718 SET_HARD_REG_SET (temp
);
719 AND_COMPL_HARD_REG_SET (temp
, ira_no_alloc_regs
);
720 add_allocno_hard_regs (temp
, 0);
721 qsort (allocno_hard_regs_vec
.address () + start
,
722 allocno_hard_regs_vec
.length () - start
,
723 sizeof (allocno_hard_regs_t
), allocno_hard_regs_compare
);
725 allocno_hard_regs_vec
.iterate (i
, &hv
);
728 add_allocno_hard_regs_to_forest (&hard_regs_roots
, hv
);
729 ira_assert (hard_regs_node_vec
.length () == 0);
731 /* We need to set up parent fields for right work of
732 first_common_ancestor_node. */
733 setup_allocno_hard_regs_nodes_parent (hard_regs_roots
, NULL
);
734 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
737 allocno_data
= ALLOCNO_COLOR_DATA (a
);
738 if (hard_reg_set_empty_p (allocno_data
->profitable_hard_regs
))
740 hard_regs_node_vec
.truncate (0);
741 collect_allocno_hard_regs_cover (hard_regs_roots
,
742 allocno_data
->profitable_hard_regs
);
743 allocno_hard_regs_node
= NULL
;
744 for (j
= 0; hard_regs_node_vec
.iterate (j
, &node
); j
++)
745 allocno_hard_regs_node
748 : first_common_ancestor_node (node
, allocno_hard_regs_node
));
749 /* That is a temporary storage. */
750 allocno_hard_regs_node
->used_p
= true;
751 allocno_data
->hard_regs_node
= allocno_hard_regs_node
;
753 ira_assert (hard_regs_roots
->next
== NULL
);
754 hard_regs_roots
->used_p
= true;
755 remove_unused_allocno_hard_regs_nodes (&hard_regs_roots
);
756 allocno_hard_regs_nodes_num
757 = enumerate_allocno_hard_regs_nodes (hard_regs_roots
, NULL
, 0);
758 allocno_hard_regs_nodes
759 = ((allocno_hard_regs_node_t
*)
760 ira_allocate (allocno_hard_regs_nodes_num
761 * sizeof (allocno_hard_regs_node_t
)));
762 size
= allocno_hard_regs_nodes_num
* allocno_hard_regs_nodes_num
;
763 allocno_hard_regs_subnode_index
764 = (int *) ira_allocate (size
* sizeof (int));
765 for (i
= 0; i
< size
; i
++)
766 allocno_hard_regs_subnode_index
[i
] = -1;
767 setup_allocno_hard_regs_subnode_index (hard_regs_roots
);
769 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
772 allocno_data
= ALLOCNO_COLOR_DATA (a
);
773 if (hard_reg_set_empty_p (allocno_data
->profitable_hard_regs
))
775 len
= get_allocno_hard_regs_subnodes_num (allocno_data
->hard_regs_node
);
776 allocno_data
->hard_regs_subnodes_start
= start
;
777 allocno_data
->hard_regs_subnodes_num
= len
;
780 allocno_hard_regs_subnodes
781 = ((allocno_hard_regs_subnode_t
)
782 ira_allocate (sizeof (struct allocno_hard_regs_subnode
) * start
));
783 hard_regs_node_vec
.release ();
786 /* Free tree of allocno hard registers nodes given by its ROOT. */
788 finish_allocno_hard_regs_nodes_tree (allocno_hard_regs_node_t root
)
790 allocno_hard_regs_node_t child
, next
;
792 for (child
= root
->first
; child
!= NULL
; child
= next
)
795 finish_allocno_hard_regs_nodes_tree (child
);
800 /* Finish work with the forest of allocno hard registers nodes. */
802 finish_allocno_hard_regs_nodes_forest (void)
804 allocno_hard_regs_node_t node
, next
;
806 ira_free (allocno_hard_regs_subnodes
);
807 for (node
= hard_regs_roots
; node
!= NULL
; node
= next
)
810 finish_allocno_hard_regs_nodes_tree (node
);
812 ira_free (allocno_hard_regs_nodes
);
813 ira_free (allocno_hard_regs_subnode_index
);
814 finish_allocno_hard_regs ();
817 /* Set up left conflict sizes and left conflict subnodes sizes of hard
818 registers subnodes of allocno A. Return TRUE if allocno A is
819 trivially colorable. */
821 setup_left_conflict_sizes_p (ira_allocno_t a
)
823 int i
, k
, nobj
, start
;
824 int conflict_size
, left_conflict_subnodes_size
, node_preorder_num
;
825 allocno_color_data_t data
;
826 HARD_REG_SET profitable_hard_regs
;
827 allocno_hard_regs_subnode_t subnodes
;
828 allocno_hard_regs_node_t node
;
829 HARD_REG_SET node_set
;
831 nobj
= ALLOCNO_NUM_OBJECTS (a
);
832 data
= ALLOCNO_COLOR_DATA (a
);
833 subnodes
= allocno_hard_regs_subnodes
+ data
->hard_regs_subnodes_start
;
834 COPY_HARD_REG_SET (profitable_hard_regs
, data
->profitable_hard_regs
);
835 node
= data
->hard_regs_node
;
836 node_preorder_num
= node
->preorder_num
;
837 COPY_HARD_REG_SET (node_set
, node
->hard_regs
->set
);
839 for (k
= 0; k
< nobj
; k
++)
841 ira_object_t obj
= ALLOCNO_OBJECT (a
, k
);
842 ira_object_t conflict_obj
;
843 ira_object_conflict_iterator oci
;
845 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
848 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
849 allocno_hard_regs_node_t conflict_node
, temp_node
;
850 HARD_REG_SET conflict_node_set
;
851 allocno_color_data_t conflict_data
;
853 conflict_data
= ALLOCNO_COLOR_DATA (conflict_a
);
854 if (! ALLOCNO_COLOR_DATA (conflict_a
)->in_graph_p
855 || ! hard_reg_set_intersect_p (profitable_hard_regs
,
857 ->profitable_hard_regs
))
859 conflict_node
= conflict_data
->hard_regs_node
;
860 COPY_HARD_REG_SET (conflict_node_set
, conflict_node
->hard_regs
->set
);
861 if (hard_reg_set_subset_p (node_set
, conflict_node_set
))
865 ira_assert (hard_reg_set_subset_p (conflict_node_set
, node_set
));
866 temp_node
= conflict_node
;
868 if (temp_node
->check
!= node_check_tick
)
870 temp_node
->check
= node_check_tick
;
871 temp_node
->conflict_size
= 0;
873 size
= (ira_reg_class_max_nregs
874 [ALLOCNO_CLASS (conflict_a
)][ALLOCNO_MODE (conflict_a
)]);
875 if (ALLOCNO_NUM_OBJECTS (conflict_a
) > 1)
876 /* We will deal with the subwords individually. */
878 temp_node
->conflict_size
+= size
;
881 for (i
= 0; i
< data
->hard_regs_subnodes_num
; i
++)
883 allocno_hard_regs_node_t temp_node
;
885 temp_node
= allocno_hard_regs_nodes
[i
+ node_preorder_num
];
886 ira_assert (temp_node
->preorder_num
== i
+ node_preorder_num
);
887 subnodes
[i
].left_conflict_size
= (temp_node
->check
!= node_check_tick
888 ? 0 : temp_node
->conflict_size
);
889 if (hard_reg_set_subset_p (temp_node
->hard_regs
->set
,
890 profitable_hard_regs
))
891 subnodes
[i
].max_node_impact
= temp_node
->hard_regs_num
;
894 HARD_REG_SET temp_set
;
895 int j
, n
, hard_regno
;
896 enum reg_class aclass
;
898 COPY_HARD_REG_SET (temp_set
, temp_node
->hard_regs
->set
);
899 AND_HARD_REG_SET (temp_set
, profitable_hard_regs
);
900 aclass
= ALLOCNO_CLASS (a
);
901 for (n
= 0, j
= ira_class_hard_regs_num
[aclass
] - 1; j
>= 0; j
--)
903 hard_regno
= ira_class_hard_regs
[aclass
][j
];
904 if (TEST_HARD_REG_BIT (temp_set
, hard_regno
))
907 subnodes
[i
].max_node_impact
= n
;
909 subnodes
[i
].left_conflict_subnodes_size
= 0;
911 start
= node_preorder_num
* allocno_hard_regs_nodes_num
;
912 for (i
= data
->hard_regs_subnodes_num
- 1; i
> 0; i
--)
915 allocno_hard_regs_node_t parent
;
917 size
= (subnodes
[i
].left_conflict_subnodes_size
918 + MIN (subnodes
[i
].max_node_impact
919 - subnodes
[i
].left_conflict_subnodes_size
,
920 subnodes
[i
].left_conflict_size
));
921 parent
= allocno_hard_regs_nodes
[i
+ node_preorder_num
]->parent
;
922 gcc_checking_assert(parent
);
924 = allocno_hard_regs_subnode_index
[start
+ parent
->preorder_num
];
925 gcc_checking_assert(parent_i
>= 0);
926 subnodes
[parent_i
].left_conflict_subnodes_size
+= size
;
928 left_conflict_subnodes_size
= subnodes
[0].left_conflict_subnodes_size
;
930 = (left_conflict_subnodes_size
931 + MIN (subnodes
[0].max_node_impact
- left_conflict_subnodes_size
,
932 subnodes
[0].left_conflict_size
));
933 conflict_size
+= ira_reg_class_max_nregs
[ALLOCNO_CLASS (a
)][ALLOCNO_MODE (a
)];
934 data
->colorable_p
= conflict_size
<= data
->available_regs_num
;
935 return data
->colorable_p
;
938 /* Update left conflict sizes of hard registers subnodes of allocno A
939 after removing allocno REMOVED_A with SIZE from the conflict graph.
940 Return TRUE if A is trivially colorable. */
942 update_left_conflict_sizes_p (ira_allocno_t a
,
943 ira_allocno_t removed_a
, int size
)
945 int i
, conflict_size
, before_conflict_size
, diff
, start
;
946 int node_preorder_num
, parent_i
;
947 allocno_hard_regs_node_t node
, removed_node
, parent
;
948 allocno_hard_regs_subnode_t subnodes
;
949 allocno_color_data_t data
= ALLOCNO_COLOR_DATA (a
);
951 ira_assert (! data
->colorable_p
);
952 node
= data
->hard_regs_node
;
953 node_preorder_num
= node
->preorder_num
;
954 removed_node
= ALLOCNO_COLOR_DATA (removed_a
)->hard_regs_node
;
955 ira_assert (hard_reg_set_subset_p (removed_node
->hard_regs
->set
,
956 node
->hard_regs
->set
)
957 || hard_reg_set_subset_p (node
->hard_regs
->set
,
958 removed_node
->hard_regs
->set
));
959 start
= node_preorder_num
* allocno_hard_regs_nodes_num
;
960 i
= allocno_hard_regs_subnode_index
[start
+ removed_node
->preorder_num
];
963 subnodes
= allocno_hard_regs_subnodes
+ data
->hard_regs_subnodes_start
;
965 = (subnodes
[i
].left_conflict_subnodes_size
966 + MIN (subnodes
[i
].max_node_impact
967 - subnodes
[i
].left_conflict_subnodes_size
,
968 subnodes
[i
].left_conflict_size
));
969 subnodes
[i
].left_conflict_size
-= size
;
973 = (subnodes
[i
].left_conflict_subnodes_size
974 + MIN (subnodes
[i
].max_node_impact
975 - subnodes
[i
].left_conflict_subnodes_size
,
976 subnodes
[i
].left_conflict_size
));
977 if ((diff
= before_conflict_size
- conflict_size
) == 0)
979 ira_assert (conflict_size
< before_conflict_size
);
980 parent
= allocno_hard_regs_nodes
[i
+ node_preorder_num
]->parent
;
984 = allocno_hard_regs_subnode_index
[start
+ parent
->preorder_num
];
989 = (subnodes
[i
].left_conflict_subnodes_size
990 + MIN (subnodes
[i
].max_node_impact
991 - subnodes
[i
].left_conflict_subnodes_size
,
992 subnodes
[i
].left_conflict_size
));
993 subnodes
[i
].left_conflict_subnodes_size
-= diff
;
997 + ira_reg_class_max_nregs
[ALLOCNO_CLASS (a
)][ALLOCNO_MODE (a
)]
998 > data
->available_regs_num
))
1000 data
->colorable_p
= true;
1004 /* Return true if allocno A has empty profitable hard regs. */
1006 empty_profitable_hard_regs (ira_allocno_t a
)
1008 allocno_color_data_t data
= ALLOCNO_COLOR_DATA (a
);
1010 return hard_reg_set_empty_p (data
->profitable_hard_regs
);
1013 /* Set up profitable hard registers for each allocno being
1016 setup_profitable_hard_regs (void)
1019 int j
, k
, nobj
, hard_regno
, nregs
, class_size
;
1022 enum reg_class aclass
;
1024 allocno_color_data_t data
;
1026 /* Initial set up from allocno classes and explicitly conflicting
1028 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
1030 a
= ira_allocnos
[i
];
1031 if ((aclass
= ALLOCNO_CLASS (a
)) == NO_REGS
)
1033 data
= ALLOCNO_COLOR_DATA (a
);
1034 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
1035 && ALLOCNO_CLASS_COST (a
) > ALLOCNO_MEMORY_COST (a
)
1036 /* Do not empty profitable regs for static chain pointer
1037 pseudo when non-local goto is used. */
1038 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a
)))
1039 CLEAR_HARD_REG_SET (data
->profitable_hard_regs
);
1042 mode
= ALLOCNO_MODE (a
);
1043 COPY_HARD_REG_SET (data
->profitable_hard_regs
,
1044 ira_useful_class_mode_regs
[aclass
][mode
]);
1045 nobj
= ALLOCNO_NUM_OBJECTS (a
);
1046 for (k
= 0; k
< nobj
; k
++)
1048 ira_object_t obj
= ALLOCNO_OBJECT (a
, k
);
1050 AND_COMPL_HARD_REG_SET (data
->profitable_hard_regs
,
1051 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
));
1055 /* Exclude hard regs already assigned for conflicting objects. */
1056 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, i
, bi
)
1058 a
= ira_allocnos
[i
];
1059 if ((aclass
= ALLOCNO_CLASS (a
)) == NO_REGS
1060 || ! ALLOCNO_ASSIGNED_P (a
)
1061 || (hard_regno
= ALLOCNO_HARD_REGNO (a
)) < 0)
1063 mode
= ALLOCNO_MODE (a
);
1064 nregs
= hard_regno_nregs
[hard_regno
][mode
];
1065 nobj
= ALLOCNO_NUM_OBJECTS (a
);
1066 for (k
= 0; k
< nobj
; k
++)
1068 ira_object_t obj
= ALLOCNO_OBJECT (a
, k
);
1069 ira_object_t conflict_obj
;
1070 ira_object_conflict_iterator oci
;
1072 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
1074 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
1076 /* We can process the conflict allocno repeatedly with
1078 if (nregs
== nobj
&& nregs
> 1)
1080 int num
= OBJECT_SUBWORD (conflict_obj
);
1082 if (REG_WORDS_BIG_ENDIAN
)
1084 (ALLOCNO_COLOR_DATA (conflict_a
)->profitable_hard_regs
,
1085 hard_regno
+ nobj
- num
- 1);
1088 (ALLOCNO_COLOR_DATA (conflict_a
)->profitable_hard_regs
,
1092 AND_COMPL_HARD_REG_SET
1093 (ALLOCNO_COLOR_DATA (conflict_a
)->profitable_hard_regs
,
1094 ira_reg_mode_hard_regset
[hard_regno
][mode
]);
1098 /* Exclude too costly hard regs. */
1099 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
1101 int min_cost
= INT_MAX
;
1104 a
= ira_allocnos
[i
];
1105 if ((aclass
= ALLOCNO_CLASS (a
)) == NO_REGS
1106 || empty_profitable_hard_regs (a
))
1108 data
= ALLOCNO_COLOR_DATA (a
);
1109 mode
= ALLOCNO_MODE (a
);
1110 if ((costs
= ALLOCNO_UPDATED_HARD_REG_COSTS (a
)) != NULL
1111 || (costs
= ALLOCNO_HARD_REG_COSTS (a
)) != NULL
)
1113 class_size
= ira_class_hard_regs_num
[aclass
];
1114 for (j
= 0; j
< class_size
; j
++)
1116 hard_regno
= ira_class_hard_regs
[aclass
][j
];
1117 if (! TEST_HARD_REG_BIT (data
->profitable_hard_regs
,
1120 if (ALLOCNO_UPDATED_MEMORY_COST (a
) < costs
[j
]
1121 /* Do not remove HARD_REGNO for static chain pointer
1122 pseudo when non-local goto is used. */
1123 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a
)))
1124 CLEAR_HARD_REG_BIT (data
->profitable_hard_regs
,
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 /* Do not empty profitable regs for static chain
1133 pointer pseudo when non-local goto is used. */
1134 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a
)))
1135 CLEAR_HARD_REG_SET (data
->profitable_hard_regs
);
1136 if (ALLOCNO_UPDATED_CLASS_COST (a
) > min_cost
)
1137 ALLOCNO_UPDATED_CLASS_COST (a
) = min_cost
;
1143 /* This page contains functions used to choose hard registers for
1146 /* Pool for update cost records. */
1147 static object_allocator
<update_cost_record
> update_cost_record_pool
1148 ("update cost records");
1150 /* Return new update cost record with given params. */
1151 static struct update_cost_record
*
1152 get_update_cost_record (int hard_regno
, int divisor
,
1153 struct update_cost_record
*next
)
1155 struct update_cost_record
*record
;
1157 record
= update_cost_record_pool
.allocate ();
1158 record
->hard_regno
= hard_regno
;
1159 record
->divisor
= divisor
;
1160 record
->next
= next
;
1164 /* Free memory for all records in LIST. */
1166 free_update_cost_record_list (struct update_cost_record
*list
)
1168 struct update_cost_record
*next
;
1170 while (list
!= NULL
)
1173 update_cost_record_pool
.remove (list
);
1178 /* Free memory allocated for all update cost records. */
1180 finish_update_cost_records (void)
1182 update_cost_record_pool
.release ();
1185 /* Array whose element value is TRUE if the corresponding hard
1186 register was already allocated for an allocno. */
1187 static bool allocated_hardreg_p
[FIRST_PSEUDO_REGISTER
];
1189 /* Describes one element in a queue of allocnos whose costs need to be
1190 updated. Each allocno in the queue is known to have an allocno
1192 struct update_cost_queue_elem
1194 /* This element is in the queue iff CHECK == update_cost_check. */
1197 /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
1198 connecting this allocno to the one being allocated. */
1201 /* Allocno from which we are chaining costs of connected allocnos.
1202 It is used not go back in graph of allocnos connected by
1206 /* The next allocno in the queue, or null if this is the last element. */
1210 /* The first element in a queue of allocnos whose copy costs need to be
1211 updated. Null if the queue is empty. */
1212 static ira_allocno_t update_cost_queue
;
1214 /* The last element in the queue described by update_cost_queue.
1215 Not valid if update_cost_queue is null. */
1216 static struct update_cost_queue_elem
*update_cost_queue_tail
;
1218 /* A pool of elements in the queue described by update_cost_queue.
1219 Elements are indexed by ALLOCNO_NUM. */
1220 static struct update_cost_queue_elem
*update_cost_queue_elems
;
1222 /* The current value of update_costs_from_copies call count. */
1223 static int update_cost_check
;
1225 /* Allocate and initialize data necessary for function
1226 update_costs_from_copies. */
1228 initiate_cost_update (void)
1232 size
= ira_allocnos_num
* sizeof (struct update_cost_queue_elem
);
1233 update_cost_queue_elems
1234 = (struct update_cost_queue_elem
*) ira_allocate (size
);
1235 memset (update_cost_queue_elems
, 0, size
);
1236 update_cost_check
= 0;
1239 /* Deallocate data used by function update_costs_from_copies. */
1241 finish_cost_update (void)
1243 ira_free (update_cost_queue_elems
);
1244 finish_update_cost_records ();
1247 /* When we traverse allocnos to update hard register costs, the cost
1248 divisor will be multiplied by the following macro value for each
1249 hop from given allocno to directly connected allocnos. */
1250 #define COST_HOP_DIVISOR 4
1252 /* Start a new cost-updating pass. */
1254 start_update_cost (void)
1256 update_cost_check
++;
1257 update_cost_queue
= NULL
;
1260 /* Add (ALLOCNO, FROM, DIVISOR) to the end of update_cost_queue, unless
1261 ALLOCNO is already in the queue, or has NO_REGS class. */
1263 queue_update_cost (ira_allocno_t allocno
, ira_allocno_t from
, int divisor
)
1265 struct update_cost_queue_elem
*elem
;
1267 elem
= &update_cost_queue_elems
[ALLOCNO_NUM (allocno
)];
1268 if (elem
->check
!= update_cost_check
1269 && ALLOCNO_CLASS (allocno
) != NO_REGS
)
1271 elem
->check
= update_cost_check
;
1273 elem
->divisor
= divisor
;
1275 if (update_cost_queue
== NULL
)
1276 update_cost_queue
= allocno
;
1278 update_cost_queue_tail
->next
= allocno
;
1279 update_cost_queue_tail
= elem
;
1283 /* Try to remove the first element from update_cost_queue. Return
1284 false if the queue was empty, otherwise make (*ALLOCNO, *FROM,
1285 *DIVISOR) describe the removed element. */
1287 get_next_update_cost (ira_allocno_t
*allocno
, ira_allocno_t
*from
, int *divisor
)
1289 struct update_cost_queue_elem
*elem
;
1291 if (update_cost_queue
== NULL
)
1294 *allocno
= update_cost_queue
;
1295 elem
= &update_cost_queue_elems
[ALLOCNO_NUM (*allocno
)];
1297 *divisor
= elem
->divisor
;
1298 update_cost_queue
= elem
->next
;
1302 /* Increase costs of HARD_REGNO by UPDATE_COST and conflict cost by
1303 UPDATE_CONFLICT_COST for ALLOCNO. Return true if we really
1304 modified the cost. */
1306 update_allocno_cost (ira_allocno_t allocno
, int hard_regno
,
1307 int update_cost
, int update_conflict_cost
)
1310 enum reg_class aclass
= ALLOCNO_CLASS (allocno
);
1312 i
= ira_class_hard_reg_index
[aclass
][hard_regno
];
1315 ira_allocate_and_set_or_copy_costs
1316 (&ALLOCNO_UPDATED_HARD_REG_COSTS (allocno
), aclass
,
1317 ALLOCNO_UPDATED_CLASS_COST (allocno
),
1318 ALLOCNO_HARD_REG_COSTS (allocno
));
1319 ira_allocate_and_set_or_copy_costs
1320 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno
),
1321 aclass
, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (allocno
));
1322 ALLOCNO_UPDATED_HARD_REG_COSTS (allocno
)[i
] += update_cost
;
1323 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno
)[i
] += update_conflict_cost
;
1327 /* Update (decrease if DECR_P) HARD_REGNO cost of allocnos connected
1328 by copies to ALLOCNO to increase chances to remove some copies as
1329 the result of subsequent assignment. Record cost updates if
1330 RECORD_P is true. */
1332 update_costs_from_allocno (ira_allocno_t allocno
, int hard_regno
,
1333 int divisor
, bool decr_p
, bool record_p
)
1335 int cost
, update_cost
, update_conflict_cost
;
1337 enum reg_class rclass
, aclass
;
1338 ira_allocno_t another_allocno
, from
= NULL
;
1339 ira_copy_t cp
, next_cp
;
1341 rclass
= REGNO_REG_CLASS (hard_regno
);
1344 mode
= ALLOCNO_MODE (allocno
);
1345 ira_init_register_move_cost_if_necessary (mode
);
1346 for (cp
= ALLOCNO_COPIES (allocno
); cp
!= NULL
; cp
= next_cp
)
1348 if (cp
->first
== allocno
)
1350 next_cp
= cp
->next_first_allocno_copy
;
1351 another_allocno
= cp
->second
;
1353 else if (cp
->second
== allocno
)
1355 next_cp
= cp
->next_second_allocno_copy
;
1356 another_allocno
= cp
->first
;
1361 if (another_allocno
== from
)
1364 aclass
= ALLOCNO_CLASS (another_allocno
);
1365 if (! TEST_HARD_REG_BIT (reg_class_contents
[aclass
],
1367 || ALLOCNO_ASSIGNED_P (another_allocno
))
1370 cost
= (cp
->second
== allocno
1371 ? ira_register_move_cost
[mode
][rclass
][aclass
]
1372 : ira_register_move_cost
[mode
][aclass
][rclass
]);
1376 update_conflict_cost
= update_cost
= cp
->freq
* cost
/ divisor
;
1378 if (ALLOCNO_COLOR_DATA (another_allocno
) != NULL
1379 && (ALLOCNO_COLOR_DATA (allocno
)->first_thread_allocno
1380 != ALLOCNO_COLOR_DATA (another_allocno
)->first_thread_allocno
))
1381 /* Decrease conflict cost of ANOTHER_ALLOCNO if it is not
1382 in the same allocation thread. */
1383 update_conflict_cost
/= COST_HOP_DIVISOR
;
1385 if (update_cost
== 0)
1388 if (! update_allocno_cost (another_allocno
, hard_regno
,
1389 update_cost
, update_conflict_cost
))
1391 queue_update_cost (another_allocno
, allocno
, divisor
* COST_HOP_DIVISOR
);
1392 if (record_p
&& ALLOCNO_COLOR_DATA (another_allocno
) != NULL
)
1393 ALLOCNO_COLOR_DATA (another_allocno
)->update_cost_records
1394 = get_update_cost_record (hard_regno
, divisor
,
1395 ALLOCNO_COLOR_DATA (another_allocno
)
1396 ->update_cost_records
);
1399 while (get_next_update_cost (&allocno
, &from
, &divisor
));
1402 /* Decrease preferred ALLOCNO hard register costs and costs of
1403 allocnos connected to ALLOCNO through copy. */
1405 update_costs_from_prefs (ira_allocno_t allocno
)
1409 start_update_cost ();
1410 for (pref
= ALLOCNO_PREFS (allocno
); pref
!= NULL
; pref
= pref
->next_pref
)
1411 update_costs_from_allocno (allocno
, pref
->hard_regno
,
1412 COST_HOP_DIVISOR
, true, true);
1415 /* Update (decrease if DECR_P) the cost of allocnos connected to
1416 ALLOCNO through copies to increase chances to remove some copies as
1417 the result of subsequent assignment. ALLOCNO was just assigned to
1418 a hard register. Record cost updates if RECORD_P is true. */
1420 update_costs_from_copies (ira_allocno_t allocno
, bool decr_p
, bool record_p
)
1424 hard_regno
= ALLOCNO_HARD_REGNO (allocno
);
1425 ira_assert (hard_regno
>= 0 && ALLOCNO_CLASS (allocno
) != NO_REGS
);
1426 start_update_cost ();
1427 update_costs_from_allocno (allocno
, hard_regno
, 1, decr_p
, record_p
);
1430 /* Restore costs of allocnos connected to ALLOCNO by copies as it was
1431 before updating costs of these allocnos from given allocno. This
1432 is a wise thing to do as if given allocno did not get an expected
1433 hard reg, using smaller cost of the hard reg for allocnos connected
1434 by copies to given allocno becomes actually misleading. Free all
1435 update cost records for ALLOCNO as we don't need them anymore. */
1437 restore_costs_from_copies (ira_allocno_t allocno
)
1439 struct update_cost_record
*records
, *curr
;
1441 if (ALLOCNO_COLOR_DATA (allocno
) == NULL
)
1443 records
= ALLOCNO_COLOR_DATA (allocno
)->update_cost_records
;
1444 start_update_cost ();
1445 for (curr
= records
; curr
!= NULL
; curr
= curr
->next
)
1446 update_costs_from_allocno (allocno
, curr
->hard_regno
,
1447 curr
->divisor
, true, false);
1448 free_update_cost_record_list (records
);
1449 ALLOCNO_COLOR_DATA (allocno
)->update_cost_records
= NULL
;
1452 /* This function updates COSTS (decrease if DECR_P) for hard_registers
1453 of ACLASS by conflict costs of the unassigned allocnos
1454 connected by copies with allocnos in update_cost_queue. This
1455 update increases chances to remove some copies. */
1457 update_conflict_hard_regno_costs (int *costs
, enum reg_class aclass
,
1460 int i
, cost
, class_size
, freq
, mult
, div
, divisor
;
1461 int index
, hard_regno
;
1462 int *conflict_costs
;
1464 enum reg_class another_aclass
;
1465 ira_allocno_t allocno
, another_allocno
, from
;
1466 ira_copy_t cp
, next_cp
;
1468 while (get_next_update_cost (&allocno
, &from
, &divisor
))
1469 for (cp
= ALLOCNO_COPIES (allocno
); cp
!= NULL
; cp
= next_cp
)
1471 if (cp
->first
== allocno
)
1473 next_cp
= cp
->next_first_allocno_copy
;
1474 another_allocno
= cp
->second
;
1476 else if (cp
->second
== allocno
)
1478 next_cp
= cp
->next_second_allocno_copy
;
1479 another_allocno
= cp
->first
;
1484 if (another_allocno
== from
)
1487 another_aclass
= ALLOCNO_CLASS (another_allocno
);
1488 if (! ira_reg_classes_intersect_p
[aclass
][another_aclass
]
1489 || ALLOCNO_ASSIGNED_P (another_allocno
)
1490 || ALLOCNO_COLOR_DATA (another_allocno
)->may_be_spilled_p
)
1492 class_size
= ira_class_hard_regs_num
[another_aclass
];
1493 ira_allocate_and_copy_costs
1494 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno
),
1495 another_aclass
, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno
));
1497 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno
);
1498 if (conflict_costs
== NULL
)
1503 freq
= ALLOCNO_FREQ (another_allocno
);
1506 div
= freq
* divisor
;
1508 for (i
= class_size
- 1; i
>= 0; i
--)
1510 hard_regno
= ira_class_hard_regs
[another_aclass
][i
];
1511 ira_assert (hard_regno
>= 0);
1512 index
= ira_class_hard_reg_index
[aclass
][hard_regno
];
1515 cost
= (int) ((unsigned) conflict_costs
[i
] * mult
) / div
;
1521 costs
[index
] += cost
;
1524 /* Probably 5 hops will be enough. */
1526 && divisor
<= (COST_HOP_DIVISOR
1529 * COST_HOP_DIVISOR
))
1530 queue_update_cost (another_allocno
, allocno
, divisor
* COST_HOP_DIVISOR
);
1534 /* Set up conflicting (through CONFLICT_REGS) for each object of
1535 allocno A and the start allocno profitable regs (through
1536 START_PROFITABLE_REGS). Remember that the start profitable regs
1537 exclude hard regs which can not hold value of mode of allocno A.
1538 This covers mostly cases when multi-register value should be
1541 get_conflict_and_start_profitable_regs (ira_allocno_t a
, bool retry_p
,
1542 HARD_REG_SET
*conflict_regs
,
1543 HARD_REG_SET
*start_profitable_regs
)
1548 nwords
= ALLOCNO_NUM_OBJECTS (a
);
1549 for (i
= 0; i
< nwords
; i
++)
1551 obj
= ALLOCNO_OBJECT (a
, i
);
1552 COPY_HARD_REG_SET (conflict_regs
[i
],
1553 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
));
1557 COPY_HARD_REG_SET (*start_profitable_regs
,
1558 reg_class_contents
[ALLOCNO_CLASS (a
)]);
1559 AND_COMPL_HARD_REG_SET (*start_profitable_regs
,
1560 ira_prohibited_class_mode_regs
1561 [ALLOCNO_CLASS (a
)][ALLOCNO_MODE (a
)]);
1564 COPY_HARD_REG_SET (*start_profitable_regs
,
1565 ALLOCNO_COLOR_DATA (a
)->profitable_hard_regs
);
1568 /* Return true if HARD_REGNO is ok for assigning to allocno A with
1569 PROFITABLE_REGS and whose objects have CONFLICT_REGS. */
1571 check_hard_reg_p (ira_allocno_t a
, int hard_regno
,
1572 HARD_REG_SET
*conflict_regs
, HARD_REG_SET profitable_regs
)
1574 int j
, nwords
, nregs
;
1575 enum reg_class aclass
;
1578 aclass
= ALLOCNO_CLASS (a
);
1579 mode
= ALLOCNO_MODE (a
);
1580 if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs
[aclass
][mode
],
1583 /* Checking only profitable hard regs. */
1584 if (! TEST_HARD_REG_BIT (profitable_regs
, hard_regno
))
1586 nregs
= hard_regno_nregs
[hard_regno
][mode
];
1587 nwords
= ALLOCNO_NUM_OBJECTS (a
);
1588 for (j
= 0; j
< nregs
; j
++)
1591 int set_to_test_start
= 0, set_to_test_end
= nwords
;
1593 if (nregs
== nwords
)
1595 if (REG_WORDS_BIG_ENDIAN
)
1596 set_to_test_start
= nwords
- j
- 1;
1598 set_to_test_start
= j
;
1599 set_to_test_end
= set_to_test_start
+ 1;
1601 for (k
= set_to_test_start
; k
< set_to_test_end
; k
++)
1602 if (TEST_HARD_REG_BIT (conflict_regs
[k
], hard_regno
+ j
))
1604 if (k
!= set_to_test_end
)
1610 /* Return number of registers needed to be saved and restored at
1611 function prologue/epilogue if we allocate HARD_REGNO to hold value
1614 calculate_saved_nregs (int hard_regno
, machine_mode mode
)
1619 ira_assert (hard_regno
>= 0);
1620 for (i
= hard_regno_nregs
[hard_regno
][mode
] - 1; i
>= 0; i
--)
1621 if (!allocated_hardreg_p
[hard_regno
+ i
]
1622 && !TEST_HARD_REG_BIT (call_used_reg_set
, hard_regno
+ i
)
1623 && !LOCAL_REGNO (hard_regno
+ i
))
1628 /* Choose a hard register for allocno A. If RETRY_P is TRUE, it means
1629 that the function called from function
1630 `ira_reassign_conflict_allocnos' and `allocno_reload_assign'. In
1631 this case some allocno data are not defined or updated and we
1632 should not touch these data. The function returns true if we
1633 managed to assign a hard register to the allocno.
1635 To assign a hard register, first of all we calculate all conflict
1636 hard registers which can come from conflicting allocnos with
1637 already assigned hard registers. After that we find first free
1638 hard register with the minimal cost. During hard register cost
1639 calculation we take conflict hard register costs into account to
1640 give a chance for conflicting allocnos to get a better hard
1641 register in the future.
1643 If the best hard register cost is bigger than cost of memory usage
1644 for the allocno, we don't assign a hard register to given allocno
1647 If we assign a hard register to the allocno, we update costs of the
1648 hard register for allocnos connected by copies to improve a chance
1649 to coalesce insns represented by the copies when we assign hard
1650 registers to the allocnos connected by the copies. */
1652 assign_hard_reg (ira_allocno_t a
, bool retry_p
)
1654 HARD_REG_SET conflicting_regs
[2], profitable_hard_regs
;
1655 int i
, j
, hard_regno
, best_hard_regno
, class_size
;
1656 int cost
, mem_cost
, min_cost
, full_cost
, min_full_cost
, nwords
, word
;
1658 enum reg_class aclass
;
1660 static int costs
[FIRST_PSEUDO_REGISTER
], full_costs
[FIRST_PSEUDO_REGISTER
];
1662 enum reg_class rclass
;
1665 bool no_stack_reg_p
;
1668 ira_assert (! ALLOCNO_ASSIGNED_P (a
));
1669 get_conflict_and_start_profitable_regs (a
, retry_p
,
1671 &profitable_hard_regs
);
1672 aclass
= ALLOCNO_CLASS (a
);
1673 class_size
= ira_class_hard_regs_num
[aclass
];
1674 best_hard_regno
= -1;
1675 memset (full_costs
, 0, sizeof (int) * class_size
);
1677 memset (costs
, 0, sizeof (int) * class_size
);
1678 memset (full_costs
, 0, sizeof (int) * class_size
);
1680 no_stack_reg_p
= false;
1683 start_update_cost ();
1684 mem_cost
+= ALLOCNO_UPDATED_MEMORY_COST (a
);
1686 ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a
),
1687 aclass
, ALLOCNO_HARD_REG_COSTS (a
));
1688 a_costs
= ALLOCNO_UPDATED_HARD_REG_COSTS (a
);
1690 no_stack_reg_p
= no_stack_reg_p
|| ALLOCNO_TOTAL_NO_STACK_REG_P (a
);
1692 cost
= ALLOCNO_UPDATED_CLASS_COST (a
);
1693 for (i
= 0; i
< class_size
; i
++)
1694 if (a_costs
!= NULL
)
1696 costs
[i
] += a_costs
[i
];
1697 full_costs
[i
] += a_costs
[i
];
1702 full_costs
[i
] += cost
;
1704 nwords
= ALLOCNO_NUM_OBJECTS (a
);
1705 curr_allocno_process
++;
1706 for (word
= 0; word
< nwords
; word
++)
1708 ira_object_t conflict_obj
;
1709 ira_object_t obj
= ALLOCNO_OBJECT (a
, word
);
1710 ira_object_conflict_iterator oci
;
1712 /* Take preferences of conflicting allocnos into account. */
1713 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
1715 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
1716 enum reg_class conflict_aclass
;
1717 allocno_color_data_t data
= ALLOCNO_COLOR_DATA (conflict_a
);
1719 /* Reload can give another class so we need to check all
1722 && ((!ALLOCNO_ASSIGNED_P (conflict_a
)
1723 || ALLOCNO_HARD_REGNO (conflict_a
) < 0)
1724 && !(hard_reg_set_intersect_p
1725 (profitable_hard_regs
,
1727 (conflict_a
)->profitable_hard_regs
))))
1729 /* All conflict allocnos are in consideration bitmap
1730 when retry_p is false. It might change in future and
1731 if it happens the assert will be broken. It means
1732 the code should be modified for the new
1734 ira_assert (bitmap_bit_p (consideration_allocno_bitmap
,
1735 ALLOCNO_NUM (conflict_a
)));
1738 conflict_aclass
= ALLOCNO_CLASS (conflict_a
);
1739 ira_assert (ira_reg_classes_intersect_p
1740 [aclass
][conflict_aclass
]);
1741 if (ALLOCNO_ASSIGNED_P (conflict_a
))
1743 hard_regno
= ALLOCNO_HARD_REGNO (conflict_a
);
1745 && (ira_hard_reg_set_intersection_p
1746 (hard_regno
, ALLOCNO_MODE (conflict_a
),
1747 reg_class_contents
[aclass
])))
1749 int n_objects
= ALLOCNO_NUM_OBJECTS (conflict_a
);
1752 mode
= ALLOCNO_MODE (conflict_a
);
1753 conflict_nregs
= hard_regno_nregs
[hard_regno
][mode
];
1754 if (conflict_nregs
== n_objects
&& conflict_nregs
> 1)
1756 int num
= OBJECT_SUBWORD (conflict_obj
);
1758 if (REG_WORDS_BIG_ENDIAN
)
1759 SET_HARD_REG_BIT (conflicting_regs
[word
],
1760 hard_regno
+ n_objects
- num
- 1);
1762 SET_HARD_REG_BIT (conflicting_regs
[word
],
1767 (conflicting_regs
[word
],
1768 ira_reg_mode_hard_regset
[hard_regno
][mode
]);
1769 if (hard_reg_set_subset_p (profitable_hard_regs
,
1770 conflicting_regs
[word
]))
1775 && ! ALLOCNO_COLOR_DATA (conflict_a
)->may_be_spilled_p
1776 /* Don't process the conflict allocno twice. */
1777 && (ALLOCNO_COLOR_DATA (conflict_a
)->last_process
1778 != curr_allocno_process
))
1780 int k
, *conflict_costs
;
1782 ALLOCNO_COLOR_DATA (conflict_a
)->last_process
1783 = curr_allocno_process
;
1784 ira_allocate_and_copy_costs
1785 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a
),
1787 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_a
));
1789 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a
);
1790 if (conflict_costs
!= NULL
)
1791 for (j
= class_size
- 1; j
>= 0; j
--)
1793 hard_regno
= ira_class_hard_regs
[aclass
][j
];
1794 ira_assert (hard_regno
>= 0);
1795 k
= ira_class_hard_reg_index
[conflict_aclass
][hard_regno
];
1797 /* If HARD_REGNO is not available for CONFLICT_A,
1798 the conflict would be ignored, since HARD_REGNO
1799 will never be assigned to CONFLICT_A. */
1800 || !TEST_HARD_REG_BIT (data
->profitable_hard_regs
,
1803 full_costs
[j
] -= conflict_costs
[k
];
1805 queue_update_cost (conflict_a
, NULL
, COST_HOP_DIVISOR
);
1811 /* Take into account preferences of allocnos connected by copies to
1812 the conflict allocnos. */
1813 update_conflict_hard_regno_costs (full_costs
, aclass
, true);
1815 /* Take preferences of allocnos connected by copies into
1819 start_update_cost ();
1820 queue_update_cost (a
, NULL
, COST_HOP_DIVISOR
);
1821 update_conflict_hard_regno_costs (full_costs
, aclass
, false);
1823 min_cost
= min_full_cost
= INT_MAX
;
1824 /* We don't care about giving callee saved registers to allocnos no
1825 living through calls because call clobbered registers are
1826 allocated first (it is usual practice to put them first in
1827 REG_ALLOC_ORDER). */
1828 mode
= ALLOCNO_MODE (a
);
1829 for (i
= 0; i
< class_size
; i
++)
1831 hard_regno
= ira_class_hard_regs
[aclass
][i
];
1834 && FIRST_STACK_REG
<= hard_regno
&& hard_regno
<= LAST_STACK_REG
)
1837 if (! check_hard_reg_p (a
, hard_regno
,
1838 conflicting_regs
, profitable_hard_regs
))
1841 full_cost
= full_costs
[i
];
1842 if (!HONOR_REG_ALLOC_ORDER
)
1844 if ((saved_nregs
= calculate_saved_nregs (hard_regno
, mode
)) != 0)
1845 /* We need to save/restore the hard register in
1846 epilogue/prologue. Therefore we increase the cost. */
1848 rclass
= REGNO_REG_CLASS (hard_regno
);
1849 add_cost
= ((ira_memory_move_cost
[mode
][rclass
][0]
1850 + ira_memory_move_cost
[mode
][rclass
][1])
1851 * saved_nregs
/ hard_regno_nregs
[hard_regno
][mode
] - 1);
1853 full_cost
+= add_cost
;
1856 if (min_cost
> cost
)
1858 if (min_full_cost
> full_cost
)
1860 min_full_cost
= full_cost
;
1861 best_hard_regno
= hard_regno
;
1862 ira_assert (hard_regno
>= 0);
1865 if (min_full_cost
> mem_cost
1866 /* Do not spill static chain pointer pseudo when non-local goto
1868 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a
)))
1870 if (! retry_p
&& internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
1871 fprintf (ira_dump_file
, "(memory is more profitable %d vs %d) ",
1872 mem_cost
, min_full_cost
);
1873 best_hard_regno
= -1;
1876 if (best_hard_regno
>= 0)
1878 for (i
= hard_regno_nregs
[best_hard_regno
][mode
] - 1; i
>= 0; i
--)
1879 allocated_hardreg_p
[best_hard_regno
+ i
] = true;
1882 restore_costs_from_copies (a
);
1883 ALLOCNO_HARD_REGNO (a
) = best_hard_regno
;
1884 ALLOCNO_ASSIGNED_P (a
) = true;
1885 if (best_hard_regno
>= 0)
1886 update_costs_from_copies (a
, true, ! retry_p
);
1887 ira_assert (ALLOCNO_CLASS (a
) == aclass
);
1888 /* We don't need updated costs anymore. */
1889 ira_free_allocno_updated_costs (a
);
1890 return best_hard_regno
>= 0;
1895 /* An array used to sort copies. */
1896 static ira_copy_t
*sorted_copies
;
1898 /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
1899 used to find a conflict for new allocnos or allocnos with the
1900 different allocno classes. */
1902 allocnos_conflict_by_live_ranges_p (ira_allocno_t a1
, ira_allocno_t a2
)
1906 int n1
= ALLOCNO_NUM_OBJECTS (a1
);
1907 int n2
= ALLOCNO_NUM_OBJECTS (a2
);
1911 reg1
= regno_reg_rtx
[ALLOCNO_REGNO (a1
)];
1912 reg2
= regno_reg_rtx
[ALLOCNO_REGNO (a2
)];
1913 if (reg1
!= NULL
&& reg2
!= NULL
1914 && ORIGINAL_REGNO (reg1
) == ORIGINAL_REGNO (reg2
))
1917 for (i
= 0; i
< n1
; i
++)
1919 ira_object_t c1
= ALLOCNO_OBJECT (a1
, i
);
1921 for (j
= 0; j
< n2
; j
++)
1923 ira_object_t c2
= ALLOCNO_OBJECT (a2
, j
);
1925 if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1
),
1926 OBJECT_LIVE_RANGES (c2
)))
1933 /* The function is used to sort copies according to their execution
1936 copy_freq_compare_func (const void *v1p
, const void *v2p
)
1938 ira_copy_t cp1
= *(const ira_copy_t
*) v1p
, cp2
= *(const ira_copy_t
*) v2p
;
1946 /* If frequencies are equal, sort by copies, so that the results of
1947 qsort leave nothing to chance. */
1948 return cp1
->num
- cp2
->num
;
1953 /* Return true if any allocno from thread of A1 conflicts with any
1954 allocno from thread A2. */
1956 allocno_thread_conflict_p (ira_allocno_t a1
, ira_allocno_t a2
)
1958 ira_allocno_t a
, conflict_a
;
1960 for (a
= ALLOCNO_COLOR_DATA (a2
)->next_thread_allocno
;;
1961 a
= ALLOCNO_COLOR_DATA (a
)->next_thread_allocno
)
1963 for (conflict_a
= ALLOCNO_COLOR_DATA (a1
)->next_thread_allocno
;;
1964 conflict_a
= ALLOCNO_COLOR_DATA (conflict_a
)->next_thread_allocno
)
1966 if (allocnos_conflict_by_live_ranges_p (a
, conflict_a
))
1968 if (conflict_a
== a1
)
1977 /* Merge two threads given correspondingly by their first allocnos T1
1978 and T2 (more accurately merging T2 into T1). */
1980 merge_threads (ira_allocno_t t1
, ira_allocno_t t2
)
1982 ira_allocno_t a
, next
, last
;
1984 gcc_assert (t1
!= t2
1985 && ALLOCNO_COLOR_DATA (t1
)->first_thread_allocno
== t1
1986 && ALLOCNO_COLOR_DATA (t2
)->first_thread_allocno
== t2
);
1987 for (last
= t2
, a
= ALLOCNO_COLOR_DATA (t2
)->next_thread_allocno
;;
1988 a
= ALLOCNO_COLOR_DATA (a
)->next_thread_allocno
)
1990 ALLOCNO_COLOR_DATA (a
)->first_thread_allocno
= t1
;
1995 next
= ALLOCNO_COLOR_DATA (t1
)->next_thread_allocno
;
1996 ALLOCNO_COLOR_DATA (t1
)->next_thread_allocno
= t2
;
1997 ALLOCNO_COLOR_DATA (last
)->next_thread_allocno
= next
;
1998 ALLOCNO_COLOR_DATA (t1
)->thread_freq
+= ALLOCNO_COLOR_DATA (t2
)->thread_freq
;
2001 /* Create threads by processing CP_NUM copies from sorted copies. We
2002 process the most expensive copies first. */
2004 form_threads_from_copies (int cp_num
)
2006 ira_allocno_t a
, thread1
, thread2
;
2010 qsort (sorted_copies
, cp_num
, sizeof (ira_copy_t
), copy_freq_compare_func
);
2011 /* Form threads processing copies, most frequently executed
2013 for (; cp_num
!= 0;)
2015 for (i
= 0; i
< cp_num
; i
++)
2017 cp
= sorted_copies
[i
];
2018 thread1
= ALLOCNO_COLOR_DATA (cp
->first
)->first_thread_allocno
;
2019 thread2
= ALLOCNO_COLOR_DATA (cp
->second
)->first_thread_allocno
;
2020 if (thread1
== thread2
)
2022 if (! allocno_thread_conflict_p (thread1
, thread2
))
2024 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2027 " Forming thread by copy %d:a%dr%d-a%dr%d (freq=%d):\n",
2028 cp
->num
, ALLOCNO_NUM (cp
->first
), ALLOCNO_REGNO (cp
->first
),
2029 ALLOCNO_NUM (cp
->second
), ALLOCNO_REGNO (cp
->second
),
2031 merge_threads (thread1
, thread2
);
2032 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2034 thread1
= ALLOCNO_COLOR_DATA (thread1
)->first_thread_allocno
;
2035 fprintf (ira_dump_file
, " Result (freq=%d): a%dr%d(%d)",
2036 ALLOCNO_COLOR_DATA (thread1
)->thread_freq
,
2037 ALLOCNO_NUM (thread1
), ALLOCNO_REGNO (thread1
),
2038 ALLOCNO_FREQ (thread1
));
2039 for (a
= ALLOCNO_COLOR_DATA (thread1
)->next_thread_allocno
;
2041 a
= ALLOCNO_COLOR_DATA (a
)->next_thread_allocno
)
2042 fprintf (ira_dump_file
, " a%dr%d(%d)",
2043 ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
),
2045 fprintf (ira_dump_file
, "\n");
2051 /* Collect the rest of copies. */
2052 for (n
= 0; i
< cp_num
; i
++)
2054 cp
= sorted_copies
[i
];
2055 if (ALLOCNO_COLOR_DATA (cp
->first
)->first_thread_allocno
2056 != ALLOCNO_COLOR_DATA (cp
->second
)->first_thread_allocno
)
2057 sorted_copies
[n
++] = cp
;
2063 /* Create threads by processing copies of all alocnos from BUCKET. We
2064 process the most expensive copies first. */
2066 form_threads_from_bucket (ira_allocno_t bucket
)
2069 ira_copy_t cp
, next_cp
;
2072 for (a
= bucket
; a
!= NULL
; a
= ALLOCNO_COLOR_DATA (a
)->next_bucket_allocno
)
2074 for (cp
= ALLOCNO_COPIES (a
); cp
!= NULL
; cp
= next_cp
)
2078 next_cp
= cp
->next_first_allocno_copy
;
2079 sorted_copies
[cp_num
++] = cp
;
2081 else if (cp
->second
== a
)
2082 next_cp
= cp
->next_second_allocno_copy
;
2087 form_threads_from_copies (cp_num
);
2090 /* Create threads by processing copies of colorable allocno A. We
2091 process most expensive copies first. */
2093 form_threads_from_colorable_allocno (ira_allocno_t a
)
2095 ira_allocno_t another_a
;
2096 ira_copy_t cp
, next_cp
;
2099 for (cp
= ALLOCNO_COPIES (a
); cp
!= NULL
; cp
= next_cp
)
2103 next_cp
= cp
->next_first_allocno_copy
;
2104 another_a
= cp
->second
;
2106 else if (cp
->second
== a
)
2108 next_cp
= cp
->next_second_allocno_copy
;
2109 another_a
= cp
->first
;
2113 if ((! ALLOCNO_COLOR_DATA (another_a
)->in_graph_p
2114 && !ALLOCNO_COLOR_DATA (another_a
)->may_be_spilled_p
)
2115 || ALLOCNO_COLOR_DATA (another_a
)->colorable_p
)
2116 sorted_copies
[cp_num
++] = cp
;
2118 form_threads_from_copies (cp_num
);
2121 /* Form initial threads which contain only one allocno. */
2123 init_allocno_threads (void)
2129 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, j
, bi
)
2131 a
= ira_allocnos
[j
];
2132 /* Set up initial thread data: */
2133 ALLOCNO_COLOR_DATA (a
)->first_thread_allocno
2134 = ALLOCNO_COLOR_DATA (a
)->next_thread_allocno
= a
;
2135 ALLOCNO_COLOR_DATA (a
)->thread_freq
= ALLOCNO_FREQ (a
);
2141 /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
2143 /* Bucket of allocnos that can colored currently without spilling. */
2144 static ira_allocno_t colorable_allocno_bucket
;
2146 /* Bucket of allocnos that might be not colored currently without
2148 static ira_allocno_t uncolorable_allocno_bucket
;
2150 /* The current number of allocnos in the uncolorable_bucket. */
2151 static int uncolorable_allocnos_num
;
2153 /* Return the current spill priority of allocno A. The less the
2154 number, the more preferable the allocno for spilling. */
2156 allocno_spill_priority (ira_allocno_t a
)
2158 allocno_color_data_t data
= ALLOCNO_COLOR_DATA (a
);
2161 / (ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
)
2162 * ira_reg_class_max_nregs
[ALLOCNO_CLASS (a
)][ALLOCNO_MODE (a
)]
2166 /* Add allocno A to bucket *BUCKET_PTR. A should be not in a bucket
2169 add_allocno_to_bucket (ira_allocno_t a
, ira_allocno_t
*bucket_ptr
)
2171 ira_allocno_t first_a
;
2172 allocno_color_data_t data
;
2174 if (bucket_ptr
== &uncolorable_allocno_bucket
2175 && ALLOCNO_CLASS (a
) != NO_REGS
)
2177 uncolorable_allocnos_num
++;
2178 ira_assert (uncolorable_allocnos_num
> 0);
2180 first_a
= *bucket_ptr
;
2181 data
= ALLOCNO_COLOR_DATA (a
);
2182 data
->next_bucket_allocno
= first_a
;
2183 data
->prev_bucket_allocno
= NULL
;
2184 if (first_a
!= NULL
)
2185 ALLOCNO_COLOR_DATA (first_a
)->prev_bucket_allocno
= a
;
2189 /* Compare two allocnos to define which allocno should be pushed first
2190 into the coloring stack. If the return is a negative number, the
2191 allocno given by the first parameter will be pushed first. In this
2192 case such allocno has less priority than the second one and the
2193 hard register will be assigned to it after assignment to the second
2194 one. As the result of such assignment order, the second allocno
2195 has a better chance to get the best hard register. */
2197 bucket_allocno_compare_func (const void *v1p
, const void *v2p
)
2199 ira_allocno_t a1
= *(const ira_allocno_t
*) v1p
;
2200 ira_allocno_t a2
= *(const ira_allocno_t
*) v2p
;
2201 int diff
, freq1
, freq2
, a1_num
, a2_num
;
2202 ira_allocno_t t1
= ALLOCNO_COLOR_DATA (a1
)->first_thread_allocno
;
2203 ira_allocno_t t2
= ALLOCNO_COLOR_DATA (a2
)->first_thread_allocno
;
2204 int cl1
= ALLOCNO_CLASS (a1
), cl2
= ALLOCNO_CLASS (a2
);
2206 freq1
= ALLOCNO_COLOR_DATA (t1
)->thread_freq
;
2207 freq2
= ALLOCNO_COLOR_DATA (t2
)->thread_freq
;
2208 if ((diff
= freq1
- freq2
) != 0)
2211 if ((diff
= ALLOCNO_NUM (t2
) - ALLOCNO_NUM (t1
)) != 0)
2214 /* Push pseudos requiring less hard registers first. It means that
2215 we will assign pseudos requiring more hard registers first
2216 avoiding creation small holes in free hard register file into
2217 which the pseudos requiring more hard registers can not fit. */
2218 if ((diff
= (ira_reg_class_max_nregs
[cl1
][ALLOCNO_MODE (a1
)]
2219 - ira_reg_class_max_nregs
[cl2
][ALLOCNO_MODE (a2
)])) != 0)
2222 freq1
= ALLOCNO_FREQ (a1
);
2223 freq2
= ALLOCNO_FREQ (a2
);
2224 if ((diff
= freq1
- freq2
) != 0)
2227 a1_num
= ALLOCNO_COLOR_DATA (a1
)->available_regs_num
;
2228 a2_num
= ALLOCNO_COLOR_DATA (a2
)->available_regs_num
;
2229 if ((diff
= a2_num
- a1_num
) != 0)
2231 return ALLOCNO_NUM (a2
) - ALLOCNO_NUM (a1
);
2234 /* Sort bucket *BUCKET_PTR and return the result through
2237 sort_bucket (ira_allocno_t
*bucket_ptr
,
2238 int (*compare_func
) (const void *, const void *))
2240 ira_allocno_t a
, head
;
2243 for (n
= 0, a
= *bucket_ptr
;
2245 a
= ALLOCNO_COLOR_DATA (a
)->next_bucket_allocno
)
2246 sorted_allocnos
[n
++] = a
;
2249 qsort (sorted_allocnos
, n
, sizeof (ira_allocno_t
), compare_func
);
2251 for (n
--; n
>= 0; n
--)
2253 a
= sorted_allocnos
[n
];
2254 ALLOCNO_COLOR_DATA (a
)->next_bucket_allocno
= head
;
2255 ALLOCNO_COLOR_DATA (a
)->prev_bucket_allocno
= NULL
;
2257 ALLOCNO_COLOR_DATA (head
)->prev_bucket_allocno
= a
;
2263 /* Add ALLOCNO to colorable bucket maintaining the order according
2264 their priority. ALLOCNO should be not in a bucket before the
2267 add_allocno_to_ordered_colorable_bucket (ira_allocno_t allocno
)
2269 ira_allocno_t before
, after
;
2271 form_threads_from_colorable_allocno (allocno
);
2272 for (before
= colorable_allocno_bucket
, after
= NULL
;
2275 before
= ALLOCNO_COLOR_DATA (before
)->next_bucket_allocno
)
2276 if (bucket_allocno_compare_func (&allocno
, &before
) < 0)
2278 ALLOCNO_COLOR_DATA (allocno
)->next_bucket_allocno
= before
;
2279 ALLOCNO_COLOR_DATA (allocno
)->prev_bucket_allocno
= after
;
2281 colorable_allocno_bucket
= allocno
;
2283 ALLOCNO_COLOR_DATA (after
)->next_bucket_allocno
= allocno
;
2285 ALLOCNO_COLOR_DATA (before
)->prev_bucket_allocno
= allocno
;
2288 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
2291 delete_allocno_from_bucket (ira_allocno_t allocno
, ira_allocno_t
*bucket_ptr
)
2293 ira_allocno_t prev_allocno
, next_allocno
;
2295 if (bucket_ptr
== &uncolorable_allocno_bucket
2296 && ALLOCNO_CLASS (allocno
) != NO_REGS
)
2298 uncolorable_allocnos_num
--;
2299 ira_assert (uncolorable_allocnos_num
>= 0);
2301 prev_allocno
= ALLOCNO_COLOR_DATA (allocno
)->prev_bucket_allocno
;
2302 next_allocno
= ALLOCNO_COLOR_DATA (allocno
)->next_bucket_allocno
;
2303 if (prev_allocno
!= NULL
)
2304 ALLOCNO_COLOR_DATA (prev_allocno
)->next_bucket_allocno
= next_allocno
;
2307 ira_assert (*bucket_ptr
== allocno
);
2308 *bucket_ptr
= next_allocno
;
2310 if (next_allocno
!= NULL
)
2311 ALLOCNO_COLOR_DATA (next_allocno
)->prev_bucket_allocno
= prev_allocno
;
2314 /* Put allocno A onto the coloring stack without removing it from its
2315 bucket. Pushing allocno to the coloring stack can result in moving
2316 conflicting allocnos from the uncolorable bucket to the colorable
2319 push_allocno_to_stack (ira_allocno_t a
)
2321 enum reg_class aclass
;
2322 allocno_color_data_t data
, conflict_data
;
2323 int size
, i
, n
= ALLOCNO_NUM_OBJECTS (a
);
2325 data
= ALLOCNO_COLOR_DATA (a
);
2326 data
->in_graph_p
= false;
2327 allocno_stack_vec
.safe_push (a
);
2328 aclass
= ALLOCNO_CLASS (a
);
2329 if (aclass
== NO_REGS
)
2331 size
= ira_reg_class_max_nregs
[aclass
][ALLOCNO_MODE (a
)];
2334 /* We will deal with the subwords individually. */
2335 gcc_assert (size
== ALLOCNO_NUM_OBJECTS (a
));
2338 for (i
= 0; i
< n
; i
++)
2340 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
2341 ira_object_t conflict_obj
;
2342 ira_object_conflict_iterator oci
;
2344 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
2346 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
2348 conflict_data
= ALLOCNO_COLOR_DATA (conflict_a
);
2349 if (conflict_data
->colorable_p
2350 || ! conflict_data
->in_graph_p
2351 || ALLOCNO_ASSIGNED_P (conflict_a
)
2352 || !(hard_reg_set_intersect_p
2353 (ALLOCNO_COLOR_DATA (a
)->profitable_hard_regs
,
2354 conflict_data
->profitable_hard_regs
)))
2356 ira_assert (bitmap_bit_p (coloring_allocno_bitmap
,
2357 ALLOCNO_NUM (conflict_a
)));
2358 if (update_left_conflict_sizes_p (conflict_a
, a
, size
))
2360 delete_allocno_from_bucket
2361 (conflict_a
, &uncolorable_allocno_bucket
);
2362 add_allocno_to_ordered_colorable_bucket (conflict_a
);
2363 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
2365 fprintf (ira_dump_file
, " Making");
2366 ira_print_expanded_allocno (conflict_a
);
2367 fprintf (ira_dump_file
, " colorable\n");
2375 /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
2376 The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
2378 remove_allocno_from_bucket_and_push (ira_allocno_t allocno
, bool colorable_p
)
2381 delete_allocno_from_bucket (allocno
, &colorable_allocno_bucket
);
2383 delete_allocno_from_bucket (allocno
, &uncolorable_allocno_bucket
);
2384 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2386 fprintf (ira_dump_file
, " Pushing");
2387 ira_print_expanded_allocno (allocno
);
2389 fprintf (ira_dump_file
, "(cost %d)\n",
2390 ALLOCNO_COLOR_DATA (allocno
)->temp
);
2392 fprintf (ira_dump_file
, "(potential spill: %spri=%d, cost=%d)\n",
2393 ALLOCNO_BAD_SPILL_P (allocno
) ? "bad spill, " : "",
2394 allocno_spill_priority (allocno
),
2395 ALLOCNO_COLOR_DATA (allocno
)->temp
);
2398 ALLOCNO_COLOR_DATA (allocno
)->may_be_spilled_p
= true;
2399 push_allocno_to_stack (allocno
);
2402 /* Put all allocnos from colorable bucket onto the coloring stack. */
2404 push_only_colorable (void)
2406 form_threads_from_bucket (colorable_allocno_bucket
);
2407 sort_bucket (&colorable_allocno_bucket
, bucket_allocno_compare_func
);
2408 for (;colorable_allocno_bucket
!= NULL
;)
2409 remove_allocno_from_bucket_and_push (colorable_allocno_bucket
, true);
2412 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
2413 loop given by its LOOP_NODE. */
2415 ira_loop_edge_freq (ira_loop_tree_node_t loop_node
, int regno
, bool exit_p
)
2422 ira_assert (current_loops
!= NULL
&& loop_node
->loop
!= NULL
2423 && (regno
< 0 || regno
>= FIRST_PSEUDO_REGISTER
));
2427 FOR_EACH_EDGE (e
, ei
, loop_node
->loop
->header
->preds
)
2428 if (e
->src
!= loop_node
->loop
->latch
2430 || (bitmap_bit_p (df_get_live_out (e
->src
), regno
)
2431 && bitmap_bit_p (df_get_live_in (e
->dest
), regno
))))
2432 freq
+= EDGE_FREQUENCY (e
);
2436 edges
= get_loop_exit_edges (loop_node
->loop
);
2437 FOR_EACH_VEC_ELT (edges
, i
, e
)
2439 || (bitmap_bit_p (df_get_live_out (e
->src
), regno
)
2440 && bitmap_bit_p (df_get_live_in (e
->dest
), regno
)))
2441 freq
+= EDGE_FREQUENCY (e
);
2445 return REG_FREQ_FROM_EDGE_FREQ (freq
);
2448 /* Calculate and return the cost of putting allocno A into memory. */
2450 calculate_allocno_spill_cost (ira_allocno_t a
)
2454 enum reg_class rclass
;
2455 ira_allocno_t parent_allocno
;
2456 ira_loop_tree_node_t parent_node
, loop_node
;
2458 regno
= ALLOCNO_REGNO (a
);
2459 cost
= ALLOCNO_UPDATED_MEMORY_COST (a
) - ALLOCNO_UPDATED_CLASS_COST (a
);
2460 if (ALLOCNO_CAP (a
) != NULL
)
2462 loop_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2463 if ((parent_node
= loop_node
->parent
) == NULL
)
2465 if ((parent_allocno
= parent_node
->regno_allocno_map
[regno
]) == NULL
)
2467 mode
= ALLOCNO_MODE (a
);
2468 rclass
= ALLOCNO_CLASS (a
);
2469 if (ALLOCNO_HARD_REGNO (parent_allocno
) < 0)
2470 cost
-= (ira_memory_move_cost
[mode
][rclass
][0]
2471 * ira_loop_edge_freq (loop_node
, regno
, true)
2472 + ira_memory_move_cost
[mode
][rclass
][1]
2473 * ira_loop_edge_freq (loop_node
, regno
, false));
2476 ira_init_register_move_cost_if_necessary (mode
);
2477 cost
+= ((ira_memory_move_cost
[mode
][rclass
][1]
2478 * ira_loop_edge_freq (loop_node
, regno
, true)
2479 + ira_memory_move_cost
[mode
][rclass
][0]
2480 * ira_loop_edge_freq (loop_node
, regno
, false))
2481 - (ira_register_move_cost
[mode
][rclass
][rclass
]
2482 * (ira_loop_edge_freq (loop_node
, regno
, false)
2483 + ira_loop_edge_freq (loop_node
, regno
, true))));
2488 /* Used for sorting allocnos for spilling. */
2490 allocno_spill_priority_compare (ira_allocno_t a1
, ira_allocno_t a2
)
2492 int pri1
, pri2
, diff
;
2494 /* Avoid spilling static chain pointer pseudo when non-local goto is
2496 if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1
)))
2498 else if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2
)))
2500 if (ALLOCNO_BAD_SPILL_P (a1
) && ! ALLOCNO_BAD_SPILL_P (a2
))
2502 if (ALLOCNO_BAD_SPILL_P (a2
) && ! ALLOCNO_BAD_SPILL_P (a1
))
2504 pri1
= allocno_spill_priority (a1
);
2505 pri2
= allocno_spill_priority (a2
);
2506 if ((diff
= pri1
- pri2
) != 0)
2509 = ALLOCNO_COLOR_DATA (a1
)->temp
- ALLOCNO_COLOR_DATA (a2
)->temp
) != 0)
2511 return ALLOCNO_NUM (a1
) - ALLOCNO_NUM (a2
);
2514 /* Used for sorting allocnos for spilling. */
2516 allocno_spill_sort_compare (const void *v1p
, const void *v2p
)
2518 ira_allocno_t p1
= *(const ira_allocno_t
*) v1p
;
2519 ira_allocno_t p2
= *(const ira_allocno_t
*) v2p
;
2521 return allocno_spill_priority_compare (p1
, p2
);
2524 /* Push allocnos to the coloring stack. The order of allocnos in the
2525 stack defines the order for the subsequent coloring. */
2527 push_allocnos_to_stack (void)
2532 /* Calculate uncolorable allocno spill costs. */
2533 for (a
= uncolorable_allocno_bucket
;
2535 a
= ALLOCNO_COLOR_DATA (a
)->next_bucket_allocno
)
2536 if (ALLOCNO_CLASS (a
) != NO_REGS
)
2538 cost
= calculate_allocno_spill_cost (a
);
2539 /* ??? Remove cost of copies between the coalesced
2541 ALLOCNO_COLOR_DATA (a
)->temp
= cost
;
2543 sort_bucket (&uncolorable_allocno_bucket
, allocno_spill_sort_compare
);
2546 push_only_colorable ();
2547 a
= uncolorable_allocno_bucket
;
2550 remove_allocno_from_bucket_and_push (a
, false);
2552 ira_assert (colorable_allocno_bucket
== NULL
2553 && uncolorable_allocno_bucket
== NULL
);
2554 ira_assert (uncolorable_allocnos_num
== 0);
2557 /* Pop the coloring stack and assign hard registers to the popped
2560 pop_allocnos_from_stack (void)
2562 ira_allocno_t allocno
;
2563 enum reg_class aclass
;
2565 for (;allocno_stack_vec
.length () != 0;)
2567 allocno
= allocno_stack_vec
.pop ();
2568 aclass
= ALLOCNO_CLASS (allocno
);
2569 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2571 fprintf (ira_dump_file
, " Popping");
2572 ira_print_expanded_allocno (allocno
);
2573 fprintf (ira_dump_file
, " -- ");
2575 if (aclass
== NO_REGS
)
2577 ALLOCNO_HARD_REGNO (allocno
) = -1;
2578 ALLOCNO_ASSIGNED_P (allocno
) = true;
2579 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno
) == NULL
);
2581 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno
) == NULL
);
2582 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2583 fprintf (ira_dump_file
, "assign memory\n");
2585 else if (assign_hard_reg (allocno
, false))
2587 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2588 fprintf (ira_dump_file
, "assign reg %d\n",
2589 ALLOCNO_HARD_REGNO (allocno
));
2591 else if (ALLOCNO_ASSIGNED_P (allocno
))
2593 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2594 fprintf (ira_dump_file
, "spill%s\n",
2595 ALLOCNO_COLOR_DATA (allocno
)->may_be_spilled_p
2598 ALLOCNO_COLOR_DATA (allocno
)->in_graph_p
= true;
2602 /* Set up number of available hard registers for allocno A. */
2604 setup_allocno_available_regs_num (ira_allocno_t a
)
2606 int i
, n
, hard_regno
, hard_regs_num
, nwords
;
2607 enum reg_class aclass
;
2608 allocno_color_data_t data
;
2610 aclass
= ALLOCNO_CLASS (a
);
2611 data
= ALLOCNO_COLOR_DATA (a
);
2612 data
->available_regs_num
= 0;
2613 if (aclass
== NO_REGS
)
2615 hard_regs_num
= ira_class_hard_regs_num
[aclass
];
2616 nwords
= ALLOCNO_NUM_OBJECTS (a
);
2617 for (n
= 0, i
= hard_regs_num
- 1; i
>= 0; i
--)
2619 hard_regno
= ira_class_hard_regs
[aclass
][i
];
2620 /* Checking only profitable hard regs. */
2621 if (TEST_HARD_REG_BIT (data
->profitable_hard_regs
, hard_regno
))
2624 data
->available_regs_num
= n
;
2625 if (internal_flag_ira_verbose
<= 2 || ira_dump_file
== NULL
)
2629 " Allocno a%dr%d of %s(%d) has %d avail. regs ",
2630 ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
),
2631 reg_class_names
[aclass
], ira_class_hard_regs_num
[aclass
], n
);
2632 print_hard_reg_set (ira_dump_file
, data
->profitable_hard_regs
, false);
2633 fprintf (ira_dump_file
, ", %snode: ",
2634 hard_reg_set_equal_p (data
->profitable_hard_regs
,
2635 data
->hard_regs_node
->hard_regs
->set
)
2637 print_hard_reg_set (ira_dump_file
,
2638 data
->hard_regs_node
->hard_regs
->set
, false);
2639 for (i
= 0; i
< nwords
; i
++)
2641 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
2646 fprintf (ira_dump_file
, ", ");
2647 fprintf (ira_dump_file
, " obj %d", i
);
2649 fprintf (ira_dump_file
, " (confl regs = ");
2650 print_hard_reg_set (ira_dump_file
, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
),
2652 fprintf (ira_dump_file
, ")");
2654 fprintf (ira_dump_file
, "\n");
2657 /* Put ALLOCNO in a bucket corresponding to its number and size of its
2658 conflicting allocnos and hard registers. */
2660 put_allocno_into_bucket (ira_allocno_t allocno
)
2662 ALLOCNO_COLOR_DATA (allocno
)->in_graph_p
= true;
2663 setup_allocno_available_regs_num (allocno
);
2664 if (setup_left_conflict_sizes_p (allocno
))
2665 add_allocno_to_bucket (allocno
, &colorable_allocno_bucket
);
2667 add_allocno_to_bucket (allocno
, &uncolorable_allocno_bucket
);
2670 /* Map: allocno number -> allocno priority. */
2671 static int *allocno_priorities
;
2673 /* Set up priorities for N allocnos in array
2674 CONSIDERATION_ALLOCNOS. */
2676 setup_allocno_priorities (ira_allocno_t
*consideration_allocnos
, int n
)
2678 int i
, length
, nrefs
, priority
, max_priority
, mult
;
2682 for (i
= 0; i
< n
; i
++)
2684 a
= consideration_allocnos
[i
];
2685 nrefs
= ALLOCNO_NREFS (a
);
2686 ira_assert (nrefs
>= 0);
2687 mult
= floor_log2 (ALLOCNO_NREFS (a
)) + 1;
2688 ira_assert (mult
>= 0);
2689 allocno_priorities
[ALLOCNO_NUM (a
)]
2692 * (ALLOCNO_MEMORY_COST (a
) - ALLOCNO_CLASS_COST (a
))
2693 * ira_reg_class_max_nregs
[ALLOCNO_CLASS (a
)][ALLOCNO_MODE (a
)]);
2695 priority
= -priority
;
2696 if (max_priority
< priority
)
2697 max_priority
= priority
;
2699 mult
= max_priority
== 0 ? 1 : INT_MAX
/ max_priority
;
2700 for (i
= 0; i
< n
; i
++)
2702 a
= consideration_allocnos
[i
];
2703 length
= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
);
2704 if (ALLOCNO_NUM_OBJECTS (a
) > 1)
2705 length
/= ALLOCNO_NUM_OBJECTS (a
);
2708 allocno_priorities
[ALLOCNO_NUM (a
)]
2709 = allocno_priorities
[ALLOCNO_NUM (a
)] * mult
/ length
;
2713 /* Sort allocnos according to the profit of usage of a hard register
2714 instead of memory for them. */
2716 allocno_cost_compare_func (const void *v1p
, const void *v2p
)
2718 ira_allocno_t p1
= *(const ira_allocno_t
*) v1p
;
2719 ira_allocno_t p2
= *(const ira_allocno_t
*) v2p
;
2722 c1
= ALLOCNO_UPDATED_MEMORY_COST (p1
) - ALLOCNO_UPDATED_CLASS_COST (p1
);
2723 c2
= ALLOCNO_UPDATED_MEMORY_COST (p2
) - ALLOCNO_UPDATED_CLASS_COST (p2
);
2727 /* If regs are equally good, sort by allocno numbers, so that the
2728 results of qsort leave nothing to chance. */
2729 return ALLOCNO_NUM (p1
) - ALLOCNO_NUM (p2
);
2732 /* Return savings on removed copies when ALLOCNO is assigned to
2735 allocno_copy_cost_saving (ira_allocno_t allocno
, int hard_regno
)
2738 enum machine_mode allocno_mode
= ALLOCNO_MODE (allocno
);
2739 enum reg_class rclass
;
2740 ira_copy_t cp
, next_cp
;
2742 rclass
= REGNO_REG_CLASS (hard_regno
);
2743 if (ira_reg_class_max_nregs
[rclass
][allocno_mode
]
2744 > ira_class_hard_regs_num
[rclass
])
2745 /* For the above condition the cost can be wrong. Use the allocno
2746 class in this case. */
2747 rclass
= ALLOCNO_CLASS (allocno
);
2748 for (cp
= ALLOCNO_COPIES (allocno
); cp
!= NULL
; cp
= next_cp
)
2750 if (cp
->first
== allocno
)
2752 next_cp
= cp
->next_first_allocno_copy
;
2753 if (ALLOCNO_HARD_REGNO (cp
->second
) != hard_regno
)
2756 else if (cp
->second
== allocno
)
2758 next_cp
= cp
->next_second_allocno_copy
;
2759 if (ALLOCNO_HARD_REGNO (cp
->first
) != hard_regno
)
2764 cost
+= cp
->freq
* ira_register_move_cost
[allocno_mode
][rclass
][rclass
];
2769 /* We used Chaitin-Briggs coloring to assign as many pseudos as
2770 possible to hard registers. Let us try to improve allocation with
2771 cost point of view. This function improves the allocation by
2772 spilling some allocnos and assigning the freed hard registers to
2773 other allocnos if it decreases the overall allocation cost. */
2775 improve_allocation (void)
2778 int j
, k
, n
, hregno
, conflict_hregno
, base_cost
, class_size
, word
, nwords
;
2779 int check
, spill_cost
, min_cost
, nregs
, conflict_nregs
, r
, best
;
2781 enum reg_class aclass
;
2784 int costs
[FIRST_PSEUDO_REGISTER
];
2785 HARD_REG_SET conflicting_regs
[2], profitable_hard_regs
;
2789 /* Don't bother to optimize the code with static chain pointer and
2790 non-local goto in order not to spill the chain pointer
2792 if (cfun
->static_chain_decl
&& crtl
->has_nonlocal_goto
)
2794 /* Clear counts used to process conflicting allocnos only once for
2796 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
2797 ALLOCNO_COLOR_DATA (ira_allocnos
[i
])->temp
= 0;
2799 /* Process each allocno and try to assign a hard register to it by
2800 spilling some its conflicting allocnos. */
2801 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
2803 a
= ira_allocnos
[i
];
2804 ALLOCNO_COLOR_DATA (a
)->temp
= 0;
2805 if (empty_profitable_hard_regs (a
))
2808 aclass
= ALLOCNO_CLASS (a
);
2809 allocno_costs
= ALLOCNO_HARD_REG_COSTS (a
);
2810 if ((hregno
= ALLOCNO_HARD_REGNO (a
)) < 0)
2811 base_cost
= ALLOCNO_UPDATED_MEMORY_COST (a
);
2812 else if (allocno_costs
== NULL
)
2813 /* It means that assigning a hard register is not profitable
2814 (we don't waste memory for hard register costs in this
2818 base_cost
= (allocno_costs
[ira_class_hard_reg_index
[aclass
][hregno
]]
2819 - allocno_copy_cost_saving (a
, hregno
));
2821 get_conflict_and_start_profitable_regs (a
, false,
2823 &profitable_hard_regs
);
2824 class_size
= ira_class_hard_regs_num
[aclass
];
2825 /* Set up cost improvement for usage of each profitable hard
2826 register for allocno A. */
2827 for (j
= 0; j
< class_size
; j
++)
2829 hregno
= ira_class_hard_regs
[aclass
][j
];
2830 if (! check_hard_reg_p (a
, hregno
,
2831 conflicting_regs
, profitable_hard_regs
))
2833 ira_assert (ira_class_hard_reg_index
[aclass
][hregno
] == j
);
2834 k
= allocno_costs
== NULL
? 0 : j
;
2835 costs
[hregno
] = (allocno_costs
== NULL
2836 ? ALLOCNO_UPDATED_CLASS_COST (a
) : allocno_costs
[k
]);
2837 costs
[hregno
] -= allocno_copy_cost_saving (a
, hregno
);
2838 costs
[hregno
] -= base_cost
;
2839 if (costs
[hregno
] < 0)
2843 /* There is no chance to improve the allocation cost by
2844 assigning hard register to allocno A even without spilling
2845 conflicting allocnos. */
2847 mode
= ALLOCNO_MODE (a
);
2848 nwords
= ALLOCNO_NUM_OBJECTS (a
);
2849 /* Process each allocno conflicting with A and update the cost
2850 improvement for profitable hard registers of A. To use a
2851 hard register for A we need to spill some conflicting
2852 allocnos and that creates penalty for the cost
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 (ALLOCNO_COLOR_DATA (conflict_a
)->temp
== check
)
2865 /* We already processed this conflicting allocno
2866 because we processed earlier another object of the
2867 conflicting allocno. */
2869 ALLOCNO_COLOR_DATA (conflict_a
)->temp
= check
;
2870 if ((conflict_hregno
= ALLOCNO_HARD_REGNO (conflict_a
)) < 0)
2872 spill_cost
= ALLOCNO_UPDATED_MEMORY_COST (conflict_a
);
2873 k
= (ira_class_hard_reg_index
2874 [ALLOCNO_CLASS (conflict_a
)][conflict_hregno
]);
2875 ira_assert (k
>= 0);
2876 if ((allocno_costs
= ALLOCNO_HARD_REG_COSTS (conflict_a
))
2878 spill_cost
-= allocno_costs
[k
];
2880 spill_cost
-= ALLOCNO_UPDATED_CLASS_COST (conflict_a
);
2882 += allocno_copy_cost_saving (conflict_a
, conflict_hregno
);
2884 = hard_regno_nregs
[conflict_hregno
][ALLOCNO_MODE (conflict_a
)];
2885 for (r
= conflict_hregno
;
2886 r
>= 0 && r
+ hard_regno_nregs
[r
][mode
] > conflict_hregno
;
2888 if (check_hard_reg_p (a
, r
,
2889 conflicting_regs
, profitable_hard_regs
))
2890 costs
[r
] += spill_cost
;
2891 for (r
= conflict_hregno
+ 1;
2892 r
< conflict_hregno
+ conflict_nregs
;
2894 if (check_hard_reg_p (a
, r
,
2895 conflicting_regs
, profitable_hard_regs
))
2896 costs
[r
] += spill_cost
;
2901 /* Now we choose hard register for A which results in highest
2902 allocation cost improvement. */
2903 for (j
= 0; j
< class_size
; j
++)
2905 hregno
= ira_class_hard_regs
[aclass
][j
];
2906 if (check_hard_reg_p (a
, hregno
,
2907 conflicting_regs
, profitable_hard_regs
)
2908 && min_cost
> costs
[hregno
])
2911 min_cost
= costs
[hregno
];
2915 /* We are in a situation when assigning any hard register to A
2916 by spilling some conflicting allocnos does not improve the
2919 nregs
= hard_regno_nregs
[best
][mode
];
2920 /* Now spill conflicting allocnos which contain a hard register
2921 of A when we assign the best chosen hard register to it. */
2922 for (word
= 0; word
< nwords
; word
++)
2924 ira_object_t conflict_obj
;
2925 ira_object_t obj
= ALLOCNO_OBJECT (a
, word
);
2926 ira_object_conflict_iterator oci
;
2928 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
2930 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
2932 if ((conflict_hregno
= ALLOCNO_HARD_REGNO (conflict_a
)) < 0)
2935 = hard_regno_nregs
[conflict_hregno
][ALLOCNO_MODE (conflict_a
)];
2936 if (best
+ nregs
<= conflict_hregno
2937 || conflict_hregno
+ conflict_nregs
<= best
)
2938 /* No intersection. */
2940 ALLOCNO_HARD_REGNO (conflict_a
) = -1;
2941 sorted_allocnos
[n
++] = conflict_a
;
2942 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
2943 fprintf (ira_dump_file
, "Spilling a%dr%d for a%dr%d\n",
2944 ALLOCNO_NUM (conflict_a
), ALLOCNO_REGNO (conflict_a
),
2945 ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
));
2948 /* Assign the best chosen hard register to A. */
2949 ALLOCNO_HARD_REGNO (a
) = best
;
2950 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
2951 fprintf (ira_dump_file
, "Assigning %d to a%dr%d\n",
2952 best
, ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
));
2956 /* We spilled some allocnos to assign their hard registers to other
2957 allocnos. The spilled allocnos are now in array
2958 'sorted_allocnos'. There is still a possibility that some of the
2959 spilled allocnos can get hard registers. So let us try assign
2960 them hard registers again (just a reminder -- function
2961 'assign_hard_reg' assigns hard registers only if it is possible
2962 and profitable). We process the spilled allocnos with biggest
2963 benefit to get hard register first -- see function
2964 'allocno_cost_compare_func'. */
2965 qsort (sorted_allocnos
, n
, sizeof (ira_allocno_t
),
2966 allocno_cost_compare_func
);
2967 for (j
= 0; j
< n
; j
++)
2969 a
= sorted_allocnos
[j
];
2970 ALLOCNO_ASSIGNED_P (a
) = false;
2971 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2973 fprintf (ira_dump_file
, " ");
2974 ira_print_expanded_allocno (a
);
2975 fprintf (ira_dump_file
, " -- ");
2977 if (assign_hard_reg (a
, false))
2979 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2980 fprintf (ira_dump_file
, "assign hard reg %d\n",
2981 ALLOCNO_HARD_REGNO (a
));
2985 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2986 fprintf (ira_dump_file
, "assign memory\n");
2991 /* Sort allocnos according to their priorities. */
2993 allocno_priority_compare_func (const void *v1p
, const void *v2p
)
2995 ira_allocno_t a1
= *(const ira_allocno_t
*) v1p
;
2996 ira_allocno_t a2
= *(const ira_allocno_t
*) v2p
;
2999 /* Assign hard reg to static chain pointer pseudo first when
3000 non-local goto is used. */
3001 if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1
)))
3003 else if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2
)))
3005 pri1
= allocno_priorities
[ALLOCNO_NUM (a1
)];
3006 pri2
= allocno_priorities
[ALLOCNO_NUM (a2
)];
3008 return SORTGT (pri2
, pri1
);
3010 /* If regs are equally good, sort by allocnos, so that the results of
3011 qsort leave nothing to chance. */
3012 return ALLOCNO_NUM (a1
) - ALLOCNO_NUM (a2
);
3015 /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
3016 taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
3018 color_allocnos (void)
3024 setup_profitable_hard_regs ();
3025 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
3028 HARD_REG_SET conflict_hard_regs
;
3029 allocno_color_data_t data
;
3030 ira_pref_t pref
, next_pref
;
3032 a
= ira_allocnos
[i
];
3033 nr
= ALLOCNO_NUM_OBJECTS (a
);
3034 CLEAR_HARD_REG_SET (conflict_hard_regs
);
3035 for (l
= 0; l
< nr
; l
++)
3037 ira_object_t obj
= ALLOCNO_OBJECT (a
, l
);
3038 IOR_HARD_REG_SET (conflict_hard_regs
,
3039 OBJECT_CONFLICT_HARD_REGS (obj
));
3041 data
= ALLOCNO_COLOR_DATA (a
);
3042 for (pref
= ALLOCNO_PREFS (a
); pref
!= NULL
; pref
= next_pref
)
3044 next_pref
= pref
->next_pref
;
3045 if (! ira_hard_reg_in_set_p (pref
->hard_regno
,
3047 data
->profitable_hard_regs
))
3048 ira_remove_pref (pref
);
3051 if (flag_ira_algorithm
== IRA_ALGORITHM_PRIORITY
)
3054 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
3056 a
= ira_allocnos
[i
];
3057 if (ALLOCNO_CLASS (a
) == NO_REGS
)
3059 ALLOCNO_HARD_REGNO (a
) = -1;
3060 ALLOCNO_ASSIGNED_P (a
) = true;
3061 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
);
3062 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) == NULL
);
3063 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3065 fprintf (ira_dump_file
, " Spill");
3066 ira_print_expanded_allocno (a
);
3067 fprintf (ira_dump_file
, "\n");
3071 sorted_allocnos
[n
++] = a
;
3075 setup_allocno_priorities (sorted_allocnos
, n
);
3076 qsort (sorted_allocnos
, n
, sizeof (ira_allocno_t
),
3077 allocno_priority_compare_func
);
3078 for (i
= 0; i
< n
; i
++)
3080 a
= sorted_allocnos
[i
];
3081 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3083 fprintf (ira_dump_file
, " ");
3084 ira_print_expanded_allocno (a
);
3085 fprintf (ira_dump_file
, " -- ");
3087 if (assign_hard_reg (a
, false))
3089 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3090 fprintf (ira_dump_file
, "assign hard reg %d\n",
3091 ALLOCNO_HARD_REGNO (a
));
3095 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3096 fprintf (ira_dump_file
, "assign memory\n");
3103 form_allocno_hard_regs_nodes_forest ();
3104 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
3105 print_hard_regs_forest (ira_dump_file
);
3106 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
3108 a
= ira_allocnos
[i
];
3109 if (ALLOCNO_CLASS (a
) != NO_REGS
&& ! empty_profitable_hard_regs (a
))
3111 ALLOCNO_COLOR_DATA (a
)->in_graph_p
= true;
3112 update_costs_from_prefs (a
);
3116 ALLOCNO_HARD_REGNO (a
) = -1;
3117 ALLOCNO_ASSIGNED_P (a
) = true;
3118 /* We don't need updated costs anymore. */
3119 ira_free_allocno_updated_costs (a
);
3120 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3122 fprintf (ira_dump_file
, " Spill");
3123 ira_print_expanded_allocno (a
);
3124 fprintf (ira_dump_file
, "\n");
3128 /* Put the allocnos into the corresponding buckets. */
3129 colorable_allocno_bucket
= NULL
;
3130 uncolorable_allocno_bucket
= NULL
;
3131 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
3133 a
= ira_allocnos
[i
];
3134 if (ALLOCNO_COLOR_DATA (a
)->in_graph_p
)
3135 put_allocno_into_bucket (a
);
3137 push_allocnos_to_stack ();
3138 pop_allocnos_from_stack ();
3139 finish_allocno_hard_regs_nodes_forest ();
3141 improve_allocation ();
3146 /* Output information about the loop given by its LOOP_TREE_NODE. */
3148 print_loop_title (ira_loop_tree_node_t loop_tree_node
)
3152 ira_loop_tree_node_t subloop_node
, dest_loop_node
;
3156 if (loop_tree_node
->parent
== NULL
)
3157 fprintf (ira_dump_file
,
3158 "\n Loop 0 (parent -1, header bb%d, depth 0)\n bbs:",
3162 ira_assert (current_loops
!= NULL
&& loop_tree_node
->loop
!= NULL
);
3163 fprintf (ira_dump_file
,
3164 "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
3165 loop_tree_node
->loop_num
, loop_tree_node
->parent
->loop_num
,
3166 loop_tree_node
->loop
->header
->index
,
3167 loop_depth (loop_tree_node
->loop
));
3169 for (subloop_node
= loop_tree_node
->children
;
3170 subloop_node
!= NULL
;
3171 subloop_node
= subloop_node
->next
)
3172 if (subloop_node
->bb
!= NULL
)
3174 fprintf (ira_dump_file
, " %d", subloop_node
->bb
->index
);
3175 FOR_EACH_EDGE (e
, ei
, subloop_node
->bb
->succs
)
3176 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
3177 && ((dest_loop_node
= IRA_BB_NODE (e
->dest
)->parent
)
3179 fprintf (ira_dump_file
, "(->%d:l%d)",
3180 e
->dest
->index
, dest_loop_node
->loop_num
);
3182 fprintf (ira_dump_file
, "\n all:");
3183 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node
->all_allocnos
, 0, j
, bi
)
3184 fprintf (ira_dump_file
, " %dr%d", j
, ALLOCNO_REGNO (ira_allocnos
[j
]));
3185 fprintf (ira_dump_file
, "\n modified regnos:");
3186 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node
->modified_regnos
, 0, j
, bi
)
3187 fprintf (ira_dump_file
, " %d", j
);
3188 fprintf (ira_dump_file
, "\n border:");
3189 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node
->border_allocnos
, 0, j
, bi
)
3190 fprintf (ira_dump_file
, " %dr%d", j
, ALLOCNO_REGNO (ira_allocnos
[j
]));
3191 fprintf (ira_dump_file
, "\n Pressure:");
3192 for (j
= 0; (int) j
< ira_pressure_classes_num
; j
++)
3194 enum reg_class pclass
;
3196 pclass
= ira_pressure_classes
[j
];
3197 if (loop_tree_node
->reg_pressure
[pclass
] == 0)
3199 fprintf (ira_dump_file
, " %s=%d", reg_class_names
[pclass
],
3200 loop_tree_node
->reg_pressure
[pclass
]);
3202 fprintf (ira_dump_file
, "\n");
3205 /* Color the allocnos inside loop (in the extreme case it can be all
3206 of the function) given the corresponding LOOP_TREE_NODE. The
3207 function is called for each loop during top-down traverse of the
3210 color_pass (ira_loop_tree_node_t loop_tree_node
)
3212 int regno
, hard_regno
, index
= -1, n
;
3213 int cost
, exit_freq
, enter_freq
;
3217 enum reg_class rclass
, aclass
, pclass
;
3218 ira_allocno_t a
, subloop_allocno
;
3219 ira_loop_tree_node_t subloop_node
;
3221 ira_assert (loop_tree_node
->bb
== NULL
);
3222 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
3223 print_loop_title (loop_tree_node
);
3225 bitmap_copy (coloring_allocno_bitmap
, loop_tree_node
->all_allocnos
);
3226 bitmap_copy (consideration_allocno_bitmap
, coloring_allocno_bitmap
);
3228 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, j
, bi
)
3230 a
= ira_allocnos
[j
];
3232 if (! ALLOCNO_ASSIGNED_P (a
))
3234 bitmap_clear_bit (coloring_allocno_bitmap
, ALLOCNO_NUM (a
));
3237 = (allocno_color_data_t
) ira_allocate (sizeof (struct allocno_color_data
)
3239 memset (allocno_color_data
, 0, sizeof (struct allocno_color_data
) * n
);
3240 curr_allocno_process
= 0;
3242 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, j
, bi
)
3244 a
= ira_allocnos
[j
];
3245 ALLOCNO_ADD_DATA (a
) = allocno_color_data
+ n
;
3248 init_allocno_threads ();
3249 /* Color all mentioned allocnos including transparent ones. */
3251 /* Process caps. They are processed just once. */
3252 if (flag_ira_region
== IRA_REGION_MIXED
3253 || flag_ira_region
== IRA_REGION_ALL
)
3254 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node
->all_allocnos
, 0, j
, bi
)
3256 a
= ira_allocnos
[j
];
3257 if (ALLOCNO_CAP_MEMBER (a
) == NULL
)
3259 /* Remove from processing in the next loop. */
3260 bitmap_clear_bit (consideration_allocno_bitmap
, j
);
3261 rclass
= ALLOCNO_CLASS (a
);
3262 pclass
= ira_pressure_class_translate
[rclass
];
3263 if (flag_ira_region
== IRA_REGION_MIXED
3264 && (loop_tree_node
->reg_pressure
[pclass
]
3265 <= ira_class_hard_regs_num
[pclass
]))
3267 mode
= ALLOCNO_MODE (a
);
3268 hard_regno
= ALLOCNO_HARD_REGNO (a
);
3269 if (hard_regno
>= 0)
3271 index
= ira_class_hard_reg_index
[rclass
][hard_regno
];
3272 ira_assert (index
>= 0);
3274 regno
= ALLOCNO_REGNO (a
);
3275 subloop_allocno
= ALLOCNO_CAP_MEMBER (a
);
3276 subloop_node
= ALLOCNO_LOOP_TREE_NODE (subloop_allocno
);
3277 ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno
));
3278 ALLOCNO_HARD_REGNO (subloop_allocno
) = hard_regno
;
3279 ALLOCNO_ASSIGNED_P (subloop_allocno
) = true;
3280 if (hard_regno
>= 0)
3281 update_costs_from_copies (subloop_allocno
, true, true);
3282 /* We don't need updated costs anymore. */
3283 ira_free_allocno_updated_costs (subloop_allocno
);
3286 /* Update costs of the corresponding allocnos (not caps) in the
3288 for (subloop_node
= loop_tree_node
->subloops
;
3289 subloop_node
!= NULL
;
3290 subloop_node
= subloop_node
->subloop_next
)
3292 ira_assert (subloop_node
->bb
== NULL
);
3293 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, j
, bi
)
3295 a
= ira_allocnos
[j
];
3296 ira_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
3297 mode
= ALLOCNO_MODE (a
);
3298 rclass
= ALLOCNO_CLASS (a
);
3299 pclass
= ira_pressure_class_translate
[rclass
];
3300 hard_regno
= ALLOCNO_HARD_REGNO (a
);
3301 /* Use hard register class here. ??? */
3302 if (hard_regno
>= 0)
3304 index
= ira_class_hard_reg_index
[rclass
][hard_regno
];
3305 ira_assert (index
>= 0);
3307 regno
= ALLOCNO_REGNO (a
);
3308 /* ??? conflict costs */
3309 subloop_allocno
= subloop_node
->regno_allocno_map
[regno
];
3310 if (subloop_allocno
== NULL
3311 || ALLOCNO_CAP (subloop_allocno
) != NULL
)
3313 ira_assert (ALLOCNO_CLASS (subloop_allocno
) == rclass
);
3314 ira_assert (bitmap_bit_p (subloop_node
->all_allocnos
,
3315 ALLOCNO_NUM (subloop_allocno
)));
3316 if ((flag_ira_region
== IRA_REGION_MIXED
3317 && (loop_tree_node
->reg_pressure
[pclass
]
3318 <= ira_class_hard_regs_num
[pclass
]))
3319 || (pic_offset_table_rtx
!= NULL
3320 && regno
== (int) REGNO (pic_offset_table_rtx
))
3321 /* Avoid overlapped multi-registers. Moves between them
3322 might result in wrong code generation. */
3324 && ira_reg_class_max_nregs
[pclass
][mode
] > 1))
3326 if (! ALLOCNO_ASSIGNED_P (subloop_allocno
))
3328 ALLOCNO_HARD_REGNO (subloop_allocno
) = hard_regno
;
3329 ALLOCNO_ASSIGNED_P (subloop_allocno
) = true;
3330 if (hard_regno
>= 0)
3331 update_costs_from_copies (subloop_allocno
, true, true);
3332 /* We don't need updated costs anymore. */
3333 ira_free_allocno_updated_costs (subloop_allocno
);
3337 exit_freq
= ira_loop_edge_freq (subloop_node
, regno
, true);
3338 enter_freq
= ira_loop_edge_freq (subloop_node
, regno
, false);
3339 ira_assert (regno
< ira_reg_equiv_len
);
3340 if (ira_equiv_no_lvalue_p (regno
))
3342 if (! ALLOCNO_ASSIGNED_P (subloop_allocno
))
3344 ALLOCNO_HARD_REGNO (subloop_allocno
) = hard_regno
;
3345 ALLOCNO_ASSIGNED_P (subloop_allocno
) = true;
3346 if (hard_regno
>= 0)
3347 update_costs_from_copies (subloop_allocno
, true, true);
3348 /* We don't need updated costs anymore. */
3349 ira_free_allocno_updated_costs (subloop_allocno
);
3352 else if (hard_regno
< 0)
3354 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno
)
3355 -= ((ira_memory_move_cost
[mode
][rclass
][1] * enter_freq
)
3356 + (ira_memory_move_cost
[mode
][rclass
][0] * exit_freq
));
3360 aclass
= ALLOCNO_CLASS (subloop_allocno
);
3361 ira_init_register_move_cost_if_necessary (mode
);
3362 cost
= (ira_register_move_cost
[mode
][rclass
][rclass
]
3363 * (exit_freq
+ enter_freq
));
3364 ira_allocate_and_set_or_copy_costs
3365 (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno
), aclass
,
3366 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno
),
3367 ALLOCNO_HARD_REG_COSTS (subloop_allocno
));
3368 ira_allocate_and_set_or_copy_costs
3369 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno
),
3370 aclass
, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno
));
3371 ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno
)[index
] -= cost
;
3372 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno
)[index
]
3374 if (ALLOCNO_UPDATED_CLASS_COST (subloop_allocno
)
3375 > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno
)[index
])
3376 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno
)
3377 = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno
)[index
];
3378 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno
)
3379 += (ira_memory_move_cost
[mode
][rclass
][0] * enter_freq
3380 + ira_memory_move_cost
[mode
][rclass
][1] * exit_freq
);
3384 ira_free (allocno_color_data
);
3385 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, j
, bi
)
3387 a
= ira_allocnos
[j
];
3388 ALLOCNO_ADD_DATA (a
) = NULL
;
3392 /* Initialize the common data for coloring and calls functions to do
3393 Chaitin-Briggs and regional coloring. */
3397 coloring_allocno_bitmap
= ira_allocate_bitmap ();
3398 if (internal_flag_ira_verbose
> 0 && ira_dump_file
!= NULL
)
3399 fprintf (ira_dump_file
, "\n**** Allocnos coloring:\n\n");
3401 ira_traverse_loop_tree (false, ira_loop_tree_root
, color_pass
, NULL
);
3403 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
3404 ira_print_disposition (ira_dump_file
);
3406 ira_free_bitmap (coloring_allocno_bitmap
);
3411 /* Move spill/restore code, which are to be generated in ira-emit.c,
3412 to less frequent points (if it is profitable) by reassigning some
3413 allocnos (in loop with subloops containing in another loop) to
3414 memory which results in longer live-range where the corresponding
3415 pseudo-registers will be in memory. */
3417 move_spill_restore (void)
3419 int cost
, regno
, hard_regno
, hard_regno2
, index
;
3421 int enter_freq
, exit_freq
;
3423 enum reg_class rclass
;
3424 ira_allocno_t a
, parent_allocno
, subloop_allocno
;
3425 ira_loop_tree_node_t parent
, loop_node
, subloop_node
;
3426 ira_allocno_iterator ai
;
3431 if (internal_flag_ira_verbose
> 0 && ira_dump_file
!= NULL
)
3432 fprintf (ira_dump_file
, "New iteration of spill/restore move\n");
3433 FOR_EACH_ALLOCNO (a
, ai
)
3435 regno
= ALLOCNO_REGNO (a
);
3436 loop_node
= ALLOCNO_LOOP_TREE_NODE (a
);
3437 if (ALLOCNO_CAP_MEMBER (a
) != NULL
3438 || ALLOCNO_CAP (a
) != NULL
3439 || (hard_regno
= ALLOCNO_HARD_REGNO (a
)) < 0
3440 || loop_node
->children
== NULL
3441 /* don't do the optimization because it can create
3442 copies and the reload pass can spill the allocno set
3443 by copy although the allocno will not get memory
3445 || ira_equiv_no_lvalue_p (regno
)
3446 || !bitmap_bit_p (loop_node
->border_allocnos
, ALLOCNO_NUM (a
))
3447 /* Do not spill static chain pointer pseudo when
3448 non-local goto is used. */
3449 || non_spilled_static_chain_regno_p (regno
))
3451 mode
= ALLOCNO_MODE (a
);
3452 rclass
= ALLOCNO_CLASS (a
);
3453 index
= ira_class_hard_reg_index
[rclass
][hard_regno
];
3454 ira_assert (index
>= 0);
3455 cost
= (ALLOCNO_MEMORY_COST (a
)
3456 - (ALLOCNO_HARD_REG_COSTS (a
) == NULL
3457 ? ALLOCNO_CLASS_COST (a
)
3458 : ALLOCNO_HARD_REG_COSTS (a
)[index
]));
3459 ira_init_register_move_cost_if_necessary (mode
);
3460 for (subloop_node
= loop_node
->subloops
;
3461 subloop_node
!= NULL
;
3462 subloop_node
= subloop_node
->subloop_next
)
3464 ira_assert (subloop_node
->bb
== NULL
);
3465 subloop_allocno
= subloop_node
->regno_allocno_map
[regno
];
3466 if (subloop_allocno
== NULL
)
3468 ira_assert (rclass
== ALLOCNO_CLASS (subloop_allocno
));
3469 /* We have accumulated cost. To get the real cost of
3470 allocno usage in the loop we should subtract costs of
3471 the subloop allocnos. */
3472 cost
-= (ALLOCNO_MEMORY_COST (subloop_allocno
)
3473 - (ALLOCNO_HARD_REG_COSTS (subloop_allocno
) == NULL
3474 ? ALLOCNO_CLASS_COST (subloop_allocno
)
3475 : ALLOCNO_HARD_REG_COSTS (subloop_allocno
)[index
]));
3476 exit_freq
= ira_loop_edge_freq (subloop_node
, regno
, true);
3477 enter_freq
= ira_loop_edge_freq (subloop_node
, regno
, false);
3478 if ((hard_regno2
= ALLOCNO_HARD_REGNO (subloop_allocno
)) < 0)
3479 cost
-= (ira_memory_move_cost
[mode
][rclass
][0] * exit_freq
3480 + ira_memory_move_cost
[mode
][rclass
][1] * enter_freq
);
3484 += (ira_memory_move_cost
[mode
][rclass
][0] * exit_freq
3485 + ira_memory_move_cost
[mode
][rclass
][1] * enter_freq
);
3486 if (hard_regno2
!= hard_regno
)
3487 cost
-= (ira_register_move_cost
[mode
][rclass
][rclass
]
3488 * (exit_freq
+ enter_freq
));
3491 if ((parent
= loop_node
->parent
) != NULL
3492 && (parent_allocno
= parent
->regno_allocno_map
[regno
]) != NULL
)
3494 ira_assert (rclass
== ALLOCNO_CLASS (parent_allocno
));
3495 exit_freq
= ira_loop_edge_freq (loop_node
, regno
, true);
3496 enter_freq
= ira_loop_edge_freq (loop_node
, regno
, false);
3497 if ((hard_regno2
= ALLOCNO_HARD_REGNO (parent_allocno
)) < 0)
3498 cost
-= (ira_memory_move_cost
[mode
][rclass
][0] * exit_freq
3499 + ira_memory_move_cost
[mode
][rclass
][1] * enter_freq
);
3503 += (ira_memory_move_cost
[mode
][rclass
][1] * exit_freq
3504 + ira_memory_move_cost
[mode
][rclass
][0] * enter_freq
);
3505 if (hard_regno2
!= hard_regno
)
3506 cost
-= (ira_register_move_cost
[mode
][rclass
][rclass
]
3507 * (exit_freq
+ enter_freq
));
3512 ALLOCNO_HARD_REGNO (a
) = -1;
3513 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3517 " Moving spill/restore for a%dr%d up from loop %d",
3518 ALLOCNO_NUM (a
), regno
, loop_node
->loop_num
);
3519 fprintf (ira_dump_file
, " - profit %d\n", -cost
);
3531 /* Update current hard reg costs and current conflict hard reg costs
3532 for allocno A. It is done by processing its copies containing
3533 other allocnos already assigned. */
3535 update_curr_costs (ira_allocno_t a
)
3537 int i
, hard_regno
, cost
;
3539 enum reg_class aclass
, rclass
;
3540 ira_allocno_t another_a
;
3541 ira_copy_t cp
, next_cp
;
3543 ira_free_allocno_updated_costs (a
);
3544 ira_assert (! ALLOCNO_ASSIGNED_P (a
));
3545 aclass
= ALLOCNO_CLASS (a
);
3546 if (aclass
== NO_REGS
)
3548 mode
= ALLOCNO_MODE (a
);
3549 ira_init_register_move_cost_if_necessary (mode
);
3550 for (cp
= ALLOCNO_COPIES (a
); cp
!= NULL
; cp
= next_cp
)
3554 next_cp
= cp
->next_first_allocno_copy
;
3555 another_a
= cp
->second
;
3557 else if (cp
->second
== a
)
3559 next_cp
= cp
->next_second_allocno_copy
;
3560 another_a
= cp
->first
;
3564 if (! ira_reg_classes_intersect_p
[aclass
][ALLOCNO_CLASS (another_a
)]
3565 || ! ALLOCNO_ASSIGNED_P (another_a
)
3566 || (hard_regno
= ALLOCNO_HARD_REGNO (another_a
)) < 0)
3568 rclass
= REGNO_REG_CLASS (hard_regno
);
3569 i
= ira_class_hard_reg_index
[aclass
][hard_regno
];
3572 cost
= (cp
->first
== a
3573 ? ira_register_move_cost
[mode
][rclass
][aclass
]
3574 : ira_register_move_cost
[mode
][aclass
][rclass
]);
3575 ira_allocate_and_set_or_copy_costs
3576 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a
), aclass
, ALLOCNO_CLASS_COST (a
),
3577 ALLOCNO_HARD_REG_COSTS (a
));
3578 ira_allocate_and_set_or_copy_costs
3579 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
),
3580 aclass
, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a
));
3581 ALLOCNO_UPDATED_HARD_REG_COSTS (a
)[i
] -= cp
->freq
* cost
;
3582 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
)[i
] -= cp
->freq
* cost
;
3586 /* Try to assign hard registers to the unassigned allocnos and
3587 allocnos conflicting with them or conflicting with allocnos whose
3588 regno >= START_REGNO. The function is called after ira_flattening,
3589 so more allocnos (including ones created in ira-emit.c) will have a
3590 chance to get a hard register. We use simple assignment algorithm
3591 based on priorities. */
3593 ira_reassign_conflict_allocnos (int start_regno
)
3595 int i
, allocnos_to_color_num
;
3597 enum reg_class aclass
;
3598 bitmap allocnos_to_color
;
3599 ira_allocno_iterator ai
;
3601 allocnos_to_color
= ira_allocate_bitmap ();
3602 allocnos_to_color_num
= 0;
3603 FOR_EACH_ALLOCNO (a
, ai
)
3605 int n
= ALLOCNO_NUM_OBJECTS (a
);
3607 if (! ALLOCNO_ASSIGNED_P (a
)
3608 && ! bitmap_bit_p (allocnos_to_color
, ALLOCNO_NUM (a
)))
3610 if (ALLOCNO_CLASS (a
) != NO_REGS
)
3611 sorted_allocnos
[allocnos_to_color_num
++] = a
;
3614 ALLOCNO_ASSIGNED_P (a
) = true;
3615 ALLOCNO_HARD_REGNO (a
) = -1;
3616 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
);
3617 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) == NULL
);
3619 bitmap_set_bit (allocnos_to_color
, ALLOCNO_NUM (a
));
3621 if (ALLOCNO_REGNO (a
) < start_regno
3622 || (aclass
= ALLOCNO_CLASS (a
)) == NO_REGS
)
3624 for (i
= 0; i
< n
; i
++)
3626 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
3627 ira_object_t conflict_obj
;
3628 ira_object_conflict_iterator oci
;
3630 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
3632 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
3634 ira_assert (ira_reg_classes_intersect_p
3635 [aclass
][ALLOCNO_CLASS (conflict_a
)]);
3636 if (!bitmap_set_bit (allocnos_to_color
, ALLOCNO_NUM (conflict_a
)))
3638 sorted_allocnos
[allocnos_to_color_num
++] = conflict_a
;
3642 ira_free_bitmap (allocnos_to_color
);
3643 if (allocnos_to_color_num
> 1)
3645 setup_allocno_priorities (sorted_allocnos
, allocnos_to_color_num
);
3646 qsort (sorted_allocnos
, allocnos_to_color_num
, sizeof (ira_allocno_t
),
3647 allocno_priority_compare_func
);
3649 for (i
= 0; i
< allocnos_to_color_num
; i
++)
3651 a
= sorted_allocnos
[i
];
3652 ALLOCNO_ASSIGNED_P (a
) = false;
3653 update_curr_costs (a
);
3655 for (i
= 0; i
< allocnos_to_color_num
; i
++)
3657 a
= sorted_allocnos
[i
];
3658 if (assign_hard_reg (a
, true))
3660 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3663 " Secondary allocation: assign hard reg %d to reg %d\n",
3664 ALLOCNO_HARD_REGNO (a
), ALLOCNO_REGNO (a
));
3671 /* This page contains functions used to find conflicts using allocno
3674 #ifdef ENABLE_IRA_CHECKING
3676 /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
3677 intersect. This should be used when there is only one region.
3678 Currently this is used during reload. */
3680 conflict_by_live_ranges_p (int regno1
, int regno2
)
3682 ira_allocno_t a1
, a2
;
3684 ira_assert (regno1
>= FIRST_PSEUDO_REGISTER
3685 && regno2
>= FIRST_PSEUDO_REGISTER
);
3686 /* Reg info calculated by dataflow infrastructure can be different
3687 from one calculated by regclass. */
3688 if ((a1
= ira_loop_tree_root
->regno_allocno_map
[regno1
]) == NULL
3689 || (a2
= ira_loop_tree_root
->regno_allocno_map
[regno2
]) == NULL
)
3691 return allocnos_conflict_by_live_ranges_p (a1
, a2
);
3698 /* This page contains code to coalesce memory stack slots used by
3699 spilled allocnos. This results in smaller stack frame, better data
3700 locality, and in smaller code for some architectures like
3701 x86/x86_64 where insn size depends on address displacement value.
3702 On the other hand, it can worsen insn scheduling after the RA but
3703 in practice it is less important than smaller stack frames. */
3705 /* TRUE if we coalesced some allocnos. In other words, if we got
3706 loops formed by members first_coalesced_allocno and
3707 next_coalesced_allocno containing more one allocno. */
3708 static bool allocno_coalesced_p
;
3710 /* Bitmap used to prevent a repeated allocno processing because of
3712 static bitmap processed_coalesced_allocno_bitmap
;
3715 typedef struct coalesce_data
*coalesce_data_t
;
3717 /* To decrease footprint of ira_allocno structure we store all data
3718 needed only for coalescing in the following structure. */
3719 struct coalesce_data
3721 /* Coalesced allocnos form a cyclic list. One allocno given by
3722 FIRST represents all coalesced allocnos. The
3723 list is chained by NEXT. */
3724 ira_allocno_t first
;
3729 /* Container for storing allocno data concerning coalescing. */
3730 static coalesce_data_t allocno_coalesce_data
;
3732 /* Macro to access the data concerning coalescing. */
3733 #define ALLOCNO_COALESCE_DATA(a) ((coalesce_data_t) ALLOCNO_ADD_DATA (a))
3735 /* Merge two sets of coalesced allocnos given correspondingly by
3736 allocnos A1 and A2 (more accurately merging A2 set into A1
3739 merge_allocnos (ira_allocno_t a1
, ira_allocno_t a2
)
3741 ira_allocno_t a
, first
, last
, next
;
3743 first
= ALLOCNO_COALESCE_DATA (a1
)->first
;
3744 a
= ALLOCNO_COALESCE_DATA (a2
)->first
;
3747 for (last
= a2
, a
= ALLOCNO_COALESCE_DATA (a2
)->next
;;
3748 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
3750 ALLOCNO_COALESCE_DATA (a
)->first
= first
;
3755 next
= allocno_coalesce_data
[ALLOCNO_NUM (first
)].next
;
3756 allocno_coalesce_data
[ALLOCNO_NUM (first
)].next
= a2
;
3757 allocno_coalesce_data
[ALLOCNO_NUM (last
)].next
= next
;
3760 /* Return TRUE if there are conflicting allocnos from two sets of
3761 coalesced allocnos given correspondingly by allocnos A1 and A2. We
3762 use live ranges to find conflicts because conflicts are represented
3763 only for allocnos of the same allocno class and during the reload
3764 pass we coalesce allocnos for sharing stack memory slots. */
3766 coalesced_allocno_conflict_p (ira_allocno_t a1
, ira_allocno_t a2
)
3768 ira_allocno_t a
, conflict_a
;
3770 if (allocno_coalesced_p
)
3772 bitmap_clear (processed_coalesced_allocno_bitmap
);
3773 for (a
= ALLOCNO_COALESCE_DATA (a1
)->next
;;
3774 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
3776 bitmap_set_bit (processed_coalesced_allocno_bitmap
, ALLOCNO_NUM (a
));
3781 for (a
= ALLOCNO_COALESCE_DATA (a2
)->next
;;
3782 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
3784 for (conflict_a
= ALLOCNO_COALESCE_DATA (a1
)->next
;;
3785 conflict_a
= ALLOCNO_COALESCE_DATA (conflict_a
)->next
)
3787 if (allocnos_conflict_by_live_ranges_p (a
, conflict_a
))
3789 if (conflict_a
== a1
)
3798 /* The major function for aggressive allocno coalescing. We coalesce
3799 only spilled allocnos. If some allocnos have been coalesced, we
3800 set up flag allocno_coalesced_p. */
3802 coalesce_allocnos (void)
3805 ira_copy_t cp
, next_cp
;
3807 int i
, n
, cp_num
, regno
;
3811 /* Collect copies. */
3812 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, j
, bi
)
3814 a
= ira_allocnos
[j
];
3815 regno
= ALLOCNO_REGNO (a
);
3816 if (! ALLOCNO_ASSIGNED_P (a
) || ALLOCNO_HARD_REGNO (a
) >= 0
3817 || ira_equiv_no_lvalue_p (regno
))
3819 for (cp
= ALLOCNO_COPIES (a
); cp
!= NULL
; cp
= next_cp
)
3823 next_cp
= cp
->next_first_allocno_copy
;
3824 regno
= ALLOCNO_REGNO (cp
->second
);
3825 /* For priority coloring we coalesce allocnos only with
3826 the same allocno class not with intersected allocno
3827 classes as it were possible. It is done for
3829 if ((cp
->insn
!= NULL
|| cp
->constraint_p
)
3830 && ALLOCNO_ASSIGNED_P (cp
->second
)
3831 && ALLOCNO_HARD_REGNO (cp
->second
) < 0
3832 && ! ira_equiv_no_lvalue_p (regno
))
3833 sorted_copies
[cp_num
++] = cp
;
3835 else if (cp
->second
== a
)
3836 next_cp
= cp
->next_second_allocno_copy
;
3841 qsort (sorted_copies
, cp_num
, sizeof (ira_copy_t
), copy_freq_compare_func
);
3842 /* Coalesced copies, most frequently executed first. */
3843 for (; cp_num
!= 0;)
3845 for (i
= 0; i
< cp_num
; i
++)
3847 cp
= sorted_copies
[i
];
3848 if (! coalesced_allocno_conflict_p (cp
->first
, cp
->second
))
3850 allocno_coalesced_p
= true;
3851 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3854 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
3855 cp
->num
, ALLOCNO_NUM (cp
->first
), ALLOCNO_REGNO (cp
->first
),
3856 ALLOCNO_NUM (cp
->second
), ALLOCNO_REGNO (cp
->second
),
3858 merge_allocnos (cp
->first
, cp
->second
);
3863 /* Collect the rest of copies. */
3864 for (n
= 0; i
< cp_num
; i
++)
3866 cp
= sorted_copies
[i
];
3867 if (allocno_coalesce_data
[ALLOCNO_NUM (cp
->first
)].first
3868 != allocno_coalesce_data
[ALLOCNO_NUM (cp
->second
)].first
)
3869 sorted_copies
[n
++] = cp
;
3875 /* Usage cost and order number of coalesced allocno set to which
3876 given pseudo register belongs to. */
3877 static int *regno_coalesced_allocno_cost
;
3878 static int *regno_coalesced_allocno_num
;
3880 /* Sort pseudos according frequencies of coalesced allocno sets they
3881 belong to (putting most frequently ones first), and according to
3882 coalesced allocno set order numbers. */
3884 coalesced_pseudo_reg_freq_compare (const void *v1p
, const void *v2p
)
3886 const int regno1
= *(const int *) v1p
;
3887 const int regno2
= *(const int *) v2p
;
3890 if ((diff
= (regno_coalesced_allocno_cost
[regno2
]
3891 - regno_coalesced_allocno_cost
[regno1
])) != 0)
3893 if ((diff
= (regno_coalesced_allocno_num
[regno1
]
3894 - regno_coalesced_allocno_num
[regno2
])) != 0)
3896 return regno1
- regno2
;
3899 /* Widest width in which each pseudo reg is referred to (via subreg).
3900 It is used for sorting pseudo registers. */
3901 static unsigned int *regno_max_ref_width
;
3903 /* Sort pseudos according their slot numbers (putting ones with
3904 smaller numbers first, or last when the frame pointer is not
3907 coalesced_pseudo_reg_slot_compare (const void *v1p
, const void *v2p
)
3909 const int regno1
= *(const int *) v1p
;
3910 const int regno2
= *(const int *) v2p
;
3911 ira_allocno_t a1
= ira_regno_allocno_map
[regno1
];
3912 ira_allocno_t a2
= ira_regno_allocno_map
[regno2
];
3913 int diff
, slot_num1
, slot_num2
;
3914 int total_size1
, total_size2
;
3916 if (a1
== NULL
|| ALLOCNO_HARD_REGNO (a1
) >= 0)
3918 if (a2
== NULL
|| ALLOCNO_HARD_REGNO (a2
) >= 0)
3919 return regno1
- regno2
;
3922 else if (a2
== NULL
|| ALLOCNO_HARD_REGNO (a2
) >= 0)
3924 slot_num1
= -ALLOCNO_HARD_REGNO (a1
);
3925 slot_num2
= -ALLOCNO_HARD_REGNO (a2
);
3926 if ((diff
= slot_num1
- slot_num2
) != 0)
3927 return (frame_pointer_needed
3928 || (!FRAME_GROWS_DOWNWARD
) == STACK_GROWS_DOWNWARD
? diff
: -diff
);
3929 total_size1
= MAX (PSEUDO_REGNO_BYTES (regno1
),
3930 regno_max_ref_width
[regno1
]);
3931 total_size2
= MAX (PSEUDO_REGNO_BYTES (regno2
),
3932 regno_max_ref_width
[regno2
]);
3933 if ((diff
= total_size2
- total_size1
) != 0)
3935 return regno1
- regno2
;
3938 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
3939 for coalesced allocno sets containing allocnos with their regnos
3940 given in array PSEUDO_REGNOS of length N. */
3942 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos
, int n
)
3944 int i
, num
, regno
, cost
;
3945 ira_allocno_t allocno
, a
;
3947 for (num
= i
= 0; i
< n
; i
++)
3949 regno
= pseudo_regnos
[i
];
3950 allocno
= ira_regno_allocno_map
[regno
];
3951 if (allocno
== NULL
)
3953 regno_coalesced_allocno_cost
[regno
] = 0;
3954 regno_coalesced_allocno_num
[regno
] = ++num
;
3957 if (ALLOCNO_COALESCE_DATA (allocno
)->first
!= allocno
)
3960 for (cost
= 0, a
= ALLOCNO_COALESCE_DATA (allocno
)->next
;;
3961 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
3963 cost
+= ALLOCNO_FREQ (a
);
3967 for (a
= ALLOCNO_COALESCE_DATA (allocno
)->next
;;
3968 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
3970 regno_coalesced_allocno_num
[ALLOCNO_REGNO (a
)] = num
;
3971 regno_coalesced_allocno_cost
[ALLOCNO_REGNO (a
)] = cost
;
3978 /* Collect spilled allocnos representing coalesced allocno sets (the
3979 first coalesced allocno). The collected allocnos are returned
3980 through array SPILLED_COALESCED_ALLOCNOS. The function returns the
3981 number of the collected allocnos. The allocnos are given by their
3982 regnos in array PSEUDO_REGNOS of length N. */
3984 collect_spilled_coalesced_allocnos (int *pseudo_regnos
, int n
,
3985 ira_allocno_t
*spilled_coalesced_allocnos
)
3988 ira_allocno_t allocno
;
3990 for (num
= i
= 0; i
< n
; i
++)
3992 regno
= pseudo_regnos
[i
];
3993 allocno
= ira_regno_allocno_map
[regno
];
3994 if (allocno
== NULL
|| ALLOCNO_HARD_REGNO (allocno
) >= 0
3995 || ALLOCNO_COALESCE_DATA (allocno
)->first
!= allocno
)
3997 spilled_coalesced_allocnos
[num
++] = allocno
;
4002 /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
4003 given slot contains live ranges of coalesced allocnos assigned to
4005 static live_range_t
*slot_coalesced_allocnos_live_ranges
;
4007 /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
4008 ranges intersected with live ranges of coalesced allocnos assigned
4009 to slot with number N. */
4011 slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno
, int n
)
4015 for (a
= ALLOCNO_COALESCE_DATA (allocno
)->next
;;
4016 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
4019 int nr
= ALLOCNO_NUM_OBJECTS (a
);
4021 for (i
= 0; i
< nr
; i
++)
4023 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
4025 if (ira_live_ranges_intersect_p
4026 (slot_coalesced_allocnos_live_ranges
[n
],
4027 OBJECT_LIVE_RANGES (obj
)))
4036 /* Update live ranges of slot to which coalesced allocnos represented
4037 by ALLOCNO were assigned. */
4039 setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno
)
4045 n
= ALLOCNO_COALESCE_DATA (allocno
)->temp
;
4046 for (a
= ALLOCNO_COALESCE_DATA (allocno
)->next
;;
4047 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
4049 int nr
= ALLOCNO_NUM_OBJECTS (a
);
4050 for (i
= 0; i
< nr
; i
++)
4052 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
4054 r
= ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj
));
4055 slot_coalesced_allocnos_live_ranges
[n
]
4056 = ira_merge_live_ranges
4057 (slot_coalesced_allocnos_live_ranges
[n
], r
);
4064 /* We have coalesced allocnos involving in copies. Coalesce allocnos
4065 further in order to share the same memory stack slot. Allocnos
4066 representing sets of allocnos coalesced before the call are given
4067 in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
4068 some allocnos were coalesced in the function. */
4070 coalesce_spill_slots (ira_allocno_t
*spilled_coalesced_allocnos
, int num
)
4072 int i
, j
, n
, last_coalesced_allocno_num
;
4073 ira_allocno_t allocno
, a
;
4074 bool merged_p
= false;
4075 bitmap set_jump_crosses
= regstat_get_setjmp_crosses ();
4077 slot_coalesced_allocnos_live_ranges
4078 = (live_range_t
*) ira_allocate (sizeof (live_range_t
) * ira_allocnos_num
);
4079 memset (slot_coalesced_allocnos_live_ranges
, 0,
4080 sizeof (live_range_t
) * ira_allocnos_num
);
4081 last_coalesced_allocno_num
= 0;
4082 /* Coalesce non-conflicting spilled allocnos preferring most
4084 for (i
= 0; i
< num
; i
++)
4086 allocno
= spilled_coalesced_allocnos
[i
];
4087 if (ALLOCNO_COALESCE_DATA (allocno
)->first
!= allocno
4088 || bitmap_bit_p (set_jump_crosses
, ALLOCNO_REGNO (allocno
))
4089 || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno
)))
4091 for (j
= 0; j
< i
; j
++)
4093 a
= spilled_coalesced_allocnos
[j
];
4094 n
= ALLOCNO_COALESCE_DATA (a
)->temp
;
4095 if (ALLOCNO_COALESCE_DATA (a
)->first
== a
4096 && ! bitmap_bit_p (set_jump_crosses
, ALLOCNO_REGNO (a
))
4097 && ! ira_equiv_no_lvalue_p (ALLOCNO_REGNO (a
))
4098 && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno
, n
))
4103 /* No coalescing: set up number for coalesced allocnos
4104 represented by ALLOCNO. */
4105 ALLOCNO_COALESCE_DATA (allocno
)->temp
= last_coalesced_allocno_num
++;
4106 setup_slot_coalesced_allocno_live_ranges (allocno
);
4110 allocno_coalesced_p
= true;
4112 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4113 fprintf (ira_dump_file
,
4114 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
4115 ALLOCNO_NUM (allocno
), ALLOCNO_REGNO (allocno
),
4116 ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
));
4117 ALLOCNO_COALESCE_DATA (allocno
)->temp
4118 = ALLOCNO_COALESCE_DATA (a
)->temp
;
4119 setup_slot_coalesced_allocno_live_ranges (allocno
);
4120 merge_allocnos (a
, allocno
);
4121 ira_assert (ALLOCNO_COALESCE_DATA (a
)->first
== a
);
4124 for (i
= 0; i
< ira_allocnos_num
; i
++)
4125 ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges
[i
]);
4126 ira_free (slot_coalesced_allocnos_live_ranges
);
4130 /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
4131 subsequent assigning stack slots to them in the reload pass. To do
4132 this we coalesce spilled allocnos first to decrease the number of
4133 memory-memory move insns. This function is called by the
4136 ira_sort_regnos_for_alter_reg (int *pseudo_regnos
, int n
,
4137 unsigned int *reg_max_ref_width
)
4139 int max_regno
= max_reg_num ();
4140 int i
, regno
, num
, slot_num
;
4141 ira_allocno_t allocno
, a
;
4142 ira_allocno_iterator ai
;
4143 ira_allocno_t
*spilled_coalesced_allocnos
;
4145 ira_assert (! ira_use_lra_p
);
4147 /* Set up allocnos can be coalesced. */
4148 coloring_allocno_bitmap
= ira_allocate_bitmap ();
4149 for (i
= 0; i
< n
; i
++)
4151 regno
= pseudo_regnos
[i
];
4152 allocno
= ira_regno_allocno_map
[regno
];
4153 if (allocno
!= NULL
)
4154 bitmap_set_bit (coloring_allocno_bitmap
, ALLOCNO_NUM (allocno
));
4156 allocno_coalesced_p
= false;
4157 processed_coalesced_allocno_bitmap
= ira_allocate_bitmap ();
4158 allocno_coalesce_data
4159 = (coalesce_data_t
) ira_allocate (sizeof (struct coalesce_data
)
4160 * ira_allocnos_num
);
4161 /* Initialize coalesce data for allocnos. */
4162 FOR_EACH_ALLOCNO (a
, ai
)
4164 ALLOCNO_ADD_DATA (a
) = allocno_coalesce_data
+ ALLOCNO_NUM (a
);
4165 ALLOCNO_COALESCE_DATA (a
)->first
= a
;
4166 ALLOCNO_COALESCE_DATA (a
)->next
= a
;
4168 coalesce_allocnos ();
4169 ira_free_bitmap (coloring_allocno_bitmap
);
4170 regno_coalesced_allocno_cost
4171 = (int *) ira_allocate (max_regno
* sizeof (int));
4172 regno_coalesced_allocno_num
4173 = (int *) ira_allocate (max_regno
* sizeof (int));
4174 memset (regno_coalesced_allocno_num
, 0, max_regno
* sizeof (int));
4175 setup_coalesced_allocno_costs_and_nums (pseudo_regnos
, n
);
4176 /* Sort regnos according frequencies of the corresponding coalesced
4178 qsort (pseudo_regnos
, n
, sizeof (int), coalesced_pseudo_reg_freq_compare
);
4179 spilled_coalesced_allocnos
4180 = (ira_allocno_t
*) ira_allocate (ira_allocnos_num
4181 * sizeof (ira_allocno_t
));
4182 /* Collect allocnos representing the spilled coalesced allocno
4184 num
= collect_spilled_coalesced_allocnos (pseudo_regnos
, n
,
4185 spilled_coalesced_allocnos
);
4186 if (flag_ira_share_spill_slots
4187 && coalesce_spill_slots (spilled_coalesced_allocnos
, num
))
4189 setup_coalesced_allocno_costs_and_nums (pseudo_regnos
, n
);
4190 qsort (pseudo_regnos
, n
, sizeof (int),
4191 coalesced_pseudo_reg_freq_compare
);
4192 num
= collect_spilled_coalesced_allocnos (pseudo_regnos
, n
,
4193 spilled_coalesced_allocnos
);
4195 ira_free_bitmap (processed_coalesced_allocno_bitmap
);
4196 allocno_coalesced_p
= false;
4197 /* Assign stack slot numbers to spilled allocno sets, use smaller
4198 numbers for most frequently used coalesced allocnos. -1 is
4199 reserved for dynamic search of stack slots for pseudos spilled by
4202 for (i
= 0; i
< num
; i
++)
4204 allocno
= spilled_coalesced_allocnos
[i
];
4205 if (ALLOCNO_COALESCE_DATA (allocno
)->first
!= allocno
4206 || ALLOCNO_HARD_REGNO (allocno
) >= 0
4207 || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno
)))
4209 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4210 fprintf (ira_dump_file
, " Slot %d (freq,size):", slot_num
);
4212 for (a
= ALLOCNO_COALESCE_DATA (allocno
)->next
;;
4213 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
4215 ira_assert (ALLOCNO_HARD_REGNO (a
) < 0);
4216 ALLOCNO_HARD_REGNO (a
) = -slot_num
;
4217 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4218 fprintf (ira_dump_file
, " a%dr%d(%d,%d)",
4219 ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
), ALLOCNO_FREQ (a
),
4220 MAX (PSEUDO_REGNO_BYTES (ALLOCNO_REGNO (a
)),
4221 reg_max_ref_width
[ALLOCNO_REGNO (a
)]));
4226 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4227 fprintf (ira_dump_file
, "\n");
4229 ira_spilled_reg_stack_slots_num
= slot_num
- 1;
4230 ira_free (spilled_coalesced_allocnos
);
4231 /* Sort regnos according the slot numbers. */
4232 regno_max_ref_width
= reg_max_ref_width
;
4233 qsort (pseudo_regnos
, n
, sizeof (int), coalesced_pseudo_reg_slot_compare
);
4234 FOR_EACH_ALLOCNO (a
, ai
)
4235 ALLOCNO_ADD_DATA (a
) = NULL
;
4236 ira_free (allocno_coalesce_data
);
4237 ira_free (regno_coalesced_allocno_num
);
4238 ira_free (regno_coalesced_allocno_cost
);
4243 /* This page contains code used by the reload pass to improve the
4246 /* The function is called from reload to mark changes in the
4247 allocation of REGNO made by the reload. Remember that reg_renumber
4248 reflects the change result. */
4250 ira_mark_allocation_change (int regno
)
4252 ira_allocno_t a
= ira_regno_allocno_map
[regno
];
4253 int old_hard_regno
, hard_regno
, cost
;
4254 enum reg_class aclass
= ALLOCNO_CLASS (a
);
4256 ira_assert (a
!= NULL
);
4257 hard_regno
= reg_renumber
[regno
];
4258 if ((old_hard_regno
= ALLOCNO_HARD_REGNO (a
)) == hard_regno
)
4260 if (old_hard_regno
< 0)
4261 cost
= -ALLOCNO_MEMORY_COST (a
);
4264 ira_assert (ira_class_hard_reg_index
[aclass
][old_hard_regno
] >= 0);
4265 cost
= -(ALLOCNO_HARD_REG_COSTS (a
) == NULL
4266 ? ALLOCNO_CLASS_COST (a
)
4267 : ALLOCNO_HARD_REG_COSTS (a
)
4268 [ira_class_hard_reg_index
[aclass
][old_hard_regno
]]);
4269 update_costs_from_copies (a
, false, false);
4271 ira_overall_cost
-= cost
;
4272 ALLOCNO_HARD_REGNO (a
) = hard_regno
;
4275 ALLOCNO_HARD_REGNO (a
) = -1;
4276 cost
+= ALLOCNO_MEMORY_COST (a
);
4278 else if (ira_class_hard_reg_index
[aclass
][hard_regno
] >= 0)
4280 cost
+= (ALLOCNO_HARD_REG_COSTS (a
) == NULL
4281 ? ALLOCNO_CLASS_COST (a
)
4282 : ALLOCNO_HARD_REG_COSTS (a
)
4283 [ira_class_hard_reg_index
[aclass
][hard_regno
]]);
4284 update_costs_from_copies (a
, true, false);
4287 /* Reload changed class of the allocno. */
4289 ira_overall_cost
+= cost
;
4292 /* This function is called when reload deletes memory-memory move. In
4293 this case we marks that the allocation of the corresponding
4294 allocnos should be not changed in future. Otherwise we risk to get
4297 ira_mark_memory_move_deletion (int dst_regno
, int src_regno
)
4299 ira_allocno_t dst
= ira_regno_allocno_map
[dst_regno
];
4300 ira_allocno_t src
= ira_regno_allocno_map
[src_regno
];
4302 ira_assert (dst
!= NULL
&& src
!= NULL
4303 && ALLOCNO_HARD_REGNO (dst
) < 0
4304 && ALLOCNO_HARD_REGNO (src
) < 0);
4305 ALLOCNO_DONT_REASSIGN_P (dst
) = true;
4306 ALLOCNO_DONT_REASSIGN_P (src
) = true;
4309 /* Try to assign a hard register (except for FORBIDDEN_REGS) to
4310 allocno A and return TRUE in the case of success. */
4312 allocno_reload_assign (ira_allocno_t a
, HARD_REG_SET forbidden_regs
)
4315 enum reg_class aclass
;
4316 int regno
= ALLOCNO_REGNO (a
);
4317 HARD_REG_SET saved
[2];
4320 n
= ALLOCNO_NUM_OBJECTS (a
);
4321 for (i
= 0; i
< n
; i
++)
4323 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
4324 COPY_HARD_REG_SET (saved
[i
], OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
));
4325 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
), forbidden_regs
);
4326 if (! flag_caller_saves
&& ALLOCNO_CALLS_CROSSED_NUM (a
) != 0)
4327 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
),
4330 ALLOCNO_ASSIGNED_P (a
) = false;
4331 aclass
= ALLOCNO_CLASS (a
);
4332 update_curr_costs (a
);
4333 assign_hard_reg (a
, true);
4334 hard_regno
= ALLOCNO_HARD_REGNO (a
);
4335 reg_renumber
[regno
] = hard_regno
;
4337 ALLOCNO_HARD_REGNO (a
) = -1;
4340 ira_assert (ira_class_hard_reg_index
[aclass
][hard_regno
] >= 0);
4342 -= (ALLOCNO_MEMORY_COST (a
)
4343 - (ALLOCNO_HARD_REG_COSTS (a
) == NULL
4344 ? ALLOCNO_CLASS_COST (a
)
4345 : ALLOCNO_HARD_REG_COSTS (a
)[ira_class_hard_reg_index
4346 [aclass
][hard_regno
]]));
4347 if (ALLOCNO_CALLS_CROSSED_NUM (a
) != 0
4348 && ira_hard_reg_set_intersection_p (hard_regno
, ALLOCNO_MODE (a
),
4351 ira_assert (flag_caller_saves
);
4352 caller_save_needed
= 1;
4356 /* If we found a hard register, modify the RTL for the pseudo
4357 register to show the hard register, and mark the pseudo register
4359 if (reg_renumber
[regno
] >= 0)
4361 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4362 fprintf (ira_dump_file
, ": reassign to %d\n", reg_renumber
[regno
]);
4363 SET_REGNO (regno_reg_rtx
[regno
], reg_renumber
[regno
]);
4364 mark_home_live (regno
);
4366 else if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4367 fprintf (ira_dump_file
, "\n");
4368 for (i
= 0; i
< n
; i
++)
4370 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
4371 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
), saved
[i
]);
4373 return reg_renumber
[regno
] >= 0;
4376 /* Sort pseudos according their usage frequencies (putting most
4377 frequently ones first). */
4379 pseudo_reg_compare (const void *v1p
, const void *v2p
)
4381 int regno1
= *(const int *) v1p
;
4382 int regno2
= *(const int *) v2p
;
4385 if ((diff
= REG_FREQ (regno2
) - REG_FREQ (regno1
)) != 0)
4387 return regno1
- regno2
;
4390 /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
4391 NUM of them) or spilled pseudos conflicting with pseudos in
4392 SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
4393 allocation has been changed. The function doesn't use
4394 BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
4395 PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
4396 is called by the reload pass at the end of each reload
4399 ira_reassign_pseudos (int *spilled_pseudo_regs
, int num
,
4400 HARD_REG_SET bad_spill_regs
,
4401 HARD_REG_SET
*pseudo_forbidden_regs
,
4402 HARD_REG_SET
*pseudo_previous_regs
,
4408 HARD_REG_SET forbidden_regs
;
4409 bitmap temp
= BITMAP_ALLOC (NULL
);
4411 /* Add pseudos which conflict with pseudos already in
4412 SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
4413 to allocating in two steps as some of the conflicts might have
4414 a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
4415 for (i
= 0; i
< num
; i
++)
4416 bitmap_set_bit (temp
, spilled_pseudo_regs
[i
]);
4418 for (i
= 0, n
= num
; i
< n
; i
++)
4421 int regno
= spilled_pseudo_regs
[i
];
4422 bitmap_set_bit (temp
, regno
);
4424 a
= ira_regno_allocno_map
[regno
];
4425 nr
= ALLOCNO_NUM_OBJECTS (a
);
4426 for (j
= 0; j
< nr
; j
++)
4428 ira_object_t conflict_obj
;
4429 ira_object_t obj
= ALLOCNO_OBJECT (a
, j
);
4430 ira_object_conflict_iterator oci
;
4432 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
4434 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
4435 if (ALLOCNO_HARD_REGNO (conflict_a
) < 0
4436 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a
)
4437 && bitmap_set_bit (temp
, ALLOCNO_REGNO (conflict_a
)))
4439 spilled_pseudo_regs
[num
++] = ALLOCNO_REGNO (conflict_a
);
4440 /* ?!? This seems wrong. */
4441 bitmap_set_bit (consideration_allocno_bitmap
,
4442 ALLOCNO_NUM (conflict_a
));
4449 qsort (spilled_pseudo_regs
, num
, sizeof (int), pseudo_reg_compare
);
4451 /* Try to assign hard registers to pseudos from
4452 SPILLED_PSEUDO_REGS. */
4453 for (i
= 0; i
< num
; i
++)
4455 regno
= spilled_pseudo_regs
[i
];
4456 COPY_HARD_REG_SET (forbidden_regs
, bad_spill_regs
);
4457 IOR_HARD_REG_SET (forbidden_regs
, pseudo_forbidden_regs
[regno
]);
4458 IOR_HARD_REG_SET (forbidden_regs
, pseudo_previous_regs
[regno
]);
4459 gcc_assert (reg_renumber
[regno
] < 0);
4460 a
= ira_regno_allocno_map
[regno
];
4461 ira_mark_allocation_change (regno
);
4462 ira_assert (reg_renumber
[regno
] < 0);
4463 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4464 fprintf (ira_dump_file
,
4465 " Try Assign %d(a%d), cost=%d", regno
, ALLOCNO_NUM (a
),
4466 ALLOCNO_MEMORY_COST (a
)
4467 - ALLOCNO_CLASS_COST (a
));
4468 allocno_reload_assign (a
, forbidden_regs
);
4469 if (reg_renumber
[regno
] >= 0)
4471 CLEAR_REGNO_REG_SET (spilled
, regno
);
4479 /* The function is called by reload and returns already allocated
4480 stack slot (if any) for REGNO with given INHERENT_SIZE and
4481 TOTAL_SIZE. In the case of failure to find a slot which can be
4482 used for REGNO, the function returns NULL. */
4484 ira_reuse_stack_slot (int regno
, unsigned int inherent_size
,
4485 unsigned int total_size
)
4488 int slot_num
, best_slot_num
;
4489 int cost
, best_cost
;
4490 ira_copy_t cp
, next_cp
;
4491 ira_allocno_t another_allocno
, allocno
= ira_regno_allocno_map
[regno
];
4494 struct ira_spilled_reg_stack_slot
*slot
= NULL
;
4496 ira_assert (! ira_use_lra_p
);
4498 ira_assert (inherent_size
== PSEUDO_REGNO_BYTES (regno
)
4499 && inherent_size
<= total_size
4500 && ALLOCNO_HARD_REGNO (allocno
) < 0);
4501 if (! flag_ira_share_spill_slots
)
4503 slot_num
= -ALLOCNO_HARD_REGNO (allocno
) - 2;
4506 slot
= &ira_spilled_reg_stack_slots
[slot_num
];
4511 best_cost
= best_slot_num
= -1;
4513 /* It means that the pseudo was spilled in the reload pass, try
4516 slot_num
< ira_spilled_reg_stack_slots_num
;
4519 slot
= &ira_spilled_reg_stack_slots
[slot_num
];
4520 if (slot
->mem
== NULL_RTX
)
4522 if (slot
->width
< total_size
4523 || GET_MODE_SIZE (GET_MODE (slot
->mem
)) < inherent_size
)
4526 EXECUTE_IF_SET_IN_BITMAP (&slot
->spilled_regs
,
4527 FIRST_PSEUDO_REGISTER
, i
, bi
)
4529 another_allocno
= ira_regno_allocno_map
[i
];
4530 if (allocnos_conflict_by_live_ranges_p (allocno
,
4534 for (cost
= 0, cp
= ALLOCNO_COPIES (allocno
);
4538 if (cp
->first
== allocno
)
4540 next_cp
= cp
->next_first_allocno_copy
;
4541 another_allocno
= cp
->second
;
4543 else if (cp
->second
== allocno
)
4545 next_cp
= cp
->next_second_allocno_copy
;
4546 another_allocno
= cp
->first
;
4550 if (cp
->insn
== NULL_RTX
)
4552 if (bitmap_bit_p (&slot
->spilled_regs
,
4553 ALLOCNO_REGNO (another_allocno
)))
4556 if (cost
> best_cost
)
4559 best_slot_num
= slot_num
;
4566 slot_num
= best_slot_num
;
4567 slot
= &ira_spilled_reg_stack_slots
[slot_num
];
4568 SET_REGNO_REG_SET (&slot
->spilled_regs
, regno
);
4570 ALLOCNO_HARD_REGNO (allocno
) = -slot_num
- 2;
4575 ira_assert (slot
->width
>= total_size
);
4576 #ifdef ENABLE_IRA_CHECKING
4577 EXECUTE_IF_SET_IN_BITMAP (&slot
->spilled_regs
,
4578 FIRST_PSEUDO_REGISTER
, i
, bi
)
4580 ira_assert (! conflict_by_live_ranges_p (regno
, i
));
4583 SET_REGNO_REG_SET (&slot
->spilled_regs
, regno
);
4584 if (internal_flag_ira_verbose
> 3 && ira_dump_file
)
4586 fprintf (ira_dump_file
, " Assigning %d(freq=%d) slot %d of",
4587 regno
, REG_FREQ (regno
), slot_num
);
4588 EXECUTE_IF_SET_IN_BITMAP (&slot
->spilled_regs
,
4589 FIRST_PSEUDO_REGISTER
, i
, bi
)
4591 if ((unsigned) regno
!= i
)
4592 fprintf (ira_dump_file
, " %d", i
);
4594 fprintf (ira_dump_file
, "\n");
4600 /* This is called by reload every time a new stack slot X with
4601 TOTAL_SIZE was allocated for REGNO. We store this info for
4602 subsequent ira_reuse_stack_slot calls. */
4604 ira_mark_new_stack_slot (rtx x
, int regno
, unsigned int total_size
)
4606 struct ira_spilled_reg_stack_slot
*slot
;
4608 ira_allocno_t allocno
;
4610 ira_assert (! ira_use_lra_p
);
4612 ira_assert (PSEUDO_REGNO_BYTES (regno
) <= total_size
);
4613 allocno
= ira_regno_allocno_map
[regno
];
4614 slot_num
= -ALLOCNO_HARD_REGNO (allocno
) - 2;
4617 slot_num
= ira_spilled_reg_stack_slots_num
++;
4618 ALLOCNO_HARD_REGNO (allocno
) = -slot_num
- 2;
4620 slot
= &ira_spilled_reg_stack_slots
[slot_num
];
4621 INIT_REG_SET (&slot
->spilled_regs
);
4622 SET_REGNO_REG_SET (&slot
->spilled_regs
, regno
);
4624 slot
->width
= total_size
;
4625 if (internal_flag_ira_verbose
> 3 && ira_dump_file
)
4626 fprintf (ira_dump_file
, " Assigning %d(freq=%d) a new slot %d\n",
4627 regno
, REG_FREQ (regno
), slot_num
);
4631 /* Return spill cost for pseudo-registers whose numbers are in array
4632 REGNOS (with a negative number as an end marker) for reload with
4633 given IN and OUT for INSN. Return also number points (through
4634 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
4635 the register pressure is high, number of references of the
4636 pseudo-registers (through NREFS), number of callee-clobbered
4637 hard-registers occupied by the pseudo-registers (through
4638 CALL_USED_COUNT), and the first hard regno occupied by the
4639 pseudo-registers (through FIRST_HARD_REGNO). */
4641 calculate_spill_cost (int *regnos
, rtx in
, rtx out
, rtx_insn
*insn
,
4642 int *excess_pressure_live_length
,
4643 int *nrefs
, int *call_used_count
, int *first_hard_regno
)
4645 int i
, cost
, regno
, hard_regno
, j
, count
, saved_cost
, nregs
;
4651 for (length
= count
= cost
= i
= 0;; i
++)
4656 *nrefs
+= REG_N_REFS (regno
);
4657 hard_regno
= reg_renumber
[regno
];
4658 ira_assert (hard_regno
>= 0);
4659 a
= ira_regno_allocno_map
[regno
];
4660 length
+= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
) / ALLOCNO_NUM_OBJECTS (a
);
4661 cost
+= ALLOCNO_MEMORY_COST (a
) - ALLOCNO_CLASS_COST (a
);
4662 nregs
= hard_regno_nregs
[hard_regno
][ALLOCNO_MODE (a
)];
4663 for (j
= 0; j
< nregs
; j
++)
4664 if (! TEST_HARD_REG_BIT (call_used_reg_set
, hard_regno
+ j
))
4668 in_p
= in
&& REG_P (in
) && (int) REGNO (in
) == hard_regno
;
4669 out_p
= out
&& REG_P (out
) && (int) REGNO (out
) == hard_regno
;
4671 && find_regno_note (insn
, REG_DEAD
, hard_regno
) != NULL_RTX
)
4675 saved_cost
+= ira_memory_move_cost
4676 [ALLOCNO_MODE (a
)][ALLOCNO_CLASS (a
)][1];
4679 += ira_memory_move_cost
4680 [ALLOCNO_MODE (a
)][ALLOCNO_CLASS (a
)][0];
4681 cost
-= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn
)) * saved_cost
;
4684 *excess_pressure_live_length
= length
;
4685 *call_used_count
= count
;
4689 hard_regno
= reg_renumber
[regnos
[0]];
4691 *first_hard_regno
= hard_regno
;
4695 /* Return TRUE if spilling pseudo-registers whose numbers are in array
4696 REGNOS is better than spilling pseudo-registers with numbers in
4697 OTHER_REGNOS for reload with given IN and OUT for INSN. The
4698 function used by the reload pass to make better register spilling
4701 ira_better_spill_reload_regno_p (int *regnos
, int *other_regnos
,
4702 rtx in
, rtx out
, rtx_insn
*insn
)
4704 int cost
, other_cost
;
4705 int length
, other_length
;
4706 int nrefs
, other_nrefs
;
4707 int call_used_count
, other_call_used_count
;
4708 int hard_regno
, other_hard_regno
;
4710 cost
= calculate_spill_cost (regnos
, in
, out
, insn
,
4711 &length
, &nrefs
, &call_used_count
, &hard_regno
);
4712 other_cost
= calculate_spill_cost (other_regnos
, in
, out
, insn
,
4713 &other_length
, &other_nrefs
,
4714 &other_call_used_count
,
4716 if (nrefs
== 0 && other_nrefs
!= 0)
4718 if (nrefs
!= 0 && other_nrefs
== 0)
4720 if (cost
!= other_cost
)
4721 return cost
< other_cost
;
4722 if (length
!= other_length
)
4723 return length
> other_length
;
4724 #ifdef REG_ALLOC_ORDER
4725 if (hard_regno
>= 0 && other_hard_regno
>= 0)
4726 return (inv_reg_alloc_order
[hard_regno
]
4727 < inv_reg_alloc_order
[other_hard_regno
]);
4729 if (call_used_count
!= other_call_used_count
)
4730 return call_used_count
> other_call_used_count
;
4737 /* Allocate and initialize data necessary for assign_hard_reg. */
4739 ira_initiate_assign (void)
4742 = (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
)
4743 * ira_allocnos_num
);
4744 consideration_allocno_bitmap
= ira_allocate_bitmap ();
4745 initiate_cost_update ();
4746 allocno_priorities
= (int *) ira_allocate (sizeof (int) * ira_allocnos_num
);
4747 sorted_copies
= (ira_copy_t
*) ira_allocate (ira_copies_num
4748 * sizeof (ira_copy_t
));
4751 /* Deallocate data used by assign_hard_reg. */
4753 ira_finish_assign (void)
4755 ira_free (sorted_allocnos
);
4756 ira_free_bitmap (consideration_allocno_bitmap
);
4757 finish_cost_update ();
4758 ira_free (allocno_priorities
);
4759 ira_free (sorted_copies
);
4764 /* Entry function doing color-based register allocation. */
4768 allocno_stack_vec
.create (ira_allocnos_num
);
4769 memset (allocated_hardreg_p
, 0, sizeof (allocated_hardreg_p
));
4770 ira_initiate_assign ();
4772 ira_finish_assign ();
4773 allocno_stack_vec
.release ();
4774 move_spill_restore ();
4779 /* This page contains a simple register allocator without usage of
4780 allocno conflicts. This is used for fast allocation for -O0. */
4782 /* Do register allocation by not using allocno conflicts. It uses
4783 only allocno live ranges. The algorithm is close to Chow's
4784 priority coloring. */
4786 fast_allocation (void)
4788 int i
, j
, k
, num
, class_size
, hard_regno
;
4790 bool no_stack_reg_p
;
4792 enum reg_class aclass
;
4795 ira_allocno_iterator ai
;
4797 HARD_REG_SET conflict_hard_regs
, *used_hard_regs
;
4799 sorted_allocnos
= (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
)
4800 * ira_allocnos_num
);
4802 FOR_EACH_ALLOCNO (a
, ai
)
4803 sorted_allocnos
[num
++] = a
;
4804 allocno_priorities
= (int *) ira_allocate (sizeof (int) * ira_allocnos_num
);
4805 setup_allocno_priorities (sorted_allocnos
, num
);
4806 used_hard_regs
= (HARD_REG_SET
*) ira_allocate (sizeof (HARD_REG_SET
)
4808 for (i
= 0; i
< ira_max_point
; i
++)
4809 CLEAR_HARD_REG_SET (used_hard_regs
[i
]);
4810 qsort (sorted_allocnos
, num
, sizeof (ira_allocno_t
),
4811 allocno_priority_compare_func
);
4812 for (i
= 0; i
< num
; i
++)
4816 a
= sorted_allocnos
[i
];
4817 nr
= ALLOCNO_NUM_OBJECTS (a
);
4818 CLEAR_HARD_REG_SET (conflict_hard_regs
);
4819 for (l
= 0; l
< nr
; l
++)
4821 ira_object_t obj
= ALLOCNO_OBJECT (a
, l
);
4822 IOR_HARD_REG_SET (conflict_hard_regs
,
4823 OBJECT_CONFLICT_HARD_REGS (obj
));
4824 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
4825 for (j
= r
->start
; j
<= r
->finish
; j
++)
4826 IOR_HARD_REG_SET (conflict_hard_regs
, used_hard_regs
[j
]);
4828 aclass
= ALLOCNO_CLASS (a
);
4829 ALLOCNO_ASSIGNED_P (a
) = true;
4830 ALLOCNO_HARD_REGNO (a
) = -1;
4831 if (hard_reg_set_subset_p (reg_class_contents
[aclass
],
4832 conflict_hard_regs
))
4834 mode
= ALLOCNO_MODE (a
);
4836 no_stack_reg_p
= ALLOCNO_NO_STACK_REG_P (a
);
4838 class_size
= ira_class_hard_regs_num
[aclass
];
4839 for (j
= 0; j
< class_size
; j
++)
4841 hard_regno
= ira_class_hard_regs
[aclass
][j
];
4843 if (no_stack_reg_p
&& FIRST_STACK_REG
<= hard_regno
4844 && hard_regno
<= LAST_STACK_REG
)
4847 if (ira_hard_reg_set_intersection_p (hard_regno
, mode
, conflict_hard_regs
)
4848 || (TEST_HARD_REG_BIT
4849 (ira_prohibited_class_mode_regs
[aclass
][mode
], hard_regno
)))
4851 ALLOCNO_HARD_REGNO (a
) = hard_regno
;
4852 for (l
= 0; l
< nr
; l
++)
4854 ira_object_t obj
= ALLOCNO_OBJECT (a
, l
);
4855 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
4856 for (k
= r
->start
; k
<= r
->finish
; k
++)
4857 IOR_HARD_REG_SET (used_hard_regs
[k
],
4858 ira_reg_mode_hard_regset
[hard_regno
][mode
]);
4863 ira_free (sorted_allocnos
);
4864 ira_free (used_hard_regs
);
4865 ira_free (allocno_priorities
);
4866 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
4867 ira_print_disposition (ira_dump_file
);
4872 /* Entry function doing coloring. */
4877 ira_allocno_iterator ai
;
4879 /* Setup updated costs. */
4880 FOR_EACH_ALLOCNO (a
, ai
)
4882 ALLOCNO_UPDATED_MEMORY_COST (a
) = ALLOCNO_MEMORY_COST (a
);
4883 ALLOCNO_UPDATED_CLASS_COST (a
) = ALLOCNO_CLASS_COST (a
);
4885 if (ira_conflicts_p
)