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