1 /* IRA allocation based on graph coloring.
2 Copyright (C) 2006-2019 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 /* Sum of frequencies of hard register preferences of all
116 conflicting allocnos which are not the coloring stack yet. */
117 int conflict_allocno_hard_prefs
;
118 /* Allocnos in a bucket (used in coloring) chained by the following
120 ira_allocno_t next_bucket_allocno
;
121 ira_allocno_t prev_bucket_allocno
;
122 /* Used for temporary purposes. */
124 /* Used to exclude repeated processing. */
126 /* Profitable hard regs available for this pseudo allocation. It
127 means that the set excludes unavailable hard regs and hard regs
128 conflicting with given pseudo. They should be of the allocno
130 HARD_REG_SET profitable_hard_regs
;
131 /* The allocno hard registers node. */
132 allocno_hard_regs_node_t hard_regs_node
;
133 /* Array of structures allocno_hard_regs_subnode representing
134 given allocno hard registers node (the 1st element in the array)
135 and all its subnodes in the tree (forest) of allocno hard
136 register nodes (see comments above). */
137 int hard_regs_subnodes_start
;
138 /* The length of the previous array. */
139 int hard_regs_subnodes_num
;
140 /* Records about updating allocno hard reg costs from copies. If
141 the allocno did not get expected hard register, these records are
142 used to restore original hard reg costs of allocnos connected to
143 this allocno by copies. */
144 struct update_cost_record
*update_cost_records
;
145 /* Threads. We collect allocnos connected by copies into threads
146 and try to assign hard regs to allocnos by threads. */
147 /* Allocno representing all thread. */
148 ira_allocno_t first_thread_allocno
;
149 /* Allocnos in thread forms a cycle list through the following
151 ira_allocno_t next_thread_allocno
;
152 /* All thread frequency. Defined only for first thread allocno. */
157 typedef struct allocno_color_data
*allocno_color_data_t
;
159 /* Container for storing allocno data concerning coloring. */
160 static allocno_color_data_t allocno_color_data
;
162 /* Macro to access the data concerning coloring. */
163 #define ALLOCNO_COLOR_DATA(a) ((allocno_color_data_t) ALLOCNO_ADD_DATA (a))
165 /* Used for finding allocno colorability to exclude repeated allocno
166 processing and for updating preferencing to exclude repeated
167 allocno processing during assignment. */
168 static int curr_allocno_process
;
170 /* This file contains code for regional graph coloring, spill/restore
171 code placement optimization, and code helping the reload pass to do
174 /* Bitmap of allocnos which should be colored. */
175 static bitmap coloring_allocno_bitmap
;
177 /* Bitmap of allocnos which should be taken into account during
178 coloring. In general case it contains allocnos from
179 coloring_allocno_bitmap plus other already colored conflicting
181 static bitmap consideration_allocno_bitmap
;
183 /* All allocnos sorted according their priorities. */
184 static ira_allocno_t
*sorted_allocnos
;
186 /* Vec representing the stack of allocnos used during coloring. */
187 static vec
<ira_allocno_t
> allocno_stack_vec
;
189 /* Helper for qsort comparison callbacks - return a positive integer if
190 X > Y, or a negative value otherwise. Use a conditional expression
191 instead of a difference computation to insulate from possible overflow
192 issues, e.g. X - Y < 0 for some X > 0 and Y < 0. */
193 #define SORTGT(x,y) (((x) > (y)) ? 1 : -1)
197 /* Definition of vector of allocno hard registers. */
199 /* Vector of unique allocno hard registers. */
200 static vec
<allocno_hard_regs_t
> allocno_hard_regs_vec
;
202 struct allocno_hard_regs_hasher
: nofree_ptr_hash
<allocno_hard_regs
>
204 static inline hashval_t
hash (const allocno_hard_regs
*);
205 static inline bool equal (const allocno_hard_regs
*,
206 const allocno_hard_regs
*);
209 /* Returns hash value for allocno hard registers V. */
211 allocno_hard_regs_hasher::hash (const allocno_hard_regs
*hv
)
213 return iterative_hash (&hv
->set
, sizeof (HARD_REG_SET
), 0);
216 /* Compares allocno hard registers V1 and V2. */
218 allocno_hard_regs_hasher::equal (const allocno_hard_regs
*hv1
,
219 const allocno_hard_regs
*hv2
)
221 return hv1
->set
== hv2
->set
;
224 /* Hash table of unique allocno hard registers. */
225 static hash_table
<allocno_hard_regs_hasher
> *allocno_hard_regs_htab
;
227 /* Return allocno hard registers in the hash table equal to HV. */
228 static allocno_hard_regs_t
229 find_hard_regs (allocno_hard_regs_t hv
)
231 return allocno_hard_regs_htab
->find (hv
);
234 /* Insert allocno hard registers HV in the hash table (if it is not
235 there yet) and return the value which in the table. */
236 static allocno_hard_regs_t
237 insert_hard_regs (allocno_hard_regs_t hv
)
239 allocno_hard_regs
**slot
= allocno_hard_regs_htab
->find_slot (hv
, INSERT
);
246 /* Initialize data concerning allocno hard registers. */
248 init_allocno_hard_regs (void)
250 allocno_hard_regs_vec
.create (200);
251 allocno_hard_regs_htab
252 = new hash_table
<allocno_hard_regs_hasher
> (200);
255 /* Add (or update info about) allocno hard registers with SET and
257 static allocno_hard_regs_t
258 add_allocno_hard_regs (HARD_REG_SET set
, int64_t cost
)
260 struct allocno_hard_regs temp
;
261 allocno_hard_regs_t hv
;
263 gcc_assert (! hard_reg_set_empty_p (set
));
265 if ((hv
= find_hard_regs (&temp
)) != NULL
)
269 hv
= ((struct allocno_hard_regs
*)
270 ira_allocate (sizeof (struct allocno_hard_regs
)));
273 allocno_hard_regs_vec
.safe_push (hv
);
274 insert_hard_regs (hv
);
279 /* Finalize data concerning allocno hard registers. */
281 finish_allocno_hard_regs (void)
284 allocno_hard_regs_t hv
;
287 allocno_hard_regs_vec
.iterate (i
, &hv
);
290 delete allocno_hard_regs_htab
;
291 allocno_hard_regs_htab
= NULL
;
292 allocno_hard_regs_vec
.release ();
295 /* Sort hard regs according to their frequency of usage. */
297 allocno_hard_regs_compare (const void *v1p
, const void *v2p
)
299 allocno_hard_regs_t hv1
= *(const allocno_hard_regs_t
*) v1p
;
300 allocno_hard_regs_t hv2
= *(const allocno_hard_regs_t
*) v2p
;
302 if (hv2
->cost
> hv1
->cost
)
304 else if (hv2
->cost
< hv1
->cost
)
306 return SORTGT (allocno_hard_regs_hasher::hash(hv2
), allocno_hard_regs_hasher::hash(hv1
));
311 /* Used for finding a common ancestor of two allocno hard registers
312 nodes in the forest. We use the current value of
313 'node_check_tick' to mark all nodes from one node to the top and
314 then walking up from another node until we find a marked node.
316 It is also used to figure out allocno colorability as a mark that
317 we already reset value of member 'conflict_size' for the forest
318 node corresponding to the processed allocno. */
319 static int node_check_tick
;
321 /* Roots of the forest containing hard register sets can be assigned
323 static allocno_hard_regs_node_t hard_regs_roots
;
325 /* Definition of vector of allocno hard register nodes. */
327 /* Vector used to create the forest. */
328 static vec
<allocno_hard_regs_node_t
> hard_regs_node_vec
;
330 /* Create and return allocno hard registers node containing allocno
331 hard registers HV. */
332 static allocno_hard_regs_node_t
333 create_new_allocno_hard_regs_node (allocno_hard_regs_t hv
)
335 allocno_hard_regs_node_t new_node
;
337 new_node
= ((struct allocno_hard_regs_node
*)
338 ira_allocate (sizeof (struct allocno_hard_regs_node
)));
340 new_node
->hard_regs
= hv
;
341 new_node
->hard_regs_num
= hard_reg_set_size (hv
->set
);
342 new_node
->first
= NULL
;
343 new_node
->used_p
= false;
347 /* Add allocno hard registers node NEW_NODE to the forest on its level
350 add_new_allocno_hard_regs_node_to_forest (allocno_hard_regs_node_t
*roots
,
351 allocno_hard_regs_node_t new_node
)
353 new_node
->next
= *roots
;
354 if (new_node
->next
!= NULL
)
355 new_node
->next
->prev
= new_node
;
356 new_node
->prev
= NULL
;
360 /* Add allocno hard registers HV (or its best approximation if it is
361 not possible) to the forest on its level given by ROOTS. */
363 add_allocno_hard_regs_to_forest (allocno_hard_regs_node_t
*roots
,
364 allocno_hard_regs_t hv
)
366 unsigned int i
, start
;
367 allocno_hard_regs_node_t node
, prev
, new_node
;
368 HARD_REG_SET temp_set
;
369 allocno_hard_regs_t hv2
;
371 start
= hard_regs_node_vec
.length ();
372 for (node
= *roots
; node
!= NULL
; node
= node
->next
)
374 if (hv
->set
== node
->hard_regs
->set
)
376 if (hard_reg_set_subset_p (hv
->set
, node
->hard_regs
->set
))
378 add_allocno_hard_regs_to_forest (&node
->first
, hv
);
381 if (hard_reg_set_subset_p (node
->hard_regs
->set
, hv
->set
))
382 hard_regs_node_vec
.safe_push (node
);
383 else if (hard_reg_set_intersect_p (hv
->set
, node
->hard_regs
->set
))
385 temp_set
= hv
->set
& node
->hard_regs
->set
;
386 hv2
= add_allocno_hard_regs (temp_set
, hv
->cost
);
387 add_allocno_hard_regs_to_forest (&node
->first
, hv2
);
390 if (hard_regs_node_vec
.length ()
393 /* Create a new node which contains nodes in hard_regs_node_vec. */
394 CLEAR_HARD_REG_SET (temp_set
);
396 i
< hard_regs_node_vec
.length ();
399 node
= hard_regs_node_vec
[i
];
400 temp_set
|= node
->hard_regs
->set
;
402 hv
= add_allocno_hard_regs (temp_set
, hv
->cost
);
403 new_node
= create_new_allocno_hard_regs_node (hv
);
406 i
< hard_regs_node_vec
.length ();
409 node
= hard_regs_node_vec
[i
];
410 if (node
->prev
== NULL
)
413 node
->prev
->next
= node
->next
;
414 if (node
->next
!= NULL
)
415 node
->next
->prev
= node
->prev
;
417 new_node
->first
= node
;
424 add_new_allocno_hard_regs_node_to_forest (roots
, new_node
);
426 hard_regs_node_vec
.truncate (start
);
429 /* Add allocno hard registers nodes starting with the forest level
430 given by FIRST which contains biggest set inside SET. */
432 collect_allocno_hard_regs_cover (allocno_hard_regs_node_t first
,
435 allocno_hard_regs_node_t node
;
437 ira_assert (first
!= NULL
);
438 for (node
= first
; node
!= NULL
; node
= node
->next
)
439 if (hard_reg_set_subset_p (node
->hard_regs
->set
, set
))
440 hard_regs_node_vec
.safe_push (node
);
441 else if (hard_reg_set_intersect_p (set
, node
->hard_regs
->set
))
442 collect_allocno_hard_regs_cover (node
->first
, set
);
445 /* Set up field parent as PARENT in all allocno hard registers nodes
446 in forest given by FIRST. */
448 setup_allocno_hard_regs_nodes_parent (allocno_hard_regs_node_t first
,
449 allocno_hard_regs_node_t parent
)
451 allocno_hard_regs_node_t node
;
453 for (node
= first
; node
!= NULL
; node
= node
->next
)
455 node
->parent
= parent
;
456 setup_allocno_hard_regs_nodes_parent (node
->first
, node
);
460 /* Return allocno hard registers node which is a first common ancestor
461 node of FIRST and SECOND in the forest. */
462 static allocno_hard_regs_node_t
463 first_common_ancestor_node (allocno_hard_regs_node_t first
,
464 allocno_hard_regs_node_t second
)
466 allocno_hard_regs_node_t node
;
469 for (node
= first
; node
!= NULL
; node
= node
->parent
)
470 node
->check
= node_check_tick
;
471 for (node
= second
; node
!= NULL
; node
= node
->parent
)
472 if (node
->check
== node_check_tick
)
474 return first_common_ancestor_node (second
, first
);
477 /* Print hard reg set SET to F. */
479 print_hard_reg_set (FILE *f
, HARD_REG_SET set
, bool new_line_p
)
483 for (start
= -1, i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
485 if (TEST_HARD_REG_BIT (set
, i
))
487 if (i
== 0 || ! TEST_HARD_REG_BIT (set
, i
- 1))
491 && (i
== FIRST_PSEUDO_REGISTER
- 1 || ! TEST_HARD_REG_BIT (set
, i
)))
494 fprintf (f
, " %d", start
);
495 else if (start
== i
- 2)
496 fprintf (f
, " %d %d", start
, start
+ 1);
498 fprintf (f
, " %d-%d", start
, i
- 1);
506 /* Print allocno hard register subforest given by ROOTS and its LEVEL
509 print_hard_regs_subforest (FILE *f
, allocno_hard_regs_node_t roots
,
513 allocno_hard_regs_node_t node
;
515 for (node
= roots
; node
!= NULL
; node
= node
->next
)
518 for (i
= 0; i
< level
* 2; i
++)
520 fprintf (f
, "%d:(", node
->preorder_num
);
521 print_hard_reg_set (f
, node
->hard_regs
->set
, false);
522 fprintf (f
, ")@%" PRId64
"\n", node
->hard_regs
->cost
);
523 print_hard_regs_subforest (f
, node
->first
, level
+ 1);
527 /* Print the allocno hard register forest to F. */
529 print_hard_regs_forest (FILE *f
)
531 fprintf (f
, " Hard reg set forest:\n");
532 print_hard_regs_subforest (f
, hard_regs_roots
, 1);
535 /* Print the allocno hard register forest to stderr. */
537 ira_debug_hard_regs_forest (void)
539 print_hard_regs_forest (stderr
);
542 /* Remove unused allocno hard registers nodes from forest given by its
545 remove_unused_allocno_hard_regs_nodes (allocno_hard_regs_node_t
*roots
)
547 allocno_hard_regs_node_t node
, prev
, next
, last
;
549 for (prev
= NULL
, node
= *roots
; node
!= NULL
; node
= next
)
554 remove_unused_allocno_hard_regs_nodes (&node
->first
);
559 for (last
= node
->first
;
560 last
!= NULL
&& last
->next
!= NULL
;
566 *roots
= node
->first
;
568 prev
->next
= node
->first
;
588 /* Set up fields preorder_num starting with START_NUM in all allocno
589 hard registers nodes in forest given by FIRST. Return biggest set
590 PREORDER_NUM increased by 1. */
592 enumerate_allocno_hard_regs_nodes (allocno_hard_regs_node_t first
,
593 allocno_hard_regs_node_t parent
,
596 allocno_hard_regs_node_t node
;
598 for (node
= first
; node
!= NULL
; node
= node
->next
)
600 node
->preorder_num
= start_num
++;
601 node
->parent
= parent
;
602 start_num
= enumerate_allocno_hard_regs_nodes (node
->first
, node
,
608 /* Number of allocno hard registers nodes in the forest. */
609 static int allocno_hard_regs_nodes_num
;
611 /* Table preorder number of allocno hard registers node in the forest
612 -> the allocno hard registers node. */
613 static allocno_hard_regs_node_t
*allocno_hard_regs_nodes
;
616 typedef struct allocno_hard_regs_subnode
*allocno_hard_regs_subnode_t
;
618 /* The structure is used to describes all subnodes (not only immediate
619 ones) in the mentioned above tree for given allocno hard register
620 node. The usage of such data accelerates calculation of
621 colorability of given allocno. */
622 struct allocno_hard_regs_subnode
624 /* The conflict size of conflicting allocnos whose hard register
625 sets are equal sets (plus supersets if given node is given
626 allocno hard registers node) of one in the given node. */
627 int left_conflict_size
;
628 /* The summary conflict size of conflicting allocnos whose hard
629 register sets are strict subsets of one in the given node.
630 Overall conflict size is
631 left_conflict_subnodes_size
632 + MIN (max_node_impact - left_conflict_subnodes_size,
635 short left_conflict_subnodes_size
;
636 short max_node_impact
;
639 /* Container for hard regs subnodes of all allocnos. */
640 static allocno_hard_regs_subnode_t allocno_hard_regs_subnodes
;
642 /* Table (preorder number of allocno hard registers node in the
643 forest, preorder number of allocno hard registers subnode) -> index
644 of the subnode relative to the node. -1 if it is not a
646 static int *allocno_hard_regs_subnode_index
;
648 /* Setup arrays ALLOCNO_HARD_REGS_NODES and
649 ALLOCNO_HARD_REGS_SUBNODE_INDEX. */
651 setup_allocno_hard_regs_subnode_index (allocno_hard_regs_node_t first
)
653 allocno_hard_regs_node_t node
, parent
;
656 for (node
= first
; node
!= NULL
; node
= node
->next
)
658 allocno_hard_regs_nodes
[node
->preorder_num
] = node
;
659 for (parent
= node
; parent
!= NULL
; parent
= parent
->parent
)
661 index
= parent
->preorder_num
* allocno_hard_regs_nodes_num
;
662 allocno_hard_regs_subnode_index
[index
+ node
->preorder_num
]
663 = node
->preorder_num
- parent
->preorder_num
;
665 setup_allocno_hard_regs_subnode_index (node
->first
);
669 /* Count all allocno hard registers nodes in tree ROOT. */
671 get_allocno_hard_regs_subnodes_num (allocno_hard_regs_node_t root
)
675 for (root
= root
->first
; root
!= NULL
; root
= root
->next
)
676 len
+= get_allocno_hard_regs_subnodes_num (root
);
680 /* Build the forest of allocno hard registers nodes and assign each
681 allocno a node from the forest. */
683 form_allocno_hard_regs_nodes_forest (void)
685 unsigned int i
, j
, size
, len
;
688 allocno_hard_regs_t hv
;
691 allocno_hard_regs_node_t node
, allocno_hard_regs_node
;
692 allocno_color_data_t allocno_data
;
695 init_allocno_hard_regs ();
696 hard_regs_roots
= NULL
;
697 hard_regs_node_vec
.create (100);
698 for (i
= 0; i
< FIRST_PSEUDO_REGISTER
; i
++)
699 if (! TEST_HARD_REG_BIT (ira_no_alloc_regs
, i
))
701 CLEAR_HARD_REG_SET (temp
);
702 SET_HARD_REG_BIT (temp
, i
);
703 hv
= add_allocno_hard_regs (temp
, 0);
704 node
= create_new_allocno_hard_regs_node (hv
);
705 add_new_allocno_hard_regs_node_to_forest (&hard_regs_roots
, node
);
707 start
= allocno_hard_regs_vec
.length ();
708 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
711 allocno_data
= ALLOCNO_COLOR_DATA (a
);
713 if (hard_reg_set_empty_p (allocno_data
->profitable_hard_regs
))
715 hv
= (add_allocno_hard_regs
716 (allocno_data
->profitable_hard_regs
,
717 ALLOCNO_MEMORY_COST (a
) - ALLOCNO_CLASS_COST (a
)));
719 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 profitable_hard_regs
= data
->profitable_hard_regs
;
835 node
= data
->hard_regs_node
;
836 node_preorder_num
= node
->preorder_num
;
837 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 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 temp_set
= temp_node
->hard_regs
->set
& profitable_hard_regs
;
899 aclass
= ALLOCNO_CLASS (a
);
900 for (n
= 0, j
= ira_class_hard_regs_num
[aclass
] - 1; j
>= 0; j
--)
902 hard_regno
= ira_class_hard_regs
[aclass
][j
];
903 if (TEST_HARD_REG_BIT (temp_set
, hard_regno
))
906 subnodes
[i
].max_node_impact
= n
;
908 subnodes
[i
].left_conflict_subnodes_size
= 0;
910 start
= node_preorder_num
* allocno_hard_regs_nodes_num
;
911 for (i
= data
->hard_regs_subnodes_num
- 1; i
> 0; i
--)
914 allocno_hard_regs_node_t parent
;
916 size
= (subnodes
[i
].left_conflict_subnodes_size
917 + MIN (subnodes
[i
].max_node_impact
918 - subnodes
[i
].left_conflict_subnodes_size
,
919 subnodes
[i
].left_conflict_size
));
920 parent
= allocno_hard_regs_nodes
[i
+ node_preorder_num
]->parent
;
921 gcc_checking_assert(parent
);
923 = allocno_hard_regs_subnode_index
[start
+ parent
->preorder_num
];
924 gcc_checking_assert(parent_i
>= 0);
925 subnodes
[parent_i
].left_conflict_subnodes_size
+= size
;
927 left_conflict_subnodes_size
= subnodes
[0].left_conflict_subnodes_size
;
929 = (left_conflict_subnodes_size
930 + MIN (subnodes
[0].max_node_impact
- left_conflict_subnodes_size
,
931 subnodes
[0].left_conflict_size
));
932 conflict_size
+= ira_reg_class_max_nregs
[ALLOCNO_CLASS (a
)][ALLOCNO_MODE (a
)];
933 data
->colorable_p
= conflict_size
<= data
->available_regs_num
;
934 return data
->colorable_p
;
937 /* Update left conflict sizes of hard registers subnodes of allocno A
938 after removing allocno REMOVED_A with SIZE from the conflict graph.
939 Return TRUE if A is trivially colorable. */
941 update_left_conflict_sizes_p (ira_allocno_t a
,
942 ira_allocno_t removed_a
, int size
)
944 int i
, conflict_size
, before_conflict_size
, diff
, start
;
945 int node_preorder_num
, parent_i
;
946 allocno_hard_regs_node_t node
, removed_node
, parent
;
947 allocno_hard_regs_subnode_t subnodes
;
948 allocno_color_data_t data
= ALLOCNO_COLOR_DATA (a
);
950 ira_assert (! data
->colorable_p
);
951 node
= data
->hard_regs_node
;
952 node_preorder_num
= node
->preorder_num
;
953 removed_node
= ALLOCNO_COLOR_DATA (removed_a
)->hard_regs_node
;
954 ira_assert (hard_reg_set_subset_p (removed_node
->hard_regs
->set
,
955 node
->hard_regs
->set
)
956 || hard_reg_set_subset_p (node
->hard_regs
->set
,
957 removed_node
->hard_regs
->set
));
958 start
= node_preorder_num
* allocno_hard_regs_nodes_num
;
959 i
= allocno_hard_regs_subnode_index
[start
+ removed_node
->preorder_num
];
962 subnodes
= allocno_hard_regs_subnodes
+ data
->hard_regs_subnodes_start
;
964 = (subnodes
[i
].left_conflict_subnodes_size
965 + MIN (subnodes
[i
].max_node_impact
966 - subnodes
[i
].left_conflict_subnodes_size
,
967 subnodes
[i
].left_conflict_size
));
968 subnodes
[i
].left_conflict_size
-= size
;
972 = (subnodes
[i
].left_conflict_subnodes_size
973 + MIN (subnodes
[i
].max_node_impact
974 - subnodes
[i
].left_conflict_subnodes_size
,
975 subnodes
[i
].left_conflict_size
));
976 if ((diff
= before_conflict_size
- conflict_size
) == 0)
978 ira_assert (conflict_size
< before_conflict_size
);
979 parent
= allocno_hard_regs_nodes
[i
+ node_preorder_num
]->parent
;
983 = allocno_hard_regs_subnode_index
[start
+ parent
->preorder_num
];
988 = (subnodes
[i
].left_conflict_subnodes_size
989 + MIN (subnodes
[i
].max_node_impact
990 - subnodes
[i
].left_conflict_subnodes_size
,
991 subnodes
[i
].left_conflict_size
));
992 subnodes
[i
].left_conflict_subnodes_size
-= diff
;
996 + ira_reg_class_max_nregs
[ALLOCNO_CLASS (a
)][ALLOCNO_MODE (a
)]
997 > data
->available_regs_num
))
999 data
->colorable_p
= true;
1003 /* Return true if allocno A has empty profitable hard regs. */
1005 empty_profitable_hard_regs (ira_allocno_t a
)
1007 allocno_color_data_t data
= ALLOCNO_COLOR_DATA (a
);
1009 return hard_reg_set_empty_p (data
->profitable_hard_regs
);
1012 /* Set up profitable hard registers for each allocno being
1015 setup_profitable_hard_regs (void)
1018 int j
, k
, nobj
, hard_regno
, nregs
, class_size
;
1021 enum reg_class aclass
;
1023 allocno_color_data_t data
;
1025 /* Initial set up from allocno classes and explicitly conflicting
1027 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
1029 a
= ira_allocnos
[i
];
1030 if ((aclass
= ALLOCNO_CLASS (a
)) == NO_REGS
)
1032 data
= ALLOCNO_COLOR_DATA (a
);
1033 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
1034 && ALLOCNO_CLASS_COST (a
) > ALLOCNO_MEMORY_COST (a
)
1035 /* Do not empty profitable regs for static chain pointer
1036 pseudo when non-local goto is used. */
1037 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a
)))
1038 CLEAR_HARD_REG_SET (data
->profitable_hard_regs
);
1041 mode
= ALLOCNO_MODE (a
);
1042 data
->profitable_hard_regs
1043 = ira_useful_class_mode_regs
[aclass
][mode
];
1044 nobj
= ALLOCNO_NUM_OBJECTS (a
);
1045 for (k
= 0; k
< nobj
; k
++)
1047 ira_object_t obj
= ALLOCNO_OBJECT (a
, k
);
1049 data
->profitable_hard_regs
1050 &= ~OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
);
1054 /* Exclude hard regs already assigned for conflicting objects. */
1055 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, i
, bi
)
1057 a
= ira_allocnos
[i
];
1058 if ((aclass
= ALLOCNO_CLASS (a
)) == NO_REGS
1059 || ! ALLOCNO_ASSIGNED_P (a
)
1060 || (hard_regno
= ALLOCNO_HARD_REGNO (a
)) < 0)
1062 mode
= ALLOCNO_MODE (a
);
1063 nregs
= hard_regno_nregs (hard_regno
, mode
);
1064 nobj
= ALLOCNO_NUM_OBJECTS (a
);
1065 for (k
= 0; k
< nobj
; k
++)
1067 ira_object_t obj
= ALLOCNO_OBJECT (a
, k
);
1068 ira_object_t conflict_obj
;
1069 ira_object_conflict_iterator oci
;
1071 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
1073 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
1075 /* We can process the conflict allocno repeatedly with
1077 if (nregs
== nobj
&& nregs
> 1)
1079 int num
= OBJECT_SUBWORD (conflict_obj
);
1081 if (REG_WORDS_BIG_ENDIAN
)
1083 (ALLOCNO_COLOR_DATA (conflict_a
)->profitable_hard_regs
,
1084 hard_regno
+ nobj
- num
- 1);
1087 (ALLOCNO_COLOR_DATA (conflict_a
)->profitable_hard_regs
,
1091 ALLOCNO_COLOR_DATA (conflict_a
)->profitable_hard_regs
1092 &= ~ira_reg_mode_hard_regset
[hard_regno
][mode
];
1096 /* Exclude too costly hard regs. */
1097 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
1099 int min_cost
= INT_MAX
;
1102 a
= ira_allocnos
[i
];
1103 if ((aclass
= ALLOCNO_CLASS (a
)) == NO_REGS
1104 || empty_profitable_hard_regs (a
))
1106 data
= ALLOCNO_COLOR_DATA (a
);
1107 if ((costs
= ALLOCNO_UPDATED_HARD_REG_COSTS (a
)) != NULL
1108 || (costs
= ALLOCNO_HARD_REG_COSTS (a
)) != NULL
)
1110 class_size
= ira_class_hard_regs_num
[aclass
];
1111 for (j
= 0; j
< class_size
; j
++)
1113 hard_regno
= ira_class_hard_regs
[aclass
][j
];
1114 if (! TEST_HARD_REG_BIT (data
->profitable_hard_regs
,
1117 if (ALLOCNO_UPDATED_MEMORY_COST (a
) < costs
[j
]
1118 /* Do not remove HARD_REGNO for static chain pointer
1119 pseudo when non-local goto is used. */
1120 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a
)))
1121 CLEAR_HARD_REG_BIT (data
->profitable_hard_regs
,
1123 else if (min_cost
> costs
[j
])
1124 min_cost
= costs
[j
];
1127 else if (ALLOCNO_UPDATED_MEMORY_COST (a
)
1128 < ALLOCNO_UPDATED_CLASS_COST (a
)
1129 /* Do not empty profitable regs for static chain
1130 pointer pseudo when non-local goto is used. */
1131 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a
)))
1132 CLEAR_HARD_REG_SET (data
->profitable_hard_regs
);
1133 if (ALLOCNO_UPDATED_CLASS_COST (a
) > min_cost
)
1134 ALLOCNO_UPDATED_CLASS_COST (a
) = min_cost
;
1140 /* This page contains functions used to choose hard registers for
1143 /* Pool for update cost records. */
1144 static object_allocator
<update_cost_record
> update_cost_record_pool
1145 ("update cost records");
1147 /* Return new update cost record with given params. */
1148 static struct update_cost_record
*
1149 get_update_cost_record (int hard_regno
, int divisor
,
1150 struct update_cost_record
*next
)
1152 struct update_cost_record
*record
;
1154 record
= update_cost_record_pool
.allocate ();
1155 record
->hard_regno
= hard_regno
;
1156 record
->divisor
= divisor
;
1157 record
->next
= next
;
1161 /* Free memory for all records in LIST. */
1163 free_update_cost_record_list (struct update_cost_record
*list
)
1165 struct update_cost_record
*next
;
1167 while (list
!= NULL
)
1170 update_cost_record_pool
.remove (list
);
1175 /* Free memory allocated for all update cost records. */
1177 finish_update_cost_records (void)
1179 update_cost_record_pool
.release ();
1182 /* Array whose element value is TRUE if the corresponding hard
1183 register was already allocated for an allocno. */
1184 static bool allocated_hardreg_p
[FIRST_PSEUDO_REGISTER
];
1186 /* Describes one element in a queue of allocnos whose costs need to be
1187 updated. Each allocno in the queue is known to have an allocno
1189 struct update_cost_queue_elem
1191 /* This element is in the queue iff CHECK == update_cost_check. */
1194 /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
1195 connecting this allocno to the one being allocated. */
1198 /* Allocno from which we are chaining costs of connected allocnos.
1199 It is used not go back in graph of allocnos connected by
1203 /* The next allocno in the queue, or null if this is the last element. */
1207 /* The first element in a queue of allocnos whose copy costs need to be
1208 updated. Null if the queue is empty. */
1209 static ira_allocno_t update_cost_queue
;
1211 /* The last element in the queue described by update_cost_queue.
1212 Not valid if update_cost_queue is null. */
1213 static struct update_cost_queue_elem
*update_cost_queue_tail
;
1215 /* A pool of elements in the queue described by update_cost_queue.
1216 Elements are indexed by ALLOCNO_NUM. */
1217 static struct update_cost_queue_elem
*update_cost_queue_elems
;
1219 /* The current value of update_costs_from_copies call count. */
1220 static int update_cost_check
;
1222 /* Allocate and initialize data necessary for function
1223 update_costs_from_copies. */
1225 initiate_cost_update (void)
1229 size
= ira_allocnos_num
* sizeof (struct update_cost_queue_elem
);
1230 update_cost_queue_elems
1231 = (struct update_cost_queue_elem
*) ira_allocate (size
);
1232 memset (update_cost_queue_elems
, 0, size
);
1233 update_cost_check
= 0;
1236 /* Deallocate data used by function update_costs_from_copies. */
1238 finish_cost_update (void)
1240 ira_free (update_cost_queue_elems
);
1241 finish_update_cost_records ();
1244 /* When we traverse allocnos to update hard register costs, the cost
1245 divisor will be multiplied by the following macro value for each
1246 hop from given allocno to directly connected allocnos. */
1247 #define COST_HOP_DIVISOR 4
1249 /* Start a new cost-updating pass. */
1251 start_update_cost (void)
1253 update_cost_check
++;
1254 update_cost_queue
= NULL
;
1257 /* Add (ALLOCNO, FROM, DIVISOR) to the end of update_cost_queue, unless
1258 ALLOCNO is already in the queue, or has NO_REGS class. */
1260 queue_update_cost (ira_allocno_t allocno
, ira_allocno_t from
, int divisor
)
1262 struct update_cost_queue_elem
*elem
;
1264 elem
= &update_cost_queue_elems
[ALLOCNO_NUM (allocno
)];
1265 if (elem
->check
!= update_cost_check
1266 && ALLOCNO_CLASS (allocno
) != NO_REGS
)
1268 elem
->check
= update_cost_check
;
1270 elem
->divisor
= divisor
;
1272 if (update_cost_queue
== NULL
)
1273 update_cost_queue
= allocno
;
1275 update_cost_queue_tail
->next
= allocno
;
1276 update_cost_queue_tail
= elem
;
1280 /* Try to remove the first element from update_cost_queue. Return
1281 false if the queue was empty, otherwise make (*ALLOCNO, *FROM,
1282 *DIVISOR) describe the removed element. */
1284 get_next_update_cost (ira_allocno_t
*allocno
, ira_allocno_t
*from
, int *divisor
)
1286 struct update_cost_queue_elem
*elem
;
1288 if (update_cost_queue
== NULL
)
1291 *allocno
= update_cost_queue
;
1292 elem
= &update_cost_queue_elems
[ALLOCNO_NUM (*allocno
)];
1294 *divisor
= elem
->divisor
;
1295 update_cost_queue
= elem
->next
;
1299 /* Increase costs of HARD_REGNO by UPDATE_COST and conflict cost by
1300 UPDATE_CONFLICT_COST for ALLOCNO. Return true if we really
1301 modified the cost. */
1303 update_allocno_cost (ira_allocno_t allocno
, int hard_regno
,
1304 int update_cost
, int update_conflict_cost
)
1307 enum reg_class aclass
= ALLOCNO_CLASS (allocno
);
1309 i
= ira_class_hard_reg_index
[aclass
][hard_regno
];
1312 ira_allocate_and_set_or_copy_costs
1313 (&ALLOCNO_UPDATED_HARD_REG_COSTS (allocno
), aclass
,
1314 ALLOCNO_UPDATED_CLASS_COST (allocno
),
1315 ALLOCNO_HARD_REG_COSTS (allocno
));
1316 ira_allocate_and_set_or_copy_costs
1317 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno
),
1318 aclass
, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (allocno
));
1319 ALLOCNO_UPDATED_HARD_REG_COSTS (allocno
)[i
] += update_cost
;
1320 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno
)[i
] += update_conflict_cost
;
1324 /* Update (decrease if DECR_P) HARD_REGNO cost of allocnos connected
1325 by copies to ALLOCNO to increase chances to remove some copies as
1326 the result of subsequent assignment. Record cost updates if
1327 RECORD_P is true. */
1329 update_costs_from_allocno (ira_allocno_t allocno
, int hard_regno
,
1330 int divisor
, bool decr_p
, bool record_p
)
1332 int cost
, update_cost
, update_conflict_cost
;
1334 enum reg_class rclass
, aclass
;
1335 ira_allocno_t another_allocno
, from
= NULL
;
1336 ira_copy_t cp
, next_cp
;
1338 rclass
= REGNO_REG_CLASS (hard_regno
);
1341 mode
= ALLOCNO_MODE (allocno
);
1342 ira_init_register_move_cost_if_necessary (mode
);
1343 for (cp
= ALLOCNO_COPIES (allocno
); cp
!= NULL
; cp
= next_cp
)
1345 if (cp
->first
== allocno
)
1347 next_cp
= cp
->next_first_allocno_copy
;
1348 another_allocno
= cp
->second
;
1350 else if (cp
->second
== allocno
)
1352 next_cp
= cp
->next_second_allocno_copy
;
1353 another_allocno
= cp
->first
;
1358 if (another_allocno
== from
)
1361 aclass
= ALLOCNO_CLASS (another_allocno
);
1362 if (! TEST_HARD_REG_BIT (reg_class_contents
[aclass
],
1364 || ALLOCNO_ASSIGNED_P (another_allocno
))
1367 /* If we have different modes use the smallest one. It is
1368 a sub-register move. It is hard to predict what LRA
1369 will reload (the pseudo or its sub-register) but LRA
1370 will try to minimize the data movement. Also for some
1371 register classes bigger modes might be invalid,
1372 e.g. DImode for AREG on x86. For such cases the
1373 register move cost will be maximal. */
1374 mode
= narrower_subreg_mode (mode
, ALLOCNO_MODE (cp
->second
));
1376 cost
= (cp
->second
== allocno
1377 ? ira_register_move_cost
[mode
][rclass
][aclass
]
1378 : ira_register_move_cost
[mode
][aclass
][rclass
]);
1382 update_conflict_cost
= update_cost
= cp
->freq
* cost
/ divisor
;
1384 if (ALLOCNO_COLOR_DATA (another_allocno
) != NULL
1385 && (ALLOCNO_COLOR_DATA (allocno
)->first_thread_allocno
1386 != ALLOCNO_COLOR_DATA (another_allocno
)->first_thread_allocno
))
1387 /* Decrease conflict cost of ANOTHER_ALLOCNO if it is not
1388 in the same allocation thread. */
1389 update_conflict_cost
/= COST_HOP_DIVISOR
;
1391 if (update_cost
== 0)
1394 if (! update_allocno_cost (another_allocno
, hard_regno
,
1395 update_cost
, update_conflict_cost
))
1397 queue_update_cost (another_allocno
, allocno
, divisor
* COST_HOP_DIVISOR
);
1398 if (record_p
&& ALLOCNO_COLOR_DATA (another_allocno
) != NULL
)
1399 ALLOCNO_COLOR_DATA (another_allocno
)->update_cost_records
1400 = get_update_cost_record (hard_regno
, divisor
,
1401 ALLOCNO_COLOR_DATA (another_allocno
)
1402 ->update_cost_records
);
1405 while (get_next_update_cost (&allocno
, &from
, &divisor
));
1408 /* Decrease preferred ALLOCNO hard register costs and costs of
1409 allocnos connected to ALLOCNO through copy. */
1411 update_costs_from_prefs (ira_allocno_t allocno
)
1415 start_update_cost ();
1416 for (pref
= ALLOCNO_PREFS (allocno
); pref
!= NULL
; pref
= pref
->next_pref
)
1417 update_costs_from_allocno (allocno
, pref
->hard_regno
,
1418 COST_HOP_DIVISOR
, true, true);
1421 /* Update (decrease if DECR_P) the cost of allocnos connected to
1422 ALLOCNO through copies to increase chances to remove some copies as
1423 the result of subsequent assignment. ALLOCNO was just assigned to
1424 a hard register. Record cost updates if RECORD_P is true. */
1426 update_costs_from_copies (ira_allocno_t allocno
, bool decr_p
, bool record_p
)
1430 hard_regno
= ALLOCNO_HARD_REGNO (allocno
);
1431 ira_assert (hard_regno
>= 0 && ALLOCNO_CLASS (allocno
) != NO_REGS
);
1432 start_update_cost ();
1433 update_costs_from_allocno (allocno
, hard_regno
, 1, decr_p
, record_p
);
1436 /* Update conflict_allocno_hard_prefs of allocnos conflicting with
1439 update_conflict_allocno_hard_prefs (ira_allocno_t allocno
)
1441 int l
, nr
= ALLOCNO_NUM_OBJECTS (allocno
);
1443 for (l
= 0; l
< nr
; l
++)
1445 ira_object_t conflict_obj
, obj
= ALLOCNO_OBJECT (allocno
, l
);
1446 ira_object_conflict_iterator oci
;
1448 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
1450 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
1451 allocno_color_data_t conflict_data
= ALLOCNO_COLOR_DATA (conflict_a
);
1454 if (!(hard_reg_set_intersect_p
1455 (ALLOCNO_COLOR_DATA (allocno
)->profitable_hard_regs
,
1456 conflict_data
->profitable_hard_regs
)))
1458 for (pref
= ALLOCNO_PREFS (allocno
);
1460 pref
= pref
->next_pref
)
1461 conflict_data
->conflict_allocno_hard_prefs
+= pref
->freq
;
1466 /* Restore costs of allocnos connected to ALLOCNO by copies as it was
1467 before updating costs of these allocnos from given allocno. This
1468 is a wise thing to do as if given allocno did not get an expected
1469 hard reg, using smaller cost of the hard reg for allocnos connected
1470 by copies to given allocno becomes actually misleading. Free all
1471 update cost records for ALLOCNO as we don't need them anymore. */
1473 restore_costs_from_copies (ira_allocno_t allocno
)
1475 struct update_cost_record
*records
, *curr
;
1477 if (ALLOCNO_COLOR_DATA (allocno
) == NULL
)
1479 records
= ALLOCNO_COLOR_DATA (allocno
)->update_cost_records
;
1480 start_update_cost ();
1481 for (curr
= records
; curr
!= NULL
; curr
= curr
->next
)
1482 update_costs_from_allocno (allocno
, curr
->hard_regno
,
1483 curr
->divisor
, true, false);
1484 free_update_cost_record_list (records
);
1485 ALLOCNO_COLOR_DATA (allocno
)->update_cost_records
= NULL
;
1488 /* This function updates COSTS (decrease if DECR_P) for hard_registers
1489 of ACLASS by conflict costs of the unassigned allocnos
1490 connected by copies with allocnos in update_cost_queue. This
1491 update increases chances to remove some copies. */
1493 update_conflict_hard_regno_costs (int *costs
, enum reg_class aclass
,
1496 int i
, cost
, class_size
, freq
, mult
, div
, divisor
;
1497 int index
, hard_regno
;
1498 int *conflict_costs
;
1500 enum reg_class another_aclass
;
1501 ira_allocno_t allocno
, another_allocno
, from
;
1502 ira_copy_t cp
, next_cp
;
1504 while (get_next_update_cost (&allocno
, &from
, &divisor
))
1505 for (cp
= ALLOCNO_COPIES (allocno
); cp
!= NULL
; cp
= next_cp
)
1507 if (cp
->first
== allocno
)
1509 next_cp
= cp
->next_first_allocno_copy
;
1510 another_allocno
= cp
->second
;
1512 else if (cp
->second
== allocno
)
1514 next_cp
= cp
->next_second_allocno_copy
;
1515 another_allocno
= cp
->first
;
1520 if (another_allocno
== from
)
1523 another_aclass
= ALLOCNO_CLASS (another_allocno
);
1524 if (! ira_reg_classes_intersect_p
[aclass
][another_aclass
]
1525 || ALLOCNO_ASSIGNED_P (another_allocno
)
1526 || ALLOCNO_COLOR_DATA (another_allocno
)->may_be_spilled_p
)
1528 class_size
= ira_class_hard_regs_num
[another_aclass
];
1529 ira_allocate_and_copy_costs
1530 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno
),
1531 another_aclass
, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno
));
1533 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno
);
1534 if (conflict_costs
== NULL
)
1539 freq
= ALLOCNO_FREQ (another_allocno
);
1542 div
= freq
* divisor
;
1544 for (i
= class_size
- 1; i
>= 0; i
--)
1546 hard_regno
= ira_class_hard_regs
[another_aclass
][i
];
1547 ira_assert (hard_regno
>= 0);
1548 index
= ira_class_hard_reg_index
[aclass
][hard_regno
];
1551 cost
= (int) (((int64_t) conflict_costs
[i
] * mult
) / div
);
1557 costs
[index
] += cost
;
1560 /* Probably 5 hops will be enough. */
1562 && divisor
<= (COST_HOP_DIVISOR
1565 * COST_HOP_DIVISOR
))
1566 queue_update_cost (another_allocno
, allocno
, divisor
* COST_HOP_DIVISOR
);
1570 /* Set up conflicting (through CONFLICT_REGS) for each object of
1571 allocno A and the start allocno profitable regs (through
1572 START_PROFITABLE_REGS). Remember that the start profitable regs
1573 exclude hard regs which cannot hold value of mode of allocno A.
1574 This covers mostly cases when multi-register value should be
1577 get_conflict_and_start_profitable_regs (ira_allocno_t a
, bool retry_p
,
1578 HARD_REG_SET
*conflict_regs
,
1579 HARD_REG_SET
*start_profitable_regs
)
1584 nwords
= ALLOCNO_NUM_OBJECTS (a
);
1585 for (i
= 0; i
< nwords
; i
++)
1587 obj
= ALLOCNO_OBJECT (a
, i
);
1588 conflict_regs
[i
] = OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
);
1591 *start_profitable_regs
1592 = (reg_class_contents
[ALLOCNO_CLASS (a
)]
1593 &~ (ira_prohibited_class_mode_regs
1594 [ALLOCNO_CLASS (a
)][ALLOCNO_MODE (a
)]));
1596 *start_profitable_regs
= ALLOCNO_COLOR_DATA (a
)->profitable_hard_regs
;
1599 /* Return true if HARD_REGNO is ok for assigning to allocno A with
1600 PROFITABLE_REGS and whose objects have CONFLICT_REGS. */
1602 check_hard_reg_p (ira_allocno_t a
, int hard_regno
,
1603 HARD_REG_SET
*conflict_regs
, HARD_REG_SET profitable_regs
)
1605 int j
, nwords
, nregs
;
1606 enum reg_class aclass
;
1609 aclass
= ALLOCNO_CLASS (a
);
1610 mode
= ALLOCNO_MODE (a
);
1611 if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs
[aclass
][mode
],
1614 /* Checking only profitable hard regs. */
1615 if (! TEST_HARD_REG_BIT (profitable_regs
, hard_regno
))
1617 nregs
= hard_regno_nregs (hard_regno
, mode
);
1618 nwords
= ALLOCNO_NUM_OBJECTS (a
);
1619 for (j
= 0; j
< nregs
; j
++)
1622 int set_to_test_start
= 0, set_to_test_end
= nwords
;
1624 if (nregs
== nwords
)
1626 if (REG_WORDS_BIG_ENDIAN
)
1627 set_to_test_start
= nwords
- j
- 1;
1629 set_to_test_start
= j
;
1630 set_to_test_end
= set_to_test_start
+ 1;
1632 for (k
= set_to_test_start
; k
< set_to_test_end
; k
++)
1633 if (TEST_HARD_REG_BIT (conflict_regs
[k
], hard_regno
+ j
))
1635 if (k
!= set_to_test_end
)
1641 /* Return number of registers needed to be saved and restored at
1642 function prologue/epilogue if we allocate HARD_REGNO to hold value
1645 calculate_saved_nregs (int hard_regno
, machine_mode mode
)
1650 ira_assert (hard_regno
>= 0);
1651 for (i
= hard_regno_nregs (hard_regno
, mode
) - 1; i
>= 0; i
--)
1652 if (!allocated_hardreg_p
[hard_regno
+ i
]
1653 && !crtl
->abi
->clobbers_full_reg_p (hard_regno
+ i
)
1654 && !LOCAL_REGNO (hard_regno
+ i
))
1659 /* Choose a hard register for allocno A. If RETRY_P is TRUE, it means
1660 that the function called from function
1661 `ira_reassign_conflict_allocnos' and `allocno_reload_assign'. In
1662 this case some allocno data are not defined or updated and we
1663 should not touch these data. The function returns true if we
1664 managed to assign a hard register to the allocno.
1666 To assign a hard register, first of all we calculate all conflict
1667 hard registers which can come from conflicting allocnos with
1668 already assigned hard registers. After that we find first free
1669 hard register with the minimal cost. During hard register cost
1670 calculation we take conflict hard register costs into account to
1671 give a chance for conflicting allocnos to get a better hard
1672 register in the future.
1674 If the best hard register cost is bigger than cost of memory usage
1675 for the allocno, we don't assign a hard register to given allocno
1678 If we assign a hard register to the allocno, we update costs of the
1679 hard register for allocnos connected by copies to improve a chance
1680 to coalesce insns represented by the copies when we assign hard
1681 registers to the allocnos connected by the copies. */
1683 assign_hard_reg (ira_allocno_t a
, bool retry_p
)
1685 HARD_REG_SET conflicting_regs
[2], profitable_hard_regs
;
1686 int i
, j
, hard_regno
, best_hard_regno
, class_size
;
1687 int cost
, mem_cost
, min_cost
, full_cost
, min_full_cost
, nwords
, word
;
1689 enum reg_class aclass
;
1691 static int costs
[FIRST_PSEUDO_REGISTER
], full_costs
[FIRST_PSEUDO_REGISTER
];
1693 enum reg_class rclass
;
1696 bool no_stack_reg_p
;
1699 ira_assert (! ALLOCNO_ASSIGNED_P (a
));
1700 get_conflict_and_start_profitable_regs (a
, retry_p
,
1702 &profitable_hard_regs
);
1703 aclass
= ALLOCNO_CLASS (a
);
1704 class_size
= ira_class_hard_regs_num
[aclass
];
1705 best_hard_regno
= -1;
1706 memset (full_costs
, 0, sizeof (int) * class_size
);
1708 memset (costs
, 0, sizeof (int) * class_size
);
1709 memset (full_costs
, 0, sizeof (int) * class_size
);
1711 no_stack_reg_p
= false;
1714 start_update_cost ();
1715 mem_cost
+= ALLOCNO_UPDATED_MEMORY_COST (a
);
1717 ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a
),
1718 aclass
, ALLOCNO_HARD_REG_COSTS (a
));
1719 a_costs
= ALLOCNO_UPDATED_HARD_REG_COSTS (a
);
1721 no_stack_reg_p
= no_stack_reg_p
|| ALLOCNO_TOTAL_NO_STACK_REG_P (a
);
1723 cost
= ALLOCNO_UPDATED_CLASS_COST (a
);
1724 for (i
= 0; i
< class_size
; i
++)
1725 if (a_costs
!= NULL
)
1727 costs
[i
] += a_costs
[i
];
1728 full_costs
[i
] += a_costs
[i
];
1733 full_costs
[i
] += cost
;
1735 nwords
= ALLOCNO_NUM_OBJECTS (a
);
1736 curr_allocno_process
++;
1737 for (word
= 0; word
< nwords
; word
++)
1739 ira_object_t conflict_obj
;
1740 ira_object_t obj
= ALLOCNO_OBJECT (a
, word
);
1741 ira_object_conflict_iterator oci
;
1743 /* Take preferences of conflicting allocnos into account. */
1744 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
1746 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
1747 enum reg_class conflict_aclass
;
1748 allocno_color_data_t data
= ALLOCNO_COLOR_DATA (conflict_a
);
1750 /* Reload can give another class so we need to check all
1753 && ((!ALLOCNO_ASSIGNED_P (conflict_a
)
1754 || ALLOCNO_HARD_REGNO (conflict_a
) < 0)
1755 && !(hard_reg_set_intersect_p
1756 (profitable_hard_regs
,
1758 (conflict_a
)->profitable_hard_regs
))))
1760 /* All conflict allocnos are in consideration bitmap
1761 when retry_p is false. It might change in future and
1762 if it happens the assert will be broken. It means
1763 the code should be modified for the new
1765 ira_assert (bitmap_bit_p (consideration_allocno_bitmap
,
1766 ALLOCNO_NUM (conflict_a
)));
1769 conflict_aclass
= ALLOCNO_CLASS (conflict_a
);
1770 ira_assert (ira_reg_classes_intersect_p
1771 [aclass
][conflict_aclass
]);
1772 if (ALLOCNO_ASSIGNED_P (conflict_a
))
1774 hard_regno
= ALLOCNO_HARD_REGNO (conflict_a
);
1776 && (ira_hard_reg_set_intersection_p
1777 (hard_regno
, ALLOCNO_MODE (conflict_a
),
1778 reg_class_contents
[aclass
])))
1780 int n_objects
= ALLOCNO_NUM_OBJECTS (conflict_a
);
1783 mode
= ALLOCNO_MODE (conflict_a
);
1784 conflict_nregs
= hard_regno_nregs (hard_regno
, mode
);
1785 if (conflict_nregs
== n_objects
&& conflict_nregs
> 1)
1787 int num
= OBJECT_SUBWORD (conflict_obj
);
1789 if (REG_WORDS_BIG_ENDIAN
)
1790 SET_HARD_REG_BIT (conflicting_regs
[word
],
1791 hard_regno
+ n_objects
- num
- 1);
1793 SET_HARD_REG_BIT (conflicting_regs
[word
],
1797 conflicting_regs
[word
]
1798 |= ira_reg_mode_hard_regset
[hard_regno
][mode
];
1799 if (hard_reg_set_subset_p (profitable_hard_regs
,
1800 conflicting_regs
[word
]))
1805 && ! ALLOCNO_COLOR_DATA (conflict_a
)->may_be_spilled_p
1806 /* Don't process the conflict allocno twice. */
1807 && (ALLOCNO_COLOR_DATA (conflict_a
)->last_process
1808 != curr_allocno_process
))
1810 int k
, *conflict_costs
;
1812 ALLOCNO_COLOR_DATA (conflict_a
)->last_process
1813 = curr_allocno_process
;
1814 ira_allocate_and_copy_costs
1815 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a
),
1817 ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_a
));
1819 = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a
);
1820 if (conflict_costs
!= NULL
)
1821 for (j
= class_size
- 1; j
>= 0; j
--)
1823 hard_regno
= ira_class_hard_regs
[aclass
][j
];
1824 ira_assert (hard_regno
>= 0);
1825 k
= ira_class_hard_reg_index
[conflict_aclass
][hard_regno
];
1827 /* If HARD_REGNO is not available for CONFLICT_A,
1828 the conflict would be ignored, since HARD_REGNO
1829 will never be assigned to CONFLICT_A. */
1830 || !TEST_HARD_REG_BIT (data
->profitable_hard_regs
,
1833 full_costs
[j
] -= conflict_costs
[k
];
1835 queue_update_cost (conflict_a
, NULL
, COST_HOP_DIVISOR
);
1841 /* Take into account preferences of allocnos connected by copies to
1842 the conflict allocnos. */
1843 update_conflict_hard_regno_costs (full_costs
, aclass
, true);
1845 /* Take preferences of allocnos connected by copies into
1849 start_update_cost ();
1850 queue_update_cost (a
, NULL
, COST_HOP_DIVISOR
);
1851 update_conflict_hard_regno_costs (full_costs
, aclass
, false);
1853 min_cost
= min_full_cost
= INT_MAX
;
1854 /* We don't care about giving callee saved registers to allocnos no
1855 living through calls because call clobbered registers are
1856 allocated first (it is usual practice to put them first in
1857 REG_ALLOC_ORDER). */
1858 mode
= ALLOCNO_MODE (a
);
1859 for (i
= 0; i
< class_size
; i
++)
1861 hard_regno
= ira_class_hard_regs
[aclass
][i
];
1864 && FIRST_STACK_REG
<= hard_regno
&& hard_regno
<= LAST_STACK_REG
)
1867 if (! check_hard_reg_p (a
, hard_regno
,
1868 conflicting_regs
, profitable_hard_regs
))
1871 full_cost
= full_costs
[i
];
1872 if (!HONOR_REG_ALLOC_ORDER
)
1874 if ((saved_nregs
= calculate_saved_nregs (hard_regno
, mode
)) != 0)
1875 /* We need to save/restore the hard register in
1876 epilogue/prologue. Therefore we increase the cost. */
1878 rclass
= REGNO_REG_CLASS (hard_regno
);
1879 add_cost
= ((ira_memory_move_cost
[mode
][rclass
][0]
1880 + ira_memory_move_cost
[mode
][rclass
][1])
1881 * saved_nregs
/ hard_regno_nregs (hard_regno
,
1884 full_cost
+= add_cost
;
1887 if (min_cost
> cost
)
1889 if (min_full_cost
> full_cost
)
1891 min_full_cost
= full_cost
;
1892 best_hard_regno
= hard_regno
;
1893 ira_assert (hard_regno
>= 0);
1896 if (min_full_cost
> mem_cost
1897 /* Do not spill static chain pointer pseudo when non-local goto
1899 && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a
)))
1901 if (! retry_p
&& internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
1902 fprintf (ira_dump_file
, "(memory is more profitable %d vs %d) ",
1903 mem_cost
, min_full_cost
);
1904 best_hard_regno
= -1;
1907 if (best_hard_regno
>= 0)
1909 for (i
= hard_regno_nregs (best_hard_regno
, mode
) - 1; i
>= 0; i
--)
1910 allocated_hardreg_p
[best_hard_regno
+ i
] = true;
1913 restore_costs_from_copies (a
);
1914 ALLOCNO_HARD_REGNO (a
) = best_hard_regno
;
1915 ALLOCNO_ASSIGNED_P (a
) = true;
1916 if (best_hard_regno
>= 0)
1917 update_costs_from_copies (a
, true, ! retry_p
);
1918 ira_assert (ALLOCNO_CLASS (a
) == aclass
);
1919 /* We don't need updated costs anymore. */
1920 ira_free_allocno_updated_costs (a
);
1921 return best_hard_regno
>= 0;
1926 /* An array used to sort copies. */
1927 static ira_copy_t
*sorted_copies
;
1929 /* If allocno A is a cap, return non-cap allocno from which A is
1930 created. Otherwise, return A. */
1931 static ira_allocno_t
1932 get_cap_member (ira_allocno_t a
)
1934 ira_allocno_t member
;
1936 while ((member
= ALLOCNO_CAP_MEMBER (a
)) != NULL
)
1941 /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
1942 used to find a conflict for new allocnos or allocnos with the
1943 different allocno classes. */
1945 allocnos_conflict_by_live_ranges_p (ira_allocno_t a1
, ira_allocno_t a2
)
1949 int n1
= ALLOCNO_NUM_OBJECTS (a1
);
1950 int n2
= ALLOCNO_NUM_OBJECTS (a2
);
1954 reg1
= regno_reg_rtx
[ALLOCNO_REGNO (a1
)];
1955 reg2
= regno_reg_rtx
[ALLOCNO_REGNO (a2
)];
1956 if (reg1
!= NULL
&& reg2
!= NULL
1957 && ORIGINAL_REGNO (reg1
) == ORIGINAL_REGNO (reg2
))
1960 /* We don't keep live ranges for caps because they can be quite big.
1961 Use ranges of non-cap allocno from which caps are created. */
1962 a1
= get_cap_member (a1
);
1963 a2
= get_cap_member (a2
);
1964 for (i
= 0; i
< n1
; i
++)
1966 ira_object_t c1
= ALLOCNO_OBJECT (a1
, i
);
1968 for (j
= 0; j
< n2
; j
++)
1970 ira_object_t c2
= ALLOCNO_OBJECT (a2
, j
);
1972 if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1
),
1973 OBJECT_LIVE_RANGES (c2
)))
1980 /* The function is used to sort copies according to their execution
1983 copy_freq_compare_func (const void *v1p
, const void *v2p
)
1985 ira_copy_t cp1
= *(const ira_copy_t
*) v1p
, cp2
= *(const ira_copy_t
*) v2p
;
1993 /* If frequencies are equal, sort by copies, so that the results of
1994 qsort leave nothing to chance. */
1995 return cp1
->num
- cp2
->num
;
2000 /* Return true if any allocno from thread of A1 conflicts with any
2001 allocno from thread A2. */
2003 allocno_thread_conflict_p (ira_allocno_t a1
, ira_allocno_t a2
)
2005 ira_allocno_t a
, conflict_a
;
2007 for (a
= ALLOCNO_COLOR_DATA (a2
)->next_thread_allocno
;;
2008 a
= ALLOCNO_COLOR_DATA (a
)->next_thread_allocno
)
2010 for (conflict_a
= ALLOCNO_COLOR_DATA (a1
)->next_thread_allocno
;;
2011 conflict_a
= ALLOCNO_COLOR_DATA (conflict_a
)->next_thread_allocno
)
2013 if (allocnos_conflict_by_live_ranges_p (a
, conflict_a
))
2015 if (conflict_a
== a1
)
2024 /* Merge two threads given correspondingly by their first allocnos T1
2025 and T2 (more accurately merging T2 into T1). */
2027 merge_threads (ira_allocno_t t1
, ira_allocno_t t2
)
2029 ira_allocno_t a
, next
, last
;
2031 gcc_assert (t1
!= t2
2032 && ALLOCNO_COLOR_DATA (t1
)->first_thread_allocno
== t1
2033 && ALLOCNO_COLOR_DATA (t2
)->first_thread_allocno
== t2
);
2034 for (last
= t2
, a
= ALLOCNO_COLOR_DATA (t2
)->next_thread_allocno
;;
2035 a
= ALLOCNO_COLOR_DATA (a
)->next_thread_allocno
)
2037 ALLOCNO_COLOR_DATA (a
)->first_thread_allocno
= t1
;
2042 next
= ALLOCNO_COLOR_DATA (t1
)->next_thread_allocno
;
2043 ALLOCNO_COLOR_DATA (t1
)->next_thread_allocno
= t2
;
2044 ALLOCNO_COLOR_DATA (last
)->next_thread_allocno
= next
;
2045 ALLOCNO_COLOR_DATA (t1
)->thread_freq
+= ALLOCNO_COLOR_DATA (t2
)->thread_freq
;
2048 /* Create threads by processing CP_NUM copies from sorted copies. We
2049 process the most expensive copies first. */
2051 form_threads_from_copies (int cp_num
)
2053 ira_allocno_t a
, thread1
, thread2
;
2057 qsort (sorted_copies
, cp_num
, sizeof (ira_copy_t
), copy_freq_compare_func
);
2058 /* Form threads processing copies, most frequently executed
2060 for (; cp_num
!= 0;)
2062 for (i
= 0; i
< cp_num
; i
++)
2064 cp
= sorted_copies
[i
];
2065 thread1
= ALLOCNO_COLOR_DATA (cp
->first
)->first_thread_allocno
;
2066 thread2
= ALLOCNO_COLOR_DATA (cp
->second
)->first_thread_allocno
;
2067 if (thread1
== thread2
)
2069 if (! allocno_thread_conflict_p (thread1
, thread2
))
2071 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2074 " Forming thread by copy %d:a%dr%d-a%dr%d (freq=%d):\n",
2075 cp
->num
, ALLOCNO_NUM (cp
->first
), ALLOCNO_REGNO (cp
->first
),
2076 ALLOCNO_NUM (cp
->second
), ALLOCNO_REGNO (cp
->second
),
2078 merge_threads (thread1
, thread2
);
2079 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2081 thread1
= ALLOCNO_COLOR_DATA (thread1
)->first_thread_allocno
;
2082 fprintf (ira_dump_file
, " Result (freq=%d): a%dr%d(%d)",
2083 ALLOCNO_COLOR_DATA (thread1
)->thread_freq
,
2084 ALLOCNO_NUM (thread1
), ALLOCNO_REGNO (thread1
),
2085 ALLOCNO_FREQ (thread1
));
2086 for (a
= ALLOCNO_COLOR_DATA (thread1
)->next_thread_allocno
;
2088 a
= ALLOCNO_COLOR_DATA (a
)->next_thread_allocno
)
2089 fprintf (ira_dump_file
, " a%dr%d(%d)",
2090 ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
),
2092 fprintf (ira_dump_file
, "\n");
2098 /* Collect the rest of copies. */
2099 for (n
= 0; i
< cp_num
; i
++)
2101 cp
= sorted_copies
[i
];
2102 if (ALLOCNO_COLOR_DATA (cp
->first
)->first_thread_allocno
2103 != ALLOCNO_COLOR_DATA (cp
->second
)->first_thread_allocno
)
2104 sorted_copies
[n
++] = cp
;
2110 /* Create threads by processing copies of all alocnos from BUCKET. We
2111 process the most expensive copies first. */
2113 form_threads_from_bucket (ira_allocno_t bucket
)
2116 ira_copy_t cp
, next_cp
;
2119 for (a
= bucket
; a
!= NULL
; a
= ALLOCNO_COLOR_DATA (a
)->next_bucket_allocno
)
2121 for (cp
= ALLOCNO_COPIES (a
); cp
!= NULL
; cp
= next_cp
)
2125 next_cp
= cp
->next_first_allocno_copy
;
2126 sorted_copies
[cp_num
++] = cp
;
2128 else if (cp
->second
== a
)
2129 next_cp
= cp
->next_second_allocno_copy
;
2134 form_threads_from_copies (cp_num
);
2137 /* Create threads by processing copies of colorable allocno A. We
2138 process most expensive copies first. */
2140 form_threads_from_colorable_allocno (ira_allocno_t a
)
2142 ira_allocno_t another_a
;
2143 ira_copy_t cp
, next_cp
;
2146 for (cp
= ALLOCNO_COPIES (a
); cp
!= NULL
; cp
= next_cp
)
2150 next_cp
= cp
->next_first_allocno_copy
;
2151 another_a
= cp
->second
;
2153 else if (cp
->second
== a
)
2155 next_cp
= cp
->next_second_allocno_copy
;
2156 another_a
= cp
->first
;
2160 if ((! ALLOCNO_COLOR_DATA (another_a
)->in_graph_p
2161 && !ALLOCNO_COLOR_DATA (another_a
)->may_be_spilled_p
)
2162 || ALLOCNO_COLOR_DATA (another_a
)->colorable_p
)
2163 sorted_copies
[cp_num
++] = cp
;
2165 form_threads_from_copies (cp_num
);
2168 /* Form initial threads which contain only one allocno. */
2170 init_allocno_threads (void)
2176 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, j
, bi
)
2178 a
= ira_allocnos
[j
];
2179 /* Set up initial thread data: */
2180 ALLOCNO_COLOR_DATA (a
)->first_thread_allocno
2181 = ALLOCNO_COLOR_DATA (a
)->next_thread_allocno
= a
;
2182 ALLOCNO_COLOR_DATA (a
)->thread_freq
= ALLOCNO_FREQ (a
);
2188 /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
2190 /* Bucket of allocnos that can colored currently without spilling. */
2191 static ira_allocno_t colorable_allocno_bucket
;
2193 /* Bucket of allocnos that might be not colored currently without
2195 static ira_allocno_t uncolorable_allocno_bucket
;
2197 /* The current number of allocnos in the uncolorable_bucket. */
2198 static int uncolorable_allocnos_num
;
2200 /* Return the current spill priority of allocno A. The less the
2201 number, the more preferable the allocno for spilling. */
2203 allocno_spill_priority (ira_allocno_t a
)
2205 allocno_color_data_t data
= ALLOCNO_COLOR_DATA (a
);
2208 / (ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
)
2209 * ira_reg_class_max_nregs
[ALLOCNO_CLASS (a
)][ALLOCNO_MODE (a
)]
2213 /* Add allocno A to bucket *BUCKET_PTR. A should be not in a bucket
2216 add_allocno_to_bucket (ira_allocno_t a
, ira_allocno_t
*bucket_ptr
)
2218 ira_allocno_t first_a
;
2219 allocno_color_data_t data
;
2221 if (bucket_ptr
== &uncolorable_allocno_bucket
2222 && ALLOCNO_CLASS (a
) != NO_REGS
)
2224 uncolorable_allocnos_num
++;
2225 ira_assert (uncolorable_allocnos_num
> 0);
2227 first_a
= *bucket_ptr
;
2228 data
= ALLOCNO_COLOR_DATA (a
);
2229 data
->next_bucket_allocno
= first_a
;
2230 data
->prev_bucket_allocno
= NULL
;
2231 if (first_a
!= NULL
)
2232 ALLOCNO_COLOR_DATA (first_a
)->prev_bucket_allocno
= a
;
2236 /* Compare two allocnos to define which allocno should be pushed first
2237 into the coloring stack. If the return is a negative number, the
2238 allocno given by the first parameter will be pushed first. In this
2239 case such allocno has less priority than the second one and the
2240 hard register will be assigned to it after assignment to the second
2241 one. As the result of such assignment order, the second allocno
2242 has a better chance to get the best hard register. */
2244 bucket_allocno_compare_func (const void *v1p
, const void *v2p
)
2246 ira_allocno_t a1
= *(const ira_allocno_t
*) v1p
;
2247 ira_allocno_t a2
= *(const ira_allocno_t
*) v2p
;
2248 int diff
, freq1
, freq2
, a1_num
, a2_num
, pref1
, pref2
;
2249 ira_allocno_t t1
= ALLOCNO_COLOR_DATA (a1
)->first_thread_allocno
;
2250 ira_allocno_t t2
= ALLOCNO_COLOR_DATA (a2
)->first_thread_allocno
;
2251 int cl1
= ALLOCNO_CLASS (a1
), cl2
= ALLOCNO_CLASS (a2
);
2253 freq1
= ALLOCNO_COLOR_DATA (t1
)->thread_freq
;
2254 freq2
= ALLOCNO_COLOR_DATA (t2
)->thread_freq
;
2255 if ((diff
= freq1
- freq2
) != 0)
2258 if ((diff
= ALLOCNO_NUM (t2
) - ALLOCNO_NUM (t1
)) != 0)
2261 /* Push pseudos requiring less hard registers first. It means that
2262 we will assign pseudos requiring more hard registers first
2263 avoiding creation small holes in free hard register file into
2264 which the pseudos requiring more hard registers cannot fit. */
2265 if ((diff
= (ira_reg_class_max_nregs
[cl1
][ALLOCNO_MODE (a1
)]
2266 - ira_reg_class_max_nregs
[cl2
][ALLOCNO_MODE (a2
)])) != 0)
2269 freq1
= ALLOCNO_FREQ (a1
);
2270 freq2
= ALLOCNO_FREQ (a2
);
2271 if ((diff
= freq1
- freq2
) != 0)
2274 a1_num
= ALLOCNO_COLOR_DATA (a1
)->available_regs_num
;
2275 a2_num
= ALLOCNO_COLOR_DATA (a2
)->available_regs_num
;
2276 if ((diff
= a2_num
- a1_num
) != 0)
2278 /* Push allocnos with minimal conflict_allocno_hard_prefs first. */
2279 pref1
= ALLOCNO_COLOR_DATA (a1
)->conflict_allocno_hard_prefs
;
2280 pref2
= ALLOCNO_COLOR_DATA (a2
)->conflict_allocno_hard_prefs
;
2281 if ((diff
= pref1
- pref2
) != 0)
2283 return ALLOCNO_NUM (a2
) - ALLOCNO_NUM (a1
);
2286 /* Sort bucket *BUCKET_PTR and return the result through
2289 sort_bucket (ira_allocno_t
*bucket_ptr
,
2290 int (*compare_func
) (const void *, const void *))
2292 ira_allocno_t a
, head
;
2295 for (n
= 0, a
= *bucket_ptr
;
2297 a
= ALLOCNO_COLOR_DATA (a
)->next_bucket_allocno
)
2298 sorted_allocnos
[n
++] = a
;
2301 qsort (sorted_allocnos
, n
, sizeof (ira_allocno_t
), compare_func
);
2303 for (n
--; n
>= 0; n
--)
2305 a
= sorted_allocnos
[n
];
2306 ALLOCNO_COLOR_DATA (a
)->next_bucket_allocno
= head
;
2307 ALLOCNO_COLOR_DATA (a
)->prev_bucket_allocno
= NULL
;
2309 ALLOCNO_COLOR_DATA (head
)->prev_bucket_allocno
= a
;
2315 /* Add ALLOCNO to colorable bucket maintaining the order according
2316 their priority. ALLOCNO should be not in a bucket before the
2319 add_allocno_to_ordered_colorable_bucket (ira_allocno_t allocno
)
2321 ira_allocno_t before
, after
;
2323 form_threads_from_colorable_allocno (allocno
);
2324 for (before
= colorable_allocno_bucket
, after
= NULL
;
2327 before
= ALLOCNO_COLOR_DATA (before
)->next_bucket_allocno
)
2328 if (bucket_allocno_compare_func (&allocno
, &before
) < 0)
2330 ALLOCNO_COLOR_DATA (allocno
)->next_bucket_allocno
= before
;
2331 ALLOCNO_COLOR_DATA (allocno
)->prev_bucket_allocno
= after
;
2333 colorable_allocno_bucket
= allocno
;
2335 ALLOCNO_COLOR_DATA (after
)->next_bucket_allocno
= allocno
;
2337 ALLOCNO_COLOR_DATA (before
)->prev_bucket_allocno
= allocno
;
2340 /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
2343 delete_allocno_from_bucket (ira_allocno_t allocno
, ira_allocno_t
*bucket_ptr
)
2345 ira_allocno_t prev_allocno
, next_allocno
;
2347 if (bucket_ptr
== &uncolorable_allocno_bucket
2348 && ALLOCNO_CLASS (allocno
) != NO_REGS
)
2350 uncolorable_allocnos_num
--;
2351 ira_assert (uncolorable_allocnos_num
>= 0);
2353 prev_allocno
= ALLOCNO_COLOR_DATA (allocno
)->prev_bucket_allocno
;
2354 next_allocno
= ALLOCNO_COLOR_DATA (allocno
)->next_bucket_allocno
;
2355 if (prev_allocno
!= NULL
)
2356 ALLOCNO_COLOR_DATA (prev_allocno
)->next_bucket_allocno
= next_allocno
;
2359 ira_assert (*bucket_ptr
== allocno
);
2360 *bucket_ptr
= next_allocno
;
2362 if (next_allocno
!= NULL
)
2363 ALLOCNO_COLOR_DATA (next_allocno
)->prev_bucket_allocno
= prev_allocno
;
2366 /* Put allocno A onto the coloring stack without removing it from its
2367 bucket. Pushing allocno to the coloring stack can result in moving
2368 conflicting allocnos from the uncolorable bucket to the colorable
2369 one. Update conflict_allocno_hard_prefs of the conflicting
2370 allocnos which are not on stack yet. */
2372 push_allocno_to_stack (ira_allocno_t a
)
2374 enum reg_class aclass
;
2375 allocno_color_data_t data
, conflict_data
;
2376 int size
, i
, n
= ALLOCNO_NUM_OBJECTS (a
);
2378 data
= ALLOCNO_COLOR_DATA (a
);
2379 data
->in_graph_p
= false;
2380 allocno_stack_vec
.safe_push (a
);
2381 aclass
= ALLOCNO_CLASS (a
);
2382 if (aclass
== NO_REGS
)
2384 size
= ira_reg_class_max_nregs
[aclass
][ALLOCNO_MODE (a
)];
2387 /* We will deal with the subwords individually. */
2388 gcc_assert (size
== ALLOCNO_NUM_OBJECTS (a
));
2391 for (i
= 0; i
< n
; i
++)
2393 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
2394 ira_object_t conflict_obj
;
2395 ira_object_conflict_iterator oci
;
2397 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
2399 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
2402 conflict_data
= ALLOCNO_COLOR_DATA (conflict_a
);
2403 if (! conflict_data
->in_graph_p
2404 || ALLOCNO_ASSIGNED_P (conflict_a
)
2405 || !(hard_reg_set_intersect_p
2406 (ALLOCNO_COLOR_DATA (a
)->profitable_hard_regs
,
2407 conflict_data
->profitable_hard_regs
)))
2409 for (pref
= ALLOCNO_PREFS (a
); pref
!= NULL
; pref
= pref
->next_pref
)
2410 conflict_data
->conflict_allocno_hard_prefs
-= pref
->freq
;
2411 if (conflict_data
->colorable_p
)
2413 ira_assert (bitmap_bit_p (coloring_allocno_bitmap
,
2414 ALLOCNO_NUM (conflict_a
)));
2415 if (update_left_conflict_sizes_p (conflict_a
, a
, size
))
2417 delete_allocno_from_bucket
2418 (conflict_a
, &uncolorable_allocno_bucket
);
2419 add_allocno_to_ordered_colorable_bucket (conflict_a
);
2420 if (internal_flag_ira_verbose
> 4 && ira_dump_file
!= NULL
)
2422 fprintf (ira_dump_file
, " Making");
2423 ira_print_expanded_allocno (conflict_a
);
2424 fprintf (ira_dump_file
, " colorable\n");
2432 /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
2433 The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
2435 remove_allocno_from_bucket_and_push (ira_allocno_t allocno
, bool colorable_p
)
2438 delete_allocno_from_bucket (allocno
, &colorable_allocno_bucket
);
2440 delete_allocno_from_bucket (allocno
, &uncolorable_allocno_bucket
);
2441 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2443 fprintf (ira_dump_file
, " Pushing");
2444 ira_print_expanded_allocno (allocno
);
2446 fprintf (ira_dump_file
, "(cost %d)\n",
2447 ALLOCNO_COLOR_DATA (allocno
)->temp
);
2449 fprintf (ira_dump_file
, "(potential spill: %spri=%d, cost=%d)\n",
2450 ALLOCNO_BAD_SPILL_P (allocno
) ? "bad spill, " : "",
2451 allocno_spill_priority (allocno
),
2452 ALLOCNO_COLOR_DATA (allocno
)->temp
);
2455 ALLOCNO_COLOR_DATA (allocno
)->may_be_spilled_p
= true;
2456 push_allocno_to_stack (allocno
);
2459 /* Put all allocnos from colorable bucket onto the coloring stack. */
2461 push_only_colorable (void)
2463 form_threads_from_bucket (colorable_allocno_bucket
);
2464 sort_bucket (&colorable_allocno_bucket
, bucket_allocno_compare_func
);
2465 for (;colorable_allocno_bucket
!= NULL
;)
2466 remove_allocno_from_bucket_and_push (colorable_allocno_bucket
, true);
2469 /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
2470 loop given by its LOOP_NODE. */
2472 ira_loop_edge_freq (ira_loop_tree_node_t loop_node
, int regno
, bool exit_p
)
2479 ira_assert (current_loops
!= NULL
&& loop_node
->loop
!= NULL
2480 && (regno
< 0 || regno
>= FIRST_PSEUDO_REGISTER
));
2484 FOR_EACH_EDGE (e
, ei
, loop_node
->loop
->header
->preds
)
2485 if (e
->src
!= loop_node
->loop
->latch
2487 || (bitmap_bit_p (df_get_live_out (e
->src
), regno
)
2488 && bitmap_bit_p (df_get_live_in (e
->dest
), regno
))))
2489 freq
+= EDGE_FREQUENCY (e
);
2493 edges
= get_loop_exit_edges (loop_node
->loop
);
2494 FOR_EACH_VEC_ELT (edges
, i
, e
)
2496 || (bitmap_bit_p (df_get_live_out (e
->src
), regno
)
2497 && bitmap_bit_p (df_get_live_in (e
->dest
), regno
)))
2498 freq
+= EDGE_FREQUENCY (e
);
2502 return REG_FREQ_FROM_EDGE_FREQ (freq
);
2505 /* Calculate and return the cost of putting allocno A into memory. */
2507 calculate_allocno_spill_cost (ira_allocno_t a
)
2511 enum reg_class rclass
;
2512 ira_allocno_t parent_allocno
;
2513 ira_loop_tree_node_t parent_node
, loop_node
;
2515 regno
= ALLOCNO_REGNO (a
);
2516 cost
= ALLOCNO_UPDATED_MEMORY_COST (a
) - ALLOCNO_UPDATED_CLASS_COST (a
);
2517 if (ALLOCNO_CAP (a
) != NULL
)
2519 loop_node
= ALLOCNO_LOOP_TREE_NODE (a
);
2520 if ((parent_node
= loop_node
->parent
) == NULL
)
2522 if ((parent_allocno
= parent_node
->regno_allocno_map
[regno
]) == NULL
)
2524 mode
= ALLOCNO_MODE (a
);
2525 rclass
= ALLOCNO_CLASS (a
);
2526 if (ALLOCNO_HARD_REGNO (parent_allocno
) < 0)
2527 cost
-= (ira_memory_move_cost
[mode
][rclass
][0]
2528 * ira_loop_edge_freq (loop_node
, regno
, true)
2529 + ira_memory_move_cost
[mode
][rclass
][1]
2530 * ira_loop_edge_freq (loop_node
, regno
, false));
2533 ira_init_register_move_cost_if_necessary (mode
);
2534 cost
+= ((ira_memory_move_cost
[mode
][rclass
][1]
2535 * ira_loop_edge_freq (loop_node
, regno
, true)
2536 + ira_memory_move_cost
[mode
][rclass
][0]
2537 * ira_loop_edge_freq (loop_node
, regno
, false))
2538 - (ira_register_move_cost
[mode
][rclass
][rclass
]
2539 * (ira_loop_edge_freq (loop_node
, regno
, false)
2540 + ira_loop_edge_freq (loop_node
, regno
, true))));
2545 /* Used for sorting allocnos for spilling. */
2547 allocno_spill_priority_compare (ira_allocno_t a1
, ira_allocno_t a2
)
2549 int pri1
, pri2
, diff
;
2551 /* Avoid spilling static chain pointer pseudo when non-local goto is
2553 if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1
)))
2555 else if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2
)))
2557 if (ALLOCNO_BAD_SPILL_P (a1
) && ! ALLOCNO_BAD_SPILL_P (a2
))
2559 if (ALLOCNO_BAD_SPILL_P (a2
) && ! ALLOCNO_BAD_SPILL_P (a1
))
2561 pri1
= allocno_spill_priority (a1
);
2562 pri2
= allocno_spill_priority (a2
);
2563 if ((diff
= pri1
- pri2
) != 0)
2566 = ALLOCNO_COLOR_DATA (a1
)->temp
- ALLOCNO_COLOR_DATA (a2
)->temp
) != 0)
2568 return ALLOCNO_NUM (a1
) - ALLOCNO_NUM (a2
);
2571 /* Used for sorting allocnos for spilling. */
2573 allocno_spill_sort_compare (const void *v1p
, const void *v2p
)
2575 ira_allocno_t p1
= *(const ira_allocno_t
*) v1p
;
2576 ira_allocno_t p2
= *(const ira_allocno_t
*) v2p
;
2578 return allocno_spill_priority_compare (p1
, p2
);
2581 /* Push allocnos to the coloring stack. The order of allocnos in the
2582 stack defines the order for the subsequent coloring. */
2584 push_allocnos_to_stack (void)
2589 /* Calculate uncolorable allocno spill costs. */
2590 for (a
= uncolorable_allocno_bucket
;
2592 a
= ALLOCNO_COLOR_DATA (a
)->next_bucket_allocno
)
2593 if (ALLOCNO_CLASS (a
) != NO_REGS
)
2595 cost
= calculate_allocno_spill_cost (a
);
2596 /* ??? Remove cost of copies between the coalesced
2598 ALLOCNO_COLOR_DATA (a
)->temp
= cost
;
2600 sort_bucket (&uncolorable_allocno_bucket
, allocno_spill_sort_compare
);
2603 push_only_colorable ();
2604 a
= uncolorable_allocno_bucket
;
2607 remove_allocno_from_bucket_and_push (a
, false);
2609 ira_assert (colorable_allocno_bucket
== NULL
2610 && uncolorable_allocno_bucket
== NULL
);
2611 ira_assert (uncolorable_allocnos_num
== 0);
2614 /* Pop the coloring stack and assign hard registers to the popped
2617 pop_allocnos_from_stack (void)
2619 ira_allocno_t allocno
;
2620 enum reg_class aclass
;
2622 for (;allocno_stack_vec
.length () != 0;)
2624 allocno
= allocno_stack_vec
.pop ();
2625 aclass
= ALLOCNO_CLASS (allocno
);
2626 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2628 fprintf (ira_dump_file
, " Popping");
2629 ira_print_expanded_allocno (allocno
);
2630 fprintf (ira_dump_file
, " -- ");
2632 if (aclass
== NO_REGS
)
2634 ALLOCNO_HARD_REGNO (allocno
) = -1;
2635 ALLOCNO_ASSIGNED_P (allocno
) = true;
2636 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno
) == NULL
);
2638 (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno
) == NULL
);
2639 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2640 fprintf (ira_dump_file
, "assign memory\n");
2642 else if (assign_hard_reg (allocno
, false))
2644 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2645 fprintf (ira_dump_file
, "assign reg %d\n",
2646 ALLOCNO_HARD_REGNO (allocno
));
2648 else if (ALLOCNO_ASSIGNED_P (allocno
))
2650 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
2651 fprintf (ira_dump_file
, "spill%s\n",
2652 ALLOCNO_COLOR_DATA (allocno
)->may_be_spilled_p
2655 ALLOCNO_COLOR_DATA (allocno
)->in_graph_p
= true;
2659 /* Set up number of available hard registers for allocno A. */
2661 setup_allocno_available_regs_num (ira_allocno_t a
)
2663 int i
, n
, hard_regno
, hard_regs_num
, nwords
;
2664 enum reg_class aclass
;
2665 allocno_color_data_t data
;
2667 aclass
= ALLOCNO_CLASS (a
);
2668 data
= ALLOCNO_COLOR_DATA (a
);
2669 data
->available_regs_num
= 0;
2670 if (aclass
== NO_REGS
)
2672 hard_regs_num
= ira_class_hard_regs_num
[aclass
];
2673 nwords
= ALLOCNO_NUM_OBJECTS (a
);
2674 for (n
= 0, i
= hard_regs_num
- 1; i
>= 0; i
--)
2676 hard_regno
= ira_class_hard_regs
[aclass
][i
];
2677 /* Checking only profitable hard regs. */
2678 if (TEST_HARD_REG_BIT (data
->profitable_hard_regs
, hard_regno
))
2681 data
->available_regs_num
= n
;
2682 if (internal_flag_ira_verbose
<= 2 || ira_dump_file
== NULL
)
2686 " Allocno a%dr%d of %s(%d) has %d avail. regs ",
2687 ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
),
2688 reg_class_names
[aclass
], ira_class_hard_regs_num
[aclass
], n
);
2689 print_hard_reg_set (ira_dump_file
, data
->profitable_hard_regs
, false);
2690 fprintf (ira_dump_file
, ", %snode: ",
2691 data
->profitable_hard_regs
== data
->hard_regs_node
->hard_regs
->set
2693 print_hard_reg_set (ira_dump_file
,
2694 data
->hard_regs_node
->hard_regs
->set
, false);
2695 for (i
= 0; i
< nwords
; i
++)
2697 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
2702 fprintf (ira_dump_file
, ", ");
2703 fprintf (ira_dump_file
, " obj %d", i
);
2705 fprintf (ira_dump_file
, " (confl regs = ");
2706 print_hard_reg_set (ira_dump_file
, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
),
2708 fprintf (ira_dump_file
, ")");
2710 fprintf (ira_dump_file
, "\n");
2713 /* Put ALLOCNO in a bucket corresponding to its number and size of its
2714 conflicting allocnos and hard registers. */
2716 put_allocno_into_bucket (ira_allocno_t allocno
)
2718 ALLOCNO_COLOR_DATA (allocno
)->in_graph_p
= true;
2719 setup_allocno_available_regs_num (allocno
);
2720 if (setup_left_conflict_sizes_p (allocno
))
2721 add_allocno_to_bucket (allocno
, &colorable_allocno_bucket
);
2723 add_allocno_to_bucket (allocno
, &uncolorable_allocno_bucket
);
2726 /* Map: allocno number -> allocno priority. */
2727 static int *allocno_priorities
;
2729 /* Set up priorities for N allocnos in array
2730 CONSIDERATION_ALLOCNOS. */
2732 setup_allocno_priorities (ira_allocno_t
*consideration_allocnos
, int n
)
2734 int i
, length
, nrefs
, priority
, max_priority
, mult
;
2738 for (i
= 0; i
< n
; i
++)
2740 a
= consideration_allocnos
[i
];
2741 nrefs
= ALLOCNO_NREFS (a
);
2742 ira_assert (nrefs
>= 0);
2743 mult
= floor_log2 (ALLOCNO_NREFS (a
)) + 1;
2744 ira_assert (mult
>= 0);
2745 allocno_priorities
[ALLOCNO_NUM (a
)]
2748 * (ALLOCNO_MEMORY_COST (a
) - ALLOCNO_CLASS_COST (a
))
2749 * ira_reg_class_max_nregs
[ALLOCNO_CLASS (a
)][ALLOCNO_MODE (a
)]);
2751 priority
= -priority
;
2752 if (max_priority
< priority
)
2753 max_priority
= priority
;
2755 mult
= max_priority
== 0 ? 1 : INT_MAX
/ max_priority
;
2756 for (i
= 0; i
< n
; i
++)
2758 a
= consideration_allocnos
[i
];
2759 length
= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
);
2760 if (ALLOCNO_NUM_OBJECTS (a
) > 1)
2761 length
/= ALLOCNO_NUM_OBJECTS (a
);
2764 allocno_priorities
[ALLOCNO_NUM (a
)]
2765 = allocno_priorities
[ALLOCNO_NUM (a
)] * mult
/ length
;
2769 /* Sort allocnos according to the profit of usage of a hard register
2770 instead of memory for them. */
2772 allocno_cost_compare_func (const void *v1p
, const void *v2p
)
2774 ira_allocno_t p1
= *(const ira_allocno_t
*) v1p
;
2775 ira_allocno_t p2
= *(const ira_allocno_t
*) v2p
;
2778 c1
= ALLOCNO_UPDATED_MEMORY_COST (p1
) - ALLOCNO_UPDATED_CLASS_COST (p1
);
2779 c2
= ALLOCNO_UPDATED_MEMORY_COST (p2
) - ALLOCNO_UPDATED_CLASS_COST (p2
);
2783 /* If regs are equally good, sort by allocno numbers, so that the
2784 results of qsort leave nothing to chance. */
2785 return ALLOCNO_NUM (p1
) - ALLOCNO_NUM (p2
);
2788 /* Return savings on removed copies when ALLOCNO is assigned to
2791 allocno_copy_cost_saving (ira_allocno_t allocno
, int hard_regno
)
2794 machine_mode allocno_mode
= ALLOCNO_MODE (allocno
);
2795 enum reg_class rclass
;
2796 ira_copy_t cp
, next_cp
;
2798 rclass
= REGNO_REG_CLASS (hard_regno
);
2799 if (ira_reg_class_max_nregs
[rclass
][allocno_mode
]
2800 > ira_class_hard_regs_num
[rclass
])
2801 /* For the above condition the cost can be wrong. Use the allocno
2802 class in this case. */
2803 rclass
= ALLOCNO_CLASS (allocno
);
2804 for (cp
= ALLOCNO_COPIES (allocno
); cp
!= NULL
; cp
= next_cp
)
2806 if (cp
->first
== allocno
)
2808 next_cp
= cp
->next_first_allocno_copy
;
2809 if (ALLOCNO_HARD_REGNO (cp
->second
) != hard_regno
)
2812 else if (cp
->second
== allocno
)
2814 next_cp
= cp
->next_second_allocno_copy
;
2815 if (ALLOCNO_HARD_REGNO (cp
->first
) != hard_regno
)
2820 ira_init_register_move_cost_if_necessary (allocno_mode
);
2821 cost
+= cp
->freq
* ira_register_move_cost
[allocno_mode
][rclass
][rclass
];
2826 /* We used Chaitin-Briggs coloring to assign as many pseudos as
2827 possible to hard registers. Let us try to improve allocation with
2828 cost point of view. This function improves the allocation by
2829 spilling some allocnos and assigning the freed hard registers to
2830 other allocnos if it decreases the overall allocation cost. */
2832 improve_allocation (void)
2835 int j
, k
, n
, hregno
, conflict_hregno
, base_cost
, class_size
, word
, nwords
;
2836 int check
, spill_cost
, min_cost
, nregs
, conflict_nregs
, r
, best
;
2838 enum reg_class aclass
;
2841 int costs
[FIRST_PSEUDO_REGISTER
];
2842 HARD_REG_SET conflicting_regs
[2], profitable_hard_regs
;
2846 /* Don't bother to optimize the code with static chain pointer and
2847 non-local goto in order not to spill the chain pointer
2849 if (cfun
->static_chain_decl
&& crtl
->has_nonlocal_goto
)
2851 /* Clear counts used to process conflicting allocnos only once for
2853 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
2854 ALLOCNO_COLOR_DATA (ira_allocnos
[i
])->temp
= 0;
2856 /* Process each allocno and try to assign a hard register to it by
2857 spilling some its conflicting allocnos. */
2858 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
2860 a
= ira_allocnos
[i
];
2861 ALLOCNO_COLOR_DATA (a
)->temp
= 0;
2862 if (empty_profitable_hard_regs (a
))
2865 aclass
= ALLOCNO_CLASS (a
);
2866 allocno_costs
= ALLOCNO_HARD_REG_COSTS (a
);
2867 if ((hregno
= ALLOCNO_HARD_REGNO (a
)) < 0)
2868 base_cost
= ALLOCNO_UPDATED_MEMORY_COST (a
);
2869 else if (allocno_costs
== NULL
)
2870 /* It means that assigning a hard register is not profitable
2871 (we don't waste memory for hard register costs in this
2875 base_cost
= (allocno_costs
[ira_class_hard_reg_index
[aclass
][hregno
]]
2876 - allocno_copy_cost_saving (a
, hregno
));
2878 get_conflict_and_start_profitable_regs (a
, false,
2880 &profitable_hard_regs
);
2881 class_size
= ira_class_hard_regs_num
[aclass
];
2882 /* Set up cost improvement for usage of each profitable hard
2883 register for allocno A. */
2884 for (j
= 0; j
< class_size
; j
++)
2886 hregno
= ira_class_hard_regs
[aclass
][j
];
2887 if (! check_hard_reg_p (a
, hregno
,
2888 conflicting_regs
, profitable_hard_regs
))
2890 ira_assert (ira_class_hard_reg_index
[aclass
][hregno
] == j
);
2891 k
= allocno_costs
== NULL
? 0 : j
;
2892 costs
[hregno
] = (allocno_costs
== NULL
2893 ? ALLOCNO_UPDATED_CLASS_COST (a
) : allocno_costs
[k
]);
2894 costs
[hregno
] -= allocno_copy_cost_saving (a
, hregno
);
2895 costs
[hregno
] -= base_cost
;
2896 if (costs
[hregno
] < 0)
2900 /* There is no chance to improve the allocation cost by
2901 assigning hard register to allocno A even without spilling
2902 conflicting allocnos. */
2904 mode
= ALLOCNO_MODE (a
);
2905 nwords
= ALLOCNO_NUM_OBJECTS (a
);
2906 /* Process each allocno conflicting with A and update the cost
2907 improvement for profitable hard registers of A. To use a
2908 hard register for A we need to spill some conflicting
2909 allocnos and that creates penalty for the cost
2911 for (word
= 0; word
< nwords
; word
++)
2913 ira_object_t conflict_obj
;
2914 ira_object_t obj
= ALLOCNO_OBJECT (a
, word
);
2915 ira_object_conflict_iterator oci
;
2917 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
2919 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
2921 if (ALLOCNO_COLOR_DATA (conflict_a
)->temp
== check
)
2922 /* We already processed this conflicting allocno
2923 because we processed earlier another object of the
2924 conflicting allocno. */
2926 ALLOCNO_COLOR_DATA (conflict_a
)->temp
= check
;
2927 if ((conflict_hregno
= ALLOCNO_HARD_REGNO (conflict_a
)) < 0)
2929 spill_cost
= ALLOCNO_UPDATED_MEMORY_COST (conflict_a
);
2930 k
= (ira_class_hard_reg_index
2931 [ALLOCNO_CLASS (conflict_a
)][conflict_hregno
]);
2932 ira_assert (k
>= 0);
2933 if ((allocno_costs
= ALLOCNO_HARD_REG_COSTS (conflict_a
))
2935 spill_cost
-= allocno_costs
[k
];
2937 spill_cost
-= ALLOCNO_UPDATED_CLASS_COST (conflict_a
);
2939 += allocno_copy_cost_saving (conflict_a
, conflict_hregno
);
2940 conflict_nregs
= hard_regno_nregs (conflict_hregno
,
2941 ALLOCNO_MODE (conflict_a
));
2942 for (r
= conflict_hregno
;
2943 r
>= 0 && (int) end_hard_regno (mode
, r
) > conflict_hregno
;
2945 if (check_hard_reg_p (a
, r
,
2946 conflicting_regs
, profitable_hard_regs
))
2947 costs
[r
] += spill_cost
;
2948 for (r
= conflict_hregno
+ 1;
2949 r
< conflict_hregno
+ conflict_nregs
;
2951 if (check_hard_reg_p (a
, r
,
2952 conflicting_regs
, profitable_hard_regs
))
2953 costs
[r
] += spill_cost
;
2958 /* Now we choose hard register for A which results in highest
2959 allocation cost improvement. */
2960 for (j
= 0; j
< class_size
; j
++)
2962 hregno
= ira_class_hard_regs
[aclass
][j
];
2963 if (check_hard_reg_p (a
, hregno
,
2964 conflicting_regs
, profitable_hard_regs
)
2965 && min_cost
> costs
[hregno
])
2968 min_cost
= costs
[hregno
];
2972 /* We are in a situation when assigning any hard register to A
2973 by spilling some conflicting allocnos does not improve the
2976 nregs
= hard_regno_nregs (best
, mode
);
2977 /* Now spill conflicting allocnos which contain a hard register
2978 of A when we assign the best chosen hard register to it. */
2979 for (word
= 0; word
< nwords
; word
++)
2981 ira_object_t conflict_obj
;
2982 ira_object_t obj
= ALLOCNO_OBJECT (a
, word
);
2983 ira_object_conflict_iterator oci
;
2985 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
2987 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
2989 if ((conflict_hregno
= ALLOCNO_HARD_REGNO (conflict_a
)) < 0)
2991 conflict_nregs
= hard_regno_nregs (conflict_hregno
,
2992 ALLOCNO_MODE (conflict_a
));
2993 if (best
+ nregs
<= conflict_hregno
2994 || conflict_hregno
+ conflict_nregs
<= best
)
2995 /* No intersection. */
2997 ALLOCNO_HARD_REGNO (conflict_a
) = -1;
2998 sorted_allocnos
[n
++] = conflict_a
;
2999 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
3000 fprintf (ira_dump_file
, "Spilling a%dr%d for a%dr%d\n",
3001 ALLOCNO_NUM (conflict_a
), ALLOCNO_REGNO (conflict_a
),
3002 ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
));
3005 /* Assign the best chosen hard register to A. */
3006 ALLOCNO_HARD_REGNO (a
) = best
;
3007 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
3008 fprintf (ira_dump_file
, "Assigning %d to a%dr%d\n",
3009 best
, ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
));
3013 /* We spilled some allocnos to assign their hard registers to other
3014 allocnos. The spilled allocnos are now in array
3015 'sorted_allocnos'. There is still a possibility that some of the
3016 spilled allocnos can get hard registers. So let us try assign
3017 them hard registers again (just a reminder -- function
3018 'assign_hard_reg' assigns hard registers only if it is possible
3019 and profitable). We process the spilled allocnos with biggest
3020 benefit to get hard register first -- see function
3021 'allocno_cost_compare_func'. */
3022 qsort (sorted_allocnos
, n
, sizeof (ira_allocno_t
),
3023 allocno_cost_compare_func
);
3024 for (j
= 0; j
< n
; j
++)
3026 a
= sorted_allocnos
[j
];
3027 ALLOCNO_ASSIGNED_P (a
) = false;
3028 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3030 fprintf (ira_dump_file
, " ");
3031 ira_print_expanded_allocno (a
);
3032 fprintf (ira_dump_file
, " -- ");
3034 if (assign_hard_reg (a
, false))
3036 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3037 fprintf (ira_dump_file
, "assign hard reg %d\n",
3038 ALLOCNO_HARD_REGNO (a
));
3042 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3043 fprintf (ira_dump_file
, "assign memory\n");
3048 /* Sort allocnos according to their priorities. */
3050 allocno_priority_compare_func (const void *v1p
, const void *v2p
)
3052 ira_allocno_t a1
= *(const ira_allocno_t
*) v1p
;
3053 ira_allocno_t a2
= *(const ira_allocno_t
*) v2p
;
3054 int pri1
, pri2
, diff
;
3056 /* Assign hard reg to static chain pointer pseudo first when
3057 non-local goto is used. */
3058 if ((diff
= (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2
))
3059 - non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1
)))) != 0)
3061 pri1
= allocno_priorities
[ALLOCNO_NUM (a1
)];
3062 pri2
= allocno_priorities
[ALLOCNO_NUM (a2
)];
3064 return SORTGT (pri2
, pri1
);
3066 /* If regs are equally good, sort by allocnos, so that the results of
3067 qsort leave nothing to chance. */
3068 return ALLOCNO_NUM (a1
) - ALLOCNO_NUM (a2
);
3071 /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
3072 taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
3074 color_allocnos (void)
3080 setup_profitable_hard_regs ();
3081 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
3083 allocno_color_data_t data
;
3084 ira_pref_t pref
, next_pref
;
3086 a
= ira_allocnos
[i
];
3087 data
= ALLOCNO_COLOR_DATA (a
);
3088 data
->conflict_allocno_hard_prefs
= 0;
3089 for (pref
= ALLOCNO_PREFS (a
); pref
!= NULL
; pref
= next_pref
)
3091 next_pref
= pref
->next_pref
;
3092 if (! ira_hard_reg_in_set_p (pref
->hard_regno
,
3094 data
->profitable_hard_regs
))
3095 ira_remove_pref (pref
);
3099 if (flag_ira_algorithm
== IRA_ALGORITHM_PRIORITY
)
3102 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
3104 a
= ira_allocnos
[i
];
3105 if (ALLOCNO_CLASS (a
) == NO_REGS
)
3107 ALLOCNO_HARD_REGNO (a
) = -1;
3108 ALLOCNO_ASSIGNED_P (a
) = true;
3109 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
);
3110 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) == NULL
);
3111 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3113 fprintf (ira_dump_file
, " Spill");
3114 ira_print_expanded_allocno (a
);
3115 fprintf (ira_dump_file
, "\n");
3119 sorted_allocnos
[n
++] = a
;
3123 setup_allocno_priorities (sorted_allocnos
, n
);
3124 qsort (sorted_allocnos
, n
, sizeof (ira_allocno_t
),
3125 allocno_priority_compare_func
);
3126 for (i
= 0; i
< n
; i
++)
3128 a
= sorted_allocnos
[i
];
3129 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3131 fprintf (ira_dump_file
, " ");
3132 ira_print_expanded_allocno (a
);
3133 fprintf (ira_dump_file
, " -- ");
3135 if (assign_hard_reg (a
, false))
3137 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3138 fprintf (ira_dump_file
, "assign hard reg %d\n",
3139 ALLOCNO_HARD_REGNO (a
));
3143 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3144 fprintf (ira_dump_file
, "assign memory\n");
3151 form_allocno_hard_regs_nodes_forest ();
3152 if (internal_flag_ira_verbose
> 2 && ira_dump_file
!= NULL
)
3153 print_hard_regs_forest (ira_dump_file
);
3154 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
3156 a
= ira_allocnos
[i
];
3157 if (ALLOCNO_CLASS (a
) != NO_REGS
&& ! empty_profitable_hard_regs (a
))
3159 ALLOCNO_COLOR_DATA (a
)->in_graph_p
= true;
3160 update_costs_from_prefs (a
);
3161 update_conflict_allocno_hard_prefs (a
);
3165 ALLOCNO_HARD_REGNO (a
) = -1;
3166 ALLOCNO_ASSIGNED_P (a
) = true;
3167 /* We don't need updated costs anymore. */
3168 ira_free_allocno_updated_costs (a
);
3169 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3171 fprintf (ira_dump_file
, " Spill");
3172 ira_print_expanded_allocno (a
);
3173 fprintf (ira_dump_file
, "\n");
3177 /* Put the allocnos into the corresponding buckets. */
3178 colorable_allocno_bucket
= NULL
;
3179 uncolorable_allocno_bucket
= NULL
;
3180 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, i
, bi
)
3182 a
= ira_allocnos
[i
];
3183 if (ALLOCNO_COLOR_DATA (a
)->in_graph_p
)
3184 put_allocno_into_bucket (a
);
3186 push_allocnos_to_stack ();
3187 pop_allocnos_from_stack ();
3188 finish_allocno_hard_regs_nodes_forest ();
3190 improve_allocation ();
3195 /* Output information about the loop given by its LOOP_TREE_NODE. */
3197 print_loop_title (ira_loop_tree_node_t loop_tree_node
)
3201 ira_loop_tree_node_t subloop_node
, dest_loop_node
;
3205 if (loop_tree_node
->parent
== NULL
)
3206 fprintf (ira_dump_file
,
3207 "\n Loop 0 (parent -1, header bb%d, depth 0)\n bbs:",
3211 ira_assert (current_loops
!= NULL
&& loop_tree_node
->loop
!= NULL
);
3212 fprintf (ira_dump_file
,
3213 "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
3214 loop_tree_node
->loop_num
, loop_tree_node
->parent
->loop_num
,
3215 loop_tree_node
->loop
->header
->index
,
3216 loop_depth (loop_tree_node
->loop
));
3218 for (subloop_node
= loop_tree_node
->children
;
3219 subloop_node
!= NULL
;
3220 subloop_node
= subloop_node
->next
)
3221 if (subloop_node
->bb
!= NULL
)
3223 fprintf (ira_dump_file
, " %d", subloop_node
->bb
->index
);
3224 FOR_EACH_EDGE (e
, ei
, subloop_node
->bb
->succs
)
3225 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
3226 && ((dest_loop_node
= IRA_BB_NODE (e
->dest
)->parent
)
3228 fprintf (ira_dump_file
, "(->%d:l%d)",
3229 e
->dest
->index
, dest_loop_node
->loop_num
);
3231 fprintf (ira_dump_file
, "\n all:");
3232 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node
->all_allocnos
, 0, j
, bi
)
3233 fprintf (ira_dump_file
, " %dr%d", j
, ALLOCNO_REGNO (ira_allocnos
[j
]));
3234 fprintf (ira_dump_file
, "\n modified regnos:");
3235 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node
->modified_regnos
, 0, j
, bi
)
3236 fprintf (ira_dump_file
, " %d", j
);
3237 fprintf (ira_dump_file
, "\n border:");
3238 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node
->border_allocnos
, 0, j
, bi
)
3239 fprintf (ira_dump_file
, " %dr%d", j
, ALLOCNO_REGNO (ira_allocnos
[j
]));
3240 fprintf (ira_dump_file
, "\n Pressure:");
3241 for (j
= 0; (int) j
< ira_pressure_classes_num
; j
++)
3243 enum reg_class pclass
;
3245 pclass
= ira_pressure_classes
[j
];
3246 if (loop_tree_node
->reg_pressure
[pclass
] == 0)
3248 fprintf (ira_dump_file
, " %s=%d", reg_class_names
[pclass
],
3249 loop_tree_node
->reg_pressure
[pclass
]);
3251 fprintf (ira_dump_file
, "\n");
3254 /* Color the allocnos inside loop (in the extreme case it can be all
3255 of the function) given the corresponding LOOP_TREE_NODE. The
3256 function is called for each loop during top-down traverse of the
3259 color_pass (ira_loop_tree_node_t loop_tree_node
)
3261 int regno
, hard_regno
, index
= -1, n
;
3262 int cost
, exit_freq
, enter_freq
;
3266 enum reg_class rclass
, aclass
, pclass
;
3267 ira_allocno_t a
, subloop_allocno
;
3268 ira_loop_tree_node_t subloop_node
;
3270 ira_assert (loop_tree_node
->bb
== NULL
);
3271 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
3272 print_loop_title (loop_tree_node
);
3274 bitmap_copy (coloring_allocno_bitmap
, loop_tree_node
->all_allocnos
);
3275 bitmap_copy (consideration_allocno_bitmap
, coloring_allocno_bitmap
);
3277 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, j
, bi
)
3279 a
= ira_allocnos
[j
];
3281 if (! ALLOCNO_ASSIGNED_P (a
))
3283 bitmap_clear_bit (coloring_allocno_bitmap
, ALLOCNO_NUM (a
));
3286 = (allocno_color_data_t
) ira_allocate (sizeof (struct allocno_color_data
)
3288 memset (allocno_color_data
, 0, sizeof (struct allocno_color_data
) * n
);
3289 curr_allocno_process
= 0;
3291 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, j
, bi
)
3293 a
= ira_allocnos
[j
];
3294 ALLOCNO_ADD_DATA (a
) = allocno_color_data
+ n
;
3297 init_allocno_threads ();
3298 /* Color all mentioned allocnos including transparent ones. */
3300 /* Process caps. They are processed just once. */
3301 if (flag_ira_region
== IRA_REGION_MIXED
3302 || flag_ira_region
== IRA_REGION_ALL
)
3303 EXECUTE_IF_SET_IN_BITMAP (loop_tree_node
->all_allocnos
, 0, j
, bi
)
3305 a
= ira_allocnos
[j
];
3306 if (ALLOCNO_CAP_MEMBER (a
) == NULL
)
3308 /* Remove from processing in the next loop. */
3309 bitmap_clear_bit (consideration_allocno_bitmap
, j
);
3310 rclass
= ALLOCNO_CLASS (a
);
3311 pclass
= ira_pressure_class_translate
[rclass
];
3312 if (flag_ira_region
== IRA_REGION_MIXED
3313 && (loop_tree_node
->reg_pressure
[pclass
]
3314 <= ira_class_hard_regs_num
[pclass
]))
3316 mode
= ALLOCNO_MODE (a
);
3317 hard_regno
= ALLOCNO_HARD_REGNO (a
);
3318 if (hard_regno
>= 0)
3320 index
= ira_class_hard_reg_index
[rclass
][hard_regno
];
3321 ira_assert (index
>= 0);
3323 regno
= ALLOCNO_REGNO (a
);
3324 subloop_allocno
= ALLOCNO_CAP_MEMBER (a
);
3325 subloop_node
= ALLOCNO_LOOP_TREE_NODE (subloop_allocno
);
3326 ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno
));
3327 ALLOCNO_HARD_REGNO (subloop_allocno
) = hard_regno
;
3328 ALLOCNO_ASSIGNED_P (subloop_allocno
) = true;
3329 if (hard_regno
>= 0)
3330 update_costs_from_copies (subloop_allocno
, true, true);
3331 /* We don't need updated costs anymore. */
3332 ira_free_allocno_updated_costs (subloop_allocno
);
3335 /* Update costs of the corresponding allocnos (not caps) in the
3337 for (subloop_node
= loop_tree_node
->subloops
;
3338 subloop_node
!= NULL
;
3339 subloop_node
= subloop_node
->subloop_next
)
3341 ira_assert (subloop_node
->bb
== NULL
);
3342 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, j
, bi
)
3344 a
= ira_allocnos
[j
];
3345 ira_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
3346 mode
= ALLOCNO_MODE (a
);
3347 rclass
= ALLOCNO_CLASS (a
);
3348 pclass
= ira_pressure_class_translate
[rclass
];
3349 hard_regno
= ALLOCNO_HARD_REGNO (a
);
3350 /* Use hard register class here. ??? */
3351 if (hard_regno
>= 0)
3353 index
= ira_class_hard_reg_index
[rclass
][hard_regno
];
3354 ira_assert (index
>= 0);
3356 regno
= ALLOCNO_REGNO (a
);
3357 /* ??? conflict costs */
3358 subloop_allocno
= subloop_node
->regno_allocno_map
[regno
];
3359 if (subloop_allocno
== NULL
3360 || ALLOCNO_CAP (subloop_allocno
) != NULL
)
3362 ira_assert (ALLOCNO_CLASS (subloop_allocno
) == rclass
);
3363 ira_assert (bitmap_bit_p (subloop_node
->all_allocnos
,
3364 ALLOCNO_NUM (subloop_allocno
)));
3365 if ((flag_ira_region
== IRA_REGION_MIXED
3366 && (loop_tree_node
->reg_pressure
[pclass
]
3367 <= ira_class_hard_regs_num
[pclass
]))
3368 || (pic_offset_table_rtx
!= NULL
3369 && regno
== (int) REGNO (pic_offset_table_rtx
))
3370 /* Avoid overlapped multi-registers. Moves between them
3371 might result in wrong code generation. */
3373 && ira_reg_class_max_nregs
[pclass
][mode
] > 1))
3375 if (! ALLOCNO_ASSIGNED_P (subloop_allocno
))
3377 ALLOCNO_HARD_REGNO (subloop_allocno
) = hard_regno
;
3378 ALLOCNO_ASSIGNED_P (subloop_allocno
) = true;
3379 if (hard_regno
>= 0)
3380 update_costs_from_copies (subloop_allocno
, true, true);
3381 /* We don't need updated costs anymore. */
3382 ira_free_allocno_updated_costs (subloop_allocno
);
3386 exit_freq
= ira_loop_edge_freq (subloop_node
, regno
, true);
3387 enter_freq
= ira_loop_edge_freq (subloop_node
, regno
, false);
3388 ira_assert (regno
< ira_reg_equiv_len
);
3389 if (ira_equiv_no_lvalue_p (regno
))
3391 if (! ALLOCNO_ASSIGNED_P (subloop_allocno
))
3393 ALLOCNO_HARD_REGNO (subloop_allocno
) = hard_regno
;
3394 ALLOCNO_ASSIGNED_P (subloop_allocno
) = true;
3395 if (hard_regno
>= 0)
3396 update_costs_from_copies (subloop_allocno
, true, true);
3397 /* We don't need updated costs anymore. */
3398 ira_free_allocno_updated_costs (subloop_allocno
);
3401 else if (hard_regno
< 0)
3403 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno
)
3404 -= ((ira_memory_move_cost
[mode
][rclass
][1] * enter_freq
)
3405 + (ira_memory_move_cost
[mode
][rclass
][0] * exit_freq
));
3409 aclass
= ALLOCNO_CLASS (subloop_allocno
);
3410 ira_init_register_move_cost_if_necessary (mode
);
3411 cost
= (ira_register_move_cost
[mode
][rclass
][rclass
]
3412 * (exit_freq
+ enter_freq
));
3413 ira_allocate_and_set_or_copy_costs
3414 (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno
), aclass
,
3415 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno
),
3416 ALLOCNO_HARD_REG_COSTS (subloop_allocno
));
3417 ira_allocate_and_set_or_copy_costs
3418 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno
),
3419 aclass
, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno
));
3420 ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno
)[index
] -= cost
;
3421 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno
)[index
]
3423 if (ALLOCNO_UPDATED_CLASS_COST (subloop_allocno
)
3424 > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno
)[index
])
3425 ALLOCNO_UPDATED_CLASS_COST (subloop_allocno
)
3426 = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno
)[index
];
3427 ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno
)
3428 += (ira_memory_move_cost
[mode
][rclass
][0] * enter_freq
3429 + ira_memory_move_cost
[mode
][rclass
][1] * exit_freq
);
3433 ira_free (allocno_color_data
);
3434 EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap
, 0, j
, bi
)
3436 a
= ira_allocnos
[j
];
3437 ALLOCNO_ADD_DATA (a
) = NULL
;
3441 /* Initialize the common data for coloring and calls functions to do
3442 Chaitin-Briggs and regional coloring. */
3446 coloring_allocno_bitmap
= ira_allocate_bitmap ();
3447 if (internal_flag_ira_verbose
> 0 && ira_dump_file
!= NULL
)
3448 fprintf (ira_dump_file
, "\n**** Allocnos coloring:\n\n");
3450 ira_traverse_loop_tree (false, ira_loop_tree_root
, color_pass
, NULL
);
3452 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
3453 ira_print_disposition (ira_dump_file
);
3455 ira_free_bitmap (coloring_allocno_bitmap
);
3460 /* Move spill/restore code, which are to be generated in ira-emit.c,
3461 to less frequent points (if it is profitable) by reassigning some
3462 allocnos (in loop with subloops containing in another loop) to
3463 memory which results in longer live-range where the corresponding
3464 pseudo-registers will be in memory. */
3466 move_spill_restore (void)
3468 int cost
, regno
, hard_regno
, hard_regno2
, index
;
3470 int enter_freq
, exit_freq
;
3472 enum reg_class rclass
;
3473 ira_allocno_t a
, parent_allocno
, subloop_allocno
;
3474 ira_loop_tree_node_t parent
, loop_node
, subloop_node
;
3475 ira_allocno_iterator ai
;
3480 if (internal_flag_ira_verbose
> 0 && ira_dump_file
!= NULL
)
3481 fprintf (ira_dump_file
, "New iteration of spill/restore move\n");
3482 FOR_EACH_ALLOCNO (a
, ai
)
3484 regno
= ALLOCNO_REGNO (a
);
3485 loop_node
= ALLOCNO_LOOP_TREE_NODE (a
);
3486 if (ALLOCNO_CAP_MEMBER (a
) != NULL
3487 || ALLOCNO_CAP (a
) != NULL
3488 || (hard_regno
= ALLOCNO_HARD_REGNO (a
)) < 0
3489 || loop_node
->children
== NULL
3490 /* don't do the optimization because it can create
3491 copies and the reload pass can spill the allocno set
3492 by copy although the allocno will not get memory
3494 || ira_equiv_no_lvalue_p (regno
)
3495 || !bitmap_bit_p (loop_node
->border_allocnos
, ALLOCNO_NUM (a
))
3496 /* Do not spill static chain pointer pseudo when
3497 non-local goto is used. */
3498 || non_spilled_static_chain_regno_p (regno
))
3500 mode
= ALLOCNO_MODE (a
);
3501 rclass
= ALLOCNO_CLASS (a
);
3502 index
= ira_class_hard_reg_index
[rclass
][hard_regno
];
3503 ira_assert (index
>= 0);
3504 cost
= (ALLOCNO_MEMORY_COST (a
)
3505 - (ALLOCNO_HARD_REG_COSTS (a
) == NULL
3506 ? ALLOCNO_CLASS_COST (a
)
3507 : ALLOCNO_HARD_REG_COSTS (a
)[index
]));
3508 ira_init_register_move_cost_if_necessary (mode
);
3509 for (subloop_node
= loop_node
->subloops
;
3510 subloop_node
!= NULL
;
3511 subloop_node
= subloop_node
->subloop_next
)
3513 ira_assert (subloop_node
->bb
== NULL
);
3514 subloop_allocno
= subloop_node
->regno_allocno_map
[regno
];
3515 if (subloop_allocno
== NULL
)
3517 ira_assert (rclass
== ALLOCNO_CLASS (subloop_allocno
));
3518 /* We have accumulated cost. To get the real cost of
3519 allocno usage in the loop we should subtract costs of
3520 the subloop allocnos. */
3521 cost
-= (ALLOCNO_MEMORY_COST (subloop_allocno
)
3522 - (ALLOCNO_HARD_REG_COSTS (subloop_allocno
) == NULL
3523 ? ALLOCNO_CLASS_COST (subloop_allocno
)
3524 : ALLOCNO_HARD_REG_COSTS (subloop_allocno
)[index
]));
3525 exit_freq
= ira_loop_edge_freq (subloop_node
, regno
, true);
3526 enter_freq
= ira_loop_edge_freq (subloop_node
, regno
, false);
3527 if ((hard_regno2
= ALLOCNO_HARD_REGNO (subloop_allocno
)) < 0)
3528 cost
-= (ira_memory_move_cost
[mode
][rclass
][0] * exit_freq
3529 + ira_memory_move_cost
[mode
][rclass
][1] * enter_freq
);
3533 += (ira_memory_move_cost
[mode
][rclass
][0] * exit_freq
3534 + ira_memory_move_cost
[mode
][rclass
][1] * enter_freq
);
3535 if (hard_regno2
!= hard_regno
)
3536 cost
-= (ira_register_move_cost
[mode
][rclass
][rclass
]
3537 * (exit_freq
+ enter_freq
));
3540 if ((parent
= loop_node
->parent
) != NULL
3541 && (parent_allocno
= parent
->regno_allocno_map
[regno
]) != NULL
)
3543 ira_assert (rclass
== ALLOCNO_CLASS (parent_allocno
));
3544 exit_freq
= ira_loop_edge_freq (loop_node
, regno
, true);
3545 enter_freq
= ira_loop_edge_freq (loop_node
, regno
, false);
3546 if ((hard_regno2
= ALLOCNO_HARD_REGNO (parent_allocno
)) < 0)
3547 cost
-= (ira_memory_move_cost
[mode
][rclass
][0] * exit_freq
3548 + ira_memory_move_cost
[mode
][rclass
][1] * enter_freq
);
3552 += (ira_memory_move_cost
[mode
][rclass
][1] * exit_freq
3553 + ira_memory_move_cost
[mode
][rclass
][0] * enter_freq
);
3554 if (hard_regno2
!= hard_regno
)
3555 cost
-= (ira_register_move_cost
[mode
][rclass
][rclass
]
3556 * (exit_freq
+ enter_freq
));
3561 ALLOCNO_HARD_REGNO (a
) = -1;
3562 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3566 " Moving spill/restore for a%dr%d up from loop %d",
3567 ALLOCNO_NUM (a
), regno
, loop_node
->loop_num
);
3568 fprintf (ira_dump_file
, " - profit %d\n", -cost
);
3580 /* Update current hard reg costs and current conflict hard reg costs
3581 for allocno A. It is done by processing its copies containing
3582 other allocnos already assigned. */
3584 update_curr_costs (ira_allocno_t a
)
3586 int i
, hard_regno
, cost
;
3588 enum reg_class aclass
, rclass
;
3589 ira_allocno_t another_a
;
3590 ira_copy_t cp
, next_cp
;
3592 ira_free_allocno_updated_costs (a
);
3593 ira_assert (! ALLOCNO_ASSIGNED_P (a
));
3594 aclass
= ALLOCNO_CLASS (a
);
3595 if (aclass
== NO_REGS
)
3597 mode
= ALLOCNO_MODE (a
);
3598 ira_init_register_move_cost_if_necessary (mode
);
3599 for (cp
= ALLOCNO_COPIES (a
); cp
!= NULL
; cp
= next_cp
)
3603 next_cp
= cp
->next_first_allocno_copy
;
3604 another_a
= cp
->second
;
3606 else if (cp
->second
== a
)
3608 next_cp
= cp
->next_second_allocno_copy
;
3609 another_a
= cp
->first
;
3613 if (! ira_reg_classes_intersect_p
[aclass
][ALLOCNO_CLASS (another_a
)]
3614 || ! ALLOCNO_ASSIGNED_P (another_a
)
3615 || (hard_regno
= ALLOCNO_HARD_REGNO (another_a
)) < 0)
3617 rclass
= REGNO_REG_CLASS (hard_regno
);
3618 i
= ira_class_hard_reg_index
[aclass
][hard_regno
];
3621 cost
= (cp
->first
== a
3622 ? ira_register_move_cost
[mode
][rclass
][aclass
]
3623 : ira_register_move_cost
[mode
][aclass
][rclass
]);
3624 ira_allocate_and_set_or_copy_costs
3625 (&ALLOCNO_UPDATED_HARD_REG_COSTS (a
), aclass
, ALLOCNO_CLASS_COST (a
),
3626 ALLOCNO_HARD_REG_COSTS (a
));
3627 ira_allocate_and_set_or_copy_costs
3628 (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
),
3629 aclass
, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a
));
3630 ALLOCNO_UPDATED_HARD_REG_COSTS (a
)[i
] -= cp
->freq
* cost
;
3631 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
)[i
] -= cp
->freq
* cost
;
3635 /* Try to assign hard registers to the unassigned allocnos and
3636 allocnos conflicting with them or conflicting with allocnos whose
3637 regno >= START_REGNO. The function is called after ira_flattening,
3638 so more allocnos (including ones created in ira-emit.c) will have a
3639 chance to get a hard register. We use simple assignment algorithm
3640 based on priorities. */
3642 ira_reassign_conflict_allocnos (int start_regno
)
3644 int i
, allocnos_to_color_num
;
3646 enum reg_class aclass
;
3647 bitmap allocnos_to_color
;
3648 ira_allocno_iterator ai
;
3650 allocnos_to_color
= ira_allocate_bitmap ();
3651 allocnos_to_color_num
= 0;
3652 FOR_EACH_ALLOCNO (a
, ai
)
3654 int n
= ALLOCNO_NUM_OBJECTS (a
);
3656 if (! ALLOCNO_ASSIGNED_P (a
)
3657 && ! bitmap_bit_p (allocnos_to_color
, ALLOCNO_NUM (a
)))
3659 if (ALLOCNO_CLASS (a
) != NO_REGS
)
3660 sorted_allocnos
[allocnos_to_color_num
++] = a
;
3663 ALLOCNO_ASSIGNED_P (a
) = true;
3664 ALLOCNO_HARD_REGNO (a
) = -1;
3665 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a
) == NULL
);
3666 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a
) == NULL
);
3668 bitmap_set_bit (allocnos_to_color
, ALLOCNO_NUM (a
));
3670 if (ALLOCNO_REGNO (a
) < start_regno
3671 || (aclass
= ALLOCNO_CLASS (a
)) == NO_REGS
)
3673 for (i
= 0; i
< n
; i
++)
3675 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
3676 ira_object_t conflict_obj
;
3677 ira_object_conflict_iterator oci
;
3679 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
3681 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
3683 ira_assert (ira_reg_classes_intersect_p
3684 [aclass
][ALLOCNO_CLASS (conflict_a
)]);
3685 if (!bitmap_set_bit (allocnos_to_color
, ALLOCNO_NUM (conflict_a
)))
3687 sorted_allocnos
[allocnos_to_color_num
++] = conflict_a
;
3691 ira_free_bitmap (allocnos_to_color
);
3692 if (allocnos_to_color_num
> 1)
3694 setup_allocno_priorities (sorted_allocnos
, allocnos_to_color_num
);
3695 qsort (sorted_allocnos
, allocnos_to_color_num
, sizeof (ira_allocno_t
),
3696 allocno_priority_compare_func
);
3698 for (i
= 0; i
< allocnos_to_color_num
; i
++)
3700 a
= sorted_allocnos
[i
];
3701 ALLOCNO_ASSIGNED_P (a
) = false;
3702 update_curr_costs (a
);
3704 for (i
= 0; i
< allocnos_to_color_num
; i
++)
3706 a
= sorted_allocnos
[i
];
3707 if (assign_hard_reg (a
, true))
3709 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3712 " Secondary allocation: assign hard reg %d to reg %d\n",
3713 ALLOCNO_HARD_REGNO (a
), ALLOCNO_REGNO (a
));
3720 /* This page contains functions used to find conflicts using allocno
3723 #ifdef ENABLE_IRA_CHECKING
3725 /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
3726 intersect. This should be used when there is only one region.
3727 Currently this is used during reload. */
3729 conflict_by_live_ranges_p (int regno1
, int regno2
)
3731 ira_allocno_t a1
, a2
;
3733 ira_assert (regno1
>= FIRST_PSEUDO_REGISTER
3734 && regno2
>= FIRST_PSEUDO_REGISTER
);
3735 /* Reg info calculated by dataflow infrastructure can be different
3736 from one calculated by regclass. */
3737 if ((a1
= ira_loop_tree_root
->regno_allocno_map
[regno1
]) == NULL
3738 || (a2
= ira_loop_tree_root
->regno_allocno_map
[regno2
]) == NULL
)
3740 return allocnos_conflict_by_live_ranges_p (a1
, a2
);
3747 /* This page contains code to coalesce memory stack slots used by
3748 spilled allocnos. This results in smaller stack frame, better data
3749 locality, and in smaller code for some architectures like
3750 x86/x86_64 where insn size depends on address displacement value.
3751 On the other hand, it can worsen insn scheduling after the RA but
3752 in practice it is less important than smaller stack frames. */
3754 /* TRUE if we coalesced some allocnos. In other words, if we got
3755 loops formed by members first_coalesced_allocno and
3756 next_coalesced_allocno containing more one allocno. */
3757 static bool allocno_coalesced_p
;
3759 /* Bitmap used to prevent a repeated allocno processing because of
3761 static bitmap processed_coalesced_allocno_bitmap
;
3764 typedef struct coalesce_data
*coalesce_data_t
;
3766 /* To decrease footprint of ira_allocno structure we store all data
3767 needed only for coalescing in the following structure. */
3768 struct coalesce_data
3770 /* Coalesced allocnos form a cyclic list. One allocno given by
3771 FIRST represents all coalesced allocnos. The
3772 list is chained by NEXT. */
3773 ira_allocno_t first
;
3778 /* Container for storing allocno data concerning coalescing. */
3779 static coalesce_data_t allocno_coalesce_data
;
3781 /* Macro to access the data concerning coalescing. */
3782 #define ALLOCNO_COALESCE_DATA(a) ((coalesce_data_t) ALLOCNO_ADD_DATA (a))
3784 /* Merge two sets of coalesced allocnos given correspondingly by
3785 allocnos A1 and A2 (more accurately merging A2 set into A1
3788 merge_allocnos (ira_allocno_t a1
, ira_allocno_t a2
)
3790 ira_allocno_t a
, first
, last
, next
;
3792 first
= ALLOCNO_COALESCE_DATA (a1
)->first
;
3793 a
= ALLOCNO_COALESCE_DATA (a2
)->first
;
3796 for (last
= a2
, a
= ALLOCNO_COALESCE_DATA (a2
)->next
;;
3797 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
3799 ALLOCNO_COALESCE_DATA (a
)->first
= first
;
3804 next
= allocno_coalesce_data
[ALLOCNO_NUM (first
)].next
;
3805 allocno_coalesce_data
[ALLOCNO_NUM (first
)].next
= a2
;
3806 allocno_coalesce_data
[ALLOCNO_NUM (last
)].next
= next
;
3809 /* Return TRUE if there are conflicting allocnos from two sets of
3810 coalesced allocnos given correspondingly by allocnos A1 and A2. We
3811 use live ranges to find conflicts because conflicts are represented
3812 only for allocnos of the same allocno class and during the reload
3813 pass we coalesce allocnos for sharing stack memory slots. */
3815 coalesced_allocno_conflict_p (ira_allocno_t a1
, ira_allocno_t a2
)
3817 ira_allocno_t a
, conflict_a
;
3819 if (allocno_coalesced_p
)
3821 bitmap_clear (processed_coalesced_allocno_bitmap
);
3822 for (a
= ALLOCNO_COALESCE_DATA (a1
)->next
;;
3823 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
3825 bitmap_set_bit (processed_coalesced_allocno_bitmap
, ALLOCNO_NUM (a
));
3830 for (a
= ALLOCNO_COALESCE_DATA (a2
)->next
;;
3831 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
3833 for (conflict_a
= ALLOCNO_COALESCE_DATA (a1
)->next
;;
3834 conflict_a
= ALLOCNO_COALESCE_DATA (conflict_a
)->next
)
3836 if (allocnos_conflict_by_live_ranges_p (a
, conflict_a
))
3838 if (conflict_a
== a1
)
3847 /* The major function for aggressive allocno coalescing. We coalesce
3848 only spilled allocnos. If some allocnos have been coalesced, we
3849 set up flag allocno_coalesced_p. */
3851 coalesce_allocnos (void)
3854 ira_copy_t cp
, next_cp
;
3856 int i
, n
, cp_num
, regno
;
3860 /* Collect copies. */
3861 EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap
, 0, j
, bi
)
3863 a
= ira_allocnos
[j
];
3864 regno
= ALLOCNO_REGNO (a
);
3865 if (! ALLOCNO_ASSIGNED_P (a
) || ALLOCNO_HARD_REGNO (a
) >= 0
3866 || ira_equiv_no_lvalue_p (regno
))
3868 for (cp
= ALLOCNO_COPIES (a
); cp
!= NULL
; cp
= next_cp
)
3872 next_cp
= cp
->next_first_allocno_copy
;
3873 regno
= ALLOCNO_REGNO (cp
->second
);
3874 /* For priority coloring we coalesce allocnos only with
3875 the same allocno class not with intersected allocno
3876 classes as it were possible. It is done for
3878 if ((cp
->insn
!= NULL
|| cp
->constraint_p
)
3879 && ALLOCNO_ASSIGNED_P (cp
->second
)
3880 && ALLOCNO_HARD_REGNO (cp
->second
) < 0
3881 && ! ira_equiv_no_lvalue_p (regno
))
3882 sorted_copies
[cp_num
++] = cp
;
3884 else if (cp
->second
== a
)
3885 next_cp
= cp
->next_second_allocno_copy
;
3890 qsort (sorted_copies
, cp_num
, sizeof (ira_copy_t
), copy_freq_compare_func
);
3891 /* Coalesced copies, most frequently executed first. */
3892 for (; cp_num
!= 0;)
3894 for (i
= 0; i
< cp_num
; i
++)
3896 cp
= sorted_copies
[i
];
3897 if (! coalesced_allocno_conflict_p (cp
->first
, cp
->second
))
3899 allocno_coalesced_p
= true;
3900 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
3903 " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
3904 cp
->num
, ALLOCNO_NUM (cp
->first
), ALLOCNO_REGNO (cp
->first
),
3905 ALLOCNO_NUM (cp
->second
), ALLOCNO_REGNO (cp
->second
),
3907 merge_allocnos (cp
->first
, cp
->second
);
3912 /* Collect the rest of copies. */
3913 for (n
= 0; i
< cp_num
; i
++)
3915 cp
= sorted_copies
[i
];
3916 if (allocno_coalesce_data
[ALLOCNO_NUM (cp
->first
)].first
3917 != allocno_coalesce_data
[ALLOCNO_NUM (cp
->second
)].first
)
3918 sorted_copies
[n
++] = cp
;
3924 /* Usage cost and order number of coalesced allocno set to which
3925 given pseudo register belongs to. */
3926 static int *regno_coalesced_allocno_cost
;
3927 static int *regno_coalesced_allocno_num
;
3929 /* Sort pseudos according frequencies of coalesced allocno sets they
3930 belong to (putting most frequently ones first), and according to
3931 coalesced allocno set order numbers. */
3933 coalesced_pseudo_reg_freq_compare (const void *v1p
, const void *v2p
)
3935 const int regno1
= *(const int *) v1p
;
3936 const int regno2
= *(const int *) v2p
;
3939 if ((diff
= (regno_coalesced_allocno_cost
[regno2
]
3940 - regno_coalesced_allocno_cost
[regno1
])) != 0)
3942 if ((diff
= (regno_coalesced_allocno_num
[regno1
]
3943 - regno_coalesced_allocno_num
[regno2
])) != 0)
3945 return regno1
- regno2
;
3948 /* Widest width in which each pseudo reg is referred to (via subreg).
3949 It is used for sorting pseudo registers. */
3950 static machine_mode
*regno_max_ref_mode
;
3952 /* Sort pseudos according their slot numbers (putting ones with
3953 smaller numbers first, or last when the frame pointer is not
3956 coalesced_pseudo_reg_slot_compare (const void *v1p
, const void *v2p
)
3958 const int regno1
= *(const int *) v1p
;
3959 const int regno2
= *(const int *) v2p
;
3960 ira_allocno_t a1
= ira_regno_allocno_map
[regno1
];
3961 ira_allocno_t a2
= ira_regno_allocno_map
[regno2
];
3962 int diff
, slot_num1
, slot_num2
;
3963 machine_mode mode1
, mode2
;
3965 if (a1
== NULL
|| ALLOCNO_HARD_REGNO (a1
) >= 0)
3967 if (a2
== NULL
|| ALLOCNO_HARD_REGNO (a2
) >= 0)
3968 return regno1
- regno2
;
3971 else if (a2
== NULL
|| ALLOCNO_HARD_REGNO (a2
) >= 0)
3973 slot_num1
= -ALLOCNO_HARD_REGNO (a1
);
3974 slot_num2
= -ALLOCNO_HARD_REGNO (a2
);
3975 if ((diff
= slot_num1
- slot_num2
) != 0)
3976 return (frame_pointer_needed
3977 || (!FRAME_GROWS_DOWNWARD
) == STACK_GROWS_DOWNWARD
? diff
: -diff
);
3978 mode1
= wider_subreg_mode (PSEUDO_REGNO_MODE (regno1
),
3979 regno_max_ref_mode
[regno1
]);
3980 mode2
= wider_subreg_mode (PSEUDO_REGNO_MODE (regno2
),
3981 regno_max_ref_mode
[regno2
]);
3982 if ((diff
= compare_sizes_for_sort (GET_MODE_SIZE (mode2
),
3983 GET_MODE_SIZE (mode1
))) != 0)
3985 return regno1
- regno2
;
3988 /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
3989 for coalesced allocno sets containing allocnos with their regnos
3990 given in array PSEUDO_REGNOS of length N. */
3992 setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos
, int n
)
3994 int i
, num
, regno
, cost
;
3995 ira_allocno_t allocno
, a
;
3997 for (num
= i
= 0; i
< n
; i
++)
3999 regno
= pseudo_regnos
[i
];
4000 allocno
= ira_regno_allocno_map
[regno
];
4001 if (allocno
== NULL
)
4003 regno_coalesced_allocno_cost
[regno
] = 0;
4004 regno_coalesced_allocno_num
[regno
] = ++num
;
4007 if (ALLOCNO_COALESCE_DATA (allocno
)->first
!= allocno
)
4010 for (cost
= 0, a
= ALLOCNO_COALESCE_DATA (allocno
)->next
;;
4011 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
4013 cost
+= ALLOCNO_FREQ (a
);
4017 for (a
= ALLOCNO_COALESCE_DATA (allocno
)->next
;;
4018 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
4020 regno_coalesced_allocno_num
[ALLOCNO_REGNO (a
)] = num
;
4021 regno_coalesced_allocno_cost
[ALLOCNO_REGNO (a
)] = cost
;
4028 /* Collect spilled allocnos representing coalesced allocno sets (the
4029 first coalesced allocno). The collected allocnos are returned
4030 through array SPILLED_COALESCED_ALLOCNOS. The function returns the
4031 number of the collected allocnos. The allocnos are given by their
4032 regnos in array PSEUDO_REGNOS of length N. */
4034 collect_spilled_coalesced_allocnos (int *pseudo_regnos
, int n
,
4035 ira_allocno_t
*spilled_coalesced_allocnos
)
4038 ira_allocno_t allocno
;
4040 for (num
= i
= 0; i
< n
; i
++)
4042 regno
= pseudo_regnos
[i
];
4043 allocno
= ira_regno_allocno_map
[regno
];
4044 if (allocno
== NULL
|| ALLOCNO_HARD_REGNO (allocno
) >= 0
4045 || ALLOCNO_COALESCE_DATA (allocno
)->first
!= allocno
)
4047 spilled_coalesced_allocnos
[num
++] = allocno
;
4052 /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
4053 given slot contains live ranges of coalesced allocnos assigned to
4055 static live_range_t
*slot_coalesced_allocnos_live_ranges
;
4057 /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
4058 ranges intersected with live ranges of coalesced allocnos assigned
4059 to slot with number N. */
4061 slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno
, int n
)
4065 for (a
= ALLOCNO_COALESCE_DATA (allocno
)->next
;;
4066 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
4069 int nr
= ALLOCNO_NUM_OBJECTS (a
);
4070 gcc_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
4071 for (i
= 0; i
< nr
; i
++)
4073 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
4075 if (ira_live_ranges_intersect_p
4076 (slot_coalesced_allocnos_live_ranges
[n
],
4077 OBJECT_LIVE_RANGES (obj
)))
4086 /* Update live ranges of slot to which coalesced allocnos represented
4087 by ALLOCNO were assigned. */
4089 setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno
)
4095 n
= ALLOCNO_COALESCE_DATA (allocno
)->temp
;
4096 for (a
= ALLOCNO_COALESCE_DATA (allocno
)->next
;;
4097 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
4099 int nr
= ALLOCNO_NUM_OBJECTS (a
);
4100 gcc_assert (ALLOCNO_CAP_MEMBER (a
) == NULL
);
4101 for (i
= 0; i
< nr
; i
++)
4103 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
4105 r
= ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj
));
4106 slot_coalesced_allocnos_live_ranges
[n
]
4107 = ira_merge_live_ranges
4108 (slot_coalesced_allocnos_live_ranges
[n
], r
);
4115 /* We have coalesced allocnos involving in copies. Coalesce allocnos
4116 further in order to share the same memory stack slot. Allocnos
4117 representing sets of allocnos coalesced before the call are given
4118 in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
4119 some allocnos were coalesced in the function. */
4121 coalesce_spill_slots (ira_allocno_t
*spilled_coalesced_allocnos
, int num
)
4123 int i
, j
, n
, last_coalesced_allocno_num
;
4124 ira_allocno_t allocno
, a
;
4125 bool merged_p
= false;
4126 bitmap set_jump_crosses
= regstat_get_setjmp_crosses ();
4128 slot_coalesced_allocnos_live_ranges
4129 = (live_range_t
*) ira_allocate (sizeof (live_range_t
) * ira_allocnos_num
);
4130 memset (slot_coalesced_allocnos_live_ranges
, 0,
4131 sizeof (live_range_t
) * ira_allocnos_num
);
4132 last_coalesced_allocno_num
= 0;
4133 /* Coalesce non-conflicting spilled allocnos preferring most
4135 for (i
= 0; i
< num
; i
++)
4137 allocno
= spilled_coalesced_allocnos
[i
];
4138 if (ALLOCNO_COALESCE_DATA (allocno
)->first
!= allocno
4139 || bitmap_bit_p (set_jump_crosses
, ALLOCNO_REGNO (allocno
))
4140 || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno
)))
4142 for (j
= 0; j
< i
; j
++)
4144 a
= spilled_coalesced_allocnos
[j
];
4145 n
= ALLOCNO_COALESCE_DATA (a
)->temp
;
4146 if (ALLOCNO_COALESCE_DATA (a
)->first
== a
4147 && ! bitmap_bit_p (set_jump_crosses
, ALLOCNO_REGNO (a
))
4148 && ! ira_equiv_no_lvalue_p (ALLOCNO_REGNO (a
))
4149 && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno
, n
))
4154 /* No coalescing: set up number for coalesced allocnos
4155 represented by ALLOCNO. */
4156 ALLOCNO_COALESCE_DATA (allocno
)->temp
= last_coalesced_allocno_num
++;
4157 setup_slot_coalesced_allocno_live_ranges (allocno
);
4161 allocno_coalesced_p
= true;
4163 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4164 fprintf (ira_dump_file
,
4165 " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
4166 ALLOCNO_NUM (allocno
), ALLOCNO_REGNO (allocno
),
4167 ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
));
4168 ALLOCNO_COALESCE_DATA (allocno
)->temp
4169 = ALLOCNO_COALESCE_DATA (a
)->temp
;
4170 setup_slot_coalesced_allocno_live_ranges (allocno
);
4171 merge_allocnos (a
, allocno
);
4172 ira_assert (ALLOCNO_COALESCE_DATA (a
)->first
== a
);
4175 for (i
= 0; i
< ira_allocnos_num
; i
++)
4176 ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges
[i
]);
4177 ira_free (slot_coalesced_allocnos_live_ranges
);
4181 /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
4182 subsequent assigning stack slots to them in the reload pass. To do
4183 this we coalesce spilled allocnos first to decrease the number of
4184 memory-memory move insns. This function is called by the
4187 ira_sort_regnos_for_alter_reg (int *pseudo_regnos
, int n
,
4188 machine_mode
*reg_max_ref_mode
)
4190 int max_regno
= max_reg_num ();
4191 int i
, regno
, num
, slot_num
;
4192 ira_allocno_t allocno
, a
;
4193 ira_allocno_iterator ai
;
4194 ira_allocno_t
*spilled_coalesced_allocnos
;
4196 ira_assert (! ira_use_lra_p
);
4198 /* Set up allocnos can be coalesced. */
4199 coloring_allocno_bitmap
= ira_allocate_bitmap ();
4200 for (i
= 0; i
< n
; i
++)
4202 regno
= pseudo_regnos
[i
];
4203 allocno
= ira_regno_allocno_map
[regno
];
4204 if (allocno
!= NULL
)
4205 bitmap_set_bit (coloring_allocno_bitmap
, ALLOCNO_NUM (allocno
));
4207 allocno_coalesced_p
= false;
4208 processed_coalesced_allocno_bitmap
= ira_allocate_bitmap ();
4209 allocno_coalesce_data
4210 = (coalesce_data_t
) ira_allocate (sizeof (struct coalesce_data
)
4211 * ira_allocnos_num
);
4212 /* Initialize coalesce data for allocnos. */
4213 FOR_EACH_ALLOCNO (a
, ai
)
4215 ALLOCNO_ADD_DATA (a
) = allocno_coalesce_data
+ ALLOCNO_NUM (a
);
4216 ALLOCNO_COALESCE_DATA (a
)->first
= a
;
4217 ALLOCNO_COALESCE_DATA (a
)->next
= a
;
4219 coalesce_allocnos ();
4220 ira_free_bitmap (coloring_allocno_bitmap
);
4221 regno_coalesced_allocno_cost
4222 = (int *) ira_allocate (max_regno
* sizeof (int));
4223 regno_coalesced_allocno_num
4224 = (int *) ira_allocate (max_regno
* sizeof (int));
4225 memset (regno_coalesced_allocno_num
, 0, max_regno
* sizeof (int));
4226 setup_coalesced_allocno_costs_and_nums (pseudo_regnos
, n
);
4227 /* Sort regnos according frequencies of the corresponding coalesced
4229 qsort (pseudo_regnos
, n
, sizeof (int), coalesced_pseudo_reg_freq_compare
);
4230 spilled_coalesced_allocnos
4231 = (ira_allocno_t
*) ira_allocate (ira_allocnos_num
4232 * sizeof (ira_allocno_t
));
4233 /* Collect allocnos representing the spilled coalesced allocno
4235 num
= collect_spilled_coalesced_allocnos (pseudo_regnos
, n
,
4236 spilled_coalesced_allocnos
);
4237 if (flag_ira_share_spill_slots
4238 && coalesce_spill_slots (spilled_coalesced_allocnos
, num
))
4240 setup_coalesced_allocno_costs_and_nums (pseudo_regnos
, n
);
4241 qsort (pseudo_regnos
, n
, sizeof (int),
4242 coalesced_pseudo_reg_freq_compare
);
4243 num
= collect_spilled_coalesced_allocnos (pseudo_regnos
, n
,
4244 spilled_coalesced_allocnos
);
4246 ira_free_bitmap (processed_coalesced_allocno_bitmap
);
4247 allocno_coalesced_p
= false;
4248 /* Assign stack slot numbers to spilled allocno sets, use smaller
4249 numbers for most frequently used coalesced allocnos. -1 is
4250 reserved for dynamic search of stack slots for pseudos spilled by
4253 for (i
= 0; i
< num
; i
++)
4255 allocno
= spilled_coalesced_allocnos
[i
];
4256 if (ALLOCNO_COALESCE_DATA (allocno
)->first
!= allocno
4257 || ALLOCNO_HARD_REGNO (allocno
) >= 0
4258 || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno
)))
4260 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4261 fprintf (ira_dump_file
, " Slot %d (freq,size):", slot_num
);
4263 for (a
= ALLOCNO_COALESCE_DATA (allocno
)->next
;;
4264 a
= ALLOCNO_COALESCE_DATA (a
)->next
)
4266 ira_assert (ALLOCNO_HARD_REGNO (a
) < 0);
4267 ALLOCNO_HARD_REGNO (a
) = -slot_num
;
4268 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4270 machine_mode mode
= wider_subreg_mode
4271 (PSEUDO_REGNO_MODE (ALLOCNO_REGNO (a
)),
4272 reg_max_ref_mode
[ALLOCNO_REGNO (a
)]);
4273 fprintf (ira_dump_file
, " a%dr%d(%d,",
4274 ALLOCNO_NUM (a
), ALLOCNO_REGNO (a
), ALLOCNO_FREQ (a
));
4275 print_dec (GET_MODE_SIZE (mode
), ira_dump_file
, SIGNED
);
4276 fprintf (ira_dump_file
, ")\n");
4282 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4283 fprintf (ira_dump_file
, "\n");
4285 ira_spilled_reg_stack_slots_num
= slot_num
- 1;
4286 ira_free (spilled_coalesced_allocnos
);
4287 /* Sort regnos according the slot numbers. */
4288 regno_max_ref_mode
= reg_max_ref_mode
;
4289 qsort (pseudo_regnos
, n
, sizeof (int), coalesced_pseudo_reg_slot_compare
);
4290 FOR_EACH_ALLOCNO (a
, ai
)
4291 ALLOCNO_ADD_DATA (a
) = NULL
;
4292 ira_free (allocno_coalesce_data
);
4293 ira_free (regno_coalesced_allocno_num
);
4294 ira_free (regno_coalesced_allocno_cost
);
4299 /* This page contains code used by the reload pass to improve the
4302 /* The function is called from reload to mark changes in the
4303 allocation of REGNO made by the reload. Remember that reg_renumber
4304 reflects the change result. */
4306 ira_mark_allocation_change (int regno
)
4308 ira_allocno_t a
= ira_regno_allocno_map
[regno
];
4309 int old_hard_regno
, hard_regno
, cost
;
4310 enum reg_class aclass
= ALLOCNO_CLASS (a
);
4312 ira_assert (a
!= NULL
);
4313 hard_regno
= reg_renumber
[regno
];
4314 if ((old_hard_regno
= ALLOCNO_HARD_REGNO (a
)) == hard_regno
)
4316 if (old_hard_regno
< 0)
4317 cost
= -ALLOCNO_MEMORY_COST (a
);
4320 ira_assert (ira_class_hard_reg_index
[aclass
][old_hard_regno
] >= 0);
4321 cost
= -(ALLOCNO_HARD_REG_COSTS (a
) == NULL
4322 ? ALLOCNO_CLASS_COST (a
)
4323 : ALLOCNO_HARD_REG_COSTS (a
)
4324 [ira_class_hard_reg_index
[aclass
][old_hard_regno
]]);
4325 update_costs_from_copies (a
, false, false);
4327 ira_overall_cost
-= cost
;
4328 ALLOCNO_HARD_REGNO (a
) = hard_regno
;
4331 ALLOCNO_HARD_REGNO (a
) = -1;
4332 cost
+= ALLOCNO_MEMORY_COST (a
);
4334 else if (ira_class_hard_reg_index
[aclass
][hard_regno
] >= 0)
4336 cost
+= (ALLOCNO_HARD_REG_COSTS (a
) == NULL
4337 ? ALLOCNO_CLASS_COST (a
)
4338 : ALLOCNO_HARD_REG_COSTS (a
)
4339 [ira_class_hard_reg_index
[aclass
][hard_regno
]]);
4340 update_costs_from_copies (a
, true, false);
4343 /* Reload changed class of the allocno. */
4345 ira_overall_cost
+= cost
;
4348 /* This function is called when reload deletes memory-memory move. In
4349 this case we marks that the allocation of the corresponding
4350 allocnos should be not changed in future. Otherwise we risk to get
4353 ira_mark_memory_move_deletion (int dst_regno
, int src_regno
)
4355 ira_allocno_t dst
= ira_regno_allocno_map
[dst_regno
];
4356 ira_allocno_t src
= ira_regno_allocno_map
[src_regno
];
4358 ira_assert (dst
!= NULL
&& src
!= NULL
4359 && ALLOCNO_HARD_REGNO (dst
) < 0
4360 && ALLOCNO_HARD_REGNO (src
) < 0);
4361 ALLOCNO_DONT_REASSIGN_P (dst
) = true;
4362 ALLOCNO_DONT_REASSIGN_P (src
) = true;
4365 /* Try to assign a hard register (except for FORBIDDEN_REGS) to
4366 allocno A and return TRUE in the case of success. */
4368 allocno_reload_assign (ira_allocno_t a
, HARD_REG_SET forbidden_regs
)
4371 enum reg_class aclass
;
4372 int regno
= ALLOCNO_REGNO (a
);
4373 HARD_REG_SET saved
[2];
4376 n
= ALLOCNO_NUM_OBJECTS (a
);
4377 for (i
= 0; i
< n
; i
++)
4379 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
4380 saved
[i
] = OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
);
4381 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
) |= forbidden_regs
;
4382 if (! flag_caller_saves
&& ALLOCNO_CALLS_CROSSED_NUM (a
) != 0)
4383 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
) |= ira_need_caller_save_regs (a
);
4385 ALLOCNO_ASSIGNED_P (a
) = false;
4386 aclass
= ALLOCNO_CLASS (a
);
4387 update_curr_costs (a
);
4388 assign_hard_reg (a
, true);
4389 hard_regno
= ALLOCNO_HARD_REGNO (a
);
4390 reg_renumber
[regno
] = hard_regno
;
4392 ALLOCNO_HARD_REGNO (a
) = -1;
4395 ira_assert (ira_class_hard_reg_index
[aclass
][hard_regno
] >= 0);
4397 -= (ALLOCNO_MEMORY_COST (a
)
4398 - (ALLOCNO_HARD_REG_COSTS (a
) == NULL
4399 ? ALLOCNO_CLASS_COST (a
)
4400 : ALLOCNO_HARD_REG_COSTS (a
)[ira_class_hard_reg_index
4401 [aclass
][hard_regno
]]));
4402 if (ira_need_caller_save_p (a
, hard_regno
))
4404 ira_assert (flag_caller_saves
);
4405 caller_save_needed
= 1;
4409 /* If we found a hard register, modify the RTL for the pseudo
4410 register to show the hard register, and mark the pseudo register
4412 if (reg_renumber
[regno
] >= 0)
4414 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4415 fprintf (ira_dump_file
, ": reassign to %d\n", reg_renumber
[regno
]);
4416 SET_REGNO (regno_reg_rtx
[regno
], reg_renumber
[regno
]);
4417 mark_home_live (regno
);
4419 else if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4420 fprintf (ira_dump_file
, "\n");
4421 for (i
= 0; i
< n
; i
++)
4423 ira_object_t obj
= ALLOCNO_OBJECT (a
, i
);
4424 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj
) = saved
[i
];
4426 return reg_renumber
[regno
] >= 0;
4429 /* Sort pseudos according their usage frequencies (putting most
4430 frequently ones first). */
4432 pseudo_reg_compare (const void *v1p
, const void *v2p
)
4434 int regno1
= *(const int *) v1p
;
4435 int regno2
= *(const int *) v2p
;
4438 if ((diff
= REG_FREQ (regno2
) - REG_FREQ (regno1
)) != 0)
4440 return regno1
- regno2
;
4443 /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
4444 NUM of them) or spilled pseudos conflicting with pseudos in
4445 SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
4446 allocation has been changed. The function doesn't use
4447 BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
4448 PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
4449 is called by the reload pass at the end of each reload
4452 ira_reassign_pseudos (int *spilled_pseudo_regs
, int num
,
4453 HARD_REG_SET bad_spill_regs
,
4454 HARD_REG_SET
*pseudo_forbidden_regs
,
4455 HARD_REG_SET
*pseudo_previous_regs
,
4461 HARD_REG_SET forbidden_regs
;
4462 bitmap temp
= BITMAP_ALLOC (NULL
);
4464 /* Add pseudos which conflict with pseudos already in
4465 SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
4466 to allocating in two steps as some of the conflicts might have
4467 a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
4468 for (i
= 0; i
< num
; i
++)
4469 bitmap_set_bit (temp
, spilled_pseudo_regs
[i
]);
4471 for (i
= 0, n
= num
; i
< n
; i
++)
4474 int regno
= spilled_pseudo_regs
[i
];
4475 bitmap_set_bit (temp
, regno
);
4477 a
= ira_regno_allocno_map
[regno
];
4478 nr
= ALLOCNO_NUM_OBJECTS (a
);
4479 for (j
= 0; j
< nr
; j
++)
4481 ira_object_t conflict_obj
;
4482 ira_object_t obj
= ALLOCNO_OBJECT (a
, j
);
4483 ira_object_conflict_iterator oci
;
4485 FOR_EACH_OBJECT_CONFLICT (obj
, conflict_obj
, oci
)
4487 ira_allocno_t conflict_a
= OBJECT_ALLOCNO (conflict_obj
);
4488 if (ALLOCNO_HARD_REGNO (conflict_a
) < 0
4489 && ! ALLOCNO_DONT_REASSIGN_P (conflict_a
)
4490 && bitmap_set_bit (temp
, ALLOCNO_REGNO (conflict_a
)))
4492 spilled_pseudo_regs
[num
++] = ALLOCNO_REGNO (conflict_a
);
4493 /* ?!? This seems wrong. */
4494 bitmap_set_bit (consideration_allocno_bitmap
,
4495 ALLOCNO_NUM (conflict_a
));
4502 qsort (spilled_pseudo_regs
, num
, sizeof (int), pseudo_reg_compare
);
4504 /* Try to assign hard registers to pseudos from
4505 SPILLED_PSEUDO_REGS. */
4506 for (i
= 0; i
< num
; i
++)
4508 regno
= spilled_pseudo_regs
[i
];
4509 forbidden_regs
= (bad_spill_regs
4510 | pseudo_forbidden_regs
[regno
]
4511 | pseudo_previous_regs
[regno
]);
4512 gcc_assert (reg_renumber
[regno
] < 0);
4513 a
= ira_regno_allocno_map
[regno
];
4514 ira_mark_allocation_change (regno
);
4515 ira_assert (reg_renumber
[regno
] < 0);
4516 if (internal_flag_ira_verbose
> 3 && ira_dump_file
!= NULL
)
4517 fprintf (ira_dump_file
,
4518 " Try Assign %d(a%d), cost=%d", regno
, ALLOCNO_NUM (a
),
4519 ALLOCNO_MEMORY_COST (a
)
4520 - ALLOCNO_CLASS_COST (a
));
4521 allocno_reload_assign (a
, forbidden_regs
);
4522 if (reg_renumber
[regno
] >= 0)
4524 CLEAR_REGNO_REG_SET (spilled
, regno
);
4532 /* The function is called by reload and returns already allocated
4533 stack slot (if any) for REGNO with given INHERENT_SIZE and
4534 TOTAL_SIZE. In the case of failure to find a slot which can be
4535 used for REGNO, the function returns NULL. */
4537 ira_reuse_stack_slot (int regno
, poly_uint64 inherent_size
,
4538 poly_uint64 total_size
)
4541 int slot_num
, best_slot_num
;
4542 int cost
, best_cost
;
4543 ira_copy_t cp
, next_cp
;
4544 ira_allocno_t another_allocno
, allocno
= ira_regno_allocno_map
[regno
];
4547 class ira_spilled_reg_stack_slot
*slot
= NULL
;
4549 ira_assert (! ira_use_lra_p
);
4551 ira_assert (known_eq (inherent_size
, PSEUDO_REGNO_BYTES (regno
))
4552 && known_le (inherent_size
, total_size
)
4553 && ALLOCNO_HARD_REGNO (allocno
) < 0);
4554 if (! flag_ira_share_spill_slots
)
4556 slot_num
= -ALLOCNO_HARD_REGNO (allocno
) - 2;
4559 slot
= &ira_spilled_reg_stack_slots
[slot_num
];
4564 best_cost
= best_slot_num
= -1;
4566 /* It means that the pseudo was spilled in the reload pass, try
4569 slot_num
< ira_spilled_reg_stack_slots_num
;
4572 slot
= &ira_spilled_reg_stack_slots
[slot_num
];
4573 if (slot
->mem
== NULL_RTX
)
4575 if (maybe_lt (slot
->width
, total_size
)
4576 || maybe_lt (GET_MODE_SIZE (GET_MODE (slot
->mem
)), inherent_size
))
4579 EXECUTE_IF_SET_IN_BITMAP (&slot
->spilled_regs
,
4580 FIRST_PSEUDO_REGISTER
, i
, bi
)
4582 another_allocno
= ira_regno_allocno_map
[i
];
4583 if (allocnos_conflict_by_live_ranges_p (allocno
,
4587 for (cost
= 0, cp
= ALLOCNO_COPIES (allocno
);
4591 if (cp
->first
== allocno
)
4593 next_cp
= cp
->next_first_allocno_copy
;
4594 another_allocno
= cp
->second
;
4596 else if (cp
->second
== allocno
)
4598 next_cp
= cp
->next_second_allocno_copy
;
4599 another_allocno
= cp
->first
;
4603 if (cp
->insn
== NULL_RTX
)
4605 if (bitmap_bit_p (&slot
->spilled_regs
,
4606 ALLOCNO_REGNO (another_allocno
)))
4609 if (cost
> best_cost
)
4612 best_slot_num
= slot_num
;
4619 slot_num
= best_slot_num
;
4620 slot
= &ira_spilled_reg_stack_slots
[slot_num
];
4621 SET_REGNO_REG_SET (&slot
->spilled_regs
, regno
);
4623 ALLOCNO_HARD_REGNO (allocno
) = -slot_num
- 2;
4628 ira_assert (known_ge (slot
->width
, total_size
));
4629 #ifdef ENABLE_IRA_CHECKING
4630 EXECUTE_IF_SET_IN_BITMAP (&slot
->spilled_regs
,
4631 FIRST_PSEUDO_REGISTER
, i
, bi
)
4633 ira_assert (! conflict_by_live_ranges_p (regno
, i
));
4636 SET_REGNO_REG_SET (&slot
->spilled_regs
, regno
);
4637 if (internal_flag_ira_verbose
> 3 && ira_dump_file
)
4639 fprintf (ira_dump_file
, " Assigning %d(freq=%d) slot %d of",
4640 regno
, REG_FREQ (regno
), slot_num
);
4641 EXECUTE_IF_SET_IN_BITMAP (&slot
->spilled_regs
,
4642 FIRST_PSEUDO_REGISTER
, i
, bi
)
4644 if ((unsigned) regno
!= i
)
4645 fprintf (ira_dump_file
, " %d", i
);
4647 fprintf (ira_dump_file
, "\n");
4653 /* This is called by reload every time a new stack slot X with
4654 TOTAL_SIZE was allocated for REGNO. We store this info for
4655 subsequent ira_reuse_stack_slot calls. */
4657 ira_mark_new_stack_slot (rtx x
, int regno
, poly_uint64 total_size
)
4659 class ira_spilled_reg_stack_slot
*slot
;
4661 ira_allocno_t allocno
;
4663 ira_assert (! ira_use_lra_p
);
4665 ira_assert (known_le (PSEUDO_REGNO_BYTES (regno
), total_size
));
4666 allocno
= ira_regno_allocno_map
[regno
];
4667 slot_num
= -ALLOCNO_HARD_REGNO (allocno
) - 2;
4670 slot_num
= ira_spilled_reg_stack_slots_num
++;
4671 ALLOCNO_HARD_REGNO (allocno
) = -slot_num
- 2;
4673 slot
= &ira_spilled_reg_stack_slots
[slot_num
];
4674 INIT_REG_SET (&slot
->spilled_regs
);
4675 SET_REGNO_REG_SET (&slot
->spilled_regs
, regno
);
4677 slot
->width
= total_size
;
4678 if (internal_flag_ira_verbose
> 3 && ira_dump_file
)
4679 fprintf (ira_dump_file
, " Assigning %d(freq=%d) a new slot %d\n",
4680 regno
, REG_FREQ (regno
), slot_num
);
4684 /* Return spill cost for pseudo-registers whose numbers are in array
4685 REGNOS (with a negative number as an end marker) for reload with
4686 given IN and OUT for INSN. Return also number points (through
4687 EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
4688 the register pressure is high, number of references of the
4689 pseudo-registers (through NREFS), the number of psuedo registers
4690 whose allocated register wouldn't need saving in the prologue
4691 (through CALL_USED_COUNT), and the first hard regno occupied by the
4692 pseudo-registers (through FIRST_HARD_REGNO). */
4694 calculate_spill_cost (int *regnos
, rtx in
, rtx out
, rtx_insn
*insn
,
4695 int *excess_pressure_live_length
,
4696 int *nrefs
, int *call_used_count
, int *first_hard_regno
)
4698 int i
, cost
, regno
, hard_regno
, count
, saved_cost
;
4704 for (length
= count
= cost
= i
= 0;; i
++)
4709 *nrefs
+= REG_N_REFS (regno
);
4710 hard_regno
= reg_renumber
[regno
];
4711 ira_assert (hard_regno
>= 0);
4712 a
= ira_regno_allocno_map
[regno
];
4713 length
+= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a
) / ALLOCNO_NUM_OBJECTS (a
);
4714 cost
+= ALLOCNO_MEMORY_COST (a
) - ALLOCNO_CLASS_COST (a
);
4715 if (in_hard_reg_set_p (crtl
->abi
->full_reg_clobbers (),
4716 ALLOCNO_MODE (a
), hard_regno
))
4718 in_p
= in
&& REG_P (in
) && (int) REGNO (in
) == hard_regno
;
4719 out_p
= out
&& REG_P (out
) && (int) REGNO (out
) == hard_regno
;
4721 && find_regno_note (insn
, REG_DEAD
, hard_regno
) != NULL_RTX
)
4725 saved_cost
+= ira_memory_move_cost
4726 [ALLOCNO_MODE (a
)][ALLOCNO_CLASS (a
)][1];
4729 += ira_memory_move_cost
4730 [ALLOCNO_MODE (a
)][ALLOCNO_CLASS (a
)][0];
4731 cost
-= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn
)) * saved_cost
;
4734 *excess_pressure_live_length
= length
;
4735 *call_used_count
= count
;
4739 hard_regno
= reg_renumber
[regnos
[0]];
4741 *first_hard_regno
= hard_regno
;
4745 /* Return TRUE if spilling pseudo-registers whose numbers are in array
4746 REGNOS is better than spilling pseudo-registers with numbers in
4747 OTHER_REGNOS for reload with given IN and OUT for INSN. The
4748 function used by the reload pass to make better register spilling
4751 ira_better_spill_reload_regno_p (int *regnos
, int *other_regnos
,
4752 rtx in
, rtx out
, rtx_insn
*insn
)
4754 int cost
, other_cost
;
4755 int length
, other_length
;
4756 int nrefs
, other_nrefs
;
4757 int call_used_count
, other_call_used_count
;
4758 int hard_regno
, other_hard_regno
;
4760 cost
= calculate_spill_cost (regnos
, in
, out
, insn
,
4761 &length
, &nrefs
, &call_used_count
, &hard_regno
);
4762 other_cost
= calculate_spill_cost (other_regnos
, in
, out
, insn
,
4763 &other_length
, &other_nrefs
,
4764 &other_call_used_count
,
4766 if (nrefs
== 0 && other_nrefs
!= 0)
4768 if (nrefs
!= 0 && other_nrefs
== 0)
4770 if (cost
!= other_cost
)
4771 return cost
< other_cost
;
4772 if (length
!= other_length
)
4773 return length
> other_length
;
4774 #ifdef REG_ALLOC_ORDER
4775 if (hard_regno
>= 0 && other_hard_regno
>= 0)
4776 return (inv_reg_alloc_order
[hard_regno
]
4777 < inv_reg_alloc_order
[other_hard_regno
]);
4779 if (call_used_count
!= other_call_used_count
)
4780 return call_used_count
> other_call_used_count
;
4787 /* Allocate and initialize data necessary for assign_hard_reg. */
4789 ira_initiate_assign (void)
4792 = (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
)
4793 * ira_allocnos_num
);
4794 consideration_allocno_bitmap
= ira_allocate_bitmap ();
4795 initiate_cost_update ();
4796 allocno_priorities
= (int *) ira_allocate (sizeof (int) * ira_allocnos_num
);
4797 sorted_copies
= (ira_copy_t
*) ira_allocate (ira_copies_num
4798 * sizeof (ira_copy_t
));
4801 /* Deallocate data used by assign_hard_reg. */
4803 ira_finish_assign (void)
4805 ira_free (sorted_allocnos
);
4806 ira_free_bitmap (consideration_allocno_bitmap
);
4807 finish_cost_update ();
4808 ira_free (allocno_priorities
);
4809 ira_free (sorted_copies
);
4814 /* Entry function doing color-based register allocation. */
4818 allocno_stack_vec
.create (ira_allocnos_num
);
4819 memset (allocated_hardreg_p
, 0, sizeof (allocated_hardreg_p
));
4820 ira_initiate_assign ();
4822 ira_finish_assign ();
4823 allocno_stack_vec
.release ();
4824 move_spill_restore ();
4829 /* This page contains a simple register allocator without usage of
4830 allocno conflicts. This is used for fast allocation for -O0. */
4832 /* Do register allocation by not using allocno conflicts. It uses
4833 only allocno live ranges. The algorithm is close to Chow's
4834 priority coloring. */
4836 fast_allocation (void)
4838 int i
, j
, k
, num
, class_size
, hard_regno
, best_hard_regno
, cost
, min_cost
;
4841 bool no_stack_reg_p
;
4843 enum reg_class aclass
;
4846 ira_allocno_iterator ai
;
4848 HARD_REG_SET conflict_hard_regs
, *used_hard_regs
;
4850 sorted_allocnos
= (ira_allocno_t
*) ira_allocate (sizeof (ira_allocno_t
)
4851 * ira_allocnos_num
);
4853 FOR_EACH_ALLOCNO (a
, ai
)
4854 sorted_allocnos
[num
++] = a
;
4855 allocno_priorities
= (int *) ira_allocate (sizeof (int) * ira_allocnos_num
);
4856 setup_allocno_priorities (sorted_allocnos
, num
);
4857 used_hard_regs
= (HARD_REG_SET
*) ira_allocate (sizeof (HARD_REG_SET
)
4859 for (i
= 0; i
< ira_max_point
; i
++)
4860 CLEAR_HARD_REG_SET (used_hard_regs
[i
]);
4861 qsort (sorted_allocnos
, num
, sizeof (ira_allocno_t
),
4862 allocno_priority_compare_func
);
4863 for (i
= 0; i
< num
; i
++)
4867 a
= sorted_allocnos
[i
];
4868 nr
= ALLOCNO_NUM_OBJECTS (a
);
4869 CLEAR_HARD_REG_SET (conflict_hard_regs
);
4870 for (l
= 0; l
< nr
; l
++)
4872 ira_object_t obj
= ALLOCNO_OBJECT (a
, l
);
4873 conflict_hard_regs
|= OBJECT_CONFLICT_HARD_REGS (obj
);
4874 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
4875 for (j
= r
->start
; j
<= r
->finish
; j
++)
4876 conflict_hard_regs
|= used_hard_regs
[j
];
4878 aclass
= ALLOCNO_CLASS (a
);
4879 ALLOCNO_ASSIGNED_P (a
) = true;
4880 ALLOCNO_HARD_REGNO (a
) = -1;
4881 if (hard_reg_set_subset_p (reg_class_contents
[aclass
],
4882 conflict_hard_regs
))
4884 mode
= ALLOCNO_MODE (a
);
4886 no_stack_reg_p
= ALLOCNO_NO_STACK_REG_P (a
);
4888 class_size
= ira_class_hard_regs_num
[aclass
];
4889 costs
= ALLOCNO_HARD_REG_COSTS (a
);
4891 best_hard_regno
= -1;
4892 for (j
= 0; j
< class_size
; j
++)
4894 hard_regno
= ira_class_hard_regs
[aclass
][j
];
4896 if (no_stack_reg_p
&& FIRST_STACK_REG
<= hard_regno
4897 && hard_regno
<= LAST_STACK_REG
)
4900 if (ira_hard_reg_set_intersection_p (hard_regno
, mode
, conflict_hard_regs
)
4901 || (TEST_HARD_REG_BIT
4902 (ira_prohibited_class_mode_regs
[aclass
][mode
], hard_regno
)))
4906 best_hard_regno
= hard_regno
;
4910 if (min_cost
> cost
)
4913 best_hard_regno
= hard_regno
;
4916 if (best_hard_regno
< 0)
4918 ALLOCNO_HARD_REGNO (a
) = hard_regno
= best_hard_regno
;
4919 for (l
= 0; l
< nr
; l
++)
4921 ira_object_t obj
= ALLOCNO_OBJECT (a
, l
);
4922 for (r
= OBJECT_LIVE_RANGES (obj
); r
!= NULL
; r
= r
->next
)
4923 for (k
= r
->start
; k
<= r
->finish
; k
++)
4924 used_hard_regs
[k
] |= ira_reg_mode_hard_regset
[hard_regno
][mode
];
4927 ira_free (sorted_allocnos
);
4928 ira_free (used_hard_regs
);
4929 ira_free (allocno_priorities
);
4930 if (internal_flag_ira_verbose
> 1 && ira_dump_file
!= NULL
)
4931 ira_print_disposition (ira_dump_file
);
4936 /* Entry function doing coloring. */
4941 ira_allocno_iterator ai
;
4943 /* Setup updated costs. */
4944 FOR_EACH_ALLOCNO (a
, ai
)
4946 ALLOCNO_UPDATED_MEMORY_COST (a
) = ALLOCNO_MEMORY_COST (a
);
4947 ALLOCNO_UPDATED_CLASS_COST (a
) = ALLOCNO_CLASS_COST (a
);
4949 if (ira_conflicts_p
)